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

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 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;
}