问题描述:2^100(mod 5)是多少?
代码如下:
//1.8 2^100 (mod 5)
#include <iostream>
using namespace std;
int pow(int m,int n,int d)//m^n mod d
{
int index = 1;
int base = m%d;//when index = 1
while(index*2<=n){
index = index*2;
base = base*base%d;
}
while(++index<=n){
base=base*m%d;
}
return base;
}
int main(){
cout<<"2^4 mod 5:"<<pow(2,4,5)<<endl;
cout<<"3^4 mod 6:"<<pow(3,4,6)<<endl;
cout<<"2^5 mod 5:"<<pow(2,5,5)<<endl;
cout<<"2^6 mod 5:"<<pow(2,6,5)<<endl;
cout<<"2^7 mod 5:"<<pow(2,7,5)<<endl;
cout<<"2^100 mod 5:"<<pow(2,100,5)<<endl;
system("pause");
}