Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)
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 3output
Copy
91315input
Copy
542 42 4243 43 4344 44 4445 45 4546 46 46output
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';}} }轉載于:https://www.cnblogs.com/qieqiemin/p/11617911.html
總結
以上是生活随笔為你收集整理的Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GridView调用setAdapter
- 下一篇: Oracle学习笔记:通过种子数据库设置