HRY and mobius
HRY and mobius
https://ac.nowcoder.com/acm/contest/874/L
HRY and mobius
对于k = 0 ,显然最后答案为,n。
对于k = 1,答案就是这就是经典的杜教筛问题了。
对于k = 2
/* Author : lifehappy */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e7 + 10; int prime[N], mu[N], sum[N], cnt; bool st[N]; void init() { mu[1] = 1; for(int i = 2; i < N; i++) { if(!st[i]) { prime[++cnt] = i; mu[i] = -1; } for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) { st[i * prime[j]] = 1; if(i % prime[j] == 0) { break; } mu[i * prime[j]] = -mu[i]; } } for(int i = 1; i < N; i++) { sum[i] = mu[i] + sum[i - 1]; } } unordered_map<ll, ll> ans_s; ll Djs(ll n) { if(n < N) return sum[n]; if(ans_s.count(n)) return ans_s[n]; ll ans = 1; for(ll l = 2, r; l <= n; l = r + 1) { r = n / (n / l); ans -= (r - l + 1) * Djs(n / l); } return ans_s[n] = ans; } ll calc(ll n) { ll ans = 0; for(ll i = 1; i * i <= n; i++) { ans += mu[i] * (n / i / i); } return ans; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); init(); int T; scanf("%d", &T); while(T--) { ll n, k; scanf("%lld %lld", &n, &k); if(k == 0) { printf("%lld\n", n); } else if(k & 1) { printf("%lld\n", Djs(n)); } else { printf("%lld\n", calc(n)); } } return 0; }