Cod sursa(job #2786347)

Utilizator pielevladutPiele Vladut Stefan pielevladut Data 20 octombrie 2021 19:11:12
Problema Multiplu Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.53 kb
#include <bits/stdc++.h>

using namespace std;

ofstream fout("multiplu.out");

class InParser
{
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch()
    {
        ++sp;
        if (sp == 4096)
        {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume)
    {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n)
    {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n)
    {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

int a, b;
queue<int> coada;
struct elem
{
    int unu, zero;
};
elem w[2002002];
int prv[2002002];

void sol(int val)
{
    if(val == 1)
    {
        fout << val;
        return ;
    }
    sol(prv[val]);
    if(w[val].unu != w[prv[val]].unu)
        fout << 1;
    if(w[val].zero != w[prv[val]].zero)
        fout << 0;
}

int main()
{
    InParser fin("multiplu.in");
    fin >> a >> b;
    int lcm = a * b / __gcd(a, b);
    w[1].unu = 1;
    w[1].zero = 0;
    coada.push(1);
    while(!coada.empty())
    {
        int nod = coada.front();
        coada.pop();
        int newnod = nod * 10;
        int newrest = newnod % lcm;
        if(w[newrest].unu == 0 && w[newrest].zero == 0)
        {
            coada.push(newrest);
            w[newrest] = w[nod];
            w[newrest].zero ++;
            prv[newrest] = nod;
        }
        newnod = nod * 10 + 1;
        newrest = newnod % lcm;
        if(w[newrest].unu == 0 && w[newrest].zero == 0)
        {
            coada.push(newrest);
            w[newrest] = w[nod];
            w[newrest].unu ++;
            prv[newrest] = nod;
        }
    }
    sol(0);
    return 0;
}