[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 ...
随机推荐
- ES系列十九、kibana基本查询、可视化、仪表盘用法
一. 定义索引模式匹配 1.前缀模糊匹配,一个模式匹配多个索引 每一个数据集导入到Elasticsearch后会有一个索引匹配模式,在上段内容莎士比亚数据集有一个索引名称为shakespeare,账户 ...
- 提高delete效率方法
1 open c_1; loop fetch c_1 bulk collect into t2 limit 100000; exit when c_1%notfound ...
- C# Excel使用NPOI
程序处理excel使用using Microsoft.Office.Interop.Excel方式,运行程序需要电脑安装excel,而且excel版本还需要一样,使用起来不方便.使用NPOI不用电脑安 ...
- sqlserver2008r2数据库关联孤立账号的方法
重新安装mssql数据库后可能有这样的问题, 在数据还原到其他服务器时,登录名会丢失,重建非常麻烦 特別是数据库用户较多的的情况,其实这个问题解决非常简单, sql serve ...
- 教你构建好 SpringBoot + SSM 框架
来源:Howie_Y https://juejin.im/post/5b53f677f265da0f8f203914 目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵 ...
- [转]centos7 下安装MongoDB
查看MongoDB的最新版官方下载地址: https://www.mongodb.com/download-center#community 使用wget命令下载安装包 wget https://fa ...
- 【linux】16进制格式查看命令hexdump
test.txt内容 asdfsg ewtwfsdf1Hello World! hexdump -Cv test.txt 输出 |asdfsg ewtwfsdf1| 6c 6c 6f 6f 6c ...
- 并发之线程封闭与ThreadLocal解析
并发之线程封闭与ThreadLocal解析 什么是线程封闭 实现一个好的并发并非易事,最好的并发代码就是尽量避免并发.而避免并发的最好办法就是线程封闭,那什么是线程封闭呢? 线程封闭(thread c ...
- python 全栈开发,Day16(函数第一次考试)
考试题 Python11 期第二次考试(基础数据类型与函数部分) 考试时长:3个小时 满分:105分 一,选择题(每题2分,共24分) 1.python不支持的数据类型有 A.char B.int C ...
- Navicat for Oracle 绿色版 连接 Oracle 12c
亲测可用,留下文章以备忘记. Navicat for Oracle 绿色版 地址 链接:http://pan.baidu.com/s/1qY5wMok 密码:kw06 解压到一个目录下. 然后还需要O ...