码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ - 1370 Bi-shoe and Phi-shoe(欧拉函数)

时间:2017-09-07 17:04:25      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:整数   space   正整数   and   str   for   include   iostream   php   

题目链接: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(欧拉函数)

标签:整数   space   正整数   and   str   for   include   iostream   php   

原文地址:http://www.cnblogs.com/Leonard-/p/7490287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!