1. 程式人生 > >ACM-ICPC 2018 南京賽區網路預賽-J Sum(線性篩選+因數分解)

ACM-ICPC 2018 南京賽區網路預賽-J Sum(線性篩選+因數分解)

A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into product of two square-free integers, there may be more than one decomposition ways. For example, 6 = 1\cdot 6=6 \cdot 1=2\cdot 3=3\cdot 2, n=ab6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab and n=ban=ba are considered different if a \not = ba̸=b. f(n)f(n) is the number of decomposition ways that n=abn=ab such that aa and bb are square-free integers. The problem is calculating \sum_{i = 1}^nf(i)∑i=1n f(i).

Input

The first line contains an integer T(T\le 20)T(T≤20), denoting the number of test cases.

For each test case, there first line has a integer n(n \le 2\cdot 10^7)n(n≤2⋅107).

Output

For each test case, print the answer \sum_{i = 1}^n f(i)∑i=1n f(i).

Hint

\sum_{i = 1}^8 f(i)=f(1)+ \cdots +f(8)∑i=18 f(i)=f(1)+⋯+f(8)
=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14.

樣例輸入

2
5
8

樣例輸出

8
14

題解:可以將f(n)分解為:f(n)=p1^{e1}*p2^{e2}*p3^{e3}*...*px^{ex}其中px為質數ex為指數;題目要求f(n)=a*b的對數,所以我們就可以可以判斷a的型別數量a可以由p1,p2,p3...px這些質因數中的一些乘積組成,按題目要求這些質因數我們只能選擇一個,所以對於每一個質因數假如指數都為1那麼每一個質因數選擇有兩種(選或不選),如果指數為2那就只能選著一個只有一種選法;

這樣我們就可以維護f[i*一個質數】;

#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<functional>
using namespace std;
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define clr(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f3f3f3f3f3f3f
#define MAX_N 20000000+5
#define MAX_M 13
typedef long long ll;
const ll INF=10000000000000000LL;
const ll maxn=2*1e7+2;
typedef priority_queue<int,vector<int>,less<int> > pql;
typedef priority_queue<int,vector<int>,greater<int> >pqg;
/*
數學:線性篩選+因數分解規律
*/
int tot,prime[maxn],vis[maxn],f[maxn];
ll  res[maxn];
void init()
{
    clr(prime,0);
    clr(vis,0);
    clr(res,0);
    prime[1]=1;res[1]=1;tot=0;
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i])//新增到素數集合當中
        {
            prime[tot++]=i;
            f[i]=2;
        }
        for(int j=0;j<tot&&i*prime[j]<maxn;j++)//乘每一個質數
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)
            {
                if(i%(prime[j]*prime[j])==0){f[prime[j]*i]=0;}//指數超過2就代表為0
                else f[prime[j]*i]=f[i]/2;//若果指數為2代表只有一種選法
                break;//必須加只需查詢指數在2之內的就可以了
            }
            else
            {
                f[prime[j]*i]=f[i]*2;//指數為1就*2
            }
        }
        res[i]=res[i-1]+f[i];
    }
    return ;
}
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("data.txt","r",stdin);
    #endif
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
       int n;
       scanf("%d",&n);
       printf("%lld\n",res[n]);
    }
    return 0;
}