hdu 5428 The Factor(唯一分解定理)
2015-09-05 23:58
399 查看
The Factor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 477 Accepted Submission(s): 150
Problem Description
There is a sequence of n positive
integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including itself;
i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead.
Input
The first line contains one integer T (1≤T≤15),
which represents the number of testcases.
For each testcase, there are two lines:
1. The first line contains one integer denoting the value of n (1≤n≤100).
2. The second line contains n integers a1,…,an (1≤a1,…,an≤2×109),
which denote these n positive
integers.
Output
Print T answers
in T lines.
Sample Input
2 3 1 2 3 5 6 6 6 6 6
Sample Output
6 4
有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.
题解:求出所有数分解后最小的两个质因子即可。
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #define ll long long #define N 100010 #define INF 99999999999 using namespace std; int n; bool is_pr ; vector<int>vec; void init() { vec.clear(); memset(is_pr,0,sizeof is_pr); is_pr[0]=is_pr[1]=1; for(int i=2; i<N; i++) { if(!is_pr[i]) { vec.push_back(i); for(int j=i+i; j<N; j+=i) is_pr[j]=1; } } } int main() { freopen("test.in","r",stdin); int t; cin>>t; init(); while(t--) { scanf("%d",&n); ll x; ll fmin=INF,smin=INF; for(int i=0; i<n; i++) { scanf("%I64d",&x); for(int j=0; j<vec.size()&&x>=vec[j]; j++) { if(x==1)break; if(x%vec[j]==0) { while(x%vec[j]==0) { if(fmin>vec[j]) { smin=fmin; fmin=vec[j]; } else if(smin>vec[j]) { smin=vec[j]; } x/=vec[j]; } } } if(x!=1) { if(fmin>x) { smin=fmin; fmin=x; } else if(smin>x) { smin=x; } } } //cout<<fmin<<' '<<smin<<endl; if(smin==INF)printf("-1\n"); else printf("%I64d\n",fmin*smin); } return 0; }
相关文章推荐
- 生成浮点型随机数相加
- error: allocating an object of abstract class type
- 手工删除Oracle 12C数据库实例
- hdu4961 Boring Sum(数学)
- spring使用aop
- linux 第二天
- 单用户模式下修改root用户密码并设置系统默认进入多用户模式
- HDU1045 - Fire Net (深搜)
- 自学QT之字体选择对话框
- OGRE渲染过程
- 位运算
- Js制作点击输入框时默认文字消失的效果
- UISegmentedControl的详细使用
- BufferedOutputStream用法
- CCLabelAtlas,CCLabelTextFormatter解析
- 第45讲:Scala中Context Bounds代码实战及其在Spark中的应用源码解析
- 今天做了修改功能
- 关于绝对路径和相对路径
- redis配置项解释
- Java第一周:1.5