Happy 2006

Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
     

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
分析:第k个与m互质的数,对m求素因子后二分容斥即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,cnt,fac[maxn],cas;
ll x,y;
void init(int x)
{
    cnt=0;
    if(x%2==0){
        fac[++cnt]=2;
        while(x%2==0)x/=2;
    }
    for(int i=3;(ll)i*i<=x;i+=2)
    {
        if(x%i==0)
        {
            fac[++cnt]=i;
            while(x%i==0)x/=i;
        }
    }
    if(x>1)fac[++cnt]=x;
}
ll gao(ll x)
{
    ll ret=0;
    for(int i=1;i<(1<<cnt);i++)
    {
        ll num=0,now=1;
        for(int j=0;j<cnt;j++)
        {
            if(i&(1<<j))
            {
                ++num;
                now*=fac[j+1];
            }
        }
        if(num&1)ret+=x/now;
        else ret-=x/now;
    }
    return x-ret;
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        init(n);
        ll l=1,r=1e18,ret;
        while(l<=r)
        {
            ll mid=l+r>>1;
            if(gao(mid)>=m)ret=mid,r=mid-1;
            else l=mid+1;
        }
        printf("%lld\n",ret);
    }
    return 0;
}
posted @ 2017-01-30 17:42  mxzf0213  阅读(202)  评论(0编辑  收藏  举报