快速幂取模
来源:互联网 发布:厦门seo分享 编辑:程序博客网 时间:2024/06/18 05:10
快速幂取模:
int PowerMod(int a, int b, int c){ int ans = 1; a = a % c; while(b>0) { if(b&1) ans = (ans * a) % c;a = (a * a) % c; b >>= 1; } return ans;}
几道模板题:
http://acm.hdu.edu.cn/showproblem.php?pid=1061
求a^b的个位数字,即(a^b)mod 10。
int mod_exp(int a, int b, int c){ int ans = 1; int t = a%c; while(b) { if(b&1) ans = ans*t%c; t = t*t%c; b >>= 1; } return ans;}int main(){ int test,n; scanf("%d",&test); while(test--) { scanf("%d",&n); int ans = mod_exp(n,n,10); printf("%d\n",ans); } return 0;}
http://acm.hdu.edu.cn/showproblem.php?pid=2035
求a^b的最后三位数,即(a^b)mod 1000.
using namespace std;const int INF = 0x3f3f3f3f;_LL mod_exp(_LL a, _LL b, int c){ _LL ans = 1; _LL t = a%c; while(b) { if(b&1) ans = ans*t%c; t = t*t%c; b >>= 1; } return ans;}int main(){ _LL a,b; while(~scanf("%I64d %I64d",&a,&b)) { if(a == 0 && b == 0) break; _LL ans = mod_exp(a,b,1000); printf("%I64d\n",ans); } return 0;}
http://poj.org/problem?id=1995
_LL mod_exp(_LL a, _LL b, _LL c){ _LL res = 1; _LL t = a%c; while(b) { if(b&1) res = res*t%c; t = t*t%c; b >>= 1; } return res;}int main(){ int test,t; _LL ans; scanf("%d",&test); while(test--) { _LL a,b,c; ans = 0; scanf("%I64d",&c); scanf("%d",&t); while(t--) { scanf("%I64d %I64d",&a,&b); _LL res = mod_exp(a,b,c); ans = (ans%c + res%c)%c; } printf("%I64d\n",ans); } return 0;}
0 0
- 快速幂,快速幂取模
- 快速幂 + 快速幂取模
- 快速幂 快速幂取模
- 快速幂 快速幂取模
- 【快速幂】【快速幂取模】
- 快速幂,快速幂取模,矩阵快速幂
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- mysql账号权限密码设置方法
- 刚刚开通
- ScrollView滑动到最底端或者最顶端再加载数据
- Equalizer vs2010 编译 注意问题
- log4j 日志限制大小 拆分成30个 不按日期分日志 按大小拆分 按日期产生
- 快速幂取模
- bochs安装
- 一个简单的判断三角形形状的C程序
- 一个说明
- hasOwnProperty方法的使用
- android mediaRecorder详细讲解及使用
- Aptana Studio 打开报 Could not launch the product the specified workspace cannot becarated.
- Powerdesigner 连接 sqlserver2008 一些问题及解决汇总
- java JFrame去掉标题栏后如何实现鼠标移动到边框可以进行拖拉以放大缩小