結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー monnumonnu
提出日時 2021-04-17 14:50:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,659 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 3,882 ms
コンパイル使用メモリ 231,116 KB
実行使用メモリ 4,732 KB
最終ジャッジ日時 2023-09-16 23:57:38
合計ジャッジ時間 9,458 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 36 ms
4,376 KB
testcase_11 AC 40 ms
4,376 KB
testcase_12 AC 1,656 ms
4,608 KB
testcase_13 AC 26 ms
4,380 KB
testcase_14 AC 63 ms
4,380 KB
testcase_15 AC 34 ms
4,376 KB
testcase_16 AC 44 ms
4,380 KB
testcase_17 AC 28 ms
4,380 KB
testcase_18 AC 1,659 ms
4,652 KB
testcase_19 AC 927 ms
4,380 KB
testcase_20 AC 109 ms
4,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
using Graph=vector<vector<int>>;
#define MOD 998244353
#define INF 1000000000000000000
#define MAX 1000000

ll gcd(ll a,ll b){
  while(a%b!=0){
    ll tmp=a%b;
    a=b;
    b=tmp;
  }
  return b;
}

int main(){
  int N,M;
  ll K;
  cin>>N>>M>>K;
  char op;
  cin>>op;
  vector<ll> B(M),A(N);
  for(int i=0;i<M;i++){
    cin>>B[i];
  }
  for(int i=0;i<N;i++){
    cin>>A[i];
  }

  if(op=='+'){
    for(int i=0;i<N;i++){
      A[i]%=K;
    }
    for(int i=0;i<M;i++){
      B[i]%=K;
    }
    sort(B.begin(),B.end());
    ll ans=0;
    for(int i=0;i<N;i++){
      ll x=(K-A[i])%K;
      int left=lower_bound(B.begin(),B.end(),x)-B.begin();
      int right=upper_bound(B.begin(),B.end(),x)-B.begin();
      ans+=(ll)(right-left);
    }
    cout<<ans<<endl;
  }else{
    vector<ll> divd;
    for(ll i=1;i*i<=K;i++){
      if(K%i==0){
        divd.push_back(i);
        if(K/i!=i){
          divd.push_back(K/i);
        }
      }
    }
    sort(divd.begin(),divd.end());
    int n=divd.size();
    vector<int> cnt(n);
    for(int i=0;i<n;i++){
      for(int j=0;j<M;j++){
        if(B[j]%divd[i]==0){
          cnt[i]++;
        }
      }
    }
    ll ans=0;
    for(int i=0;i<N;i++){
      ll x=gcd(K,A[i]);
      int k=lower_bound(divd.begin(),divd.end(),x)-divd.begin();
      ans+=(ll)cnt[n-1-k];
    }
    cout<<ans<<endl;
  }
}
0