-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path10515.cpp
More file actions
executable file
·53 lines (46 loc) · 1.02 KB
/
10515.cpp
File metadata and controls
executable file
·53 lines (46 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Problem: Power et. All UVa 10515
Programmer: Md. Mahmud Ahsan
Description: Number Theory
Compiled: Visual C++ 7.0
Date: 21-02-06
*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int myPow(int m, int n){
int total;
if (m == 0) return 0; //base 0
if (n == 0) //powe 0
total = 1;
else
total = m;
for (int i = 1; i < n; ++i){
total = total * m;
total = total % 10;
}
return total;
}
int main(){
//freopen("input.txt", "r", stdin);
string m, n;
int _m, _n, temp;
while (cin >> m >> n){
if (m == "0" && n == "0") break;
_m = m[m.size()-1] - 48;
if (n.size() >= 2){ // we need n's last two digit
temp = n[n.size()-1] - 48;
_n = (n[n.size()-2]-48) * 10 + temp;
}
else
_n = n[n.size()-1]-48;
if (_n == 0){ //last two digit zero then
temp = myPow(_m, 99); //_m ^ 100 = _m ^ 99 * _m
temp = (temp * _m) % 10;
}
else
temp = myPow(_m, _n) % 10;
cout << temp << endl;
}
return 0;
}