[暑假集训--数论]poj2773 Happy 2006
2017-08-04 15:10
316 查看
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
问与n互质的第k大的数是多少
先算个x=phi(n),所以1到n内有x个数跟n互质
再讨论大于n的数:
对于y=tn+x,当x与n互质的时候,x也和n的所有因子互质。任取一个因子s,x%s != 0,那么(nt+x)%s != 0,所以y%s != 0,所以没有一个n的因子整除y,y和n互质
对于y=tn+x,当x与n不互质的时候,令s=gcd(x,n),s|x,则s|(tn+x),则s|y,所以y和n也不互质
所以y=tn+x跟n是否互质,可以转化为x跟n是否互质
所以在1~n有phi
个数跟n互质,n+1~2n有phi
个数跟n互质……
所以先给phi
取个模,因为m比较小点,所以剩下的直接暴力找到第k大的就行了
poj2773
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
问与n互质的第k大的数是多少
先算个x=phi(n),所以1到n内有x个数跟n互质
再讨论大于n的数:
对于y=tn+x,当x与n互质的时候,x也和n的所有因子互质。任取一个因子s,x%s != 0,那么(nt+x)%s != 0,所以y%s != 0,所以没有一个n的因子整除y,y和n互质
对于y=tn+x,当x与n不互质的时候,令s=gcd(x,n),s|x,则s|(tn+x),则s|y,所以y和n也不互质
所以y=tn+x跟n是否互质,可以转化为x跟n是否互质
所以在1~n有phi
个数跟n互质,n+1~2n有phi
个数跟n互质……
所以先给phi
取个模,因为m比较小点,所以剩下的直接暴力找到第k大的就行了
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 int n,m; 26 bool mk[1000010]; 27 int p[1000100],len; 28 int phi[1000010]; 29 int s[100010],len2; 30 bool pp[1000010]; 31 inline void work() 32 { 33 LL cur1=m%phi ,cur2=(m-1)/phi ; 34 if (n==1){printf("%d\n",m);return;} 35 if (!cur1)cur1=phi ; 36 if (n<=100000)for (int i=1;i<=n;i++)pp[i]=0; 37 else memset(pp,0,sizeof(pp)); 38 len2=0; 39 int t=n; 40 for (int i=1;i<=len;i++) 41 { 42 if (p[i]*p[i]>t)break; 43 if (t%p[i]==0) 44 { 45 s[++len2]=p[i]; 46 while (t%p[i]==0)t/=p[i]; 47 } 48 } 49 if (t!=1)s[++len2]=t; 50 int now=1; 51 for (int i=1;i<=n;i++) 52 { 53 if (now<=len2&&s[now]==i) 54 { 55 for(int j=2*i;j<=n;j+=i)pp[j]=1; 56 now++; 57 } 58 else if (!pp[i])cur1--; 59 if (cur1==0){printf("%lld\n",cur2*n+i);return;} 60 } 61 } 62 inline void getp() 63 { 64 for (int i=1;i<=1000000;i++)phi[i]=i; 65 for (int i=2;i<=1000000;i++) 66 if (!mk[i]) 67 { 68 phi[i]=i-1; 69 for (int j=2*i;j<=1000000;j+=i)mk[j]=1,phi[j]=phi[j]/i*(i-1); 70 p[++len]=i; 71 } 72 } 73 int main() 74 { 75 getp(); 76 while (~scanf("%d%d",&n,&m))work(); 77 }
poj2773
相关文章推荐
- [暑假集训--数论]poj2142 The Balance
- POJ2773 HAPPY 2006(不能对LL大方之战)
- 【周期性/容斥+二分】POJ2773-HAPPY 2006
- POJ 2773 Happy 2006 【数论,容斥原理+二分】
- [暑假集训--数论]poj2657 Comfort
- [暑假集训--数论]poj1730 Perfect Pth Powers
- [暑假集训--数论]poj3518 Prime Gap
- [暑假集训--数论]poj2909 Goldbach's Conjecture
- POJ2773-Happy 2006
- poj 2773 Happy 2006(数论:欧拉函数)
- POJ 2773 Happy 2006(数论)
- [暑假集训--数论]poj2262 Goldbach's Conjecture
- POJ 2773 Happy 2006 【数论,容斥原理+二分】
- [暑假集训--数论]poj1595 Prime Cuts
- 暑假集训-组合数学及数论
- [暑假集训--数论]poj2034 Anti-prime Sequences
- 【poj2773】 Happy 2006
- 【POJ2773】Happy 2006 欧几里德
- [暑假集训--数论]poj1365 Prime Land
- POJ2773 Happy 2006【容斥原理】