ACM-ICPC 2018南京赛区网络预赛 J Sum(线性筛)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25576697/article/details/82319883

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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

题意:f[i]表示i可以分解成为两个不能被平方数整除的数的个数(顺序不同算不同),对f[i]求前缀和。

思路:对于n,可以表示为n = a1^p1*a2^p2*...*an^pn,如果有一个pi>2,那么f[n] = 0,否则f[n] = 2^(sum(pi==1))。可以线性筛筛出来。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn =  2e7+10;
bool check[maxn];
int prime[maxn];
ll f[maxn];
int tot=0;
int main(){
    check[1] = 1;
    f[1] = 1;
    int num;
    for(int i=2;i<maxn;i++){
        if(check[i]==0){  //prime
            prime[tot++]=i;
            f[i] = 2;
        }
        for(int j = 0;j<tot&&(ll)i*prime[j]<maxn;++j)
        {
            num = i*prime[j];
            check[num] = 1;
            if(i%prime[j])
            {
                f[num] = f[i]*2;
            }
            else if((ll)i%(prime[j]*prime[j])==0)
            {
                f[num] = 0;
            }
            else
            {
                f[num] = f[num/prime[j]/prime[j]];
                break;
            }
        }
    }
    for(int i = 2;i<maxn;i++)
        f[i] += f[i-1];
    int t;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%lld\n",f[n]);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_25576697/article/details/82319883
今日推荐