BZOJ3944——Sum

来源:互联网 发布:手机淘宝上支付宝在哪 编辑:程序博客网 时间:2024/06/05 20:08

1、题意:求μϕ的前缀和,n109
2、分析:tangjz无敌!

#include <map>#include <set>#include <cmath>#include <queue>#include <vector>#include <bitset>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <tr1/unordered_map>using namespace std;using namespace std::tr1;#define M 7000010#define LL long long#define MOD 1000000007#define inf 2147483647#define llinf 4000000000000000000ll#define For(i, x, y) for(int i = (x); i < (y); i ++)#define rep(i, x, y) for(int i = (x); i <= (y); i ++)#define drep(i, x, y) for(int i = (x); i >= (y); i --)inline int read(){    char ch=getchar();int x=0,f=1;    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}inline LL llread(){    char ch=getchar();LL x=0,f=1;    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}LL phi[M];int mu[M], prime[M / 10], tot;bool vis[M];unordered_map<int , LL> Phi;unordered_map<int , int> Mu;inline void init(){    mu[1] = 1; phi[1] = 1;    For(i, 2, M){        if(!vis[i]){            mu[i] = -1;            phi[i] = i - 1;            prime[++ tot] = i;        }        rep(j, 1, tot){            if(i * prime[j] >= M) break;            vis[i * prime[j]] = 1;            if(i % prime[j] == 0){                phi[i * prime[j]] = phi[i] * prime[j];                mu[i * prime[j]] = 0;                break;            }            phi[i * prime[j]] = phi[i] * (prime[j] - 1);            mu[i * prime[j]] = -mu[i];        }    }    For(i, 1, M) mu[i] += mu[i - 1], phi[i] += phi[i - 1];}inline LL solve_phi(int x){    if(x < M) return phi[x];    if(Phi[x]) return Phi[x];    LL ret = 1ll * x * (x + 1ll) >> 1ll;    rep(i, 2, x){        int la = x / (x / i);        ret -= 1ll * (la - i + 1) * solve_phi(x / i);        i = la; if(i == inf) break;    }    Phi[x] = ret;    return ret;}inline int solve_mu(int x){    if(x < M) return mu[x];    if(Mu[x]) return Mu[x];    int ret = 1;    rep(i, 2, x){        int la = x / (x / i);        ret -= (la - i + 1) * solve_mu(x / i);        i = la; if(i == inf) break;    }    Mu[x] = ret;    return ret;}int main(){    int T = read();    init();    while(T --){        int n = read();        cout << solve_phi(n) << " " << solve_mu(n) << endl;    }    return 0;}
0 0
原创粉丝点击