Welcome, Guest! Sign Up RSS

Clever Space

Friday, 11.22.2024
Main » 2013 » November » 27 » 【数论】SPOJ—LCMSUM
8:59 AM
【数论】SPOJ—LCMSUM

来发个代码吧

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define N 1000000
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef long long LL;
using namespace std;
LL G[N+100],n,Min[N+100],pk[N+100];int prime[100010];
int len=0;bool b[N+100];
void init()
{
 int i,j;len=0;
 memset(Min,0,sizeof(Min));Min[1]=1;
 FOR(i,2,N)
 {
 if (!b[i]) {prime[++len]=i;Min[i]=i;}
 FOR(j,1,len) 
 {
 if (i*prime[j]>N) break;
 b[i*prime[j]]=1;
 if (Min[i*prime[j]]==0) Min[i*prime[j]]=prime[j];
 if (i%prime[j]==0) break;
 }
 }
 FOR(i,2,N)
 {
 int tmp=i;
 pk[i]=1;
 while (tmp%Min[i]==0&&tmp>1) tmp/=Min[i],pk[i]*=Min[i];
 }
}
void init_G()
{
 init();
 G[1]=1;int i;LL j;
 FOR(i,1,len)
 {
 int p=prime[i],po=1;
 for(j=p;;j*=p)
 {
 if (j>N) break;
 G[j]=G[j/p]+(LL)j*(LL)(p-1)*(LL)(j/p);
 }
 }
 FOR(i,2,N)
 {
 G[i]=G[pk[i]]*G[i/pk[i]];
 }
}
int main()
{
 int T;
 init_G();
 scanf("%d",&T);
 while (T--) 
 {
 scanf("%d",&n);
 printf("%lld\n",n*((G[n]-1)/2+1));
 }
}
Views: 331 | Added by: dhy0077 | Rating: 5.0/1
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]