zoukankan      html  css  js  c++  java
  • [暑假集训--数论]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
    ",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
    ",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
  • 相关阅读:
    KDrive 與 Embedded Linux
    windows内存管理初探
    开源方案
    boot time 优化
    psplash
    linux下操纵大于2G文件
    【技术贴】Windows图片和传真查看器打开图片慢,正在生成预览的解决办法!
    【转】c++.primer.plus.第五版.中文版[下载]
    【技术贴】魂斗罗坦克Normal Tanks第五关以及第5、6、7、关的LICENCE CODE的查
    【转】奇文共欣赏,疑义相与析:原文转载《电脑维护技巧》(N条举措N条理由)并请大家交流研讨
  • 原文地址:https://www.cnblogs.com/zhber/p/7285237.html
Copyright © 2011-2022 走看看