惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
Recent Announcements
Recent Announcements
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
J
Java Code Geeks
博客园_首页
Jina AI
Jina AI
美团技术团队
H
Help Net Security
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
S
SegmentFault 最新的问题

博客园 - ZefengYao

Markdown 入门与 Word 使用指南 关于崩溃报告的日志以及dump文件 hdu 6223 Infinite Fraction Path 2017南宁现场赛E The Champion c语言几个字符串处理函数的简单实现 各种类型排序的实现及比较 随机洗牌算法Knuth Shuffle和错排公式 两个栈实现队列 面试杂题 面试题——栈的压入、弹出顺序 C++ 智能指针的简单实现 openGL初学函数解释汇总 foj Problem 2107 Hua Rong Dao foj Problem 2282 Wand UVA-1400 Ray, Pass me the dishes! 《挑战程序设计竞赛》 利用后缀数组求最长回文串 Uva 11174 Stand in a Line UVA 11375 Matches poj 3729 Facer’s string
ACM-ICPC 2018 南京赛区网络预赛 Sum
ZefengYao · 2018-09-10 · via 博客园 - ZefengYao

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.

样例输出

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;
}