結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2020-02-14 22:04:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,019 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 213,696 KB
実行使用メモリ 117,792 KB
最終ジャッジ日時 2024-04-27 19:39:54
合計ジャッジ時間 5,873 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int64_t calcGCD(int64_t a, int64_t b)
{
	while (a)
	{
		b %= a;
		std::swap(a, b);
	}
	return b;
}

int main()
{
	int N, M;
	int64_t K;
	char op;
	scanf("%d%d%lld %c", &N, &M, &K, &op);
	std::vector<int64_t> A(N), B(M);
	for (auto& e: B)
		scanf("%lld", &e);
	for (auto& e: A)
		scanf("%lld", &e);

	int64_t ans{};
	if (op == '+')
	{
		std::map<int64_t, int64_t> rest_a, rest_b;
		for (auto& e: A)
			rest_a[e % K]++;
		for (auto& e: B)
			rest_b[e % K]++;
		for (auto& e: rest_a)
			ans += e.second * rest_b[K - e.first];
		ans += rest_a[0] * rest_b[0];
	}
	else
	{
		std::map<int64_t, int64_t> gcd_a, gcd_b;
		for (auto& e: A)
			gcd_a[calcGCD(e, K)]++;
		for (auto& e: B)
			gcd_b[calcGCD(e, K)]++;

		for (int64_t i{1}; i * i <= K; i++)
			if (K % i == 0)
				gcd_b[i] += 0;
		for (auto& e: gcd_b)
			for (int64_t i{2 * e.first}; i <= K; i += e.first)
				gcd_b[e.first] += gcd_b[i];

		for (auto& e: gcd_a)
			ans += e.second * gcd_b[K / e.first];
	}
	printf("%lld\n", ans);

	return 0;
}
0