快速幂取模
来源:互联网 发布:mac pro评测视频 编辑:程序博客网 时间:2024/06/07 18:19
原文:http://blog.sina.com.cn/s/blog_8619a25801010wcy.html
计算x^ymod n;如果采用常规方法,当x与y都比较小的情况下,采用直接计算可以,但是如果当x跟y都非常大的时候,如2^1000mod 100000,那该如何解决呢?
#define LL long long intLL exp_mod( LL x , LL y , LL n ){ LL ret=1; while(y){ if(y&1) ret=(ret*x)%n; x=(x*x)%n; y=y>>1; } return ret;}
#include <stdio.h>#define LL long long intLL exp_mod(LL x,LL y,LL n){ LL ret=1; while(y){ if(y&1) ret=(ret*x)%n; x=(x*x)%n; y=y>>1; } return ret;} int main(){ LL a,b,k,ans; while(scanf("%lld%lld%lld",&a,&b,&k)==3){ ans=exp_mod(a,b,k-1); if(ans==0) ans=k-1; printf("%lld\n",ans); } return 0;}
-----------Amazing Mathematics
- 快速幂,快速幂取模
- 快速幂 + 快速幂取模
- 快速幂 快速幂取模
- 快速幂 快速幂取模
- 【快速幂】【快速幂取模】
- 快速幂,快速幂取模,矩阵快速幂
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 文本框中自动完成的应用
- PHP再学习4—— slim框架学习和使用
- Windows服务、批处理项目实战)
- Windows下android adb使用
- Android JNI调用OpenCV,长时间运行内存异常,导致闪退的log分析和解决---(ReferenceTable overflow (max=1024)造成的)
- 快速幂取模
- Vertica 数据库的保留字(词)列表。建议收藏开发时方便查找
- 手绘移动端极致体验
- 最大公约数
- 经验分享:App设计中4类便捷的单手操作
- anr文件、wifi密码文件路径
- 读书笔记:“集体智慧编程”之第五章:“求最优解”的算法
- LeetCode OJ:Unique Binary Search Trees II
- 从Delphi开始学Cocos2dx-3.0[15]:关于精灵帧和精灵帧缓存