結果

問題 No.2249 GCDistance
ユーザー SSRSSSRS
提出日時 2023-03-17 21:36:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,572 ms / 5,000 ms
コード長 905 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 171,784 KB
実行使用メモリ 121,576 KB
最終ジャッジ日時 2023-10-18 14:06:25
合計ジャッジ時間 33,008 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,234 ms
121,436 KB
testcase_01 AC 2,534 ms
121,576 KB
testcase_02 AC 2,536 ms
121,576 KB
testcase_03 AC 2,572 ms
121,576 KB
testcase_04 AC 2,299 ms
121,576 KB
testcase_05 AC 2,538 ms
121,576 KB
testcase_06 AC 2,522 ms
121,576 KB
testcase_07 AC 2,530 ms
121,576 KB
testcase_08 AC 2,148 ms
121,576 KB
testcase_09 AC 2,525 ms
121,576 KB
testcase_10 AC 2,465 ms
121,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int MAX = 10000000;
int main(){
  vector<bool> prime(MAX + 1, true);
  vector<int> mobius(MAX + 1, 1);
  for (int i = 2; i <= MAX; i++){
    if (prime[i]){
      mobius[i] *= -1;
      for (int j = i * 2; j <= MAX; j += i){
        mobius[j] *= -1;
        prime[j] = false;
      }
    }
  }
  for (int i = 2; i * i <= MAX; i++){
    for (int j = i * i; j <= MAX; j += i * i){
      mobius[j] = false;
    }
  }
  vector<long long> C(MAX + 1, 0);
  for (int i = 2; i <= MAX; i++){
    for (int j = i; j <= MAX; j += i){
      C[j] -= (j / i - 1) * mobius[i];
    }
  }
  for (int i = 2; i <= MAX; i++){
    C[i] += C[i - 1];
  }
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N;
    cin >> N;
    long long all = (long long) N * (N - 1) / 2;
    long long two = C[N];
    long long one = all - two;
    cout << one + two * 2 << endl;
  }
}
0