Codeforces - 151C 质因子分解

显然只需要能跑到第二个因子就赢了
需要特判非平凡因子
常数优化:不用求出所有因子,跑完第二个素数就行了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 233;
ll n,cnt;
ll prime[maxn],num[maxn];
void chai(ll a){
	cnt=0;
	memset(num,0,sizeof num);
	memset(prime,0,sizeof prime);
	for(ll i = 2; i*i <= a; i++){
		if(cnt>2) break;
		if(a%i==0){
			cnt++;
			prime[cnt]=i;num[cnt]++;
			a/=i;
			while(a%i==0){
				num[cnt]++;
				a/=i;
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	while(cin>>n){
		chai(n);
		if((cnt>=2)||(cnt==1&&num[cnt]>=2)||cnt==0){
			if(cnt==1&&num[cnt]==2&&n==prime[cnt]*prime[cnt]){
				cout<<2<<endl;
				continue; 
			}
			cout<<1<<endl;
			if(cnt==0) cout<<0<<endl;
			else if(num[1]>1) cout<<prime[1]*prime[1]<<endl;
			else cout<<prime[1]*prime[2]<<endl;
		}
		else cout<<2<<endl;
	}
}
posted @ 2017-12-09 11:28  Caturra  阅读(194)  评论(0编辑  收藏  举报