結果

問題 No.2249 GCDistance
ユーザー umezo
提出日時 2023-03-18 03:57:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,905 ms / 5,000 ms
コード長 1,248 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 200,584 KB
最終ジャッジ日時 2025-02-11 14:54:44
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

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