Description:

\(t\)组询问,求第\(k\)个大于\(x\)且与\(p\)互质的数

Hint:

\(x,k,p<=1e6,t<=30000\)

Solution:

推出式子后,由于要求第k大,二分答案就好了

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int mxn=1e6+5;
  4. int tot,vis[mxn],mu[mxn],sum[mxn],p[mxn];
  5. void sieve(int lim)
  6. {
  7. mu[1]=1;
  8. for(int i=2;i<=lim;++i) {
  9. if(!vis[i]) mu[i]=-1,p[++tot]=i;
  10. for(int j=1;j<=tot&&p[j]*i<=lim;++j) {
  11. vis[p[j]*i]=1;
  12. if(i%p[j]==0) {
  13. mu[p[j]*i]=0;
  14. break;
  15. }
  16. mu[p[j]*i]=-mu[i];
  17. }
  18. }
  19. for(int i=1;i<=lim;++i) sum[i]=sum[i-1]+mu[i];
  20. }
  21. int check(int x,int p)
  22. {
  23. int ans=0;
  24. for(int i=1;i<=sqrt(p);++i)
  25. if(p%i==0) {
  26. ans+=mu[i]*(x/i);
  27. if(i*i!=p) ans+=mu[p/i]*(x/(p/i)); //这里很重要,直接将O(n)的check优化到了O(√n)
  28. }
  29. return ans;
  30. }
  31. int main()
  32. {
  33. int t,x,p,k;
  34. scanf("%d",&t); sieve(1000000);
  35. while(t--) {
  36. scanf("%d%d%d",&x,&p,&k);
  37. int l=x+k-1,r=9e6+5;
  38. while(l<r) {
  39. int mid=(l+r)>>1;
  40. if(check(mid,p)-check(x,p)>=k) r=mid;
  41. else l=mid+1;
  42. }
  43. printf("%d\n",r);
  44. }
  45. return 0;
  46. }

[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. 64位Win7系统WMware安装Mac OS

    1.         准备工作 l  VMWare Workstation,我的版本是 l  MAC OS安装光盘镜像文件,种子地址 http://www.kuaipan.cn/file/id_611 ...

  2. java 基础(转自索宁)

    一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...

  3. 整理一下odoo10在windows系统下部署的流程

    odoo10环境搭建 所需依赖: Python3.5 odoo10.0 Node.js PostgreSQL 9.5 PyCharm 专业版 1.首先先安装好Python3.5,并设置好环境变量 2. ...

  4. C# 多种方式连接Oracle。

    废话不多说直接正题: 首先我们先在Oracle数据库下建了一个用户叫做lisi,密码为lisi,在这个用户下建立一张表叫做“USERS”,在这个表下新增三个数据. 方式一:利用OleDb连接Oracl ...

  5. 配置mysql5.5主从复制、半同步复制、主主复制

    mysql主服务器 192.168.8.40 mysql从服务器 192.168.8.41 全新配置过程(主和从数据库都没有数据): 主从复制主服务器设置: 1.改server-id      2.启 ...

  6. SIP 认证

    理解SIP的认证 1. 认证和加密    认证(Authorization)的作用在于表明自己是谁,即向别人证明自己是谁.而相关的概念是MD5,用于认证安全.注意MD5仅仅是个hash函数而已,并不是 ...

  7. 单点登录SSO+鉴权

    一.单点登录原理 1.登录 2.注销 --------------------------------------------------------------------------------- ...

  8. Linux服务器性能评估

    一.影响Linux服务器性能的因素 1. 操作系统级 CPU 内存 磁盘I/O带宽 网络I/O带宽 2. 程序应用级 二.系统性能评估标准 影响性能因素 影响性能因素 评判标准 好 坏 糟糕 CPU ...

  9. 测试开发之Django——No6.Django模板中的标签语言

    模板中的标签语言 1.if/else {% if  %} 标签检查(evaluate)一个变量,如果这个变量为真(即:变量存在,非空,不是布尔值假),系统会显示在{% if  %} 和 {% endi ...

  10. jquery----扩展事件

    常用事件 blur([[data],fn]) 失去焦点 focus([[data],fn]) 获取焦点( 搜索框例子) change([[data],fn]) 当select下拉框中的元素发生改变的时 ...