結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー StrorkisStrorkis
提出日時 2020-02-14 22:34:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,405 bytes
コンパイル時間 566 ms
コンパイル使用メモリ 75,216 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-27 19:52:11
合計ジャッジ時間 4,480 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
6,944 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 RE -
testcase_05 WA -
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 34 ms
6,944 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

int gcd(int a, int b) {
  int r = a % b;
  return r ? gcd(b, r) : b;
}

int main() {
  int n, m, k; cin >> n >> m >> k;
  char op; cin >> op;

  ll ans = 0;
  if (op == '+') {
    vector<int> v(m);
    for (int i = 0; i < m; i++) {
      int b; cin >> b;
      v[i] = b % k;
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
      int a; cin >> a;
      int x = k - a % k;
      int l = 0, r = m;
      while (l < r) {
        int c = (l + r) / 2;
        if (v[c] < x)
          l = c + 1;
        else
          r = c;
      }
      if (v[l] != x) continue;
      int tmp = l;
      r = m;
      while (l < r) {
        int c = (l + r) / 2;
        if (v[c] <= x)
          l = c + 1;
        else
          r = c;
      }
      ans += l - tmp;
    }
    cout << ans << endl;
  }
  else {
    int d = 0;
    while (d * d <= k) d++;
    vector<int> v(d+1);
    int count = 0;
    for (int i = 0; i < m; i++) {
      int b; cin >> b;
      if (b % k == 0) {
        count++;
        continue;
      }
      for (int j = 2; j <= d; j++) {
        if (b % j == 0) v[j]++;
      }
    }
    for (int i = 0; i < n; i++) {
      int a; cin >> a;
      if (a % k == 0) {
        ans += m;
        continue;
      }
      ans += v[k / gcd(a, k)] + count;
    }
  }
  cout << ans << endl;
}
0