ACM-ICPC 2018 南京赛区网络预赛 J.sum

时间:2022-05-07 20:51:02

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 南京赛区网络预赛

这题显示用sum【i】表示第i及i前面的非平方因子的数量,f【cnt】 用来存储非平方因子i,

主要是这部分:

for(int i = ; i < cnt && f[i] <= n; i ++)
{
int res = (int)(n/f[i]);
ans += sum[res];
}
题目要求∑a*b=i (i= 1:n)  其实就可以看成有多少个a*b <= n (a b由非平分因子构成)
假设n = 100   非平分因子有2,3,5,6,7.......   
当a=7时,计算有多少个b  100/7 = 14  所以b最大才14    只要加上前14个非平分因子的个数就行了

贴上代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
const int N=2e7+;
int vis[N],f[N],sum[N];
int cnt;
void init()
{
memset(vis,,sizeof(vis));
for(int i=;i*i<N;i++)
for(int j=i*i;j<N;j+=i*i)
vis[j]=;
for(int i=;i<N;i++)
{
if(vis[i])
{
sum[i]=sum[i-]+;
f[cnt++]=i;
}
else
sum[i]=sum[i-];
}
}
int main()
{
int n,t;
cnt=;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ll ans=;
for(int i=;i<=cnt&&f[i]<=n;i++)
{
int res=(int)(n/f[i]);
ans+=sum[res];
}
printf("%lld\n",ans);
}
return ;
}