题目链接:http://lightoj.com/volume_showproblem.php?problem=1370
题意:给你n个整数,第i个整数为Xi。定义phi(k)为k的欧拉函数值,设pi为满足phi(pi)>=Xi的最小整数,题目就是要求sum(p1,p2,p3,...,pn)
思路:对任意x,有prime[i]<=x<prime[i+1]必定有EulerPhi[x]<=prime[i],要满足phi(p)>=x那么p必定为x后面的第一个素数,进行素数打表即可。
code:
1 #include <cstdio> 2 #include <cstring> 3 typedef long long LL; 4 const int MAXN = 1000005; 5 6 int p[MAXN]; 7 bool isPrime[MAXN]; 8 9 void init() 10 { 11 memset(isPrime, true, sizeof(isPrime)); 12 isPrime[0] = false; 13 isPrime[1] = false; 14 // isPrime[1000001] = true; 15 for (int i = 2; i * i < MAXN; ++i) { 16 if (isPrime[i]) { 17 int j = i * i; 18 while (j < MAXN) { 19 isPrime[j] = false; 20 j += i; 21 } 22 } 23 } 24 int k = 1000003; 25 for (int i = 1000000; i >= 1; --i) { 26 if (isPrime[i]) { 27 p[i] = k; 28 k = i; 29 continue; 30 } 31 p[i] = k; 32 } 33 } 34 35 int main() 36 { 37 init(); 38 int nCase; 39 scanf("%d", &nCase); 40 for (int cas = 1; cas <= nCase; ++cas) { 41 int n, k; 42 scanf("%d", &n); 43 LL ans = 0; 44 for (int i = 0; i < n; ++i) { 45 scanf("%d", &k); 46 ans += p[k]; 47 } 48 printf("Case %d: %lld Xukha ", cas, ans); 49 } 50 return 0; 51 }