题目链接: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处理出来。暴力搜索维护最小值,尽管看上去复杂度很高,可是由于对于垒乘来说,增长很快,所以搜索范围大大被缩小了。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxp = 1e4;
  7. const int INF = 0x3f3f3f3f;
  8. int ans;
  9. int np, vis[maxp+5], pri[maxp+5];
  10. int nf, fact[maxp+5], v[maxp+5];
  11. void prime_table (int n) {
  12. np = 0;
  13. for (int i = 2; i <= n; i++) {
  14. if (vis[i])
  15. continue;
  16. pri[np++] = i;
  17. for (int j = i * i; j <= n; j += i)
  18. vis[j] = 1;
  19. }
  20. }
  21. void get_fact (int n) {
  22. nf = 0;
  23. for (int i = 0; i < np && (pri[i]-1) * (pri[i]-1) <= n; i++) {
  24. if (n % (pri[i]-1) == 0)
  25. fact[nf++] = pri[i];
  26. }
  27. }
  28. bool judge (int n) {
  29. if (n == 2)
  30. return true;
  31. for (int i = 0; i < np && pri[i] * pri[i] <= n; i++)
  32. if (n % pri[i] == 0)
  33. return false;
  34. for (int i = 0; i < nf; i++)
  35. if (v[i] && fact[i] == n)
  36. return false;
  37. return true;
  38. }
  39. void dfs (int ret, int cur, int d) {
  40. if (d == nf) {
  41. if (judge(cur+1)) {
  42. if (cur == 1)
  43. cur = 0;
  44. ans = min(ans, ret * (cur+1));
  45. }
  46. return;
  47. }
  48. dfs(ret, cur, d+1);
  49. if (cur % (fact[d]-1) == 0) {
  50. v[d] = 1;
  51. ret *= fact[d];
  52. cur /= (fact[d]-1);
  53. while (true) {
  54. dfs(ret, cur, d+1);
  55. if (cur % fact[d])
  56. return;
  57. ret *= fact[d];
  58. cur /= fact[d];
  59. }
  60. v[d] = 0;
  61. }
  62. }
  63. int solve (int n) {
  64. ans = INF;
  65. get_fact(n);
  66. memset(v, 0, sizeof(v));
  67. dfs(1, n, 0);
  68. return ans;
  69. }
  70. int main () {
  71. prime_table(maxp);
  72. int cas = 1, n;
  73. while (scanf("%d", &n) == 1 && n) {
  74. printf("Case %d: %d %d\n", cas++, n, solve(n));
  75. }
  76. return 0;
  77. }

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

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. 「OC」 多态

    一.基本概念 多态在代码中的体现,即为某一类事物的多种形态,OC对象具有多态性.必须要有继承,没有继承就没有多态. 在使用多态时,会进行动态检测,以调用真实的对象方法. 多态在代码中的体现即父类指针指 ...

  2. JavaSE学习总结第05天_Java语言基础1

      05.01 方法概述和格式说明 简单的说:方法就是完成特定功能的代码块 在很多语言里面都有函数的定义,函数在Java中被称为方法 格式: 修饰符返回值类型方法名(参数类型参数名1,参数类型参数名2 ...

  3. 基本的编程原则SOLID

    1.单一职责原则:每个类只负责完成一个职责,当它需要完成多个职责时就需要将它拆分开来. 2.开放封闭原则:对扩展开放,对修改封闭. 3.里氏替换原则:子类对象可以替换(代替)它的所有父类(超类)对象. ...

  4. 模仿jquery的一些实现

    wylUtil.js //w作为window的形参,就表示window (function(w) { // 定义一个全局的window.wyl变量,就类似于jquery里的$,Jquery对象 w.w ...

  5. oracle 集合变量以及自定义异常的用法

    oracle 集合变量以及自定义异常的用法, 在过程 record_practice 有record变量和自定义异常的用法实例.具体在3284行. CREATE OR REPLACE Package ...

  6. CXF 开发 WebService

    什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...

  7. QT设置前景图位置(配色简单漂亮)

    QPushButton { background-image: url(:/Resources/green_click.png); image: url(:/Resources/toolsbutton ...

  8. python Unable to find vcvarsall.bat 错误

    今天遇到了这个方面的问题,目前找到两种办法.一种是换编译器如mingw,另一种是装vc.第一种方法没成功,现在正在等第二种. 第一种: 首先安装MinGW: 把MinGW的路径添加到环境变量path中 ...

  9. 1104--DNA排序

    问题描述: 逆序数可以用来描述一个序列混乱程度的量.例如,“DAABEC”的逆序数为5,其中D大于它右边的4个数·,E大于它右边的1的个数,4+1=5,又如,“ZWQM”的逆序数为3+2+1+0=6. ...

  10. 【Maven】Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0

    1.到 http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html 下载你所需要的oraclejar包 2.在 ...