Codeforces Round #615 (Div. 3)C. Product of Three Numbers

C. Product of Three Numbers

题目链接C. Product of Three Numbers
在这里插入图片描述

  • 题目大意
    给一个数n(n <= 1e9),如果存在三个不同的数2≤a,b,c满足a,b,c的乘积等于n,输出YES,并且输出任意一个方案,否则输出NO。

  • 解题思路
    若三个不相同的数乘积为n,那么这三个数一定是n的因子,把n进行质因子分解,判断是否有三个不一样的因子满足条件。

  • 附上代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=0x3f3f3f;
int main(){
 ios::sync_with_stdio(0);
 cin.tie(0);cout.tie(0);
 
 int t;
 cin>>t;
 while(t--){
  int n;
  cin>>n;
  int a=0,b=0,c=0;
  for(int i=2;i<=sqrt(n);i++){
   if(n%i==0){
    a=i;
    b=n/i;
    break;
   }
  }
  for(int i=2;i<=sqrt(b);i++){
   if(b%i==0&&i!=a&&(b/i)!=a&&(b/i)!=i){
    c=i;
    b/=i;
    break;
   }
  }
  if(a==0||b==0||c==0)
   cout<<"NO"<<endl;
  else{
   cout<<"YES"<<endl;
   cout<<a<<" "<<b<<" "<<c<<endl; 
  }
 }
 return 0;
}
发布了8 篇原创文章 · 获赞 3 · 访问量 4126

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104081172