CF 1294C Product of Three Numbers

题目链接:cf1294c
思路:每次暴力找出n的一个因子,如果不存在则输出no。
code:

#include <bits/stdc++.h>
using namespace std;
 
int p(int x,int y) {
    if(x==1)return 0;
    int sq=sqrt(x);
    for(int i=y;i<=sq;i++) {
        if(x%i==0)
            return i;
    }
    return 0;
}
 
void solve() {
    int t;
    cin>>t;
    while(t--) {
        long long n;
        cin>>n;
        vector<long long > ans;
        long long num=n;
        int tmp=2;
        int sq=sqrt(num);
        while(true) {
            int di=p(n,tmp);
            if(di) {
                n/=di;
                ans.push_back((di));
                tmp=di+1;
            }
            else {
                cout<<"NO"<<endl;
                break;
            }
            if(ans.size()==2&&n>=tmp) {
                ans.push_back(n);
                cout<<"YES"<<endl;
                cout<<ans[0]<<' '<<ans[1]<<' '<<ans[2]<<endl;
                break;
            }
        }
    }
}
 
int main() {
    ios::sync_with_stdio(false);
 
    solve();
    return 0;
}
发布了50 篇原创文章 · 获赞 52 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_43058685/article/details/104073752