[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 ...
随机推荐
- 64位Win7系统WMware安装Mac OS
1. 准备工作 l VMWare Workstation,我的版本是 l MAC OS安装光盘镜像文件,种子地址 http://www.kuaipan.cn/file/id_611 ...
- java 基础(转自索宁)
一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...
- 整理一下odoo10在windows系统下部署的流程
odoo10环境搭建 所需依赖: Python3.5 odoo10.0 Node.js PostgreSQL 9.5 PyCharm 专业版 1.首先先安装好Python3.5,并设置好环境变量 2. ...
- C# 多种方式连接Oracle。
废话不多说直接正题: 首先我们先在Oracle数据库下建了一个用户叫做lisi,密码为lisi,在这个用户下建立一张表叫做“USERS”,在这个表下新增三个数据. 方式一:利用OleDb连接Oracl ...
- 配置mysql5.5主从复制、半同步复制、主主复制
mysql主服务器 192.168.8.40 mysql从服务器 192.168.8.41 全新配置过程(主和从数据库都没有数据): 主从复制主服务器设置: 1.改server-id 2.启 ...
- SIP 认证
理解SIP的认证 1. 认证和加密 认证(Authorization)的作用在于表明自己是谁,即向别人证明自己是谁.而相关的概念是MD5,用于认证安全.注意MD5仅仅是个hash函数而已,并不是 ...
- 单点登录SSO+鉴权
一.单点登录原理 1.登录 2.注销 --------------------------------------------------------------------------------- ...
- Linux服务器性能评估
一.影响Linux服务器性能的因素 1. 操作系统级 CPU 内存 磁盘I/O带宽 网络I/O带宽 2. 程序应用级 二.系统性能评估标准 影响性能因素 影响性能因素 评判标准 好 坏 糟糕 CPU ...
- 测试开发之Django——No6.Django模板中的标签语言
模板中的标签语言 1.if/else {% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即:变量存在,非空,不是布尔值假),系统会显示在{% if %} 和 {% endi ...
- jquery----扩展事件
常用事件 blur([[data],fn]) 失去焦点 focus([[data],fn]) 获取焦点( 搜索框例子) change([[data],fn]) 当select下拉框中的元素发生改变的时 ...