zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    vim 编辑器使用
    PHP高并发高负载系统架构(转载)
    类的使用
    linux下EC20 4G模块驱动移植
    linux 4G模块拨号脚本
    linux4.1.4上移植ME909s-821,MU609 4G模块驱动
    shell脚本语之运算符
    vim的列编辑操作
    linux下普通用户添加 sudo 免密码
    4G模块在AM335x上的移植
  • 原文地址:https://www.cnblogs.com/ERKE/p/3652824.html
Copyright © 2011-2022 走看看