function
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1700 Accepted Submission(s): 607
Problem Description
There is a function f(x),which is defined on the natural numbers set N,satisfies the following eqaution
calulate mod 109+7.
Input
the first line contains a positive integer T,means the number of the test cases.
next T lines there is a number N
T≤500,N≤109
only 5 test cases has N>106.
Output
Tlines,each line contains a number,means the answer to the i-th test case.
Sample Input
1
3
Sample Output
2
題意
有這麼一個函數滿足
然後求
思路
令
那麼有
左邊是恆等函數和的狄利克雷卷積,那麼根據,兩邊再乘上一個,有
所以有
那麼
當前的含義是枚舉中每一個i的因子,這個和枚舉中每一個數的倍數是等價的,那麼就有
令則有
那麼原式就有
莫比烏斯函數的前綴和就可以用杜教篩求了,和函數就可以用分塊來做,提前處理一下6的逆元和2的逆元就可以了
#include<bits/stdc++.h>
using namespace std;
const int N=5e6+5;
const int mod=1e9+7;
const int inv6=166666668;
const int inv2=500000004;
bool vis[N];
int mu[N];
int prime[N];
int cnt;
void init()
{
cnt=0;
mu[1]=1;
for(int i=2;i<N;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
mu[i]=-1;
}
for(int j=0;j<cnt&&i*prime[j]<=N;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else
mu[i*prime[j]]=-mu[i];
}
}
for(int i=2;i<N;i++)
mu[i]=mu[i-1]+mu[i];
}
unordered_map<int,int>S;
int Sum(int n)
{
if(n<N) return mu[n];
else if(S.find(n)!=S.end())return S[n];
int ans=1;
for(int i=2,last;i<=n;i=last+1)
{
last=n/(n/i);
ans=ans-(last-i+1)*Sum(n/i);
}
S[n]=ans;
return ans;
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
long long ans=0;
for(int i=1,last;i<=n;i=last+1)
{
last=n/(n/i);
long long m=n/i;
ans=(ans+(Sum(last)-Sum(i-1)+mod)%mod*(m*(m+1)%mod*(2*m%mod+1)%mod*inv6%mod-3*m%mod*(m+1)%mod*inv2%mod+2*m%mod+mod)%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}