hdu3388 Coprime【容斥原理】

简介: Problem Description Please write a program to calculate the k-th positive integer that is coprime with m and n simultaneously.
Problem Description
Please write a program to calculate the k-th positive integer that is coprime with m and n simultaneously. A is coprime with B when their greatest common divisor is 1.
 

Input
The first line contains one integer T representing the number of test cases.
For each case, there's one line containing three integers m, n and k (0 < m, n, k <= 10^9).
 

Output
For each test case, in one line print the case number and the k-th positive integer that is coprime with m and n.
Please follow the format of the sample output.
 

Sample Input
 
 
3 6 9 1 6 9 2 6 9 3
 
题目翻译:求同时与m,n互质的第k个数是多少!
解题思路:直接求m,n的最小公倍,然后通过二分查找找到第k个数!
不得不说题目数据的严密性,卡超时比较严,直接求m,n的最小公倍其实是花费不了很多时间的,gcd你懂得
但是求m*n的素因子时就不一样了,由于m,n较大,直接遍历m*n势必会花费很多时间,自然没有分开求m的素因子和n的素因子省时间,由于m,n可能存在相同的素因子,那么需要我们处理一下,我采取的是里哟办法set容器,排序去重,由于在set里面对数据的操作不方便,于是又把里面的元素取出放到数组里,接下来就是容斥原理+二分了!

#include <cstdio>
#include <set>
using namespace std;
#define LL long long
LL p[200],top;
set<LL>pp;
void getp(LL m){
    LL i;
    for(i=2,top=0;i*i<=m;i++)
        if(m%i==0){
            pp.insert(i);
            while(m%i==0) m/=i;
        }
    if(m>1) pp.insert(m);
}
void GETP(){
    top = 0;
    while(!pp.empty()){
        p[top++] = *pp.begin();
        pp.erase(pp.begin());
    }
}
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,n;
    int T,q=0;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld",&m,&n,&k);
        getp(m);
        getp(n);
        GETP();
        LL mid,l=0,r=0x3f3f3f3f3f3f3f3f,t;
        while(l<=r){
            mid=(l+r)>>1;
            t=mid-nop(mid,0);
            if(t>=k) r=mid-1;
            else l=mid+1;
        }
        printf("Case %d: %lld\n",++q,l);
    }
    return 0;
}


目录
相关文章
|
2月前
|
Java
hdu 1427 速算24点【暴力枚举】
hdu 1427 速算24点【暴力枚举】
22 0
|
5月前
hdu1406 完数 (水题)
hdu1406 完数 (水题)
27 0
HDU-1058,Humble Numbers(丑数打表)
HDU-1058,Humble Numbers(丑数打表)
HDU-1370,Biorhythms(中国剩余定理)
本题主要就是应用中国剩余定理。
|
C++ 人工智能 BI
HDU2032杨辉三角
有点强迫症,主函数必须简洁,但是这里的if判断语句很碍眼,自己也并没有想到什么不画蛇添足的方法使代码更加简洁......
1478 0
|
并行计算 算法 Java
HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53806    Accepted Submission(s): 20092 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。
1063 0
|
Java 测试技术
HDU 1248 寒冰王座(完全背包裸题)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17092    Accepted Submission(s): 8800 ...
1185 0
【HDU 4451 Dressing】水题,组合数
有衣服、裤子、鞋数量分别为n,m,k,给出p对不和谐的衣-裤或裤-鞋搭配,问一共有多少种和谐的衣裤鞋的搭配。 全部的组合有Cn1Cm1Ck1种。 设p对中有p1对衣-裤,p2对裤-鞋,则不和谐的搭配共有p1*Ck1+p2*Cn1种,但有被重复计算两次的搭配共p3对,它们引用了同一裤。
884 0