結果
問題 |
No.990 N×Mマス計算(Kの倍数)
|
ユーザー |
![]() |
提出日時 | 2020-04-11 22:34:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 126 ms / 2,000 ms |
コード長 | 1,907 bytes |
コンパイル時間 | 999 ms |
コンパイル使用メモリ | 91,464 KB |
実行使用メモリ | 16,512 KB |
最終ジャッジ日時 | 2024-09-19 15:10:11 |
合計ジャッジ時間 | 2,821 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 19 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef long long ll; template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); while (b != 0) { T tmp = b; b = a % b; a = tmp; } return a; } int N, M, K; char op; int A[100000]; int B[100000]; map<ll, ll> mpa, mpb; void solve_plus(){ ll ans = 0; for(int i = 0; i < M; i++) { int m = B[i]%K; if(mpb.count(m) == 0){ mpb[m] = 1; }else{ mpb[m]++; } } for(int i = 0; i < N; i++) { int m = A[i]%K; if(mpa.count(m) == 0){ mpa[m] = 1; }else{ mpa[m]++; } } for(auto i : mpa){ int rem = (K-i.first)%K; if(mpb.count(rem) != 0){ ans += ((ll)i.second)*mpb[rem]; } } cout << ans << endl; } void solve_minus(){ ll ans = 0; for(int i = 0; i < M; i++) { int m = gcd<int>(B[i], K); if(mpb.count(m) == 0){ mpb[m] = 1; }else{ mpb[m]++; } } for(int i = 0; i < N; i++) { int m = gcd<int>(A[i], K); if(mpa.count(m) == 0){ mpa[m] = 1; }else{ mpa[m]++; } } for(auto i : mpa){ for(auto j : mpb){ ll m = i.first; ll n = j.first; // cout << n << ' ' << m << endl; if((n*m)%K == 0){ ans += i.second*j.second; } } } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> N >> M >> K; cin >> op; for(int i = 0; i < M; i++) cin >> B[i]; for(int i = 0; i < N; i++) cin >> A[i]; if(op == '+'){ solve_plus(); }else{ solve_minus(); } }