• poj 2773 Happy 2006解题报告 <欧拉函数>


    链接:http://poj.org/problem?id=2773

    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
    
    题意:输入 M K 求第 K 个与 M 互素的数
    思路:因为 gcd( a, b ) == gcd( a+k*b, b ), 所以可以先用欧拉函数求出 M 内所有与 M 互素的数的个数再暴力搜索
    欧拉函数:http://www.cnblogs.com/jian1573/archive/2012/02/24/2366953.html
    代码:
     1 #include <stdio.h>
    2 #include <string.h>
    3 int gcd( int a, int b )
    4 {
    5 return b==0?a:gcd( b, a%b );
    6 }
    7
    8 int Eular( int a )
    9 {
    10 int l=1;
    11 for( int i=2; i*i<=a; ++i )
    12 {
    13 if( a%i==0 )
    14 {
    15 l *= ( i-1 );
    16 a /= i;
    17 while( a%i==0 )
    18 {
    19 l *= i;
    20 a /= i;
    21 }
    22 }
    23 }
    24 if( a>1 )
    25 l*=(a-1);
    26 return l;
    27 }
    28
    29 int main( )
    30 {
    31 int M, K;
    32 while( scanf( "%d%d", &M, &K ) != EOF )
    33 {
    34 if( M==1 )
    35 {
    36 printf("%d\n", K );
    37 continue;
    38 }
    39 int k=Eular(M);
    40 int t=K%k;
    41 if( t )
    42 {
    43 int n=0;
    44 for( int i=1; ;++i )
    45 {
    46 if( gcd( M, i )==1 )
    47 {
    48 n++;
    49 }
    50 if( n==t )
    51 {
    52 printf( "%d\n",K/k*M+i );
    53 break;
    54 }
    55 }
    56 }
    57 else
    58 {
    59 printf("%d\n", (K/k)*M-1 );
    60 }
    61 }
    62 return 0;
    63 }
     


  • 相关阅读:
    WML1.1[zt]
    Collections sort() 合并排序
    PowerBuliderconnect the oracle database
    window.location.reload;刷新
    PowerBuliderWorking with validation rules
    PowerBulider create database
    Jstl Core标签
    页面验证
    El表达式详解
    servlet的生命周期
  • 原文地址:https://www.cnblogs.com/jian1573/p/2367139.html
Copyright © 2020-2023  润新知