[暑假集训--数论]poj2773 Happy 2006

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

 

问与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互质,n+1~2n有phi[n]个数跟n互质……

所以先给phi[n]取个模,因为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[n],cur2=(m-1)/phi[n];
34     if (n==1){printf("%d\n",m);return;}
35     if (!cur1)cur1=phi[n];
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

 

转载于:https://www.cnblogs.com/zhber/p/7285237.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值