题目链接: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. 'nmake' 不是内部或外部命令,VCVARS32.BAT路径问题

    'nmake' 不是内部或外部命令,VCVARS32.BAT路径问题 2014-5-24 VC运行不正确基本上都是路径的问题,今天在进行Openssl开发的环境搭建时,需要使用nmake进行源码和库的 ...

  2. jquery自定义分页插件

    //每次只显示5个页码(function ($) { //设定页码方法,初始化 $.fn.setPager = function (options) { var opts = $.extend({}, ...

  3. jquery 动态增加的html元素,初始化设置在id或class上的事件无效

    一般情况,我们会在页面初始化完成后对class定义一些全局事件,举个栗子: $(document).ready(function(){ $(".class").on("m ...

  4. 配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题

    转载自:-杨博的日志 - 网易博客 Vim / gVim 在中文 Windows 下的字符编码有两个问题: 默认没有编码检测功能 如果一个文件本身采用的字符集比 GBK 大(如 UTF-8.UTF-1 ...

  5. The Priest Mathematician

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/F f[0] = 1 , f[ i ] = f[ i - 1 ] ...

  6. poj 1200 Crazy Search(hash)

    题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...

  7. uva11021 - Tribles(概率)

    11021 - Tribles GRAVITATION, n.“The tendency of all bodies to approach one another with a strengthpr ...

  8. jQuery UI Widget 原理

    先看下代码的相关注释: /*! * jQuery UI Widget 1.8.1 * * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/abo ...

  9. Codeforces 489C Given Length and Sum of Digits...

    m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...

  10. Chapter 1.简单工厂模式

    该篇文章通过一个写计算器控制台程序,来导入文章主题. 首先,要注意代码规范,变量命名有意义,不能随意用A,B,C;    功能要封装好,不要写在一个主函数里,另外要考虑后期需求的更改,如果出现多个函数 ...