jerry99的序列 --binary search, math
作者:互联网
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int N = 1e5 + 10; const int M = N - 10; int tot, vis[N], prime[N]; //#define LOCAL void sieve () { for (int i = 2; i <= M; ++i) { if (!vis[i]) prime[tot++] = i; for (int j = 0; j < tot; ++j) { if (1LL * prime[j] * i > M) break; vis[i * prime[j]] = 1; if (i % prime[j] == 0) break; } } } bool check(int mid, vector<pair<int, int>> &factor) { for (auto it : factor) { int prime = it.first; int cnt = it.second; i64 tmp = 1, res = 0; while (tmp <= mid) { tmp *= prime; res += mid / tmp; } #ifdef LOCAL cout << "res: " << res << "\n"; #endif if (res < cnt) return true; } return false; } int n, m; void solve() { if (n == 1) { cout << m << "\n"; return ; } int l = 1, r = m + 1; vector<pair<int, int>> factor; int tmp = n; for (int i = 0; i < tot && prime[i] * prime[i] <= tmp; ++i) { if (tmp % prime[i] == 0) { int cnt = 0; while (tmp % prime[i] == 0) { cnt++; tmp /= prime[i]; } factor.emplace_back(prime[i], cnt); } } if (tmp > 1) { factor.emplace_back(tmp, 1); } #ifdef LOCAL for (auto [x, y] : factor) { cout << x << ' ' << y << "\n"; } #endif while (l < r) { int mid = l + r >> 1; if (check(mid, factor)) { l = mid + 1; } else { r = mid; } } #ifdef LOCAL cout << "l: " << l << "\n"; #endif cout << m - l + 1 << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; sieve(); for (; t--;) { cin >> n >> m; solve(); } return 0; }
标签:prime,binary,search,--,mid,int,factor,tmp,LOCAL 来源: https://www.cnblogs.com/zrzsblog/p/16661510.html