http://acm.hdu.edu.cn/showproblem.php?pid=2837

  1. // a^b%p=a^(b%phi(p)+phi(p))%p
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<bitset>
  7. #include<iomanip>
  8.  
  9. using namespace std;
  10. #define INT long long
  11.  
  12. INT euler( int n)
  13. {
  14. INT ret=1,i;
  15. for (i=2;i*i<=n;i++)
  16. if (n%i==0){
  17. n/=i,ret*=i-1;
  18. while (n%i==0)
  19. n/=i,ret*=i;
  20. }
  21. if (n>1)
  22. ret*=n-1;
  23. return ret;
  24. }
  25.  
  26. INT Pow( INT a , INT b , INT m )
  27. {
  28. INT ans = 1 ;
  29. while( b )
  30. {
  31. if( b & 1 )
  32. {
  33. ans = ans * a % m ;
  34. }
  35. a *= a ;
  36. a %= m ;
  37. b >>= 1 ;
  38. }
  39. return ans % m ;
  40. }
  41.  
  42. INT fun2( int a , int b , int m )
  43. {
  44. INT ans = 1 ;
  45. for( int i = 1 ; i <= b ; ++i )
  46. {
  47. ans *= a ;
  48. if( ans >= m )
  49. return ans ;
  50. }
  51. return ans ;
  52. }
  53.  
  54. INT fun1( int n , int m )
  55. {
  56. INT phi = euler( m ) ;
  57. if( n < 10 )
  58. return n ;
  59. INT a = fun1( n / 10 , phi ) ;
  60. INT b = fun2( n % 10 , a , m ) ;
  61. if( b >= m )
  62. {
  63. INT ans = Pow( n % 10 , a + phi , m ) ;
  64. if( ans == 0 )
  65. ans += m ;
  66. return ans ;
  67. }
  68. return b ;
  69. }
  70.  
  71. int main()
  72. {
  73. int Case , n , m , p ;
  74. cin >> Case ;
  75. while( Case-- )
  76. {
  77. cin >> n >> m ;
  78. INT ans = fun1( n , m ) % m ;
  79. cout << ans << endl ;
  80. }
  81. return 0 ;
  82. }

hdu2837数论的更多相关文章

  1. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  2. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  3. 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)

    ~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...

  4. hdu 1299 Diophantus of Alexandria (数论)

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  6. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

  8. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  9. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

随机推荐

  1. Codeforces Round #243 (Div. 1)-A,B,C-D

    此CF真是可笑.. . 由于早晨7初始点,因此,要做好CF时间已经17没有休息一小时,加上中午5小时耐力赛. 心里很清楚.是第一个问题的时候,几乎被解读为寻求最大的领域和.然后找到一个水体,快速A降. ...

  2. jenkins综合cobertura,来电显示cobertura的report

    我的项目是使用maven作为构建工具.左右maven如何整合jenkins请参阅: http://blog.csdn.net/yaominhua/article/details/40684355 本文 ...

  3. 图片alpha blending的计算

    转载时请注明出处和作者联系方式:http://blog.csdn.net/mimepp作者联系方式:YU TAO <yut616 at sohu dot com> 一幅彩色图像的每一个像素 ...

  4. vim打开出现的文档^M什么

    网上公开的一些代码,发现里面多^M符号.这是什么? 我搜索^M没有效果,这应该是一个特殊的控制字符.找换行的结果是不.在每一行的末尾是回车,代替它周围包裹,对于由线定义不同的编码系统是不一样的. li ...

  5. ReSharper C++计划上市

    ReSharper是著名的VS代码生成工具,去年夏天,JetBrains公司就发布了ReSharper将推出支持C++的版本. ReSharper C++ EAP(Early Access Progr ...

  6. Guava之简介

    1.介绍 Guava最初是在2007年作为“Google Collection  Library” 出现的,这个项目在处理Java集合时提供了一些有用的工具,Google的这个guava项目已经成为了 ...

  7. leetcode第八题--String to Integer (atoi)

    Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...

  8. javascript 10进制和64进制的转换

    原文:javascript 10进制和64进制的转换 function string10to64(number) { var chars = '0123456789abcdefghigklmnopqr ...

  9. /bin/bash^M: bad interpreter: No such file or directory

    <今天遇到的问题,  网上有非常多类似的文章,  个人记录下来,  加深记忆,  已经明确的就绕路吧.> 运行一个脚本full_build.sh 时, 一直是提示我: -bash: ./f ...

  10. css3动画实例测试

    1.css3动画属性分析(2016-5-11) 1.transition: 规定属性变换规则,可以这样讲.transition(a,b,c,d); a:要变换的属性: b:过渡时间: c:运动方式: ...