結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー leaf_1415leaf_1415
提出日時 2020-02-14 21:46:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,730 ms / 2,000 ms
コード長 1,379 bytes
コンパイル時間 2,539 ms
コンパイル使用メモリ 81,316 KB
実行使用メモリ 23,680 KB
最終ジャッジ日時 2023-08-10 01:46:27
合計ジャッジ時間 7,045 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 50 ms
10,472 KB
testcase_11 AC 20 ms
4,508 KB
testcase_12 AC 1,716 ms
16,608 KB
testcase_13 AC 15 ms
4,376 KB
testcase_14 AC 32 ms
4,864 KB
testcase_15 AC 17 ms
4,376 KB
testcase_16 AC 61 ms
11,240 KB
testcase_17 AC 15 ms
4,376 KB
testcase_18 AC 1,730 ms
16,556 KB
testcase_19 AC 965 ms
8,336 KB
testcase_20 AC 173 ms
23,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define llint long long

using namespace std;

llint h, w, k;
char op;
llint a[100005], b[100005];
vector<llint> vec;
llint num[2005];
map<llint, llint> mp, mpA, mpB;

llint gcd(llint a,llint b)
{
  if(b == 0) return a;
  return gcd(b, a%b);
}

int main(void)
{
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> h >> w >> k;
  cin >> op;
  for(int i = 1; i <= w; i++) cin >> a[i];
  for(int i = 1; i <= h; i++) cin >> b[i];

  for(int i = 1; i <= w; i++) mpA[a[i]%k]++;
  for(int i = 1; i <= h; i++) mpB[b[i]%k]++;

  if(op == '+'){
    llint ans = 0;
    for(auto it = mpA.begin(); it != mpA.end(); it++){
      ans += it->second * mpB[(k-it->first) % k];
    }
    cout << ans << endl;
    return 0;
  }
  else{

    for(int i = 1; i*i <= k; i++){
      if(k % i == 0){
        vec.push_back(i);
        vec.push_back(k/i);
      }
    }
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());

    llint m = vec.size();
    for(int i = 0; i < m; i++) mp[vec[i]] = i;
    for(int i = 1; i <= w; i++){
      for(int j = 0; j < m; j++){
        if(a[i] % vec[j] == 0) num[j]++;
      }
    }

    llint ans = 0;
    for(int i = 1; i <= h; i++){
      llint g = gcd(k, b[i]);
      ans += num[mp[k / g]];
    }
    cout << ans << endl;
    return 0;
  }

  return 0;
}
0