結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー SSRSSSRS
提出日時 2021-12-17 03:11:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,500 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 180,024 KB
実行使用メモリ 17,888 KB
最終ジャッジ日時 2023-10-12 03:38:43
合計ジャッジ時間 3,792 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 56 ms
7,068 KB
testcase_11 AC 38 ms
4,348 KB
testcase_12 AC 130 ms
4,348 KB
testcase_13 AC 25 ms
4,352 KB
testcase_14 AC 53 ms
4,348 KB
testcase_15 AC 30 ms
4,348 KB
testcase_16 AC 70 ms
8,576 KB
testcase_17 AC 24 ms
4,352 KB
testcase_18 AC 129 ms
4,348 KB
testcase_19 AC 63 ms
4,352 KB
testcase_20 AC 211 ms
17,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
  if (b == 0){
    return a;
  } else {
    return gcd(b, a % b);
  }
}
int main(){
  int N, M, K;
  cin >> N >> M >> K;
  char op;
  cin >> op;
  vector<int> B(M);
  for (int i = 0; i < M; i++){
    cin >> B[i];
  }
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  if (op == '+'){
    map<int, int> cntA;
    for (int i = 0; i < N; i++){
      cntA[A[i] % K]++;
    }
    map<int, int> cntB;
    for (int i = 0; i < M; i++){
      cntB[B[i] % K]++;
    }
    long long ans = 0;
    for (auto P : cntA){
      ans += (long long) P.second * cntB[(K - P.first) % K];
    }
    cout << ans << endl;
  }
  if (op == '*'){
    vector<int> F;
    for (int i = 1; i * i <= K; i++){
      if (K % i == 0){
        F.push_back(i);
        if (i * i < K){
          F.push_back(K / i);
        }
      }
    }
    sort(F.begin(), F.end());
    int c = F.size();
    vector<int> cntA(c, 0);
    for (int i = 0; i < N; i++){
      int p = lower_bound(F.begin(), F.end(), gcd(K, A[i])) - F.begin();
      cntA[p]++;
    }
    vector<int> cntB(c, 0);
    for (int i = 0; i < M; i++){
      int p = lower_bound(F.begin(), F.end(), gcd(K, B[i])) - F.begin();
      cntB[p]++;
    }
    long long ans = 0;
    for (int i = 0; i < c; i++){
      for (int j = 0; j < c; j++){
        if ((long long) F[i] * F[j] % K == 0){
          ans += (long long) cntA[i] * cntB[j];
        }
      }
    }
    cout << ans << endl;
  }
}
0