Coprime
来源:互联网 发布:js图片渐变轮换效果 编辑:程序博客网 时间:2024/05/16 15:38
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).
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.
Please follow the format of the sample output.
Sample Input
36 9 16 9 26 9 3
Sample Output
Case 1: 1Case 2: 5Case 3: 7
Author
Source
The 5th Guangting Cup Central China Invitational Programming Contest
思路:这题我是用的二分+容斥,令d为一个很大的数字,那么第k个与m和n同时互素的数一定在1~d里面,先判断1~h(h=(1+d)/2)内与m和
n同时互素的有多少个(用容斥定理进行判断),如果大于k就从(h~l)进行判断,如果小于k,就从1~h进行判断;直到等于k,这时候判断h--,直到1~h内与m和n互素的个数为k-1这时候返回h+1;
代码如下:
#include<iostream>#include<algorithm>using namespace std;typedef long long ll;int cnt,p[100];ll relatively_prime(ll m){ ll sum=0; for(ll i=1;i<(1<<cnt);i++) { ll s=1,l=0; for(int j=0;j<cnt;j++) { if(i&(1<<j)) { l++; s=s*p[j]; } } s=m/s; if(l%2) sum=sum-s; else sum=sum+s; } return m+sum;}//判断(1~m)内互素的个数;ll binary_find(ll l,ll h,ll n){ ll m=(l+h)/2,s; s=relatively_prime(m); if(s>n) return binary_find(l,m,n); else if(s<n) return binary_find(m,h,n); else { while(1) { m--; ll t=relatively_prime(m); if(t!=n) break; } return m+1; }}//二分;int main(){ ll t,n,m,k,flag,s; cin>>t; for(int i=1;i<=t;i++) { cin>>n>>m>>k; cnt=0; for(int j=2;j*j<=n||j*j<=m;j++) { flag=1; if(n%j==0&&j!=m) { flag=0; p[cnt++]=j; } if(m%j==0&&j!=n) { if(flag) p[cnt++]=j; } while(n%j==0) n=n/j; while(m%j==0) m=m/j; } if(n>1) p[cnt++]=n; if(m>1&&m!=n) p[cnt++]=m; sort(p,p+cnt); s=binary_find(1,1000000000000000,k); cout<<"Case "<<i<<": "<<s<<endl; } return 0;}
阅读全文
0 0
- Coprime
- Coprime
- 4314. Coprime
- 4314. Coprime
- HDU5072 Coprime
- hdu5072 Coprime
- Coprime Sequence
- Coprime Sequence
- Coprime HDU
- 1227. Coprime
- Coprime HDU
- Coprime HDU
- HDU5072 coprime
- hdu 3388 Coprime
- hdu 5072 Coprime(数论)
- HDU 5072 Coprime
- HDU 5072 Coprime
- HDU 5072 Coprime [数学]
- HDU 6069 数学题,区间素数筛
- 多校第四场 1003 HDU 6069 素数晒应用
- 结构体指针与函数
- 用户管理
- 数据分析:pandas分析链家网二手房信息
- Coprime
- html使用转义字符带来的问题
- 【Android】换行符(\n)在TextView中显示不正常的问题
- mysqlid进程使用了相同的InnoDB或者使用了同一个日志
- 友好城市
- 线程的生命周期
- 你可能需要为你的APP适配iOS11
- iOS常用的传值方式
- 自定义view实现单点拖动