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. python实战===教你用微信每天给女朋友说晚安

    但凡一件事,稍微有些重复.我就考虑怎么样用程序来实现它. 这里给各位程序员朋友分享如何每天给朋友定时微信发送”晚安“,故事,新闻,等等··· ···最好运行在服务器上,这样后台挂起来更方便. 准备: ...

  2. zabbix系列(十)zabbix添加对zookeeper集群的监控

    1.应用场景描述 在目前公司的业务中,有部分ESB架构用ZooKeeper作为协同服务的场景,做好ZooKeeper的监控很重要. 2.ZooKeeper监控要点 系统监控 内存使用量    ZooK ...

  3. nagios监控客户端报错/usr/bin/perl^M: bad interpreter: No such file or directory

    nagios服务端监控客户端内存时发现监控不上 在客户端直接执行脚本,报错如下: # /usr/local/nagios/libexec/check_memory.pl -w 6% -c 3% -ba ...

  4. sqlserver2008r2数据库关联孤立账号的方法

    重新安装mssql数据库后可能有这样的问题,     在数据还原到其他服务器时,登录名会丢失,重建非常麻烦     特別是数据库用户较多的的情况,其实这个问题解决非常简单,     sql serve ...

  5. 转载:gc的概念,如果A和B对象循环引用,是否可以被GC?

    原文:https://www.cnblogs.com/zhchoutai/p/6784929.html ①首先说一下,GC里边在JVM其中是使用的ROOT算法,ROOT算法,什么称作为ROOT呢,就是 ...

  6. Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 inconsistent binding

    1.发生原因  springAOP 里面绑定参数出现错误  核对绑定参数的名称    核对 springAOP的版本 2.aop切面表达式写的有误

  7. Clipboard深度实践与采坑记录

    1.css禁止选择导致IOS无法复制 body{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: ...

  8. 根据条件返回相应值 decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

    decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值) [功能]根据条件返回相应值 [参数]c1, c2, ...,cn,字符型/数值型/日期型,必须类型相同或null 注 ...

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

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

  10. python+selenium九:ddt数据驱动

    第一种,测试数据放在Excel里面 test_Login: import unittestimport timeimport ddtimport osfrom selenium import webd ...