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

题面
C. Product of Three Numbers

You are given one integer number n. Find three distinct integers a,b,c such that 2≤a,b,c and a⋅b⋅c=n or say that it is impossible to do it.

If there are several answers, you can print any.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2≤n≤109).

Output
For each test case, print the answer on it. Print “NO” if it is impossible to represent n as a⋅b⋅c for some distinct integers a,b,c such that 2≤a,b,c.

Otherwise, print “YES” and any possible such representation.

大意:给你一个整数n,是否存在三个大于等于2且互不相等的因数a,b,c 使abc=n;
如果存在,输出YES并输出任意一种可能,否则输出NO。
思路:先算出n的所有因数,然后三个for循环暴力找a,b,c;

#include<bits/stdc++.h>
using namespace std;
int b[100005];
int z;

void find(int a)					//找因数的函数。
{
	memset(b,0,sizeof(b));				//注意清0
	z=0;								//同上
	for(int i=1;i<=sqrt(a);i++) 
	{
		if(a%i==0) 
		{
			b[z]=i;
			z++;
			int g=a/i;
			b[z]=g;
			z++;
		}
 	}
}

int panduan(int a,int b,int c)
{
	if(a>=2&&b>=2&&c>=2&&(a!=b&&a!=c&&b!=c))
	return 1;
	else return 0;
}

int main()
{
	int t;
	cin >>t;
	while(t--)
	{
		int n;
		cin >> n;
		find(n);
		int flag=0;
		int aa,bb,cc;
		int i,j,k;
		for( i=0;i<z;i++)									//暴力找abc
			for( j=0;j<z;j++)
				for( k=0;k<z;k++)
				{
					if(panduan(b[i],b[j],b[k])&&b[i]*b[j]*b[k]==n)
					{
						aa=i;
						bb=j;
						cc=k;
						flag=1;
						break;
					}
				}
		if(flag==1)
		{

			cout << "YES" <<endl;
			cout << b[aa] << " " <<b[bb] <<" " << b[cc] <<endl;
		}
		else if(flag==0)
		{
			cout << "NO" <<endl;
		}
	
	}
	return 0;
 } 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值