zoukankan      html  css  js  c++  java
  • 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
    ",ret);
        }
        return 0;
    }
  • 相关阅读:
    asp.net实现页面的一般处理程序(CGI)学习笔记
    .NET下的状态(State)模式 行为型模式
    (插件Plugin)AssemblyLoader解决方案(插件开发)
    SQL基础编写基本的SQL SELECT 语句
    在查询语句中使用NOLOCK和READPAST(ZT)
    C# 3.0语言增强学习笔记(一)
    ram,rom,flash
    自动激活你的ActiveX控件
    用C#编写ActiveX控件(二)
    用C#编写ActiveX控件(一)
  • 原文地址:https://www.cnblogs.com/dyzll/p/6358215.html
Copyright © 2011-2022 走看看