結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー square1001square1001
提出日時 2020-02-14 21:29:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,415 bytes
コンパイル時間 924 ms
コンパイル使用メモリ 82,120 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 01:43:44
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 33 ms
4,376 KB
testcase_11 AC 38 ms
4,380 KB
testcase_12 AC 141 ms
4,380 KB
testcase_13 AC 24 ms
4,376 KB
testcase_14 AC 50 ms
4,376 KB
testcase_15 AC 30 ms
4,380 KB
testcase_16 AC 42 ms
4,376 KB
testcase_17 AC 23 ms
4,376 KB
testcase_18 AC 137 ms
4,376 KB
testcase_19 AC 67 ms
4,376 KB
testcase_20 AC 93 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int gcd(int x, int y) {
	if (y == 0) return x;
	return gcd(y, x % y);
}
int main() {
	int N, M, K; string op;
	cin >> N >> M >> K >> op;
	vector<int> B(M), A(N);
	for (int i = 0; i < M; ++i) cin >> B[i], B[i] %= K;
	for (int i = 0; i < N; ++i) cin >> A[i], A[i] %= K;
	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	if (op == "+") {
		long long ans = 0;
		for (int i = 0; i < N; ++i) {
			int target = (K - A[i]) % K;
			ans += lower_bound(B.begin(), B.end(), target + 1) - lower_bound(B.begin(), B.end(), target);
		}
		cout << ans << endl;
	}
	else {
		vector<int> ds;
		for (int i = 1; i * i <= K; ++i) {
			if (K % i == 0) {
				ds.push_back(i);
				if (i * i != K) ds.push_back(K / i);
			}
		}
		sort(ds.begin(), ds.end());
		vector<int> ca(ds.size()), cb(ds.size());
		for (int i = 0; i < N; ++i) {
			int g = gcd(A[i], K);
			if (g == 0) g = K;
			++ca[lower_bound(ds.begin(), ds.end(), g) - ds.begin()];
		}
		for (int i = 0; i < M; ++i) {
			int g = gcd(B[i], K);
			if (g == 0) g = K;
			++cb[lower_bound(ds.begin(), ds.end(), g) - ds.begin()];
		}
		long long ans = 0;
		for (int i = 0; i < ds.size(); ++i) {
			for (int j = 0; j < ds.size(); ++j) {
				if (1LL * ds[i] * ds[j] % K == 0) {
					ans += 1LL * ca[i] * cb[j];
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}
0