POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10309   Accepted: 3566

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

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

题目大意:求第k个与m互质的数

题目分析:两种方法,主流方法是分解质因数+二分+容斥,0ms过,先对m分解质因数,然后二分,二分时用容斥计算1到当前数字与m不互质的数的个数,然后用m减,就是与m互质的数的个数。

#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 1e6 + 6;
int fac[MAX];
int n, k, cnt;
ll sum;

void get_Factor(int x)
{
    cnt = 0;
    for(int i = 2; i <= x; i++)
    {
        if(x % i == 0)
            fac[cnt ++] = i;
        while(x % i == 0)
            x /= i;
    }
    if(x > 1)
        fac[cnt ++] = x;
}

void DFS(int pos, int num, ll cur, ll x)
{
    if(pos == cnt)
    {
        if(cur == 1)
            return;
        if(num & 1)
            sum += x / cur;
        else
            sum -= x / cur;
        return;
    }
    DFS(pos + 1, num, cur, x);
    DFS(pos + 1, num + 1, cur * fac[pos], x);
    return;
}

ll cal(ll mid)
{
    sum = 0;
    DFS(0, 0, 1, mid);
    return sum;
}

int main()
{
    while(scanf("%d %d", &n, &k) != EOF)
    {
        get_Factor(n);
        ll l = 0, r = 1e17, mid;
        while(l <= r)
        {
            mid = (l + r) >> 1;
            if(mid - cal(mid) < k)
                l = mid + 1;
            else
                r = mid - 1;
        }
        printf("%lld\n", l);
    }
}

第二种方法利用了欧几里德算法,gcd(a, b) = gcd(b, a % b),令a = km + i,b = m则有:

gcd(km + i, m) = gcd(i, m),说明与m互质的数对m取模有周期性,因此设小于m且与m互质的数有cnt个,第i个为ai,则第m*cnt+i个为m*cnt+ai,其次要注意k整除cnt的时候,取到的实际上是a[cnt]而不是a[0],这个方法跑了2400ms+

#include <cstdio>
int const MAX = 1e6 + 5;
int a[MAX];

int gcd(int a, int b)
{
    while(b)
    {
        int tmp = a;
        a = b;
        b = tmp % b;
    }
    return a;
}

int main()
{
    int m, k;
    while(scanf("%d %d", &m, &k) != EOF)
    {
        if(k == 1)
        {
            printf("1\n");
            continue;
        }
        if(m == 1)
        {
            printf("%d\n", k);
            continue;
        }
        int cnt = 1;
        for(int i = 1; i <= m; i++)
            if(gcd(i, m) == 1)
                a[cnt ++] = i;
        cnt --;
        if(k % cnt == 0)
            printf("%d\n", (k / cnt - 1) * m + a[cnt]);
        else
            printf("%d\n", (k / cnt) * m + a[k % cnt]);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 21:35:22

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)的相关文章

[二分+容斥原理] poj 2773 Happy 2006

题目链接: http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9131   Accepted: 3073 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.

POJ 2773 Happy 2006

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9170   Accepted: 3092 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 al

poj 2773 Happy 2006(欧拉函数应用)

http://poj.org/problem?id=2773 题意:输入n,k,求与n不互素的第k个数,k可能大于n. 思路:以n=6为例,与6互素的数有一定规律.{1,5},{7,12},{13,18}......,发现在[1,n],[n+1,n*2]......[m*n+1,(m+1)*n]区间内素数个数相同,且对应位置的数都相差n的整数倍.因此只要求出[1,n]内的与n互素的数即可.这个过程没必要一个一个枚举,可以用欧拉函数解决.因为欧拉函数已经求出了n的所有质因子,与n不互素的数都与n有

POJ 1091 跳蚤(分解质因数 + 容斥 + 大数)

跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8910   Accepted: 2676 Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字.跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向

POJ 2773 Happy 2006 (二分答案+容斥)

题目链接:http://poj.org/problem?id=2773 题意: 求第k个与m互质的数: 分析: 很明显随着数的增大与m互质的数就越多,因此我们可以二分答案, 中间需要用到容斥原理求[1,mid]内与m互质的数的个数: 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <vector> using namespace std; const int ma

POJ 2773 Happy 2006 二分+容斥(入门

题目链接:点击打开链接 题意: 输入n ,k 求与n互质的第k个数(这个数可能>n) 思路: solve(mid)表示[1,mid]中有多少个和n互质,然后二分一下最小的mid 使得互质个数==k solve(x) 实现: 与n互质的个数=所有数-与n不互质的数=所有数-(与n有一个因子-与n有2个因子的+与n有3个因子的) 状压n的因子个数,然后根据上面的公式容斥得到. #include <stdio.h> #include <iostream> #include <

POJ 2773 Happy 2006【容斥原理】

题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K,找到第k个与N互素的数(互素的数从小到大排列),其中 (1 <= m <= 1000000,1 <= K <= 100000000 ). 解题思路: K很大,直接从小到大枚举找出不现实,只能二分答案.二分枚举[1,INF]范围内所有的数x, 找到1~x范围内与N互素的数个数,如果等于K,则就是结果. 然后考虑1~x范围内与N互素的数个数 = x - 1~x范围内与N不互素的数个

POJ 2773 Happy 2006#素数筛选+容斥原理+二分

http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1,m]中与m互质的第k个数,先打表,找到m的所有质因数,然后用二分实现,最开始区间为[1,2^60],利用容斥原理去找区间[1,mid]内素数的个数t,不断进行二分,直到所查找的区间[l,r]内素数的个数t等于k,mid=l=r,则此时的l就是第k个与m互质的数. #include<iostream>

POJ2773---Happy 2006(容斥+二分)

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