Coprime

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 849    Accepted Submission(s): 232

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
 
Sample Output
Case 1: 1
Case 2: 5
Case 3: 7
这里有两个数n,m;这里用容斥原理同样可以求在1到任意范围内,和n,m两个数互质的个数。把n,m分别分解质因数,然后把相同的合并
和求一个数的本质没什么区别,套模版。然后就要去找最小的数x使得1到x与n,m互质的个数等于k,x就是答案。用二分去找n,上限直接设成
1<<62
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <algorithm> using namespace std;
typedef long long int LL;
const LL INF=(LL)1<<62;
#define MAX 1000000
bool check[MAX+5];
LL prime[MAX+5];
LL sprime[MAX+5];
LL q[MAX+5];
LL n,m,k,cnt;
void eular()//线性筛
{
memset(check,false,sizeof(check));
int tot=0;
for(int i=2;i<=MAX+5;i++)
{
if(!check[i])
prime[tot++]=i;
for(int j=0;j<tot;j++)
{
if(i*prime[j]>MAX+5) break;
check[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
void Divide(LL n,LL m)
{
cnt=0;
LL t=(LL)sqrt(1.0*n);
for(LL i=0;prime[i]<=t;i++)
{
if(n%prime[i]==0)
{
sprime[cnt++]=prime[i];
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n>1)
sprime[cnt++]=n;
t=(LL)sqrt(1.0*m);
for(LL i=0;prime[i]<=t;i++)
{
if(m%prime[i]==0)
{
sprime[cnt++]=prime[i];
while(m%prime[i]==0)
m/=prime[i];
}
}
if(m>1)
sprime[cnt++]=m;
}
LL Ex(LL n)//容斥原理
{
LL sum=0,t=1;
q[0]=-1;
for(LL i=0;i<cnt;i++)
{
LL x=t;
for(LL j=0;j<x;j++)
{
q[t]=q[j]*sprime[i]*(-1);
t++;
}
}
for(LL i=1;i<t;i++)
sum+=n/q[i];
return sum;
}
LL Binary()
{
LL l=1,r=INF;
LL ans,mid;
while(l<=r)
{
mid=(l+r)/2;
if((mid-Ex(mid))>=k)
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int cas=0;
eular();
while(t--)
{
scanf("%lld%lld%lld",&m,&n,&k);
if(n==1&&m==1)
{
printf("Case %d: %lld\n",++cas,k);
continue;
}
Divide(n,m);
sort(sprime,sprime+cnt);
int cot=1;
for(LL i=1;i<cnt;i++)
{
if(sprime[i]!=sprime[i-1])
{
sprime[cot++]=sprime[i];
}
}
cnt=cot;
printf("Case %d: %lld\n",++cas,Binary()); }
return 0;
}


 

HDU 3388 Coprime(容斥原理+二分)的更多相关文章

  1. HDU 4135 Co-prime(容斥原理)

    Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...

  2. hdu 5072 Coprime 容斥原理

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  3. hdu 3388 Coprime

    第一个容斥的题,感觉这东西好神啊.于是扒了一发题解2333 首先想对于[1,x]内有多少与n,m都互质的数,显然x是存在单调性的,所以可以二分一下. 那么互质的数的求法,就是x-存在n,m一个质因数的 ...

  4. POJ 2773 Happy 2006(容斥原理+二分)

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10827   Accepted: 3764 Descr ...

  5. Bzoj 2440: [中山市选2011]完全平方数(莫比乌斯函数+容斥原理+二分答案)

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平 ...

  6. Happy 2006 POJ - 2773 容斥原理+二分

    题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...

  7. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  8. HDU 5072 Coprime (单色三角形+容斥原理)

    题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  9. hdu 4135 Co-prime (素数打表+容斥原理)

    题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...

随机推荐

  1. 悟道—位IT高管20年的职场心经(读书笔记一)

    悟道--一位IT高管20年的职场心经 第一章  修炼! 修炼! 别跟我谈事业,先把工作做好. 别跟我说理想,先把饭碗端好: 志不可天天立.道必须日日修.没有实力,一切皆为妄谈. 修炼是硬道理. 1.1 ...

  2. js 获取距离顶部的相对高度

    getTop (e) { var offset=e.offsetTop; if(e.offsetParent!=null) offset+=this.getTop(e.offsetParent); r ...

  3. unity, access sprite of UGUI Image

    首先需要using UnityEngine.UI; 然后调用下面语句就不报错了: Image.GetComponent<Image>().sprite 参考:http://answers. ...

  4. 点滴积累【C#】---验证码,ajax提交

    效果: 思路: 借用ashx文件创建四位验证,首先生成四位随机数字.然后创建画布,再将创建好的验证码存入session,然后前台进行button按钮将文本框中的值进行ajax请求到后台,和sessio ...

  5. [css]后台管理系统布局

    知识点: 绝对定位+overflowhidden 整体思路 三大块 pg-header---需要固定 (height:48px) pg-content menu 右侧菜单-需要固定(width:200 ...

  6. android-退出动画无效

    在调用 overridePendingTransition(R.anim.anim_scale_in, R.anim.anim_scale_out); 方法设置某个Activity进入和退出动画的时候 ...

  7. python学习之getdefaultlocale()函数

    def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')) 返回一个二元组. >>> local ...

  8. Linux命令之rename

    一.引言 今天才知道Linux下的rename有两个版本,util-linux工具集的rename和Perl版本的rename,而两者的用法是明显不一样的,Perl版rename相对比较强大 二.对比 ...

  9. BootCamp支持软件6

    最新版本的 Boot Camp 6 苹果驱动支持的机型列表 苹果官方已经公布了 BootCamp 6 驱动支持的机型列表了,基本上 2012 年后的 Macbook / Pro / Air / iMa ...

  10. mysql—Access denied for user 'root'@'localhost' (using password:NO)

    安装mysql未设置初始密码,登录提示Access denied for user 'root'@'localhost' (using password:NO): 解决方案:  sudo /etc/i ...