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

A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=23 is square-free, but 12 = 2^2 \cdot 312=223 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=16=61=23=32,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=1nf(i).

Input

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

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

Output

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

Hint

\sum_{i = 1}^8 f(i)=f(1)+ \cdots +f(8)i=18f(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

思路:如果某个数字x拥有某一个素因子超过2个,则x的f值为0;若x的某个素因子数量为2个,则这个素因子不会对x的f值有任何的贡献;若x的某个素因子只有1个,则这个素因子贡献为2,举个例子:
60=2^2*3*5,则2没有贡献,3,5都贡献2,所以f(60)=2*2=4;
利用线性筛,每个合数只被它最小的素因子筛去,同时处理出这个数字的f值即可
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
#define EPS 1e-5
const ll MOD = 1000000007;
const int N_MAX =2*10000000+10;
bool is_prime[N_MAX];
int prime[N_MAX],p,f[N_MAX],sum[N_MAX],number;
void sieve(int n) {
    is_prime[0] = is_prime[1] = true;
    p = 0; f[1] = 1;
    for (int i = 2; i < n;i++) {
        if (!is_prime[i]) {
            prime[p++] = i;
            f[i] = 2;
        }
        for (int j = 0; j < p;j++) {
            number = prime[j] * i;
            if (number >= N_MAX)break;
            is_prime[number] =true;
            if (i%prime[j] != 0) {
                f[number] = f[i]<<1;
            }
            else {
                if (i % (prime[j] * prime[j]) == 0) { f[number] = 0; }
                else f[number] = f[i] >> 1;
                break;//线性筛,保证每个数字只被最小的素数筛去
            }
        }
    }
}
int main() {
    sieve(N_MAX-5); sum[1] = 1;
    for (int i = 2; i < N_MAX - 9; i++) {
        sum[i] = sum[i - 1] + f[i];
    }
    int t; scanf("%d",&t);
    while (t--) {
        int n; scanf("%d",&n);
        printf("%d\n",sum[n]);
    }
    return 0;
}    

 

posted on 2018-09-10 19:21  ZefengYao  阅读(143)  评论(0编辑  收藏  举报

导航