Cod sursa(job #979140)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 31 iulie 2013 21:02:35
Problema Calcul Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <cstdio>
#include <cassert>

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int Mod, Base, Solution;
string Power;

int ComputeSum(const int base, const string &power) {
    int result = base;
    for (int i = 1, basePow = base; i < static_cast<int>(power.length()); ++i) {
        result = (result + 1LL * basePow * result) % Mod;
        basePow = (1LL * basePow * basePow) % Mod;
        if (power[i] == '1') {
            basePow = (1LL * basePow * base) % Mod;
            result = (result + 1LL * basePow) % Mod;
        }
    }
    return result;
}

void Solve() {
    Solution = ComputeSum(Base, Power);
}

void Read() {
    assert(freopen("calcul.in", "r", stdin));
    string stringBase, base16Power;
    int modPower;
    cin >> stringBase >> base16Power >> modPower;
    for (Mod = 1; modPower > 0; --modPower)
        Mod *= 10;
    for (int i = 0; i < static_cast<int>(stringBase.length()); ++i)
        Base = (10LL * Base + (stringBase[i] - '0')) % Mod;
    for (int i = static_cast<int>(base16Power.length()) - 1; i >= 0; --i) {
        int digit;
        if ('0' <= base16Power[i] && base16Power[i] <= '9')
            digit = base16Power[i] - '0';
        else
            digit = 10 + base16Power[i] - 'A';
        for (int bit = 0; bit < 4; ++bit)
            Power.push_back('0' + ((digit >> bit) & 1));
    }
    for (; !Power.empty() && Power.back() == '0'; Power.pop_back());
    reverse(Power.begin(), Power.end());
}

void Print() {
    assert(freopen("calcul.out", "w", stdout));
    int pow10 = 1;
    for (int value = Solution; value > 0; value /= 10, pow10 *= 10);
    if (pow10 == 1)
        pow10 = 10;
    for (; pow10 < Mod; pow10 *= 10)
        printf("0");
    printf("%d\n", Solution);
}

int main() {
    Read();
    Solve();
    Print();
    return 0;
}