結果

問題 No.2249 GCDistance
ユーザー umezoumezo
提出日時 2023-03-18 03:57:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,138 ms / 5,000 ms
コード長 1,248 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 209,744 KB
実行使用メモリ 120,408 KB
最終ジャッジ日時 2023-10-18 17:03:43
合計ジャッジ時間 29,504 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,073 ms
120,408 KB
testcase_01 AC 2,130 ms
120,408 KB
testcase_02 AC 2,133 ms
120,408 KB
testcase_03 AC 2,120 ms
120,408 KB
testcase_04 AC 2,102 ms
120,408 KB
testcase_05 AC 2,116 ms
120,408 KB
testcase_06 AC 2,133 ms
120,408 KB
testcase_07 AC 2,138 ms
120,408 KB
testcase_08 AC 2,081 ms
120,408 KB
testcase_09 AC 2,128 ms
120,408 KB
testcase_10 AC 2,120 ms
120,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

//各整数の最小素因数
template<typename T>
vector<T> smallest_prime_factors(T n){
  vector<T> spf(n+1);
  for(int i=0;i<=n;i++) spf[i]=i;

  for(T i=2;i*i<=n;i++){
    if(spf[i]==i){ //素数だったら
      for(T j=i*i;j<=n;j+=i){ 
        if(spf[j]==j) spf[j]=i; //iを持つ整数かつまだ素数が決まっていないなら
      }
    }
  }
  return spf;
}

//高速因数分解(クエリ<=10^5,整数<=10^6程度)
template<typename T>
set<T> factolization(T x,vector<T> &spf){
  set<T> ret;
  while(x!=1) {
    ret.insert(spf[x]);
    x/=spf[x];
  }
  return ret;
}

template<typename T>
ll euler(T x,vector<T> &spf){
  ll tmp=x;
  auto A=factolization(x,spf);
  for(auto a:A){
    tmp/=a;
    tmp*=a-1;
  }
  return tmp;
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  constexpr int MAX=10000010; 
  auto spf=smallest_prime_factors(MAX);
  
  vector<ll> ANS(MAX);
  ll sum=0;
  for(int i=2;i<MAX;i++){
    sum+=2*i-2-euler(i,spf);
    ANS[i]=sum;
  }

  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    cout<<ANS[n]<<'\n';
  }

  return 0;
}
0