[bzoj4916]神犇和蒟蒻

来自FallDream的博客,未经允许,请勿转载, 谢谢。


请你输出一个整数$A=\sum_{i=1}^N{\mu (i^2)}$;
请你输出一个整数$B=\sum_{i=1}^N{\varphi (i^2)}$;
1<=n<=10^9
 
第一问炸胡 输出1即可
第二问就是在问$F(n)=\sum_{i=1}^N{\varphi (i)*i}$
杜教筛即可,
$\sum_{i=1}^{n}\sum_{d|i}\mu(d)*i=\frac{n(n+1)(2n+1)}{6}$
$\sum_{i=1}^{n}\sum_{d|i}\mu(d)*d*(i/d)=\frac{n(n+1)(2n+1)}{6}$
$\sum_{i=1}^{n}i*F(\lfloor\frac{n}{i}\rfloor)=\frac{n(n+1)(2n+1)}{6}$
$F(n)=\frac{n(n+1)(2n+1)}{6}-\sum_{i=2}^{n}i*F(\lfloor\frac{n}{i}\rfloor)$
#include<iostream>
#include<cstdio>
#define MN 10000000
#define magic 9875321
#define inv2 500000004
#define inv6 166666668
#define mod 1000000007
using namespace std;
inline int read()
{
    int x = 0 , f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

bool b[MN+5];
int n,s[MN/5],num=0,phi[MN+5];

struct My_Map
{
    int head[magic+5],cnt;
    struct edge{int x,ans,next;}e[MN+5];
    void ins(int x,int ans)
    {
        int j=x%magic;
        e[++cnt]=(edge){x,ans,head[j]};head[j]=cnt;
    }
    int check(int x)
    {
        for(int i=head[x%magic];i;i=e[i].next)
            if(e[i].x==x) return e[i].ans;
        return -1;    
    }
}mp;
inline int Sum(int x){return 1LL*x*(x+1)%mod*inv2%mod;}
int calc(int x)
{
    if(x<=MN) return phi[x];    
    int Mp=mp.check(x);if(Mp!=-1) return Mp;
    int sum=1LL*x*(x+1)%mod*(2*x+1)%mod*inv6%mod;
    for(int i=2,last;i<=x;i=last+1)
    {
        last=x/(x/i);
        sum=((sum-1LL*(Sum(last)-Sum(i-1)+mod)*calc(x/i))%mod+mod)%mod;    
    }
    return mp.ins(x,sum),sum;
}

int main()
{
    phi[1]=1;
    for(int i=2;i<=MN;++i)
    {
        if(!b[i]) s[++num]=i,phi[i]=i-1;
        for(int j=1;s[j]*i<=MN;++j)
        {
            b[s[j]*i]=1;
            if(i%s[j]==0){phi[s[j]*i]=phi[i]*s[j];break;}
            else phi[s[j]*i]=phi[i]*(s[j]-1);
        }    
        phi[i]=(phi[i-1]+1LL*phi[i]*i)%mod;
    }
    n=read();
    printf("1\n%d\n",calc(n));
    return 0;
}
 
posted @ 2017-05-31 18:10  FallDream  阅读(1290)  评论(0编辑  收藏  举报