bzoj2226(欧拉函数+线性筛积性函数)

和gcd那题很像。。不过这题组数非常多,需要进一步优化 。。

仍然是把lcm转成gcd,然后枚举gcd

\sum_{i=1}^{n}[i,n]\\= \sum_{i=1}^{n}\frac{in}{(i,n)}\\= n\sum_{d|n}\sum_{i=1}^{n}\frac{i}{d}[(i,n)=d]\\= n\sum_{d|n}\sum_{i=1}^{n}\frac{i}{d}[(\frac{i}{d},\frac{n}{d})=1]\\= n\sum_{d|n}\sum_{i=1}^{\frac{n}{d}}i[(i,\frac{n}{d})=1]\\= n\sum_{d|n}\sum_{i=1}^{d}i[(i,d)=1]

然后可以发现后面其实就是在求n/d内与n/d互质的数的个数(n>2时等于nφ(n)/2),于是

\sum_{i=1}^{n}[i,n]= \frac{n}{2}(\sum_{d|n} d\varphi(d)+1)

显然f(n)=\sum_{d|n}d\varphi(d)符合狄利克雷卷积形式,所以f(n)为积性函数。。而组数那么多,O(n)线性筛应该是最好的选择。。

然后f(p^k)=\sum_{i=1}^{k}p^k(p-1)p^{k-1}+1=(p-1)\frac{p^{2k+1}-p}{p^2-1}+1=\frac{p^{2k+1}+1}{p+1}

这个去线性筛的话有点难度,不能直接乘除,因此我们对将n做素因子分解,其中对素因子最小的项p_1^{a_1},维护p_1^{2a_1+1}+1,这样才方便乘除,至于为什么要维护最小的素因子p1,是因为线性筛的过程中每个数只会被他最小的素因子筛到,转移也自然是从最小素因子转移过来,所以只对最小素因子维护就可以了。。。

这样总复杂度为O(n),比网上的方法要优,然而跑起来还是排到了rk50+,bzoj神仙真是多啊。。。orz

 

 

 

 

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1000005
#define nm 220005
#define N 40005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e16;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 

int n,prime[NM],tot;
ll p[NM],f[NM];
bool v[NM];

void init(){
    n=1e6;f[1]=1;
    inc(i,2,n){
	if(!v[i])prime[++tot]=i,p[i]=(ll)i*i*i+1,f[i]=1+(ll)i*(i-1);
	inc(j,1,tot){
	    int t=i*prime[j];
	    if(t>n)break;
	    v[t]++;
	    if(i%prime[j]==0){
		p[t]=(p[i]-1)*sqr(prime[j])+1;
		f[t]=(prime[j]+1)*f[i]/p[i]*p[t]/(prime[j]+1);
		break;
	    }
	    p[t]=(ll)prime[j]*prime[j]*prime[j]+1;
	    f[t]=f[i]*(1+prime[j]*(prime[j]-1));
	}
    }
    inc(i,1,n)f[i]=i*(f[i]+1)/2;
}


int main(){
    init();
    int _=read();while(_--)printf("%lld\n",f[read()]);
    return 0;
}

 

 

2226: [Spoj 5971] LCMSum

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 2080  Solved: 919
[Submit][Status][Discuss]

Description

Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.

Input

The first line contains T the number of test cases. Each of the next T lines contain an integer n.

Output

Output T lines, one for each test case, containing the required sum.

Sample Input

3
1
2
5
 

Sample Output

1
4
55

HINT

Constraints

1 <= T <= 300000
1 <= n <= 1000000
 

Source

 

[Submit][Status][Discuss]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值