2013年11月18日 星期一

5 的 100次方 mod 7 (更精簡版)

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
 map<int,bool> m; //記錄已經出現的餘數
 vector<int> v;  //依序記錄餘數
 int a,b,c,d;  //a被除數, b除數, c次方, d餘數
 cin >> a >> b >> c;
 d = a%b;  //處理 a<b 的情形
 while(!m[d]){  
  m[d]=true;
  v.push_back(d);
  d=(d*a)%b; 
 } 
 c = c%m.size(); 
 cout<<v[c-1];
 system("pause");
 return 0;
}
/*
 5 7 1 => 5 //(5^1)%7, 5%7
 5 7 100 => 2 //(5^100)%7
*/

沒有留言:

張貼留言