程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ 2773 Happy 2006 (分解質因數+容斥+二分 或 歐幾裡德算法應用)

POJ 2773 Happy 2006 (分解質因數+容斥+二分 或 歐幾裡德算法應用)

編輯:關於C++

 

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 
#include 
#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
, 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 
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
);
            continue;
        }
        if(m == 1)
        {
            printf(%d
, 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
, (k / cnt - 1) * m + a[cnt]);
        else
            printf(%d
, (k / cnt) * m + a[k % cnt]);
    }
}


 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved