题目链接

题意

给定 \(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. mysql的性能优化案例

    在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...

  2. Python知识点入门笔记——基本控制流程

    复合赋值语句 在Python中,可以使用一次赋值符号,给多个变量同时赋值:                  划重点:age_1,age_2 = age_2,age_1这种操作是Python独有的 i ...

  3. django之配置静态文件

    # 别名 STATIC_URL = '/static/' # 配置静态文件,名字必须是STATICFILES_DIRS STATICFILES_DIRS = [ os.path.join(BASE_D ...

  4. 721. Accounts Merge

    https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...

  5. BFS:CF356C-Compartments

    C. Compartments time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Birthday Paradox

    Birthday Paradox Sometimes some mathematical results are hard to believe. One of the common problems ...

  7. springMVC中接收数组参数

    方式一. 后台:public ResultBean queryItemRulesByItemIds(int userId, int[] itemIds) 方式二.

  8. IOS开发---菜鸟学习之路--(二十)-二维码扫描功能的实现

    本章将讲解如何实现二维码扫描的功能 首先在github上下载ZBar SDK地址https://github.com/bmorton/ZBarSDK 然后将如下的相关类库添加进去 AVFoundati ...

  9. leetcode 【 Linked List Cycle II 】 python 实现

    公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...

  10. LeetCode with Python -> String

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...