您的位置:首页 > 其它

HDU 2035 快速幂取模

2016-10-03 12:59 204 查看
HDU 2035

题解 : 模板题, 快速幂入门.

code: 

/*
adrui's submission
Language : C++
Result : Accepted
Love : ll
Favorite : Dragon Balls

Standing in the Hall of Fame
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<bitset>
#include<map>
using namespace std;

#define M(a, b) memset(a, b, sizeof(a))
#define mid ((l + r) >> 1)
#define ls rt << 1, l, mid
#define rs rt << 1|1, mid + 1, r
#define lowbit(x) (x & (-x))
#define LL long long
#define REP(n) for(int i = 1; i <= n; i++)
#define debug 0
#define mod 1000

int n, m;

int fast_multi(int a, int b) {
int ans = 0;

while (b) {
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}

return ans;
}
int fast_mod() {
int res = 1;

while (m) {
if (m & 1) res = fast_multi(res, n);
n = fast_multi(n, n);
m >>= 1;
}

return res;
}

int main() {
#if debug
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif //debug

while (~scanf("%d%d", &n, &m), n + m) {
printf("%d\n", fast_mod());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  快速幂取模