LightOJ 1370 - Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/volume_showproblem.php?problem=1370

题意:给你n个数,每个数要找一个欧拉函数值大于等于这个数,并求和。

题解:就是素数打个表,欧拉函数值是 < n的最大互质个数,但这题可以等于,素数x的欧拉函数值是 x - 1,所以从x + 1 开始判断。这题是利用了欧拉函数的思想。

 1 #include<iostream>
 2 using namespace std;
 3 #define ll long long
 4 const int N = 1e6 + 10; 
 5 bool prime[N]; 
 6 
 7 void init(){
 8     for(int i = 0; i < N ;i++){
 9         prime[i] = true;
10     }
11     prime[1] = false;
12     for(int i = 2; i < N; i++){
13         for(int j = i + i; j <= N; j += i){
14             prime[j] = false;
15         }
16     }
17     
18 }
19 
20 int main(){
21     init();
22     /*
23     for(int i = 0 ; i < 10 ; i++){
24         cout<<prime[i]<<endl;
25     } */    
26     int T,n;
27     cin>>T;
28     
29     for(int t = 1; t <= T ; t++){
30         cin>>n;
31         ll ans = 0;
32         int x;
33         for(int i = 0 ; i < n; i++){
34              cin>>x;
35              for(int j = x+1 ;;j++){
36                  if(prime[j]){
37 //                     cout<<j<<endl;
38                      ans += j;
39                      break;
40                  }
41              }
42         }
43         
44         cout<<"Case "<<t<<": "<<ans<<" Xukha"<<endl;
45     }    
46     return 0;
47 }
View Code

猜你喜欢

转载自www.cnblogs.com/Asumi/p/8996219.html