Pagini recente » Cod sursa (job #2738091) | Cod sursa (job #1228833) | Cod sursa (job #1054327) | Cod sursa (job #2388066) | Cod sursa (job #1397566)
#include <fstream>
#include <stack>
#include <cstring>
using namespace std;
long long int MOD = 10;
char input[100002];
stack<char> s;
int main()
{
FILE *fin = fopen("calcul.in", "r");
FILE *fout = fopen("calcul.out", "w");
long long int x = 0, i, j;
char c;
fgets(input, 100002, fin);
j = strlen(input) - 1;
i = j - 10;
if (i < 0)
i = 0;
for (; i < j; ++i)
{
x = x * 10 + input[i] - '0';
}
fgets(input, 50000, fin);
for (i = 0; input[i] != '\n'; ++i)
{
if (input[i] <= '9') {
j = input[i] - '0';
s.push(j & 8);
s.push(j & 4);
s.push(j & 2);
s.push(j & 1);
} else {
j = input[i] - 'A' + 10;
s.push(1);// no need to check
s.push(j & 4);
s.push(j & 2);
s.push(j & 1);
}
}
fscanf(fin, "%hhd", &c);
for (i = 0; i < c; ++i)
MOD *= 10;
/// Next line for test
//MOD *= 10000;
x %= MOD;
j = 0;
long long int rasp = 0, y = x;
while (!s.empty())
{
j = s.top();
s.pop();
if (j) {
rasp = (rasp + y) % MOD;
y = y * ((x * (1 + x)) % MOD) % MOD;
} else {
y = y * (1 + x) % MOD;
}
x = (x * x) % MOD;
}
char format[8];
format[0] = '%';
format[1] = '0';
format[2] = c + '0';
format[3] = format[4] = 'l';
format[4] = 'd';
format[5] = '\n';
if (rasp / MOD)
fprintf(fout, format, rasp % MOD);
else
fprintf(fout, "%lld", rasp % MOD);
fclose(fin);
fclose(fout);
return 0;
}