题意:给你一个数,让你判断是否是非素数,同时a^n%n==a (其中 a 的范围为 2~n-1)

思路:先判断是不是非素数,然后利用快速幂对每个a进行判断

代码:

  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #define ll long long
  6. using namespace std;
  7.  
  8. bool isprime(ll num)
  9. {
  10. if(num==) return false;
  11. for(int i=;i<=sqrt(num);i++)
  12. {
  13. if(num%i==)
  14. {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20.  
  21. ll qmod(ll a,ll b)
  22. {
  23. ll mod=b;
  24. ll ans=;
  25. while(b)
  26. {
  27. if(b%)
  28. {
  29. ans=(ans*a)%mod;
  30. }
  31. b=b/;
  32. a=(a*a)%mod;
  33. }
  34. return ans;
  35. }
  36.  
  37. int main()
  38. {
  39. ll n;
  40. while(cin>>n&&n)
  41. {
  42. if(isprime(n)==true)
  43. {
  44. cout<<n<<" is normal."<<endl;
  45. continue;
  46. }
  47. int flag=;
  48. for(int i=;i<=n-;i++)
  49. {
  50. if(i!=qmod(i,n))
  51. {
  52. flag=;
  53. break;
  54. }
  55. }
  56. if(flag)
  57. {
  58. printf("The number %d is a Carmichael number.\n",n);
  59. }
  60. else
  61. {
  62. cout<<n<<" is normal."<<endl;
  63. }
  64. }
  65. return ;
  66. }

Uva 10006 Carmichael Numbers (快速幂)的更多相关文章

  1. UVa 10006 - Carmichael Numbers

    UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...

  2. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  3. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  4. pojPseudoprime numbers (快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  5. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

  6. POJ 1995:Raising Modulo Numbers 快速幂

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5532   Accepted: ...

  7. UVA 11609 Teams 组合数学+快速幂

    In a galaxy far far away there is an ancient game played among the planets. The specialty of the gam ...

  8. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  9. UVa 10870 Recurrences (矩阵快速幂)

    题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...

随机推荐

  1. Spring Mvc + Easyui中根据查询结果导出文件

    项目是典型的SpringMvc + Easyui,需求是前台页面根据查询条件导出生成CSV文件. 基本流程是:前台页面通过表单提交的方式,请求后台,后台接受到查询参数,根据查询参数查询到数据集合,最后 ...

  2. STM32F4的FPU单元讲解

    搞STM32F407单片机的时候 看见的关于STM32F4系列的FPU 单元讲解 比较精彩的博客  于是特意转载 和大家分享 转自:http://blog.renren.com/blog/256814 ...

  3. iOS runtime的应用实例

      一直想弄明白runtime是怎么回事,因为面试的时候这是一道必备问题,但是平时用的机会真的少之又少,我一度以为runtime只是用来装13的利器,没什么卵用.但是随着学习的增多,发现runtime ...

  4. 解决新建maven项目速度慢的问题

    问题描述 通过idea新建maven项目,参数设置好后,idea自动构建maven项目时,速度很慢. 参数设置如图: 执行时间如下图: Total time为8:49,花了将近十分钟时间. 连续尝试了 ...

  5. 信号处理——Hilbert变换及谱分析

    作者:桂. 时间:2017-03-03  23:57:29 链接:http://www.cnblogs.com/xingshansi/articles/6498913.html 声明:转载请注明出处, ...

  6. 未来工厂——电器行业ERP案例

    江苏科兴电器有限公司位于全国著名的“银杏之乡”泰兴市南首,主要生产35kV及以下电流.电压互感器等系列产品.产品多次经国家及省市技术监督部门抽检合格,广泛应用于国家重点工程.“COSINE”商标荣获泰 ...

  7. Android中使用findViewByMe提升组件查找效率

    1.引出 安卓初学者一般在写android Activity的时候总是会在onCreate方法中加上setContentView方法来加载layout,通过findViewById来实现控件的绑定,刚 ...

  8. 棋盘覆盖(一) ACM

    棋盘覆盖 描述 在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的2×2方格(图2为其中缺右下角的一个),去覆盖2k×2k未被覆盖过的方格,求 ...

  9. 关于 jquery html 动态添加的元素绑定事件——On()

    Ajax动态生成的数据,动作绑定需要重新执行 $(document).on('click','.btn1',function(){}); 替换: $('btn1').on('click') = fun ...

  10. 多线程爬坑之路-ThreadLocal源码及原理的深入分析

    ThreadLocal<T>类:以空间换时间提供一种多线程更快捷访问变量的方式.这种方式不存在竞争,所以也不存在并发的安全性问题. This class provides thread-l ...