题目链接:https://nanti.jisuanke.com/t/30999
猜想..........f(x)是积性函数,即f(ab)=f(a)*f(b)其中a和b互质
建议学习一下线性筛就会了。特殊处理n=1,n是质数以及n是质数的k次幂的情况
#include
#include
#define MOD 20101009
#define For(i,m,n) for(int i=m;i<=n;i++)
#define LL long long
#define lan(a,b) memset(a,b,sizeof(a))
#define sqr(x) (x*x)
using namespace std;
const int maxn=2e7+5;
LL low[maxn]
int zhi[maxn],pri[maxn];
int tot=0;
LL f[maxn];
LL sum[maxn];
void sieve()
{
int n=maxn-5;
tot=0;
zhi[1]=low[1]=1;
f[1]=1;
sum[1]=1;
for (LL i=2;i<=n;i++)
{
if (!zhi[i]) low[i]=pri[++tot]=i,f[i]=2;
for (LL j=1;j<=tot&&i*pri[j]<=n;j++)
{
zhi[i*pri[j]]=1;
if (i%pri[j]==0)
{
low[i*pri[j]]=low[i]*pri[j];
if (low[i]==i)
f[i*pri[j]]=(i==pri[j])?1:0;
else
f[i*pri[j]]=f[i/low[i]]*f[low[i]*pri[j]];
break;
}
low[i*pri[j]]=pri[j];
f[i*pri[j]]=f[i]*f[pri[j]];
}
sum[i]=sum[i-1]+1LL*f[i];
}
// For(i,1,100)
// printf("f[%d]=%d\n",i,f[i]);
}
int main()
{
sieve();
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
printf("%lld\n",sum[n]);
}
return 0;
}