题目链接:uva 10837 - A Research Problem

题目大意:给定一个phin。要求一个最小的n。欧拉函数n等于phin

解题思路:欧拉函数性质有,p为素数的话有phip=p−1;假设p和q互质的话有phip∗q=phip∗phiq

然后依据这种性质,n=pk11(p1−1)∗pk22(p2−1)∗⋯∗pkii(pi−1),将全部的pi处理出来。暴力搜索维护最小值,尽管看上去复杂度很高,可是由于对于垒乘来说,增长很快,所以搜索范围大大被缩小了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std;
const int maxp = 1e4;
const int INF = 0x3f3f3f3f; int ans;
int np, vis[maxp+5], pri[maxp+5];
int nf, fact[maxp+5], v[maxp+5]; void prime_table (int n) {
np = 0;
for (int i = 2; i <= n; i++) {
if (vis[i])
continue; pri[np++] = i;
for (int j = i * i; j <= n; j += i)
vis[j] = 1;
}
} void get_fact (int n) {
nf = 0; for (int i = 0; i < np && (pri[i]-1) * (pri[i]-1) <= n; i++) {
if (n % (pri[i]-1) == 0)
fact[nf++] = pri[i];
}
} bool judge (int n) {
if (n == 2)
return true;
for (int i = 0; i < np && pri[i] * pri[i] <= n; i++)
if (n % pri[i] == 0)
return false; for (int i = 0; i < nf; i++)
if (v[i] && fact[i] == n)
return false;
return true;
} void dfs (int ret, int cur, int d) {
if (d == nf) {
if (judge(cur+1)) { if (cur == 1)
cur = 0; ans = min(ans, ret * (cur+1));
}
return;
} dfs(ret, cur, d+1);
if (cur % (fact[d]-1) == 0) {
v[d] = 1;
ret *= fact[d];
cur /= (fact[d]-1); while (true) {
dfs(ret, cur, d+1); if (cur % fact[d])
return;
ret *= fact[d];
cur /= fact[d];
}
v[d] = 0;
}
} int solve (int n) {
ans = INF;
get_fact(n);
memset(v, 0, sizeof(v));
dfs(1, n, 0);
return ans;
} int main () {
prime_table(maxp);
int cas = 1, n;
while (scanf("%d", &n) == 1 && n) {
printf("Case %d: %d %d\n", cas++, n, solve(n));
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

uva 10837 - A Research Problem(欧拉功能+暴力)的更多相关文章

  1. UVa 10837 A Research Problem 欧拉函数

    题意: 给你一个欧拉函数值 phi(n),问最小的n是多少. phi(n) <= 100000000 , n <= 200000000 解题思路: 对于欧拉函数值可以写成 这里的k有可能是 ...

  2. UVA 10837 A Research Problem

    https://vjudge.net/problem/UVA-10837 求最小的n,使phi(n)=m #include<cstdio> #include<algorithm> ...

  3. UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)

    题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...

  4. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

  5. TOJ 3151: H1N1's Problem(欧拉降幂)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151 时间限制(普通/Java): ...

  6. POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 ...

  7. UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)

    Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...

  8. poj 2480 Longge's problem [ 欧拉函数 ]

    传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2 ...

  9. UVA 11426 - GCD - Extreme (II) 欧拉函数-数学

    Given the value of N, you will have to find the value of G. The definition of G is given below:G =i< ...

随机推荐

  1. String to Integer (atoi) - 复杂的测试

    这个题..是要把字符串转为整数.注意是整数,我看到整数的时候松了一口气,没有小数点的判断应该更好做.而且基本的转化函数我想每个程序员都无法忘记: res=res*+(str[i]-'); 其实就是这么 ...

  2. 关于MyEclipse启动时的插件启动(Maven4MyEclipse)

    在myEclipse的应用中有许多插件在开发的时候都用不到,那么,这些插件在启动myEclipse的时候一起启动的越少越好了 Maven4Myeclipse update 每当启动myEclipse的 ...

  3. Spark1.5.1的安装与部署 每一步详细测试截图

    转载或借鉴请注明转自 http://www.cnblogs.com/FG123/p/5101733.html  谢谢! 1.安装Spark之前需要先安装Java,Scala及Python(个人喜欢用p ...

  4. 第三节 ISBN 码 / ISSN 码

    ISBN与ISSNEAN的用途很广,除了我国的商品条码CAN以及日本商品条码JAN外,目前国际认可的书籍代号与期刊号的条码,也都是由EAN变身而来的.书籍的国际认可代号称为国际标准书号(Interna ...

  5. POJ 1472 Coins (多重背包+滚动数组)

    Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 25827 Accepted: 8741 Description Pe ...

  6. POJ输出状态的逻辑。

    实測POJ应该是採取一个一个点測.哪个点fail了就输出哪个点的状态,但接下来的点貌似还是要測. 測试方法,1000先測出有6个測点1,2,3,4,6.15,然后交了下面代码. #include &l ...

  7. Gstreamer中加入�x265编解码器

    官方的当前gstreamer版本号还不支持x265编解码,因此要加入�x265,须要自己编译.本文基于gstreamer1.3.3版进行编译安装.须要首先自己编译gstreamer1.3.3,以及对应 ...

  8. 用AS3清空容器下所有子显示对象

    容器中的子显示对象分为两类: 处于显示列表中的子显示对象.被numChildren所记录的. 由容器graphics对象绘制出来的矢量图.这个矢量图不属于Shape类型,不在容器的显示列表中,不被nu ...

  9. 【Oracle】nvl与nvl2对比

    1.nvl(v1,v2),只要v1为null,即将值变为v2. pay(类型):number; 在执行 nvl(pay_type,'null') 函数的时候会提示无效数字,可能是nvl函数会自动将其转 ...

  10. Xcode 静态库调试策略

    Xcode 静态库调试策略  (已经有现成的工程和静态库源码)    ***** 为安全期间建议备份一下静态库 Step1: 下载最新的工程[工程中有所要测试的静态库和头文件需要删除]: Step2: ...