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;
}


目录
相关文章
|
6月前
|
Java C++
poj 1503 高精度加法
把输入的数加起来,输入0表示结束。 先看我Java代码,用BigINteger类很多东西都不需要考虑,比如前导0什么的,很方便。不过java效率低点,平均用时600ms,C/C++可以0ms过。
16 1
|
9月前
AcWing 867. 分解质因数
AcWing 867. 分解质因数
|
5月前
hdu1406 完数 (水题)
hdu1406 完数 (水题)
27 0
|
6月前
|
图形学 C++
ZOJ1117 POJ1521 HDU1053 Huffman编码
Huffman编码的思想就是贪心,我们这里使用stl里的优先队列,priority_queue使用堆进行优化,虽然自己也可以写一个堆,但我感觉对于这道题有点主次不分了,再次感觉到stl确实是一个很强大的东西。
16 0
|
6月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
20 0
|
6月前
华为机试HJ100:等差数列
华为机试HJ100:等差数列
|
6月前
华为机试HJ88:扑克牌大小
华为机试HJ88:扑克牌大小
|
API
L - 小希的迷宫 HDU - 1272
L - 小希的迷宫 HDU - 1272
56 0
|
Java
「日更刷题」59.螺旋矩阵
「日更刷题」59.螺旋矩阵
49 0
LeetCode每日一题——812. 最大三角形面积
给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。
57 0
LeetCode每日一题——812. 最大三角形面积

热门文章

最新文章