题目链接

题意

给定 \(x,p,k\),求大于 \(x\) 的第 \(k\) 个与 \(p\) 互质的数。

思路

参考 蒟蒻JHY.

二分答案 \(y\),再去 \(check\) 在 \([x,y]\) 区间中是否有 \(k\) 个与 \(p\) 互质的数。

\(check\) 采用容斥,将 \(p\) 质因数分解,用这些质数组合成的数在 \([1,y]\) 范围内 容斥

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int f[210], a[10], b[210], cnt;
void init(int p) {
cnt = 0;
for (int i = 2; i * i <= p; ++i) {
if (!(p % i)) {
a[cnt++] = i;
while (!(p % i)) p /= i;
}
}
if (p != 1) a[cnt++] = p;
for (int i = 1; i < (1<<cnt); ++i) {
b[i] = 1;
for (int j = 0; j < cnt; ++j) if (i & (1<<j)) b[i] *= a[j];
}
}
int get(int x) {
int ret = 0;
for (int i = 1; i < (1<<cnt); ++i) ret += f[i] * (x/b[i]);
return x-ret;
}
void work() {
int x, p, k, ans;
scanf("%d%d%d", &x,&p,&k);
init(p);
int l = x+1, r = 1000000000, num = get(x);
while (l<=r) {
int mid = l+r>>1;
if (get(mid)-num >= k) ans = mid, r = mid-1;
else l = mid+1;
}
printf("%d\n", ans);
}
int main() {
f[0] = -1;
for (int i = 1; i < 128; ++i) f[i] = -f[i^(i&-i)];
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}

Codeforces 920G List Of Integers 二分 + 容斥的更多相关文章

  1. codeforces B. Friends and Presents(二分+容斥)

    题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值 ...

  2. BZOJ 2440 [中山市选2011]完全平方数 二分+容斥

    直接筛$\mu$?+爆算?再不行筛素数再筛个数?但不就是$\mu^2$的前缀和吗? 放...怕不是数论白学了$qwq$ 思路:二分+容斥 提交:两次(康了题解) 题解: 首先答案满足二分性质(递增), ...

  3. Codeforces 920G(二分+容斥)

    题意: 定义F(x,p)表示的是一个数列{y},其中gcd(y,p)=1且y>x 给出x,p,k,求出F(x,p)的第k项 x,p,k<=10^6 分析: 很容易想到先二分,再做差 然后问 ...

  4. Codeforces 920G - List Of Integers

    920G - List Of Integers 思路:容斥+二分 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  5. YYHS-分数(二分+容斥)

    题目描述 KJDH是个十分善于探索的孩子,有一天他把分子分母小于等于n的最简分数列在了纸上,他想找到这些分数里第k小的数,这对于KJDH来说当然是非常轻易,但是KJDH最近多了很多妹子,他还要去找妹子 ...

  6. 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数

    Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...

  7. 第k个互质数(二分 + 容斥)

    描述两个数的a,b的gcd为1,即a,b互质,现在给你一个数m,你知道与它互质的第k个数是多少吗?与m互质的数按照升序排列. 输入 输入m ,k (1<=m<=1000000;1<= ...

  8. poj2773(欧基里德算法 或 二分+容斥)

    题目链接:https://vjudge.net/problem/POJ-2773 题意:给定m,k,求与m互质的第k个数. 思路一:利用gcd(a,b)=gcd(b*t+a,b)知道,与m互质的数是以 ...

  9. Codeforces Round 450 D 隔板法+容斥

    题意: Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers ...

随机推荐

  1. django+xadmin在线教育平台(二)

    老话总是没错的,工欲善其事,必先利其器 教你安装pycharm,mysql,navicat,python相关环境. windows下搭建开发环境 2-1 pycharm,mysql,Navicat安装 ...

  2. Centos7之WEB服务器

    1.安装httpd服务 输入命令:yum -y install httpd [root@N37012 ~]# yum -y install httpc Loaded plugins: fastestm ...

  3. 使用paramiko报错:CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_poi

    1.paramiko不支持cryptography-2.6.1 pip3 uninstall cryptography==2.6.1 2.paramiko 支持cryptography-2.4.2 p ...

  4. nginx安装php环境

    1.php下载地址 https://secure.php.net/downloads.php(此次安装版本为7.0.33) 2.安装依赖的包 yum -y install libxml2 yum -y ...

  5. mysql中的FROM_UNIXTIME()函数和UNIX_TIMESTAMP()函数

    unix_timestamp 是时间戳,可以用数据库里的存储时间数据的字段 from_unixtime 是将时间戳格式化为你想要时间

  6. Intellij Idea 创建JavaWeb项目

    折腾Tomcat折腾了两个晚上,第一个晚上怎么都进不了Tomcat的首页,第二个晚上进去了,但是新建的Web项目,在浏览器中运行,总是 Error on Apache Tomcat: The requ ...

  7. 使用观察者模式更新Fragment的内容

    最近有个需求,就是在Fragment没有切换的时候(show,hide)更新Fragment显示的内容,想了一会,终于想到可以用观察者模式来解决这个问题的. 定义一个[被观察者(接口)]: publi ...

  8. Diycode开源项目 BaseApplication分析+LeakCanary第三方+CrashHandler自定义异常处理

    1.BaseApplication整个应用的开始 1.1.看一下代码 /* * Copyright 2017 GcsSloop * * Licensed under the Apache Licens ...

  9. Spring---环境搭建与包介绍

    jar包下载路径 首先需要下载Spring框架 spring-framework-5.0.0.RELEASE-dist,官方地址为https://repo.spring.io/release/org/ ...

  10. IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决

    (此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...