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的更多相关文章

  1. [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 ...

  2. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  3. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  4. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  5. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  6. 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 ...

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. 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 ...

  9. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

随机推荐

  1. uboot中的快捷菜单的制作说明 【转】

    转自:http://blog.chinaunix.net/uid-22030783-id-366971.html   在uboot中加入快捷操作菜单的方法非常简单,在论坛发布的uboot201003V ...

  2. php- post表单 input name属性的问题

    <input type='text'  style='width: 99px' name='deptNo'></td> name为字符串的时候传递的是单个字符串 <inp ...

  3. 【转】Visual Studio——多字节编码与Unicode码

    多字节字符与宽字节字符 1) char与wchar_t 我们知道C++基本数据类型中表示字符的有两种:char.wchar_t. char叫多字节字符,一个char占一个字节,之所以叫多字节字符是因为 ...

  4. Windows2008 r2 x64下安装FTP工具File Zilla server报错:could not load tls libraries filezilla

    安装file zilla server的时候报错: could not load tls libraries filezilla 搜索了下发现是新版本有这个问题,降低到0.9.43就没这个问题了

  5. jumperserver3.0的安装部署

    适用于jumperserver版本:v0.3.1-2  官网:http://www.jumpserver.org/ 系统:centos7.2 基本安装 备注:如果是centos系统最好使用基本安装,否 ...

  6. cocos creator 的scorllview 滑动事件和 子内容触摸事件会产生冲突

    1:问题描叙: UI上的 scorllview 的子元素需要拖动到游戏场景.所以子元素需要绑定触摸事件,scorllview 默认的事件处理方式就会和子元素的触摸事件冲突.2:解决方案: Scroll ...

  7. 中文多分类 BERT

    直接把自己的工作文档导入的,由于是在外企工作,所以都是英文写的 Steps: git clone https://github.com/google-research/bert prepare dat ...

  8. github 推送代码

    一.所有更新一起推送 .git init //初始化本地仓库 . git add . //添加全部文件 .git commit -m 'add all the file' //提交修改 .git st ...

  9. bootstrap----几个插件网址

    1.SweetAlert (弹出框):https://github.com/t4t5/sweetalert 2.SweetAlert2 (弹出框):https://github.com/limonte ...

  10. JavaScript错误:Maximum call stack size exceeded错误

    错误的表面意思是,因为递归次数太多而内存溢出, 当然引起溢出的原因很多 找了下问题来源,发现引用了两个版本的jquery,在layout.cshtml母模块页中和视图中都引用了jq.导致循环调用,从而 ...