G. List Of Integers

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.

You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).

Input

The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.

Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 ≤ x, p, k ≤ 106).

Output

Print t integers, where i-th integer is the answer to i-th query.

Examples

input

Copy

37 22 17 22 27 22 3

output

Copy

91315

input

Copy

542 42 4243 43 4344 44 4445 45 4546 46 46

output

Copy

18787139128141

题目链接:

https://codeforces.com/contest/920/problem/G

题意:

有t组询问,对于每一组询问,

给你三个整数x,p,k

问有在大于x的整数中,与p互质的第k小的数y是哪个?

思路:

对于每一组询问我们在区间\([x+1,1e9]\) 这个区间内,二分答案y

同时容斥定律可以求得区间\([l,r]\) 中与一个数num互质的数个数。——知识点[1]

那么我们可以求区间\([x+1,y]\)中与p互质的数个数与k比较,然后进行转移区间即可。

先筛出\(1e6\) 内的所有质数,然后\(log(p)\) 的时间复杂度去唯一分解询问中的p,然后二进制枚举+容斥定律辅助二分即可。

不会的话,建议先学一下知识点1,再来解决本题。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
// const int maxn = 1e7 + 50;
bool noprime[maxn + 50];
vector <int> p;
int getPrime()
{
// 华丽的初始化
memset(noprime, false, sizeof(noprime));
p.clear(); int m = (int)sqrt(maxn + 0.5);
// 优化的埃筛
for (int i = 2; i <= m; i++) {
if (!noprime[i]) {
for (int j = i * i; j <= maxn; j += i) {
noprime[j] = true;
}
}
}
// 把素数加到vector里
for (int i = 2; i <= maxn; i++) {
if (!noprime[i]) {
p.push_back(i);
}
}
//返回vector的大小
return p.size(); }
std::vector<ll> v;
void breakdown(ll n, ll len)
{
int pos = 0;
for (int i = 0; 1ll * p[i]*p[i] <= n && i < len; i++) {
if ( n % p[i] == 0) {
v.push_back(p[i]);
while (n % p[i] == 0) {
n /= p[i];
}
}
}
if ( n > 1) {
v.push_back(n);
} }
int x, pw, k;
int len ;
ll solve(ll l, ll r)
{
int maxstate = (1 << len) - 1;
ll ans = 0ll;
l--;
for (int i = 0; i <= maxstate; ++i) {
int num = 0;
ll p = 1ll;
for (int j = 0; j < len; ++j) {
if (i & (1 << j)) {
num++;
p *= v[j];
}
}
ans += (r / p - l / p) * ((num & 1) ? -1ll : 1ll);
}
return ans;
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int n;
int w = getPrime();
du1(n);
while (n--) {
du3(x, pw, k);
v.clear();
breakdown(pw, w);
len = sz(v);
ll l = x + 1ll;
ll r = 1e8;
ll mid;
ll ans;
while (l <= r) {
mid = (l + r) >> 1;
if (solve(x + 1ll, mid) >= k) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%lld\n", ans );
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)的更多相关文章

  1. Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)

    G. Castle Defense time limit per test 1.5 seconds memory limit per test 256 megabytes input standard ...

  2. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  3. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  4. Educational Codeforces Round 37 (Rated for Div. 2) G

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  5. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. Educational Codeforces Round 37 (Rated for Div. 2)

    我的代码应该不会被hack,立个flag A. Water The Garden time limit per test 1 second memory limit per test 256 mega ...

  8. [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)

    Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...

  9. 【Educational Codeforces Round 37 E】Connected Components?

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs. 用一个链表来记录哪些点已经确定在某一个联通快里了. 一开始每个点都能用. 然后从第一个点开始进行bfs. 然后对于它的所有 ...

随机推荐

  1. git命令自动补全

    git安装好后发现命令不能自动补全于是搜了很多方法,先按博客里面的方法试了下: 1.下载配置文件 git clone git://git.kernel.org/pub/scm/git/git.git ...

  2. mybatis ibatis 使用时出现 语法错误

    最近在使用mybatis的时候遇到一个问题,在执行数据库分页查询的时候报语法错误,但是检查sql之后并没有发现语法错误.在反复查询各种资料后(百度搜索‘mybatis分页’),最后发现是sql语句参数 ...

  3. 分布式消息通信之RabbitMQ Tutorials

    目录 官网 1 Hello World! 1.1 生产者demo producer 1.2 消费者demo consumer 1.3 查看queue队列中的信息 页面查看,可看到有4条消息 命令查看 ...

  4. 最新 科大讯飞java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.科大讯飞等10家互联网公司的校招Offer,因为某些自身原因最终选择了科大讯飞.6.7月主要是做系统复习.项目复盘.Leet ...

  5. 软件测试工具LoadRunner常见问题

    1.LoadRunner录制脚本时为什么不弹出IE浏览器? 当一台主机上安装多个浏览器时,LoadRunner录制脚本经常遇到不能打开浏览器的情况,可以用下面的方法来解决. 启动浏览器,打开Inter ...

  6. [NOI2019]序列

    LOJ3158 , Luogu5470 从 \(a_1\dots a_n\) , \(b_1\dots b_n\) 中各选出 \(K\) 个数 , 且至少 \(L\) 组下标在两个数组中都被选择 , ...

  7. axios对请求各种异常情况处理的封装

    前端网络请求封装 前端采用了axios来处理网络请求,为了避免在每次请求时都去判断各种各样的网络情况,比如连接超时.服务器内部错误.权限不足等等不一而足,我对axios进行了简单的封装,这里主要使用了 ...

  8. 什么是DataV数据可视化

    DataV数据可视化是使用可视化大屏的方式来分析并展示庞杂数据的产品.DataV旨让更多的人看到数据可视化的魅力,帮助非专业的工程师通过图形化的界面轻松搭建专业水准的可视化应用,满足您会议展览.业务监 ...

  9. HDU 4578 线段树玄学算法?

    Transformation 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4578 Problem Description Yuanfang is p ...

  10. 初识php语法

    初到一家php公司,由于之前做的java,现在记录一些学习php中的语法细节. =>的用法 => 是数组成员访问符号.在php中数组默认键名是整数,也可以自己定义任意字符键名(最好是有实际 ...