Cod sursa(job #1435944)

Utilizator o_micBianca Costin o_mic Data 14 mai 2015 19:55:44
Problema Multiplu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#define DN 2000005
#define LL long long
using namespace std;

int viz[DN], pre[DN], val[DN];
vector <int> gr;

int main() {
  int a, b, x, l, tmp, ok = 0;
  ifstream fin("multiplu.in");
  ofstream fout("multiplu.out");
  fin >> a >> b;
  x = a*b;
  l = 0;
  gr.push_back(1 % x);
  viz[1] = 1;
  pre[1] = -1;
  val[1] = 1;
  while(!ok){
    vector <int> aux;
    for(int i = 0; i < gr.size(); ++i){
      tmp = (gr[i] * 10) % x;
      if(!viz[tmp]){
        pre[tmp] = gr[i];
        val[tmp] = 0;
        viz[tmp] = 1;
        if(tmp == 0){
          ok = 1;
          break;
        }
        aux.push_back(tmp);
      }

      tmp = (gr[i] * 10 + 1) % x;
      if(!viz[tmp]){
        pre[tmp] = gr[i];
        val[tmp] = 1;
        viz[tmp] = 1;
        if(tmp == 0){
          ok = 1;
          break;
        }
        aux.push_back(tmp);
      }
    }
    gr = aux;
  }
  x = 0;
  string res = "";
  res += val[0] + '0';
  while(pre[x] != -1){
    res += val[pre[x]] + '0';
    x = pre[x];
  }
  reverse(res.begin(), res.end());
  fout << res;
  return 0;
}