[codeforces 1294C] Product of Three Numbers 质数线性筛+质因数分解

[codeforces 1294C] Product of Three Numbers 质数线性筛+质因数分解

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址http://codeforces.com/contest/1294/problem/C

 

ProblemLangVerdictTimeMemory
C - Product of Three Numbers GNU C++11Accepted31 ms300 KB

 

#include <stdio.h>
#include <string.h>
int prime[10010],not_prime[100010],cnt=0,n,b[50],tot,c[50];
void linear_shaker(int x){//质数线性筛
	int i,j;
	for(i=2;i<=x;i++){
		if(!not_prime[i])prime[++cnt]=i;
		for(j=1;prime[j]*i<=x;j++){
			not_prime[i*prime[j]]=1;
			if(i%prime[j]==0)break;
		}
	}
}
void solve(int x){
	int i,j;
	memset(b,0,sizeof(b)),tot=0;
	for(i=1;i<=cnt;i++)
		if(prime[i]*prime[i]>x)break;
		else{
			if(x%prime[i]==0){
				tot++,c[tot]=prime[i];
				while(x%prime[i]==0)x/=prime[i],b[tot]++;
			}
		}
	if(x>1)tot++,c[tot]=x,b[tot]++;
}
int main(){
	int t,a,e,f;
	linear_shaker(100000);
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		solve(n);
		if(tot>=3){//tot表示不同的质因数个数
			printf("YES\n");
			a=1;
			for(int i=1;i<=b[1];i++)a*=c[1];
			printf("%d",a);
			e=1;
			for(int i=1;i<=b[2];i++)e*=c[2];
			printf(" %d",e);
			printf(" %d\n",n/a/e);
		}else{//tot<3
			if(tot==1){
				if(b[1]<=5)printf("NO\n");//1+2+3=6
				else{//b[1]>=6
					printf("YES\n");
					a=1;
					for(int i=1;i<=1;i++)a*=c[1];
					printf("%d",a);
					e=1;
					for(int i=1;i<=2;i++)e*=c[1];
					printf(" %d",e);
					printf(" %d\n",n/a/e);
				}
			}
			else if(tot==2){
				if(b[1]+b[2]<=3)printf("NO\n");
				else{//b[1]+b[2]>=4
					printf("YES\n");
					printf("%d %d %d\n",c[1],c[2],n/c[1]/c[2]);
				}
			}
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值