首页 > 代码库 > hdu2035【二分快速幂】
hdu2035【二分快速幂】
求AˆB取模。直接二分快速幂即可。比如9ˆ9=(9ˆ2)ˆ4 * 9,将B一直模2然后A自乘,复杂度long(n)。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 int quickpow(int a, int b, int mod) { 7 int ans = 1; 8 while(b) { 9 if(b & 1) {10 ans = ans * a % mod;11 }12 a = a * a % mod;13 b >>= 1;14 } 15 return ans;16 }17 18 int main() {19 int a, b;20 while(scanf("%d%d", &a, &b), a + b) {21 printf("%d\n", quickpow(a, b, 1000));22 }23 return 0;24 }25
hdu2035【二分快速幂】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。