why TLE 20
查看原帖
why TLE 20
906320
Milky_Cat楼主2024/7/26 16:17
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define int long long
using namespace std;
using namespace __gnu_pbds;
const int lm = 5000000;
int q, n, phi[lm + 5], mu[lm + 5], v[lm + 5];
gp_hash_table<int, bool> exist;
gp_hash_table<int, pair<int, int>> mp;
void sieve(int n){
	for (int i = 1; i <= n; i++){
		phi[i] = i;
		mu[i] = 1;
		v[i] = 0;
	}
	for (int i = 2; i <= n; i++){
		if (v[i])
			continue;
		phi[i] = i - 1;
		mu[i] = -1;
		for (int j = (i << 1); j <= n; j += i){
			phi[j] = (phi[j] / i * (i - 1));
			v[j] = 1;
			if ((j / i) % i == 0)
				mu[j] = 0;
			else
				mu[j] = -mu[j];
		}
	}
	for (int i = 1; i <= n; i++){
		phi[i] += phi[i - 1];
		mu[i] += mu[i - 1];
	} 
}
int Sphi(int n){
	if (n <= lm)
		return phi[n];
	int ans = (n * (n + 1) >> 1), l = 2, r;
	while (l <= n){
		r = n / (n / 1);
		ans -= (r - l + 1) * Sphi(n / l);
		l = r + 1;
	}
	return ans;
}
int Smu(int n){
	if (n <= lm)
		return mu[n];
	int ans = 1, l = 2, r;
	while (l <= n){
		r = n / (n / 1);
		ans -= (r - l + 1) * Smu(n / l);
		l = r + 1;
	}
	return ans;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	sieve(lm);
	cin >> q;
	while (q--){
		cin >> n;
		if (n <= lm){
		    cout << Sphi(n) << ' ' << Smu(n) << '\n';
		    continue;
		}
		if (!exist[n]){
			mp[n].first = Sphi(n);
			mp[n].second = Smu(n);
			exist[n] = true; 
		}
		cout << mp[n].first << ' ' << mp[n].second << '\n';
	}
	return 0;
}
2024/7/26 16:17
加载中...