HDU5428 The Factor【分解质因子】
来源:互联网 发布:数码兽网络侦探下载pc 编辑:程序博客网 时间:2024/05/22 14:25
问题链接:HDU5428 The Factor。
题意简述:对于给出的一组数 找出这组数乘积中的一个最小的至少三个质因子的数。
问题分析:
其实就是最小的两个素数因子的乘积。
将各个数分别进行质因子分解,将质因子保存在数组中,排序后找到最小的两个乘积即可。
程序说明:
给出2个C++语言程序,前一个程序使用变量maxfact记录已经找到的最大因子,限制质因子分解计算,也许会快一些。
AC的C++语言程序如下:
/* HDU5428 The Factor */#include <iostream>#include <algorithm>using namespace std;const int N = 10000;unsigned int fact[N], maxfact;int fcount;void solve(unsigned int x){ for(int i=2; i*i<=x; i++) { if(i >= maxfact && fcount >= 2) break; while(x % i == 0) { fact[fcount++] = i; x /= i; if(i > maxfact) maxfact = i; } } if(x > 1) { if(x >= maxfact && fcount >= 2) ; else { fact[fcount++] = x; if(x > maxfact) maxfact = x; } }}int main(){ int t, n; unsigned int a; cin >> t; while(t--) { cin >> n; fcount = 0; maxfact = 0; while(n--) { cin >> a; solve(a); } sort(fact, fact + fcount); if(fcount < 2) cout << -1 << endl; else cout << (long long)fact[0] * fact[1] << endl; } return 0;}
AC的C++语言程序如下:
/* HDU5428 The Factor */#include <iostream>#include <algorithm>using namespace std;const int N = 10000;unsigned int fact[N] ;int fcount;void solve(unsigned int x){ for (unsigned int i=2; i*i<=x; i++) { while(x % i == 0) { fact[fcount++] = i; x /= i ; } } if (x > 1) fact[fcount++] = x;}int main(){ int t, n; unsigned int a; cin >> t ; while(t--) { cin >> n ; fcount = 0 ; while(n--) { cin >> a; solve(a); } sort(fact, fact + fcount); if (fcount < 2) cout<< -1 << endl; else cout << (long long) fact[0] * fact[1] << endl; } return 0 ;}
阅读全文
0 0
- HDU5428 The Factor【分解质因子】
- hdu 5428 the factor(分解质因子)
- HDU 5428-The Factor(分解质因子)
- HDU5428------The Factor
- The Factor(hdu5428)
- hdu 5428 The Factor(分解质因子)
- HDU 5428 The Factor(分解质因子)
- HDU 5428 The Factor(分解质因子)
- HDU5428 - The Factor (因式分解)
- hdu 5428 The Factor 找质因子
- POJ 3421X-factor Chains【分解质因子+组合数学】
- POJ3421 X-factor Chains【分解质因子+组合数学】
- BestCoder Round #54 (div.2) HDU5428 The Factor
- POJ3421_X-factor Chains_素因子分解&&排列组合
- The Factor(数论规律题+求质因子)
- BC水题--The Factor(质因分解)
- Problem 3 Largest prime factor (分解素因子)
- poj 3421 X-factor Chains(数因子分解)
- java-jvm-架构
- Sqoop安装部署
- 蒟蒻的回炉重造划水记录
- 欢迎使用CSDN-markdown编辑器
- poj 2112 Optimal Milking 解题报告
- HDU5428 The Factor【分解质因子】
- arduino的超声波测距和pulseIn()函数的使用说明
- android各版本特性
- 创建Django项目
- Raspberry Pi 4: Google announces partnership with Raspberry Pi Foundation
- Java中的集合
- shell 的读取流程
- P(2~16)进制的乘法表
- Linux基础