Mai intai trebuie sa te autentifici.

Cod sursa(job #3278682)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 20 februarie 2025 15:38:46
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <queue>
#include <vector>
#include <iostream>

using namespace std;

const int M = 2e6;

int cmmdc(int a, int b)
{
    while (b != 0)
    {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int cmmmc(int a, int b)
{
    return a * b / cmmdc(a, b);
}

void refac_numarul(int x, vector <pair <int, int>> &pred, ofstream &out)
{
    if (x == 1)
    {
        out << 1;
        return;
    }
    refac_numarul(pred[x].first, pred, out);
    out << pred[x].second;
}

int main()
{
    ifstream in("multiplu.in");
    ofstream out("multiplu.out");
    int a, b;
    in >> a >> b;
    int m = cmmmc(a, b);
    vector < pair <int, int>> pred(m, {-1, -1});
    queue <int> q;
    q.push(1);
    pred[1].first = 0;
    pred[1].second = -1;
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        int y = x * 10 % m;
        if (pred[y].first == -1)
        {
            q.push(y);
            pred[y].first = x;
            pred[y].second = 0;
        }
        y = (x * 10 + 1) % m;
        if (pred[y].first == -1)
        {
            q.push(y);
            pred[y].first = x;
            pred[y].second = 1;
        }
    }
    refac_numarul(0, pred, out);
    in.close();
    out.close();
    return 0;
}