HDU 5432/BC 54B The Factor
来源:互联网 发布:windows数据库 编辑:程序博客网 时间:2024/05/18 03:15
题意有点绕,实质是求所求数列乘积的最小的两个素因子的积,没有输出-1。
先筛素数,对于数列中的每个数质因数分解(剪枝可以分解出两个素因子后就退出),将得到的素因子排序,找到最小的两个输出,不足两个特判即可,注意结果可能超出int,需要用long long
#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<set>#include<map>#include<stack>#include<queue>using namespace std;#define mem(x,y) memset(x,y,sizeof(x))#define pb push_back#define bug puts("===========");#define REP(i,n) for(int i=0;i<n;i++)#define REP1(i,n) for(int i=1;i<=n;i++)#define PER(i,n) for(int i=((n)-1);i>=0;i--)#define PER1(i,n) for(int i=(n);i>0;i--)#define ALL(A) A.begin(), A.end()#define T_T for(int _=RD(),test=1;test<=_;test++)typedef long long ll;//typedef pair<int,int> pii;#define CHG ch=getchar()#define FRD x=bo=0; for(CHG;ch<'0'||ch>'9';CHG) if(ch=='-')bo=1;#define FR2 for(;ch>='0'&&ch<='9';x=(x<<1)+(x<<3)+ch-'0',CHG);char ch; int bo;inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}inline void RD(int &x){ FRD FR2 if (bo)x=-x; }inline void RD(ll &x){ FRD FR2 if (bo)x=-x; }inline ll RD(){ ll x; RD(x); return x; }inline void RD(char *s){///scanf %s for (CHG;blank(ch);CHG); for (;!blank(ch);CHG)*s++=ch; *s=0;}inline void RD(char &c){for(CHG;blank(c);CHG);}template<class T> inline void OT(T x){ static char buf[20]; char *p1=buf;if (!x)*p1++='0';if (x<0)putchar('-'),x=-x; while(x)*p1++=x%10+'0',x/=10; while(p1--!=buf)putchar(*p1);}inline void pe(){puts("");}//输出回车inline void pk(){putchar(' ');}//输出空格const double eps=1e-8;const int inf=0x3f3f3f3f;const ll mod=1e9+7;const ll mod2=1e9+6;const int maxn=13+10;const ll llinf=1e18;/*==================================*/vector<int>prime;bool no_prime[maxn];void init_P(){ for(int i=2;i<maxn;i++){ if(!no_prime[i]) prime.pb(i); for(int j=0;j<prime.size()&&i*prime[j]<maxn;j++){ no_prime[i*prime[j]]=1; if(i%prime[j]==0) break; } }}vector<int>pfac,pcnt;void get_pfac(ll n){//也可传入vector int cnt=0; for(int i=0;i<prime.size()&&(ll)prime[i]*prime[i]<=n;i++)if(n%prime[i]==0){ while(n%prime[i]==0){ n/=prime[i]; pfac.pb(prime[i]); cnt++; if(cnt>=2) return; } } if(n!=1) pfac.pb(n),pcnt.pb(1);}int main(){ init_P(); T_T{ pfac.clear(); int n=RD(); REP(i,n){ get_pfac(RD()); } sort(ALL(pfac)); if(pfac.size()<=1) puts("-1"); else{ OT((ll)pfac[0]*pfac[1]); pe(); } } return 0;}
0 0
- HDU 5432/BC 54B The Factor
- BC - The Factor(模拟题 + 素数)
- HDU 5428 The Factor
- HDU 5428 The Factor
- hdu 5428 The Factor
- HDU 5428 The Factor
- HDU 5428:The Factor
- hdu 5428 The Factor
- HDU 5428 The Factor
- hdu 5428 The Factor
- BC水题--The Factor(质因分解)
- hdu 5428 The Factor(数论)
- Hdu 5428 The Factor【思维】
- BestCoder Round #54 (div.2) HDU 5428 The Factor(1002)
- bestcoder#54 The Factor
- 【CUGBACM15级BC第13场 B】hdu 5063 Operation the Sequence
- hdu 5365 /BC 50B Run
- HDU 5059\BC 12B Help him
- mongodb的简介和应用
- Hadoop-HDFS
- mysql优化
- 第八周项目5—— 计数的模式匹配
- 完美中文tty, fbterm+yong(小小输入法 )
- HDU 5432/BC 54B The Factor
- test
- H264编码之GOP含义
- 构造一个学生类Student,每一个学生对象有基本信息,同时还有课程考试成绩信息。利用集合框架类完成。
- HOG构造函数
- hihoCoder 搜索一·24点
- 自定义view在XML中使用
- Docker——使测试更美好
- Docker 简介