hdu 2588 gcd 欧拉函数
2016-05-07 20:45
357 查看
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)[align=left]Problem Description[/align]
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.
[align=left]Input[/align]
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.
[align=left]Output[/align]
For each test case,output the answer on a single line.
[align=left]Sample Input[/align]
3
1 1
10 2
10000 72
[align=left]Sample Output[/align]
1
6
260
[align=left]Source[/align]
ECJTU 2009 Spring Contest
思路:跟poj 2480 差不多;
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define mod 1000000007 #define inf 999999999 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - '0' ; while( ( ch = getchar() ) >= '0' && ch <= '9' ) res = res * 10 + ( ch - '0' ) ; return res ; } #define MAXN 100001 ll prime[MAXN];//保存素数 ll vis[MAXN],ji;//初始化 ll Prime(ll n) { ll cnt=0; //memset(vis,0,sizeof(vis)); for(ll i=2;i<=n;i++) { if(!vis[i]) prime[cnt++]=i; for(ll j=0;j<cnt&&i*prime[j]<n;j++) { vis[i*prime[j]]=1; if(i%prime[j]==0)//关键 break; } } return cnt; } ll phi(ll n) { ll i,rea=n; for(i=0;i<ji;i++) { if(prime[i]*prime[i]>n)break; if(n%prime[i]==0) { rea=rea-rea/prime[i]; while(n%prime[i]==0) n/=prime[i]; } } if(n>1) rea=rea-rea/n; return rea; } int main() { ll x,y,z,i,t; ji=Prime(52010); int T; scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&x,&y); ll ans=0; for(i=1;i*i<=x;i++) { if(x%i==0) { ll gg=i; ll hh=x/i; if(gg>=y) ans+=phi(hh); if(gg!=hh&&hh>=y) ans+=phi(gg); } } printf("%I64d\n",ans); } return 0; }
View Code
相关文章推荐
- secure crt 基本设置
- CodeForces 598C Nearest vectors
- powerpc eldk:Invalid configuration ppc_6xx :machine ppc_6xx not recognized
- 插入排序法
- 结构体的偏移量计算
- Web前端性能优化(二)使用内容分发网络
- CodeForces 598B Queries on a String
- CodeForces 598A Tricky Sum
- Contest 2016-5-7 B(HDU 5616)
- 言叶にできない
- CodeForces 599D Spongebob and Squares
- MySQL在有索引列情况下 和 无索引情况下 select *的输出结果顺序
- Android半透明背景
- 小さな手のひら
- ASP.NET中数据库数据导入Excel并打印
- 学习进度条九
- CodeForces 599C Day at the Beach
- HDU A+B类型
- 周记一发
- C++第五次上机实验