結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー milanis48663220milanis48663220
提出日時 2020-04-11 22:34:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 1,907 bytes
コンパイル時間 1,082 ms
コンパイル使用メモリ 91,368 KB
実行使用メモリ 16,940 KB
最終ジャッジ日時 2023-10-19 18:54:06
合計ジャッジ時間 3,544 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 46 ms
7,728 KB
testcase_11 AC 17 ms
4,348 KB
testcase_12 AC 109 ms
4,608 KB
testcase_13 AC 13 ms
4,348 KB
testcase_14 AC 41 ms
4,484 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 59 ms
8,668 KB
testcase_17 AC 19 ms
4,348 KB
testcase_18 AC 109 ms
4,608 KB
testcase_19 AC 34 ms
4,348 KB
testcase_20 AC 164 ms
16,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
    }
}
0