POJ 2773 Happy 2006------欧几里得 or 欧拉函数。
2013-08-09 11:33
323 查看
Happy 2006
Description
Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are all relatively prime to 2006.
Now your job is easy: for the given integer m, find the K-th element which is relatively prime to m when these elements are sorted in ascending order.
Input
The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).
Output
Output the K-th element in a single line.
Sample Input
Sample Output
Source
POJ Monthly--2006.03.26,static
这题,以前做的时候用欧几里得,枚举,2300ms,这次用现在的思路,欧拉来做500ms。
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8359 | Accepted: 2737 |
Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are all relatively prime to 2006.
Now your job is easy: for the given integer m, find the K-th element which is relatively prime to m when these elements are sorted in ascending order.
Input
The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).
Output
Output the K-th element in a single line.
Sample Input
2006 1 2006 2 2006 3
Sample Output
1 3 5
Source
POJ Monthly--2006.03.26,static
这题,以前做的时候用欧几里得,枚举,2300ms,这次用现在的思路,欧拉来做500ms。
/* 题意:求第几个与N互素的数字。 周期性问题。 举例。 5的互素有:1.2,3,4 很明显: 第一个互素是1 第二个是 2 ...... 第五个是 6=5+1; 第六个是 8=6+2; 这里就存在着周期T. 1.需要注意对%==0 的时候的讨论。 2.M的值可以为1.要特判。否则对后面的/法,有影响,会RE的。 3.基本的思路也很简单,求出N的欧拉值,那么T就求出来了,然后 求出它的素数因子,扫一遍,找到余数的那个互素数。 */ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int opl[1000003]; int s[1000003]; int prime[1000003],len; int f[1000],flen; void make_prime()//素数打表 { int i,j; len=0; for(i=2;i<=1000000;i++) if(s[i]==false) { prime[++len]=i; for(j=i*2;j<=1000000;j=j+i) s[j]=true; } } void make_Euler()//欧拉函数[1,1000000]全部打表。 { int i,j; make_prime(); for(i=2;i<=1000000;i++) opl[i]=i; opl[1]=0; for(i=1;i<=len;i++) for(j=prime[i];j<=1000000;j=j+prime[i]) opl[j]=opl[j]/prime[i]*(prime[i]-1); } void make_dEuler(int n)//素因子装在f[] { int i; flen=0; for(i=2;i*i<=n;i++) if(n%i==0) { while(n%i==0) n=n/i; f[++flen]=i; } if(n!=1) f[++flen]=n; } int make_ini(int n,int k1) { int i,j; int num=0; make_dEuler(n); memset(s,false,sizeof(s)); for(i=1;i<=flen;i++) for(j=f[i];j<=n;j=j+f[i]) s[j]=true; for(i=1;i<=n;i++) if(s[i]==false) { num++; if(num==k1) return i; } } int main() { int n,m,sum,k,k1,T; make_Euler(); while(scanf("%d%d",&n,&m)>0) { if(n==1)//特判 { printf("%d\n",m); continue; } sum=0; T=opl ; if(m%T==0)//!!~ { sum=sum+n*((m-1)/T); sum=sum+make_ini(n,T); } else { sum=sum+n*(m/T); sum=sum+make_ini(n,m%T); } printf("%d\n",sum); } return 0; }
相关文章推荐
- Happy 2006 - POJ 2773 欧几里得
- POJ 2773-Happy 2006(欧拉函数)
- POJ 2773 Happy 2006 欧拉函数的应用
- poj 2773 Happy 2006——欧拉函数
- POJ 2773 Happy 2006 【gcd的性质(也可用欧拉函数)】
- poj 2773 Happy 2006解题报告 <欧拉函数>
- POJ 2773 Happy 2006 (公式法 or 二分容斥定理)
- POJ 2773 Happy 2006(求第k个和m互素的数/欧拉函数)
- POJ 2773 Happy 2006 欧拉函数
- poj 2773 Happy 2006(欧拉函数应用)
- poj 2773 Happy 2006(数论:欧拉函数)
- (Relax 1.15)POJ 2773 Happy 2006(欧拉函数的应用:求与n互质的第k个数)
- POJ 2773 Happy 2006(容斥原理)
- poj 2773 Happy 2006
- Happy 2006 (poj_2773) 欧几里德
- POJ_2773_happy_2006(欧拉公式)
- POJ 2773 Happy 2006(求第k个与m互素的数)
- poj 2773 Happy 2006
- POJ_2773_Happy 2006_ 欧几里得算法
- POJ 2773 Happy 2006