CF1294C 题解

题目传送门。

简单来说:

我们可以枚举 nn 的最小的两个因数,设它们分别为 aabb

则另外一个因数为 nab\frac{n}{ab}

所以我们可以直接输出

所以我们需要几个判定才输出:

  • a=na=nb=nb=n 时,不满足,输出 NO\tt NO

  • 如果 nn33 个因数都没有,直接输出 NO\tt NO

写出代码,此题终。

#include<iostream>
using namespace std;
int n,t,flag,a,b;
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		flag=1;a=0;b=0;
		for(int i=2;i*i<=n;i++){
			if(n%i==0){
				n/=i;
				if(a==0)a=i;
				else {
					b=i;
					if(n!=1)flag=0;
					else flag=1;
					break;
				}
			}
		}
		if(n==a||n==b)flag=1;
		if(flag)cout<<"NO\n";
		else cout<<"YES\n"<<a<<' '<<b<<' '<<n<<endl;
	}
	return 0;
}

为什么不考虑 a=ba=b 呢? 因为 bb 一定在 aa 之后被赋值,则 bb 一定大于 aa。)

关于时间复杂度: 外层循环 O(t)\operatorname{O}(t),内层循环 O(n)\operatorname{O}(\sqrt{n}),总时间复杂度约为 O(tn)\operatorname{O}(t\sqrt{n}),按 t100,n109t\leq100,n\leq10^9 来算,总时间复杂度约为 O(100×31622.78)=O(3162278)\operatorname{O}(100\times31622.78)=\operatorname{O}(3162278),不会 TLE\tt TLE。)

posted @   Weslie_qwq  阅读(2)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示