poj 2006 Happy2006

Happy 2006

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9936   Accepted: 3411

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
题目翻译:给出m,k,求第k个和m互质的数字!
解题思路:数据量较大,因此全部采用long long,想求出第k个互质的数,数据量太大,因此采用2分查找,来找d第K个数字,想知道当前数字t是第几个与m的互质的数,需要求出t前有几个与m不互质的数字,利用容斥原理,可以解决,然后对比即可!
#include<cstdio>
#define LL long long
LL p[20],top;
void getp(LL m){
    LL i;
    for(i=2,top=0;i*i<=m;i++)
        if(m%i==0){
            p[top++]=i;
            while(m%i==0) m/=i;
        }
    if(m>1) p[top++]=m;
}
LL nop(LL mid,LL t){
    LL i,sum=0;
    for(i=t;i<top;i++)
        sum+=mid/p[i]-nop(mid/p[i],i+1);
    return sum;
}
int main(){
    LL k,m;
    while(scanf("%lld%lld",&m,&k)==2){
        LL mid,l=0,r=0x3f3f3f3f3f3f3f3f,t,ans=0;
        getp(m);
        while(l<=r){
            mid=(l+r)>>1;
            t=mid-nop(mid,0);
            if(t>=k){
                r=mid-1;
                if(t==k) ans=mid;
            }
            else l=mid+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2025-01-15 09:14:47

poj 2006 Happy2006的相关文章

poj 2006 Litmus Test 【即zoj 2351:计算酸的PH】

公式:  pH = -log10 [H+]  PH值根据氢离子浓度求出 ,[这里的 [H+] 浓度是摩尔每升为单位的] Ka = [H+] [acid ions] / [acid]   平衡常数K等于分解的氢离子和酸根离子乘积与未分解的酸分子的比值 输入格式:   Ka :常数   ori :初始酸浓度    m :1摩酸分子完全溶解 分解出氢离子数    n :1摩酸分子完全溶解 分解出氢离子数[注意是完全溶解] [这题需要特别注意的是]指数形式是可以直接输入的.如用scanf 或者 prin

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 a

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(欧拉函数)

Happy 2006 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2773 Appoint description:  System Crawler  (2015-04-06) Description Two positive integers are said to be relatively prime to each other

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 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#素数筛选+容斥原理+二分

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>

【BZOJ】【1662】/【POJ】【3252】 【USACO 2006 Nov】Round Number

数位DP 同上一题Windy数 预处理求个组合数 然后同样的方法,这次是记录一下0和1的个数然后搞搞 Orz cxlove 1 /************************************************************** 2 Problem: 1662 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:0 ms 7 Memory:1280 kb 8 **************************

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不互素的数个