# 幂运算 C++（快速幂和大数运算）

1. 快速幂

$\inline \text{For example}: 3^5 = ?$

$\inline \text{Because } 5_{10} = 101_2 \text{, which means} 5 = 2^2\times1+2^1\times0+2^0\times1,$

$\inline \text{so }3^5 = 3^{2^2\times1+2^1\times0+2^0\times1} = 3^{2^2\times1}\times3^{2^1\times0}\times3^{2^0\times1}$

base*=base 这里就是在算$\inline 3^{2^k}$

int poww(int a, int b){ // return a ^ b
int ans = 1, base = a;
while(b != 0){
if(b&1 != 0) ans *= base;
base *= base;
b>>=1;
}
return ans;
}

2. 大数运算

$\inline \text{What's the result of }34^{67}?$

34 67

4065251716212557932550840533486793017502084747731049146444657475519303778647807844035539194338024095744

#include <iostream>
#include <stdio.h>
#define MAX 1000000
using namespace std;

int nbit;
int ans[MAX];

void init(){
nbit = 1;
ans[0] = 1;
}

void mul(int num){
int temp_mul = 0;
for(int i = 0; i < nbit; i++){
temp_mul = num * ans[i];
ans[i] = (temp_mul + temp_add) % 10;
}
}
}

void exp(int b, int e){
for(int i = 0; i < e; i++) mul(b);
}

void vout(){
for(int i = nbit - 1; i >= 0; i--) printf("%d", ans[i]);
printf("\n");
}

int main(){
int b, e;
while(~scanf("%d %d", &b, &e)){
init();
exp(b, e);
vout();
}
return 0;
}