結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー Strorkis
提出日時 2020-02-14 22:34:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,405 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 75,856 KB
実行使用メモリ 13,640 KB
最終ジャッジ日時 2024-11-16 01:01:20
合計ジャッジ時間 11,360 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 1
other AC * 4 WA * 11 RE * 1 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

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