题意
求第 个无平方因子数。 组数据,。
想到幸运数字题,可以二分答案 ,求 内的无平方因子数数量。然后容斥, 中 的倍数有 个。答案就是
显然 中只有质数是有用的,因为有平方因子数一定能被一个质因子的平方整除。所以只保留质数即可。
分析一下时间复杂度。枚举质数 ,要求 即 ,所以只会搜索至多 个数。由此也可见 很小,递归层数不多。
事实上 ,所以二分上界不会很大。取 即可。时间复杂度 。
Code
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <random>
#include <map>
#include <ctime>
#include <iomanip>
// #define int long long
// #define lson (id << 1)
// #define rson (id << 1 | 1)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// const int N = ;
ll k;
vector<ll> P;
bool np[100005];
void calc(ll mx, int idx, int cnt, ll now, ll& res) {
if (now > mx) return;
(cnt & 1) ? res -= mx / now : res += mx / now;
for (int i = idx; i < (int)P.size() && now <= mx / P[i]; i ++) {
calc(mx, i + 1, cnt + 1, now * P[i], res);
}
}
bool check(ll x) {
ll res = 0; calc(x, 0, 0, 1, res);
return res >= k;
}
const bool TEST = false;
signed main() {
clock_t start = clock();
// freopen("", "r", stdin);
// freopen("", "w", stdout);
// ios::sync_with_stdio(0), cin.tie(0);
np[1] = 1;
for (int i = 2; i <= 100000; i ++) {
if (!np[i]) {
P.push_back(i);
}
for (int j = 0; j < (int)P.size() && P[j] * i <= 100000; j ++) {
np[P[j] * i] = 1;
if (i % P[j] == 0) break;
}
}
for (int i = 0; i < (int)P.size(); i ++) P[i] *= P[i];
int T; cin >> T;
while (T --) {
cin >> k;
ll l = 1, r = k * 2, res = 0;
while (l <= r) {
ll mid = (l + r) >> 1;
if (check(mid)) r = mid - 1, res = mid;
else l = mid + 1;
}
cout << res << endl;
}
if (TEST) cerr << "time used: " << (double)(clock() - start) / 1000 << "ms\n";
return 0;
}