Cod sursa(job #484032)

Utilizator andra23Laura Draghici andra23 Data 11 septembrie 2010 17:13:18
Problema Multiplu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<fstream>
#include<iostream>
#define maxn 2000005

using namespace std;

int c[maxn], viz[maxn], tata[maxn], x, y;
ofstream g("multiplu.out");

void afisare(int nod){
    if (nod == 1)
        g<<1; 
    else {
        int t = tata[nod];
        int r = (t*(10%x))%x; 
        if (r == nod)
            g<<0;
        else 
            g<<1;
        afisare(t);    
    }  
}

int main(){
    ifstream f("multiplu.in");
    
    int a, b;
    f>>a>>b;
    x = a, y = b;
    
    while (x != y){
        if (x < y)
            x = x+a;
        else 
            y = y+b;    
    }
    
    int p = 1, u = 1;
    c[p] = 1;
    viz[1] = 1;
    int r1, r2;
    
    while (true){
        y = c[p];
        p++;
        r1 = (y*(10%x))%x; 
        r2 = (y*(10%x)+1)%x;   
        if (viz[r1] == 0){
            tata[r1] = y;
            viz[r1] = 1;
            u++;
            c[u] = r1;
        }   
        if (viz[r2] == 0){
            tata[r2] = y;
            viz[r2] = 1;
            u++;
            c[u] = r2;
        }
        if (r1 == 0 || r2 == 0)
            break;     
    }
    
    if (r1 == 0)
        afisare(r1);
    else    
        afisare(r2);
    
    return 0;
}