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. 一个优秀的 ring buffer 或 cycle buffer 的实现代码

    #define CIRCLE_BUFFSIZE 1024 * 1024#define min(x, y) ((x) < (y) ? (x) : (y)) struct cycle_buffer ...

  2. .NET CORE 1.1 迁移到.NET 2.0正式版

    以下操作参考官方文档 1:首先你需要升级到最新版的VS 2017 15.3 升级的地方在VS右上角有个黄色的更新提醒,如果没有请挂VPN或者重新下载一个新的. 2:第二步 和之前改.NET Frame ...

  3. 前景检测(1):VIBE

    参考:http://blog.csdn.net/zouxy09/article/details/9622285 看论文很多细节不明白,看这位博主实现的代码就能明白了.

  4. PHP替换指定字符串

    在PHP中,有两个函数可以实现字符串替换,strtr()和str_repalce()函数. 首先我们简单了解下strtr()函数的定义及语法. strtr:转换指定字符. 两个语法: 第一种语法: s ...

  5. js----DOM对象(3

    表格示例(取消,全选,反选): <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  6. cf1061c 普通dp题

    题解见https://blog.csdn.net/godleaf/article/details/84402128 这一类dp题是可以压缩掉一维空间的,本题枚举a1到an,枚举到ai时枚举ai的每个约 ...

  7. unittest中更多的测试用例

    随着软件功能的不断增加,对应的测试用例也会呈指数级增长.一个实现几十个功能的项目,对应的单 元测试用例可能达到上百个.如果把所有的测试用例都写在一个 test.py 文件中,那么这个文件会越来越臃肿, ...

  8. SqlServr分页存储过程的写法

    CREATE PROCEDURE [dbo].[GetDataByPager] ( --从第几条数据取 @startIndex INT, --分页的表 @tableName VARCHAR(50), ...

  9. Redis设置Key/value的规则定义和注意事项(附工具类)

    对于redis的存储key/value键值对,经过多次踩坑之后,我们总结了一套规则:这篇文章主要讲解定义key/value键值对时的定义规则和注意事项. 前面一篇文章讲了如何定义Redis的客户端和D ...

  10. AOJ 2249 Road Construction (dijkstra)

    某国王需要修路,王国有一个首都和多个城市,需要修路.已经有修路计划了,但是修路费用太高. 为了减少修路费用,国王决定从计划中去掉一些路,但是需要满足一下两点: 保证所有城市都能连通 所有城市到首都的最 ...