LG8448

题目要求了 \(z\) 为奇素数,而为了进行更多次的有效操作,很容易想到直接让 \(z=3\)。这样做,对于原题中 \(1 \le n \le 10^{18}\),最大的 \(\sqrt[3]{n}\) 不会超过 \(10^6\),完全可以在 \(O(n)\) 的时间复杂度内解决。

于是思路便产生了:\(y\)\(2\) 开始枚举,判断此时的 \(n\) 是否为 \(y^3\) 的倍数,是则不断用 \(y^3\)\(n\),统计次数,直到 \(n\) 不为 \(y^3\) 的倍数。当 \(y^3 > n\) 时无法有效操作,结束枚举并输出。

题目中 \(y\) 被要求为素数,但对程序没有影响,因为任何一个合数都是由比它小的质因子构成的。证明方法十分容易,这里不再详解。

程序实现十分简单,代码如下:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		long long n,k=2,ans=0;
		cin>>n;
		while(k*k*k<=n)
		{
			while(n%(k*k*k)==0)
			{
				ans++;
				n/=(k*k*k);
			} 
			k++;
		}
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2024-01-20 17:38  liyilang2021  阅读(2)  评论(0编辑  收藏  举报