UVA 10837 - A Research Problem(欧拉函数)
来源:互联网 发布:自动建站系统源码 编辑:程序博客网 时间:2024/06/15 08:34
UVA 10837 - A Research Problem
题目链接
题意:给定phi(n),求最小满足的最小的n
思路:phi(n)=pk11(p1−1)∗pk22(p2−1)∗pk33(p3−1)....(p为质数),因此对于给定phi(n),先把满足条件phi(n)%(p−1)=0的素数全找出来,在这些素数基础上进行暴力搜索,枚举哪些素数用与不用,求出最小值。这样做看似时间复杂度很高,但是实际上,由于每次多选一个素数之后对于值是呈指数上升的,所以实际组合出来的情况并不会太多,因此是可行的。还有一个注意点就是由于素数表只打到1W,所以实际上p是可能超过1W的,但是这样的值只可能有一个(不然p*p都超过上限了),因此暴力枚举完以后最后一个素数要单独判断,判断是否是素数与是否在之前被用掉了。
代码:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 10005;int vis[N], prime[N], pn, n, f[N], fn, ans;void get_prime(int n) { pn = 0; memset(vis, 0, sizeof(vis)); for (int i = 2; i <= n; i++) {if (vis[i]) continue;prime[pn++] = i;for (int j = i * i; j < N; j += i) vis[j] = 1; }}void build(int n) { fn = 0; ans = 200000000; for (int i = 0; i < pn && (prime[i] - 1) * (prime[i] - 1) <= n; i++) {if (n % (prime[i] - 1)) continue;f[fn++] = prime[i]; }}bool judge(int sum) { for (int i = 0; i < pn && prime[i] * prime[i] <= sum; i++)if (sum % prime[i] == 0) return false; for (int i = 0; i < fn; i++) {if (vis[i] && f[i] == sum) return false; } return true;}void dfs(int now, int sum, int tot) { if (now == fn) {if (sum == 1) ans= min(ans, tot);else if (judge(sum + 1)) {tot *= (sum + 1); ans = min(ans, tot);}return; } dfs(now + 1, sum, tot); if (sum % (f[now] - 1)) return; vis[now] = 1; sum /= (f[now] - 1); tot *= f[now]; dfs(now + 1, sum, tot); while (sum % f[now] == 0) {sum /= f[now];tot *= f[now];dfs(now + 1, sum, tot); } vis[now] = 0;}int main() { get_prime(10000); int cas = 0; while (~scanf("%d", &n) && n) { build(n);memset(vis, 0, sizeof(vis));dfs(0, n, 1);printf("Case %d: %d %d\n", ++cas, n, ans); } return 0;}
1 0
- UVa 10837 A Research Problem 欧拉函数
- UVA 10837 - A Research Problem(欧拉函数)
- uva 10837 - A Research Problem(欧拉函数+暴力)
- uva 10837 A Research problem 欧拉函数+暴力
- A Research Problem UVA
- A Research Problem UVA
- UVA 10837(p345)----A Research Problem
- Send a Table 欧拉函数UVA
- uva10837 A Research Problem
- uva-10820-Send a Table 欧拉函数,筛法求欧拉函数
- 【HDU】5152 A Strange Problem 【线段树+欧拉函数】
- UVA 10820 - Send a Table 数论 (欧拉函数)
- uva 10820 - Send a Table(欧拉函数)
- UVa 10820 Send a Table(欧拉函数)
- UVa 10820 - Send a Table 欧拉函数模板
- UVA - 10820 Send a Table 欧拉函数
- Send a Table - UVa 10820 欧拉函数
- uva 10820 Send a Table 欧拉函数
- Spring 学习笔记——第一天
- 泛型知识点整理
- 文件相似性判断 -- SimHash
- Microsoft Visual Studio 2015 iso 下载
- linux v4l2 ioctl(fd, VIDIOC_QUERYBUF, &dev.buffer)出错与返回长度为0的问题
- UVA 10837 - A Research Problem(欧拉函数)
- 如何消除UIBarButtonItem系统默认的10像素点
- sqlite3命令读出sqlite3格式的文件内容案例
- iOS中本地存储
- POJ-2201-Cartesian Tree(笛卡尔树)
- HDU-#4006 The kth great number(优先队列)
- PPPoE技术白皮书
- http协议
- Activity状态的保存