POJ2773 Happy 2006GCD+枚举

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2773 Happy 2006GCD+枚举相关的知识,希望对你有一定的参考价值。

Happy 2006
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 17883 Accepted: 6261

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

Source

POJ Monthly–2006.03.26,static

问题链接 POJ2773 Happy 2006
问题简述:(略)
问题分析:简单题,用GCD加枚举来解决。也可以用欧拉函数来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ2773 Happy 2006 */

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 1000000 + 1;
int prime[N];

int main()
{
    int m, k;
    while (scanf("%d%d", &m, &k) != EOF) {
        int cnt = 0;
        for (int i = 1; i <= m; i++)
            if(__gcd(m, i) == 1)
                prime[cnt++] = i;

        if(k % cnt)
            printf("%d\\n", k / cnt * m + prime[k % cnt - 1]);
        else
            printf("%d\\n", (k / cnt - 1) * m + prime[cnt - 1]);
    }

    return 0;
}

以上是关于POJ2773 Happy 2006GCD+枚举的主要内容,如果未能解决你的问题,请参考以下文章

POJ2773Happy2006题解--数论好题

poj_2773_Happy 2006

poj2773Happy 2006 欧几里德

POJ2773 Happy 2006容斥原理

POJ 2773 Happy 2006(欧几里德算法)

poj 2773 Happy 2006