-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10551.cpp
executable file
·102 lines (88 loc) · 1.85 KB
/
10551.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Problem: Basic remains UVa 10551
Programmer: Md. Mahmud Ahsan
Description: Base Conversion + Moduler Arithmetic
Compiled: Visual C++ 7.0
Date: 16-09-05
*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long lint;
lint pow(int b, int n, lint mod){
lint num = 1;
for (int i = 0; i < n; ++i){
num = num * b;
num = num % mod;
}
return num;
}
lint convertBase10(char *str, int base, lint mod){
int len = strlen(str) - 1;
int i, n;
lint number = 0;
for (i = 0; str[i]; ++i){
switch(str[i]){
case 'a':
case 'A': n = 10; break;
case 'b':
case 'B': n = 11; break;
case 'c':
case 'C': n = 12; break;
case 'd':
case 'D': n = 13; break;
case 'e':
case 'E': n = 14; break;
case 'f':
case 'F': n = 15; break;
default: n = str[i]-48;
}
number = number + (n * pow(base, len, mod));
number = number % mod;
--len;
}
return number;
}
void revStr(char *str){
char temp[100];
int len = strlen(str) - 1;
int i;
for (i = 0; str[i]; ++i){
temp[i] = str[len-i];
}
temp[i] = '\0';
strcpy(str, temp);
}
char* decToOther(int number, int base){
char str[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char final[100];
int n, temp, j;
j = -1;
do{
temp = number % base;
final[++j] = str[temp];
number = number / base;
}while (number != 0);
final[++j] = '\0';
revStr(final);
return final;
}
int main(){
int b;
lint mod, final;
char num[2000], _mod[100], res[2000];
while (cin >> b){
if (b == 0) break;
cin >> num >> _mod;
mod = convertBase10(_mod, b, 999999999);
final = convertBase10(num, b, mod);
//special case
if (mod == 1){
cout << 0 << endl;
continue;
}
strcpy(res, decToOther(final, b));
cout << res << endl;
}
return 0;
}