杭电ACMOJ 人见人爱A^B Easy *第一次见到快速幂取模

基本思想:

快速幂取模,如果不去模,就是快速幂大数问题;

 

关键点:

无;

 

#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
string s;

//int f[maxn];


void multip(string &s, int n) {
	int carry = 0;
	for (int i = 0; i < s.size(); i++) {
		int temp = carry  + (s[i] - '0')*n;
		s[i] = temp % 10 + '0';
		carry = temp / 10;
	}
	while (carry != 0) {
		s += char('0' + carry % 10);
		carry /= 10;
	}
}

int charge(int n,int m) {
	int base = n;
	int res = 1;
	while (m != 0) {
		if (m % 2 !=0) {
			res = res * base;
			res %= 1000;
		}
		base *= base;
		base %= 1000;
		m /= 2;
	}
	return res;
}

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0)
			break;
		cout << charge(n, m) << endl;
	}
	return 0;
}

  

上一篇:数组-67.二进制求和


下一篇:2.两数相加