• Happy 2006 poj2773


    Happy 2006
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 9049   Accepted: 3031

    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

    2006 1
    2006 2
    2006 3
    

    Sample Output

    1
    3
    5


     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<math.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 using namespace std;
     8 #define maxn 10000010
     9 bool a[maxn];
    10 int euler(int n)
    11 {
    12     int i,j,m=n,ann=n;
    13     memset(a,0,sizeof(a));
    14     int size=sqrt(m+0.5);
    15     for(i=2; i<=size; i++)
    16     {
    17         if(n%i==0)
    18         {
    19             ann=ann/i*(i-1);
    20             for(j=i; j<=m; j+=i)a[j]=1;
    21             while(n%i==0)n/=i;
    22         }
    23     }
    24     if(n>1)
    25     {
    26         ann=ann/n*(n-1);
    27         for(j=n; j<=m; j+=n)a[j]=1;
    28     }
    29     return ann;
    30 }
    31 int main()
    32 {
    33     int n,k,i,m,t,q;
    34     while(~scanf("%d%d",&n,&k))
    35     {
    36         m=euler(n);
    37         if(k%m==0)
    38         {
    39            t=k/m-1;
    40         }else t=k/m;
    41         k=k-m*t;
    42         q=0;
    43         for(i=1;i<=n;i++)
    44         {
    45             if(!a[i])q++;
    46             if(q==k)break;
    47         }
    48         printf("%lld
    ",(long long)i+n*t);
    49     }
    50 }
    View Code
  • 相关阅读:
    页面中多个小图片元素合成一个大图片之后用CSS调用
    腾讯设计中心博客
    php 配置 curl , gd , openssl , mbstring
    Apache开启Rewrite环境
    防止入侵:My SQL各种攻击方法大全
    Css背景图合并工具功能增强
    php防CC攻击代码
    网站地址栏的图标代码
    PHP漏洞全解(一)PHP网页的安全性问题
    用PHP实现飞信api接口发飞信短信
  • 原文地址:https://www.cnblogs.com/ERKE/p/3652824.html
Copyright © 2020-2023  润新知