Battlestation Operational
Input
There are multiple test cases.Each line of the input, there is an integer n (1≤n≤106), as described in the problem.
There are up to 104 test cases.
Output
For each test case, output one integer in one line denoting the total damage of the Superlaser, f(n) mod 109+7.Sample Input
12
3
10
Sample Output
13
8
110
Source
2017 Multi-University Training Contest - Team 8计算步骤:1、素筛
2、线性筛,计算1-N所有数的因子个数d[i],计算莫比乌斯函数m[i]
3、计算g[n],g[n]=g[n-1]+d[n-1]+1;
4、计算g[n]的前缀和res[n],m[n]的前缀和
g
g
注意:计算f[n]时要进行优化(对于相同的n/d一起加),否则会t
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#define ll long long
using namespace std;
const int N=1000005;
const int mod=1e9+7;
bool isprime[N];
int prime[N];
int c;
int d[N];
int m[N];
ll g[N];
ll res[N];
void getprime()
{
c=0;
memset(isprime,1,sizeof(isprime));
memset(m,0,sizeof(m));
isprime[0]=0;
isprime[1]=0;
for(int i=2,j=0;i<N;i++)
{
if(isprime[i])
{
c++;
prime[j++]=i;
m[i]=1;
for(int k=2;i*k<N;k++)
isprime[i*k]=0;
}
}
}
void work()
{
for(int i=1;i<N;i++)
d[i]=1;
m[1]=1;
for(int i=0;i<c;i++)
{
for(int j=1;j*prime[i]<N;j++)
{
if(j%prime[i]==0) m[j*prime[i]]=0;
else m[j*prime[i]]=-m[j];
int t=1;
int s=prime[i];
int jj=j;
while(jj%prime[i]==0)
{
jj/=prime[i];
t++;
s*=prime[i];
}
d[j*prime[i]]*=t+1;
}
}
g[1]=1;
res[1]=1;
for(int i=2;i<N;i++)
{
g[i]=(g[i-1]+d[i-1]+1)%mod;
g[i]%=mod;
res[i]+=(res[i-1]+g[i])%mod;
res[i]%=mod;
m[i] = (m[i] + m[i-1])%mod;
}
}
int main()
{
// freopen("1.txt","r",stdin);
int n;
getprime();
work();
while(scanf("%d",&n)!=EOF)
{
ll ans = 0;
int last;
for(int i=1 ;i<=n ;i=last + 1){
last = n/(n/i);
ans = (ans + (m[last]-m[i-1])%mod*(res[n/i])%mod)%mod;
}
ans = (ans%mod + mod)%mod;
cout<<ans<<endl;
}
return 0;
}