2018南京網路賽 j題 sum(篩法、非平方數相乘)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define maxz 200005
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
#define ev 2.71828182
using namespace std;
const double PI=acos(-1.0);
const int maxn=2e7+5;
int t,n;
int ans[maxn];
bool prime[maxn];
int prime_num[maxn];
void solve()
{
memset(prime,true,sizeof(prime));
prime[1]=false;
int cnt=0;
ans[1]=1;
for(int i=2;i<=20000000;i++)
{
if(prime[i])
{
prime_num[cnt++]=i;
ans[i]=2;
}
for(int j=0;j<cnt;j++)
{
if(i*prime_num[j]>20000000)
break;
if(i%prime_num[j]==0)
{
if(i%(prime_num[j]*prime_num[j])==0)
{
ans[i*prime_num[j]]=0;
}
else
ans[i*prime_num[j]]=ans[i]/2;
}
else
ans[i*prime_num[j]]=ans[i]*2;
}
if(prime[i]==false)
continue;
for(int j=i+i;j<=maxn;j+=i)
prime[j]=false;
}
for(int i=1;i<=20000000;i++)
{
ans[i]=ans[i-1]+ans[i];
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>t;
solve();
while(t--)
{
cin>>n;
cout << ans[n] << endl;
}
return 0;
}