結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2020-02-14 23:02:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 2,376 ms
コンパイル使用メモリ 212,928 KB
実行使用メモリ 23,328 KB
最終ジャッジ日時 2023-08-10 02:26:28
合計ジャッジ時間 4,072 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 46 ms
8,544 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 80 ms
4,792 KB
testcase_13 AC 16 ms
4,380 KB
testcase_14 AC 34 ms
4,416 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 66 ms
10,656 KB
testcase_17 AC 16 ms
4,376 KB
testcase_18 AC 80 ms
4,616 KB
testcase_19 AC 39 ms
4,376 KB
testcase_20 AC 189 ms
23,328 KB
権限があれば一括ダウンロードができます

ソースコード

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::vector<int> div, count;
		div.reserve(10'000);
		for (int64_t i{1}; i * i <= K; i++)
			if (K % i == 0)
			{
				div.push_back(i);
				if (i * i != K)
					div.push_back(K / i);
			}
		std::sort(div.begin(), div.end());
		count.resize(div.size());
		
		for (auto& e: B)
			count[std::lower_bound(div.begin(), div.end(), K / calcGCD(e, K)) - div.begin()]++;
		for (int i{(int)div.size() - 1}; i >= 0; i--)
			for (int j{i + 1}; j < (int)div.size(); j++)
				if (div[j] % div[i] == 0)
					count[j] += count[i];
		
		for (auto& e: A)
			ans += count[std::lower_bound(div.begin(), div.end(), calcGCD(e, K)) - div.begin()];
	}
	printf("%lld\n", ans);

	return 0;
}
0