[cf920G][容斥原理+二分]
5 seconds
256 megabytes
standard input
standard output
Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.
You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).
The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.
Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 ≤ x, p, k ≤ 106).
Print t integers, where i-th integer is the answer to i-th query.
3
7 22 1
7 22 2
7 22 3
9
13
15
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
187
87
139
128
141
题意:求出与p互质并且>x的第k小数
题解:首先求出p所有的质因子,状压枚举出质因子相乘的情况,比如000001表示只有一个最小的质因子,则在1-y中有y/prime[1]个,再通过容斥即可求出与p不互质的个数,此时二分答案y即可
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
ll sol(ll x,vector<ll>&f){
ll res=;
int n=(int)f.size();
for(int i=;i<(<<n);i++)
{
ll now=,sgn=-;
for(int j=;j<n;j++)
if(i&(<<j))now*=f[j],sgn*=-;
res+=sgn*(x/now);
}
return x-res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
ll x,p,k;
scanf("%lld%lld%lld",&x,&p,&k);
vector<ll>g;
for(ll i=;i*i<=p;i++){
if(p%i==){
g.push_back(i);
while(p%i==){
p/=i;
}
}
}
if(p>)g.push_back(p);
ll r=1000000000000LL;
ll l=;
k+=sol(x,g);
ll ans;
while(l<=r){
ll mid=(l+r)/;
if(sol(mid,g)>=k){
ans=mid;
r=mid-;
}
else{
l=mid+;
}
}
printf("%lld\n",ans);
}
return ;
}
[cf920G][容斥原理+二分]的更多相关文章
- Bzoj 2440: [中山市选2011]完全平方数(莫比乌斯函数+容斥原理+二分答案)
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平 ...
- Happy 2006 POJ - 2773 容斥原理+二分
题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...
- POJ 2773 Happy 2006#素数筛选+容斥原理+二分
http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...
- HDU 3388 Coprime(容斥原理+二分)
Coprime Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- poj 2773 Happy 2006 容斥原理+二分
题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...
- BZOJ 2986: Non-Squarefree Numbers [容斥原理 二分]
题意:求第$n \le 10^{10}$个不是无平方因子数 二分答案, 容斥一下,0个质数的平方因子-1个..... 枚举$\sqrt$的平方因子乘上莫比乌斯函数,最后求出无平方因子数的个数取补集 # ...
- 【BZOJ】2440: [中山市选2011]完全平方数(莫比乌斯+容斥原理+二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=2440 我觉得网上很多题解都没说清楚...(还是我太弱了? 首先我们可以将问题转换为判定性问题,即给出 ...
- POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Descr ...
- POJ2773(容斥原理)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11458 Accepted: 4001 Descr ...
随机推荐
- HTTP权威指南-概述
URI 统一资源标识符 类似于邮件地址,邮箱. URL 统一资源定位符 URN 统一资源名 HTTP方法 get post put delete post head 状态码 200 OK 302 重定 ...
- 稀疏数组(java实现)
1.稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.1记录数组一共有几行几列,有多少个不同的值 1.2把具有不同值的元素的行列 ...
- 【Qt】Qt5.12编译MySQl5.7驱动(亲自测试成功)
目录 00. 目录 01. 安装Qt5.12 02. 打开MySQL源码项目 03. 编译MySQL驱动代码 04. 修改mysql.pro文件 05. 编译之后得到对应的库 06. 拷贝动态库到指定 ...
- PB笔记之调用数据窗口时的过滤条件添加方式
在PB查询数据窗口的数据时 通常可以有两种方式 一是在数据窗口事先写好查询条件,然后用retrieve()函数通过参数传递给数据窗口 这种方式适合查询条件较为简单,条件数较少的数据窗口 二是使用Set ...
- QQ、微信 唯一登陆设计
唯一登陆设计指一个账号可以在多个不同的客户端进行登陆,例如PC.Android.IOS等.每一个客户端就会生成一个对应的tokan,相当于生成三个token分别对应不同的客户端. 但是同一个客户端同时 ...
- C# 字符串按设置的格试在前面或后面增加空格或其它字符
public string lengadd(string stringa, string stringb, int count, int mode) //以stringa的长度,未到count的长度则 ...
- Go 操作 Mysql(二)
查询数据方法回顾整理 上一篇博客中,主要是快速过了一遍 demo 代码和 DB 类型对象中方法的使用 在整理查询数据方法的时候,使用了 Query() 方法,其实 sqlx 还提供了 QueryRow ...
- ubuntu下使用JNI Java调用C++的例子
TestJNI.java public class TestJNI { static{ System.load("/home/buyizhiyou/workspace/JNI/src/lib ...
- 安装HANA Rules Framework(HRF)
1. 收集文档 1.1 SAP HANA Rules Framework by the SAP HANA Academy link 1.2 HANA Rules Framework (HRF) b ...
- Axure总结
https://blog.csdn.net/xlegendxblog/article/details/54378705 1.常用符号 2.箭头的绘制 3.绘制流程图基本要点:流程路径不要交叉 4.对复 ...