結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー edamame882edamame882
提出日時 2020-02-14 22:42:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,359 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 180,260 KB
実行使用メモリ 265,888 KB
最終ジャッジ日時 2024-04-27 19:55:00
合計ジャッジ時間 5,286 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,940 KB
testcase_04 WA -
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 40 ms
10,240 KB
testcase_11 AC 14 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 <bits/stdc++.h>
using namespace std;

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
//const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
//const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

// Mod int
const int mod = 1000000007;
struct mint {
  ll x;
  mint():x(0){}
  mint(ll x):x((x%mod+mod)%mod){}
  // mint(ll x):x(x){}
  mint& fix() { x = (x%mod+mod)%mod; return *this;}
  mint operator-() const { return mint(0) - *this;}
  mint& operator+=(const mint& a){ if((x+=a.x)>=mod) x-=mod; return *this;}
  mint& operator-=(const mint& a){ if((x+=mod-a.x)>=mod) x-=mod; return *this;}
  mint& operator*=(const mint& a){ (x*=a.x)%=mod; return *this;}
  mint operator+(const mint& a)const{ return mint(*this) += a;}
  mint operator-(const mint& a)const{ return mint(*this) -= a;}
  mint operator*(const mint& a)const{ return mint(*this) *= a;}
  bool operator<(const mint& a)const{ return x < a.x;}
  bool operator==(const mint& a)const{ return x == a.x;}
};
istream& operator>>(istream&i,mint&a){i>>a.x;return i;}
ostream& operator<<(ostream&o,const mint&a){o<<a.x;return o;}
typedef vector<mint> vm;
typedef vector<vm> vvm;
//

ll modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

long long gcd( long long m, long long n ){

  if ( n == 0) return m;
  return gcd(n,m%n);
}//最大公約数

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  char cmd;
  int n,m;
  ll k;
  cin >> m >> n >> k;
  cin >> cmd;
  VLL x(n);
  VLL y(m);
  rep(i,n) cin >>  x[i];
  rep(i,m) cin >> y[i];
  map<ll,ll> mpx;
  map<ll,ll> mpy;

  rep(i,n) mpx[x[i]%k]++;
  rep(j,m) mpy[y[j]%k]++;

  if(cmd == '+'){
    ll ans = 0;
    for(auto x: mpx){
      ll less = k-x.first;
      ans += x.second * mpy[less];
    }
    cout << ans << endl;
  }else{
    ll ans = 0;
    for(auto x: mpx){
      // cout << x.first << " ,add ";
      if(x.first == 0){
        // cout << x.second * m << endl;
        ans += x.second * m;
      }else{
        ll gcdNum = gcd(k,x.first);
        if(gcdNum == 1){
          ll inv = modinv(x.first,k);
          // cout << x.second * mpy[inv] << " inv " << inv << endl;
          ans += x.second * mpy[inv];
        }else{
          ll less = k / gcdNum;
          ll cnt = 0;
          while(cnt < k){
            ans += x.second * mpy[cnt];
            cnt += less;
          }
        }

      }
    }
    cout << ans << endl;
  }

  // rep(i,12) cout << modinv(i,12) << " ";
  // cout << endl;
  // rep(j,m) {
  //   rep(i,n){
  //     cout << x[i] * y[j] % k << " ";
  //   }
  //   cout << endl;
  // }

  return 0;
}
0