2019南昌网络赛G. tsy's number

题意:\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\frac{\phi(i)*\phi(j^2)*\phi(k^3)}{\phi(i)*\phi(j)*\phi(k)}\phi(gcd(i,j,k))\),1e4组询问,每次给\(n(1<=n<=1e7)\).
题解:由\(\phi(x)\)的性质\(x=p_1^{k_1}*p_2^{k_2}*...*p_n^{k_n}\),\(\phi(x)=p_1^{k_1-1}*(p_1-1)*p_2^{k_2-1}*(p_2-1)...*p_n^{k_n-1}*(p_n-1)\),\(\phi(x^2)=p_1^{2*k_1-1}*(p_1-1)*p_2^{2*k_2-1}*(p_2-1)...*p_n^{2*k_n-1}*(p_n-1)\).
\(\phi(x^2)=x*\phi(x)\).
\(\phi(x^3)=x^2*\phi(x)\).
原式化为:
\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^nj*k^2*\phi(gcd(i,j,k))\)
\(=\sum_{d=1}^n\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^nj*k^2*\phi(d)[gcd(i,j,k)==d]\)
\(=\sum_{d=1}^n\phi(d)\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^nj*k^2*[gcd(i,j,k)==d]\)
\(f(n)=\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^nj*k^2*[gcd(i,j,k)==d]\).
\(F(n)=\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^nj*k^2*[d|gcd(i,j,k)]\)
\(F(n)=d^3*\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{n}{d} \right \rfloor}j\sum_{k=1}^{\left \lfloor \frac{n}{d} \right \rfloor}k^2\)
\(F(n)=d^3*\frac{{\left \lfloor \frac{n}{d} \right \rfloor}^3*({\left \lfloor \frac{n}{d} \right \rfloor}+1)^2*({\left \lfloor \frac{n}{d} \right \rfloor}*2+1)}{12}\)
由莫比乌斯反演
\(f(n)=\sum_{x=1}^{{\left \lfloor \frac{n}{d} \right \rfloor}}\mu(x)*(d*x)^3*\frac{{\left \lfloor \frac{n}{d*x} \right \rfloor}^3*({\left \lfloor \frac{n}{d*x} \right \rfloor}+1)^2*({\left \lfloor \frac{n}{d*x} \right \rfloor}*2+1)}{12}\)
原式化为:
\(\sum_{d=1}^n\phi(d)*\sum_{x=1}^{{\left \lfloor \frac{n}{d} \right \rfloor}}\mu(x)*(d*x)^3*\frac{{\left \lfloor \frac{n}{d*x} \right \rfloor}^3*({\left \lfloor \frac{n}{d*x} \right \rfloor}+1)^2*({\left \lfloor \frac{n}{d*x} \right \rfloor}*2+1)}{12}\)
\(=\sum_{t=1}(t)^3*\frac{{\left \lfloor \frac{n}{t} \right \rfloor}^3*({\left \lfloor \frac{n}{t} \right \rfloor}+1)^2*({\left \lfloor \frac{n}{t} \right \rfloor}*2+1)}{12}\sum_{d|t}\phi(d)*\mu(\frac{t}{d})\)
假设\(g(n)=\sum_{d|n}\phi(d)*\mu(\frac{n}{d})\),g是积性函数,考虑线筛求g,只需考虑递推求\(g(p^k)\)
\(g(p^k)=\sum_{i=0}^k\phi(p^i)*\mu(p^{k-i})\)
由莫比乌斯函数性质\(g(p^k)=phi(p^k)*\mu(1)+phi(p^{k-1})*\mu(p)=\phi(p^k)-\phi(p^{k-1})\)
\(k==1\)\(g(p)=p-2\).
\(k!=1\)\(g(p^k)=p^{k-2}*(p-1)^2\)
线筛求g,之后和\(i^3\)一起求前缀和,然后每次询问分块即可
注意模数是\(2^{30}\)没法直接求12的逆元,要先除去4再求3和模数的逆元(扩展gcd)

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1073741824
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=10000000+10,maxn=1000000+10,inf=0x3f3f3f3f;

ll f[N];
int prime[N],cnt,num[N];
bool mark[N];
void init()
{
    f[1]=1;
    for(int i=2;i<N;i++)
    {
        if(!mark[i])prime[++cnt]=i,f[i]=i-2,num[i]=1;
        for(int j=1;j<=cnt&&i*prime[j]<N;j++)
        {
            mark[i*prime[j]]=1;
            if(i%prime[j]==0)
            {
                num[i*prime[j]]=num[i]+1;
                if(num[i]==1)
                {
                    if(prime[j]==2)f[i*prime[j]]=f[i/prime[j]];
                    else f[i*prime[j]]=f[i]/(prime[j]-2)*(prime[j]-1)*(prime[j]-1);
                }
                else f[i*prime[j]]=f[i]*prime[j];
                break;
            }
            f[i*prime[j]]=f[i]*f[prime[j]];
            num[i*prime[j]]=1;
        }
    }
    for(int i=1;i<N;i++)
        f[i]=(f[i-1]+f[i]*i%mod*i%mod*i%mod)%mod;
}
int main()
{
    ll gan=715827883;
    init();
    int T;scanf("%d",&T);
    while(T--)
    {
        int n;scanf("%d",&n);
        ll ans=0;
        for(int i=1,j;i<=n;i=j+1)
        {
            j=n/(n/i);
            ll te=n/i,pp=(f[j]-f[i-1]+mod)%mod;
            if(te%2==0)
            {
                add(ans,(te/2)*(te/2)%mod*te%mod*(te+1)%mod*(te+1)%mod*(te*2+1)%mod*pp%mod*gan%mod);
            }
            else
            {
                add(ans,te*te%mod*te%mod*((te+1)/2)%mod*((te+1)/2)%mod*(te*2+1)%mod*pp%mod*gan%mod);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
/********************

********************/
posted @ 2019-04-21 09:56  walfy  阅读(962)  评论(0编辑  收藏  举报