結果

問題 No.886 Direct
ユーザー QCFiumQCFium
提出日時 2019-08-20 12:27:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 299 ms / 4,000 ms
コード長 1,510 bytes
コンパイル時間 2,487 ms
コンパイル使用メモリ 187,932 KB
実行使用メモリ 26,760 KB
最終ジャッジ日時 2024-04-27 02:54:53
合計ジャッジ時間 5,170 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 51 ms
14,268 KB
testcase_24 AC 102 ms
15,472 KB
testcase_25 AC 40 ms
10,736 KB
testcase_26 AC 39 ms
13,132 KB
testcase_27 AC 178 ms
25,044 KB
testcase_28 AC 182 ms
24,376 KB
testcase_29 AC 41 ms
26,656 KB
testcase_30 AC 39 ms
26,656 KB
testcase_31 AC 292 ms
26,652 KB
testcase_32 AC 299 ms
26,716 KB
testcase_33 AC 298 ms
26,760 KB
testcase_34 AC 290 ms
26,712 KB
testcase_35 AC 294 ms
26,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define MOD 1000000007

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int solve(int h, int w) {
	if (h < w) std::swap(h, w);
	
	std::vector<std::pair<int, int> > pf_table(h + 1);
	for (int i = 2; i <= h; i++)
		if (!pf_table[i].first) for (int j = 2; j <= h / i; j++) pf_table[j * i] = {i, j};
	
	int res = ((int64_t) h * (w - 1) + (int64_t) w * (h - 1)) % MOD;
	for (int i = 1; i < w; i++) {
		static int facts[30];
		int n_fact = 0;
		for (int cur = i; ; ) {
			std::pair<int, int> next = pf_table[cur];
			if (!next.first) {
				if (cur > 1) facts[n_fact++] = cur;
				break;
			}
			facts[n_fact++] = next.first;
			cur = next.second;
		}
		// facts should be sorted in descending order here
		n_fact = std::unique(facts, facts + n_fact) - facts;
		
		int prod[1 << n_fact];
		for (int j = 0; j < n_fact; j++) prod[1 << j] = facts[j];
		prod[0] = 1;
		int64_t so_num = 0;
		int64_t so_sum = 0;
		for (unsigned int j = 0; j < 1 << n_fact; j++) {
			int cur = prod[j] = prod[j & j - 1] * prod[j & ~(j - 1)];
			int num = (h - 1) / cur;
			int64_t sum = (int64_t) num * (num + 1) / 2 * cur;
			if (__builtin_popcount(j) & 1) so_num -= num, so_sum -= sum;
			else so_num += num, so_sum += sum;
		}
		res = (res + (int64_t)(2 * (w - i)) * ((so_num * h - so_sum) % MOD)) % MOD;
	}
	
	return res;
}

int main() {
	int h = ri(), w = ri();
	std::cout << solve(h, w) << std::endl;
	return 0;
}
0