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;
}
题解 : 模板题, 快速幂入门.
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;
}
相关文章推荐
- 【快速幂取模】HDU 2035---人见人爱A^B
- hdu 2035 人见人爱A^B (快速幂取模)
- 人见人爱A^B 【hdu-2035】【快速幂】
- hdu 2035 人见人爱A^B(快速幂入门)
- hdu 2035快速幂解法
- HDU 2035 人见人爱A^B 快速幂取模
- HDU 2035 人见人爱A^B(二分求幂,快速求幂)
- hdu 2035 人见人爱A^B (快速幂)
- hdu 2035 快速幂
- HDU 2035 快速幂
- HDU 2035 人见人爱A^B(快速幂取模 )
- HDU—2035—人见人爱A^B—【快速幂取模】
- hdu 2035 人见人爱A^B(两种解决方式:第二种快速幂取模)
- Hdu 2035 人见人爱A^B【快速幂】
- hdu 2035 (快速乘方)快速乘方取模
- HDU-2035-人见人爱A^B【快速幂】
- 快速幂 hdu 2035
- HDU 2035 人见人爱A^B (快速幂取模)
- 矩阵快速幂AC代码HDU 2035
- HDU 2035 人见人爱A^B(二分求幂,快速求幂)