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的表打出来

一个数可以分解成 n = p1^a1 * p2^a2 * p3^a3.......其中p1,p2,p3...都是素数

如果a1,a2,a3...中有一个大于2,那么f(n)=0因为不管怎样组合都会使一个因子是平方数

如果a1,a2,a3...中有m个是1,剩下的都是2 那么只有那些是2的就必须分开,剩下的每一个数都有2中可能所以f(n)=2^m

打表 prime[p] = p prime[i*p]=1

对于比i小的所有素数 i是作为他们的倍数进行处理的

如果i%prime[j]==0 而且i%(prime[j]*prime[j])==0那么i*prime[j]肯定指数有一个大于2了 f[i*prime[j]] = 0

如果i%prime[j]==0 那么i*prime[j]要少掉一个数可以选择 所以f[i*prime[j]] = f[i] / 2

如果上面一个都不是 那么i*prime[j]之后就多了一个数可以选择 所以f[i*prime[j]] = f[i] * 2

不加if(i%prime[j]==0)break;会T,还没想懂为什么....


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std; typedef long long LL; int t, n, cnt;
const int maxn = 2e7 + 5;
int f[maxn], prime[maxn]; void table()
{
memset(f, 0, sizeof(f));
memset(prime, 0, sizeof(prime));
cnt = 0;
f[1] = 1;
for(int i = 2; i < maxn; i++){
if(!prime[i]){
prime[cnt++] = i;
f[i] = 2;
}
for(int j = 0; j < cnt && prime[j] * i < maxn; j++){
prime[prime[j] * i] = 1;
if(i % prime[j] == 0){
if(i % (prime[j] * prime[j]) == 0){
f[i * prime[j]] = 0;
}
else{
f[i * prime[j]] = f[i] / 2;
}
}
else{
f[i * prime[j]] = f[i] * 2;
}
if(i % prime[j] == 0)break;
}
}
} int main()
{
cin>>t;
table();
while(t--){
scanf("%d", &n);
LL ans = 0;
for(int i = 1; i <= n; i++){
ans += f[i];
}
printf("%lld\n", ans);
}
return 0;
}

最新文章

  1. [LeetCode] Permutations II 全排列之二
  2. Web 前端之HTML和CSS
  3. centos7查看端口命令
  4. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】
  5. Windows KB2984972安装后堵住了一个windows 7 桌面可以多个用户远程访问桌面的漏洞。
  6. 什么是POJO?
  7. Careercup - Facebook面试题 - 4892713614835712
  8. 对github中项目进行更新
  9. [020] Android模拟器访问本地Web应用
  10. golang Date format
  11. storm的并发机制
  12. TypeError: &#39;module&#39; object is not callable
  13. python内置函数 和模块函数总结
  14. error eslint@5.12.0: The engine &quot;node&quot; is incompatible with this module.
  15. [CF1132F]Clear the String
  16. pip3 install requests Cannot open D:\Python35\Scripts\pip3-script.py
  17. pycharm破解版
  18. updateByPrimaryKey 和 updateByPrimaryKeySelective
  19. 用SVN进行团队开发协作生命周期详解
  20. 【Oracle 】tablespace 表空间创建和管理

热门文章

  1. Java的四种引用类型之弱引用
  2. \avformat.h(40) : fatal error C1083: 无法打开包括文件:“libavcodec/avcodec.h”: No such file or directory
  3. linux ffmpeg编译配置安装详解
  4. linux -- at命令
  5. 根据多表条件更新表.............. 一条sql语句.............
  6. 【Java面试题】60 接口是否可继承接口? 抽象类是否可实现(implements)接口? 抽象类是否可继承具体类(concrete class)? 抽象类中是否可以有静态的main方法?
  7. mysql数据库要按当天、昨天、前七日、近三十天、季度、年查询
  8. LaTeX公式
  9. Davlik虚拟机
  10. oracle中REF Cursor用法