結果

問題 No.3686 Coprime Sum
コンテスト
ユーザー hiromi_ayase
提出日時 2026-09-12 07:03:02
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 212 ms / 2,000 ms
+ 558µs
コード長 1,140 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,258 ms
コンパイル使用メモリ 375,520 KB
実行使用メモリ 47,828 KB
最終ジャッジ日時 2026-09-12 07:03:09
合計ジャッジ時間 6,586 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

#include <atcoder/all>
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define FAST_IO                \
  ios::sync_with_stdio(false); \
  cin.tie(0);
const i64 INF = 1001001001001001001;
using Modint = atcoder::static_modint<998244353>;

int main() {
  FAST_IO

  int N, M;
  cin >> N >> M;

  // gcd(a, b) = 1  sum(ab)
  // d|gcd(a,b) sum(ab * sum(mu(d)))
  // d|a, d|b  sum(ab * sum(mu(d)))

	vector<int> ps;
	vector<bool> comp(N + 1, 0);
	vector<int> mu(N + 1, 1);
	for(int i = 2; i <= N; ++i) {
		if(!comp[i]) {
			ps.push_back(i);
			mu[i] = -1;
		}

		for(auto p : ps) {
			if(i * p > N) break;
			comp[i * p] = 1;

			if(i % p == 0) {
				mu[i * p] = 0;
				break;
			}
			mu[i * p] = -mu[i];
		}
  }

  Modint ans = 0;
  Modint inv4 = Modint(4).inv();
  for (int d = 1; d <= min(N, M); d ++) {
    i64 p = N / d;
    i64 q = M / d;
    // (d + 2d + ... + pd) * (d + ... + qd)
    auto ps = Modint(1) * d * p * (p + 1);
    auto qs = Modint(1) * d * q * (q + 1);

    ans += ps * qs * mu[d] * inv4;
  }
  cout << ans.val() << endl;

}
0