HDU5528 迪利克雷卷积

被卡常了wuwuwu,这道题最后也没过,本机测试随机满数据不到2s,20000个1e9 4s

这种做法比较奇葩,我估计没人会这么做

这道题的本质是

\sum_{i|n}i*i-\phi (i)*\sigma (n/i)

左边那个希腊符号是欧拉函数,右边的是因子之和,都是积性函数,数论卷积后还是积性函数

所以n因式分解,化成答案变成几个积性函数的积

总复杂度 T*\sqrt{n}

啊,计蒜客上A了

详细的写下这道题的题解:容易的看出,答案是:            \sum _{a|n}{(a*a-\sum_{b=1}^a{gcd(a,b)}\ )}

等价于     \sum_{a|n}{(a*a\ -\ \sum_{b=1}^a{\sum_{c|a,c|b}{\phi(c)}}}\ )}

等价于     \sum_{a|n}{(a*a\ -\ \sum_{c|a}{\phi (c)*(a/c)}\ )}

等价于     \sum_{a|n}{a*a}\ -\ \sum_{a|n}{ \sum_{c|a}{\phi (c)*(a/c)}}

等价于     \sum_{a|n}{a*a}\ -\ \sum_{c|n}{\phi (c) \sum_{c|d,d|n}{(d/c)}}

等价于     \sum_{a|n}{a*a}\ -\ \sum_{c|n}{\phi (c) \sum_{d|(n/c)}{d}}

等价于     \sum_{a|n}{a*a}\ -\ \sum_{c|n}{\phi (c) *\sigma (n/c)}

设F是欧拉函数和因子和函数的数论卷积     f=\phi*\sigma

此题等价于     \sum_{a|n}{a*a}\ -f(n)

F函数也是积性函数,因式分解后,按定义乘起来就行


#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
typedef unsigned long long ll;
const ll N=4e4;
bool P[N+10];
vector<int> V;
ll A[100],B[100];
ll F(ll x){
    ll ans=1;
    for(int i=0;V[i]*V[i]<=x;++i){
        if(x%V[i]==0){
            int a=0;
            while(x%V[i]==0){
                x/=V[i];
                ++a;
            }
            A[0]=1;
            for(int j=1;j<=a;++j){
            	A[j]=A[j-1]*V[i]*V[i];
			}
            for(int j=1;j<=a;++j){
            	A[j]+=A[j-1];
			}
			ans*=A[a];
    	}
    }
    if(x>1)ans=ans*(x*x+1);
    return ans;
}
ll G(ll x){
    ll ans=1;
    for(int i=0;V[i]*V[i]<=x;++i){
        if(x%V[i]==0){
            ll a=0;
            while(x%V[i]==0){
                x/=V[i];
                ++a;
            }
            ll b=0;
            A[0]=1;
            B[0]=1;
            for(int j=1;j<=a;++j)A[j]=A[j-1]*V[i];
            for(int j=1;j<=a;++j){
                B[j]=B[j-1]+A[j];
            }
            for(int j=1;j<=a;++j){
                b+=A[j-1]*(V[i]-1)*B[a-j];
            }
            b+=B[a];
            ans=ans*b;
        }
    }
    if(x>1)ans=ans*2*x;
    return ans;
}
int main(){
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
    for(int i=2;i<=N;++i){
        if(P[i]==0){
            V.push_back(i);
            for(int j=2*i;j<=N;j+=i)P[j]=1;
        }
    }
    ll T;
    scanf("%d",&T);
    while(T--){
        ll x;
        scanf("%lld",&x);
        if(x==0)printf("1\n");
        else printf("%lld\n",F(x)-G(x));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值