結果

問題 No.2249 GCDistance
ユーザー tnakao0123tnakao0123
提出日時 2023-05-18 15:38:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,856 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 55,400 KB
実行使用メモリ 81,172 KB
最終ジャッジ日時 2023-08-22 10:14:21
合計ジャッジ時間 68,296 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2249.cc:  No.2249 GCDistance - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 10000000;
const int MAX_P = 3200;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

bool primes[MAX_P + 1];
ll ans[MAX_N + 1];

/* subroutines */

int gen_primes(int maxp, vi &pnums) {
  fill(primes, primes + maxp + 1, true);
  primes[0] = primes[1] = false;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums.push_back(p);
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums.push_back(p);
  return (int)pnums.size();
}

bool prime_decomp(int n, vi &pnums, vpii& pds) {
  pds.clear();

  int pn = pnums.size();
  for (int i = 0; i < pn; i++) {
    int pi = pnums[i];
    if (pi * pi > n) {
      if (n > 1) pds.push_back(pii(n, 1));
      return true;
    }

    if (n % pi == 0) {
      int fi = 0;
      while (n % pi == 0) n /= pi, fi++;
      pds.push_back(pii(pi, fi));
    }
  }
  return false;
}

int powi(int a, int b) {
  int p = 1;
  while (b > 0) {
    if (b & 1) p *= a;
    a *= a;
    b >>= 1;
  }
  return p;
}

int phi(int n, vi &pnums) {
  vpii pds;
  prime_decomp(n, pnums, pds);

  int p = 1;
  for (auto &pd: pds) {
    int e = powi(pd.first, pd.second - 1);
    int s = e * pd.first - e;
    p *= s;
  }
  return p;
}

/* main */

int main() {
  vi pnums;
  gen_primes(MAX_P, pnums);

  for (int n = 2; n <= MAX_N; n++) {
    int p = phi(n, pnums);
    int s = p + (n - 1 - p) * 2;
    ans[n] = ans[n - 1] + s;
  }

  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);
    printf("%lld\n", ans[n]);
  }
  return 0;
}
0