[CF920G]List Of Integers
Description:
\(t\)组询问,求第\(k\)个大于\(x\)且与\(p\)互质的数
Hint:
\(x,k,p<=1e6,t<=30000\)
Solution:
推出式子后,由于要求第k大,二分答案就好了
#include<bits/stdc++.h>
using namespace std;
const int mxn=1e6+5;
int tot,vis[mxn],mu[mxn],sum[mxn],p[mxn];
void sieve(int lim)
{
mu[1]=1;
for(int i=2;i<=lim;++i) {
if(!vis[i]) mu[i]=-1,p[++tot]=i;
for(int j=1;j<=tot&&p[j]*i<=lim;++j) {
vis[p[j]*i]=1;
if(i%p[j]==0) {
mu[p[j]*i]=0;
break;
}
mu[p[j]*i]=-mu[i];
}
}
for(int i=1;i<=lim;++i) sum[i]=sum[i-1]+mu[i];
}
int check(int x,int p)
{
int ans=0;
for(int i=1;i<=sqrt(p);++i)
if(p%i==0) {
ans+=mu[i]*(x/i);
if(i*i!=p) ans+=mu[p/i]*(x/(p/i)); //这里很重要,直接将O(n)的check优化到了O(√n)
}
return ans;
}
int main()
{
int t,x,p,k;
scanf("%d",&t); sieve(1000000);
while(t--) {
scanf("%d%d%d",&x,&p,&k);
int l=x+k-1,r=9e6+5;
while(l<r) {
int mid=(l+r)>>1;
if(check(mid,p)-check(x,p)>=k) r=mid;
else l=mid+1;
}
printf("%d\n",r);
}
return 0;
}
[CF920G]List Of Integers的更多相关文章
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- LeetCode 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
随机推荐
- JS实现双击内容变为可编辑状态
在一些网站上我们经常看到交互性很强的功能.一些用户资料可以直接双击出现文本框,并在此输入新的资料即可修改,无需再按确定按钮等.. 我在网上查了很多资料,但都有一个小bug,就是当获取焦点后,光标的位置 ...
- C++:UNREFERENCED_PARAMETER用法
原文地址:http://www.cnblogs.com/kex1n/archive/2010/08/05/2286486.html 作用:告诉编译器,已经使用了该变量,不必检测警告! 在VC编译器下, ...
- eclipse引入系统类库
引入系统类库1.第一步:项目,右键,build path,remove报错的类库 2.第二步:项目,右键,build path→Add Library→JRE System Library,Next3 ...
- 有pom.xml文件但是无法用maven构建问题
java项目转maven项目,要注意pom.xml文件中是否定义了JDK的版本,要与环境保持一致.项目,右键,configure,选择转换为maven项目即可.转换后,有三个位置需要注意: 1.Jav ...
- mysql字符串 转 int-double CAST与CONVERT 函数的用法
MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值.两者具体的语法如下: CAST(value as type); CONVERT(value, type) ...
- keepalived实现haproxy负载均衡器的高可用
一.keepalived简介 keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于,用来防止单点故障. 二.vrrp协议2.1 vrrp协议简介 在现实的网络环境中,两台需要通信 ...
- STM32应用实例六:与MS5837压力传感器的I2C通讯
MS5837压力传感器是一种可用于电路板上,适用于检测10-1200mbar压力范围的传感器,灵敏度非常高,理论上能够检测到0.01mbar的压力变化,实际使用过程中测试并无明显的变化. MS5837 ...
- Hibernate 常用jar包 分析
antlr-2.7.6.jar的作用 ANTLR (ANother Tool for Language Recognition) 是一个PCCTS制定的语言工具,它为他创建认定者,程序编译者,翻译者提 ...
- linux下各目录的作用
这么久了,一直觉得对于linux的运作情况还是懵懵懂懂的样子,刚才专门又看了一下 linux 下各目录的作用,记下来,以备以后再忘了. 下面内容来自:http://www.linuxidc.com/L ...
- vue修饰符学习
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...