UVA - 1323 Vivian's Problem
来源:互联网 发布:手机淘宝无响应怎么办 编辑:程序博客网 时间:2024/06/06 02:55
Description
The desire to explore the unknown has been a driving force in human history since the dawn of time. From the earliestdocumented accounts, ancient civilizations had explored the earth by sailing around. Early adventurers were motivatedby religious beliefs, the desire conquest, the need to establish trade routes, and hunger for gold.
You never know what will happen before the exploration. Neither does Bruce Lee. Someday, Mr. Lee entered a desolatetropical rainforest. And after several days' exploring, he came in front of a cave with something blinking in it.A beautiful girl named Vivian came out just before he tried to go into the cave. And Vivian told Mr. Lee that he mustanswer some questions before he entered the cave. As the best friend of Mr. Lee, you should help him to work it out.
You will get k positive integers p1, p2...pi...pk (1ik) from Vivian. From thesenumbers, you can calculate N,
Input
Input contains several testcases. For each testcase, the first line contains only one integerk (0 < k100),representing the number of positive integers. Then there are k positive integersp1,p2...pi...pk (1 < pi < 231, 1ik) in the second line, representingthe given numbers.
Input is terminated by end of file.
Output
For each testcase, you should output your result in a single line. If you can findN from the given numbers,output the largest exponent. Otherwise, output `NO'. Extra spaces are not allowed.
Sample Input
1232 3 4
Sample Output
NO2
题意:输入k个正整数p1,p2,p3,..,pk,找出k个非符整数ei使得,N = pi^ei(1<=i<=k),注意,由于x>0,ei不能全为0,如果无解输出NO,否则输出最大的x
思路:梅森素数问题:关于梅森素数,有一个重要的定理:“一个数能够写成几个不重复的梅森素数的乘积” 等价于 “这个数的约数和是2的幂次”,根据这个原理,我们去分解每个数,然后求解
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>typedef long long ll;using namespace std;const int maxn = 105;int f[8] = {2,3,5,7,13,17,19,31} ;int ans, cnt, status[maxn];int dp[10];void insert(int a) {status[cnt] = 0;for (int i = 0; i < 8 && a >= dp[i]; i++) if (a % dp[i] == 0) {status[cnt] |= 1<<i;a /= dp[i];if (a % dp[i] == 0)return;}if (a == 1)cnt++;}int getans(int st) {int res = 0;for (int i = 0; i < 8; i++)if (st & (1<<i))res += f[i];return res;}void dfs(int cur, int st) {ans = max(ans, getans(st));for (int i = cur; i < cnt; i++)if ((st & status[i]) == 0)dfs(i+1, st | status[i]);}int main() {for (int i = 0; i < 8; i++)dp[i] = (1 << f[i]) - 1;int k, a;while (scanf("%d", &k) != EOF) {cnt = 0;for (int i = 0; i < k; i++) {scanf("%d", &a);insert(a);}if (cnt == 0) {printf("NO\n");} else {ans = 0;dfs(0, 0);printf("%d\n", ans);}}return 0;}
- UVA - 1323 Vivian's Problem
- uva 1323 - Vivian's Problem(梅森素数)
- Vivian's Problem
- poj 1777 Vivian's Problem
- Vivian's Problem 梅森素数
- POJ 1777 Vivian's Problem(梅森素数)
- 【数论】poj1777 Vivian's Problem(梅森素数)
- UVA Shoemaker's Problem
- UVa 10026 - Shoemaker's Problem
- uva 10026 - Shoemaker's Problem
- uva 10026 - Shoemaker's Problem
- uva 10026 - Shoemaker's Problem
- uva 10026 Shoemaker's Problem
- UVa 10026 - Shoemaker's Problem
- uva 10026 - Shoemaker's Problem
- UVa 10026 Shoemaker's Problem
- uva 10026 Shoemaker's Problem
- uva 10026 Shoemaker's Problem
- halcon11 halcon11.0.1 全功能完美无限制版本带视频教程/中文全功能 最全资料
- GTK3学习笔记2 Hello World in GTK+
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践
- Navigation Drawer(导航抽屉)
- Linux写时拷贝技术(copy-on-write)
- UVA - 1323 Vivian's Problem
- 织梦 无法更换文章缩略图与文章编辑器不能粘贴
- xp使用4G内存方法
- Grenander.Software.Workshop.LoudSpeaker.Lab.v3.1.3 1CD(专业级的音箱设计软件)
- HTTP 协议详解
- 我的oracle笔记-02 之 回滚段
- jquery 跳出each循环
- Objective—C中的排序及Compare陷阱
- Fragment理解的图