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

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

题意:找出一个数的所有乘数组合,使这些乘数组合中的任一因数都不存在完全平方因数。求从1到n的的所有数的乘数组合之和。

思路:用线性筛法,求出所有数的最小质因数,不难发现,假如一个数的因数中有一质因数的三次方,则其定然不存在符合上述条件 的组合,而假使存在一质因数的平方,那么这个质因数的平方就必须分配到一个组合的两侧,因此其组合数的数量就等于该数除以这一质因数的数的组合数,而如果只存在一个质因数时,则等于该数的组合数除去该质因数乘2.

AC代码:

#include
using namespace std;
typedef long long LL;
const int size=2e7+10;
int minPrime[size];
int ans[size];
bool prime[size];
int p[size];
int tot;
void init()
{
	int n=size;
	prime[1]=true;
	for(int i=1;i<=n;i++) prime[i]=true;
	for(int i=2;i=Mip*Mip*Mip&&i%(Mip*Mip*Mip)==0) ans[i]=0;
		else if(i>=Mip*Mip&&i%(Mip*Mip)==0)
		{
			ans[i]=ans[i/(Mip*Mip)];
		}
		else
		{
			ans[i]=ans[i/Mip]*2;
		}
	}
	
}
int main()
{
	init();
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		LL res=0;
		for(int i=1;i<=n;i++)
		{
			res=res+ans[i];
		}
		printf("%lld\n",res);
	}
}

 

转载于:https://www.cnblogs.com/fly-white/p/10092726.html

你可能感兴趣的:(ACM-ICPC 2018 南京赛区网络预赛 J题Sum)