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. angularJS 使用自定义指令输出模板

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  2. 跨浏览器的CORS

    function createCORSRequest(method, url){ var xhr = new XMLHttpRequest(); if("withCredentials&qu ...

  3. python 2,3版本自动识别导入

     import sys if str(sys.version[0]) == "3":    from urllib.parse import quote_plus    from  ...

  4. 删除Git记录里的大文件

    删除Git记录里的大文件 仓库自身的增长 大多数版本控制系统存储的是一组初始文件,以及每个文件随着时间的演进而逐步积累起来的差异:而 Git 则会把文件的每一个差异化版本都记录在案.这意味着,即使你只 ...

  5. object-c全局变量

    跟c++一定,在.m里Obj*obj=NULL,在.h里extern Obj*obj 即可.

  6. tomcat中的Manager App帐号password管理

    tomcat根文件夹下的conf文件夹下有个tomcat-users.xml文件 填写内容例如以下 <? xml version='1.0' encoding='utf-8'? >< ...

  7. html-文本处理集-持续学习更新

    文件处理2:分区分块.有序无序 <!-- 申明HTML5版本 --> <!DOCTYPE html> <html lang="en"> < ...

  8. Apache: You don't have permission to access / on this server

    当我们需要使用Apache配置虚拟主机时,有可能会出现这个问题:Apache: You don't have permission to access / on this server # 同IP不同 ...

  9. Powershell对象条件查询筛选

    在 Windows PowerShell 中,与所需的对象数量相比,通常生成的对象数量以及要传递给管道的对象数量要多得多.可以使用 Format cmdlet 来指定要显示的特定对象的属性,但这并不能 ...

  10. python学习笔记2---函数

    函数主要是为了代码复用. 函数分为两种:系统库预定义函数,自定义函数. 函数格式: def functionName(): statement 函数调用: funtionName() 函数的参数:形参 ...