1. 程式人生 > >LightOJ - 1370 Bi-shoe and Phi-shoe(歐拉函數)

LightOJ - 1370 Bi-shoe and Phi-shoe(歐拉函數)

php lightoj iostream printf cnblogs log esp color ++

題目鏈接:http://lightoj.com/volume_showproblem.php?problem=1370

題意:在數論,對正整數n,歐拉函數是小於n的正整數中與n互的數的數目(φ(1)=1)

題解:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int N=1111111;
 8 int phi[N],cost[N];
9 typedef long long LL; 10 11 void init(){ 12 //歐拉函數打表 13 memset(phi,0,sizeof(phi)); 14 memset(cost,0,sizeof(cost)); 15 for(int i=2;i<=N;i++){ 16 if(phi[i]) continue; 17 for(int j=i;j<=N;j+=i){ 18 if(!phi[j]) phi[j]=j; 19 phi[j]=phi[j]/i*(i-1
); 20 } 21 } 22 //枚舉花費 23 for(int i=1;i<=N;i++){ 24 for(int j=phi[i];j>=1;j--){ 25 if(cost[j]) break; 26 cost[j]=i; 27 } 28 } 29 } 30 31 int main(){ 32 init(); 33 int t,n,tmp; 34 scanf("%d",&t); 35
for(int i=1;i<=t;i++){ 36 LL ans=0; 37 scanf("%d",&n); 38 for(int j=1;j<=n;j++){ 39 scanf("%d",&tmp); 40 ans+=cost[tmp]; 41 } 42 printf("Case %d: %lld Xukha\n",i,ans); 43 } 44 return 0; 45 }

LightOJ - 1370 Bi-shoe and Phi-shoe(歐拉函數)