hdu 5428 The Factor(分解质因子)
来源:互联网 发布:node modules目录详解 编辑:程序博客网 时间:2024/05/21 11:01
题意:
给一个序列,设所有项乘积为a,求a的因子中因子个数大于2的最小因子。
解析:
将每个数分解质因子,记录每个质因子的个数。
然后从最小的质因子开始取(累乘),直到取够两个(因为1和其本身也是该数的因子),此时得到的结果就是满足条件的最小因子。
#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;typedef long long ll;const int N = 105;map<int, int> prime;map<int, int>::iterator it;ll a[N];int n;void getFactor(ll num) { for(int i = 2; i*i <= num; i++) { if(num % i != 0) continue; while(num % i == 0) { prime[i]++; num /= i; } } if(num != 1) prime[num]++;}ll modpow(ll a, ll k) { ll c = 1; while(k) { if(k & 1) c *= a; a *= a; k >>= 1; } return c;}ll cal() { ll ret = 1; int cnt = 2; it = prime.begin(); for(; it != prime.end() && cnt > 0; it++) { ll tmp = (ll)min(cnt, it->second); cnt -= tmp; ret *= modpow((ll)(it->first), tmp); } return cnt == 0 ? ret : -1;}int main() { int T; scanf("%d", &T); while(T--) { prime.clear(); scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for(int i = 1; i <= n; i++) { getFactor(a[i]); } printf("%lld\n", cal()); } return 0;}
0 0
- hdu 5428 the factor(分解质因子)
- HDU 5428-The Factor(分解质因子)
- hdu 5428 The Factor(分解质因子)
- HDU 5428 The Factor(分解质因子)
- HDU 5428 The Factor(分解质因子)
- hdu 5428 The Factor 找质因子
- HDU5428 The Factor【分解质因子】
- hdu(5428)——The Factor(质因数分解)
- hdu 5428 The Factor(唯一分解定理)
- hdu 5428(分解质因子)
- HDOJ 5428 The Factor(分解质因数)
- 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
- C语言实现字符串转化成整数
- R语言包_stats::optim
- [git] error: .repo/manifests/: contains uncommitted changes
- tomcat如何避免遭遇ClassNotFoundException
- 日志8-20150906-20150911
- hdu 5428 The Factor(分解质因子)
- Linux学习记录(9)Linux基本网络命令
- Android时事新闻客户端
- 数据库操作优化(insert、update、delete、select)
- time函数解析
- 广泛结交而谨慎择友
- Hadoop2.2.0中HDFS的高可用性实现原理
- docker-安装
- tomcat 域名解析