Cod sursa(job #1705130)

Utilizator brczBereczki Norbert Cristian brcz Data 19 mai 2016 23:19:42
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <iostream>
#include<vector>
#include<fstream>
#include<queue>

using namespace std;

bool used[5000001];
queue<long long> numbers;
int main()
{
    ifstream fin("cifre4.in");
    ofstream fout("cifre4.out");

    int t;
    fin >> t;
    int n,p,x;
    for(int ti=0;ti<t;ti++){
        fin >> n >> p;

        for(int j=0;j<p;j++){
            used[j]=0;
        }
        while(!numbers.empty()){
            numbers.pop();
        }
        numbers.push(0);
        long long no;
        while(!numbers.empty()){
            x=numbers.front();
            numbers.pop();
            no=10*x+2;
            if(used[no%p] == false){
                numbers.push(no);
                used[no%p]=true;
                if( n == no%p){
                    break;
                }
            }
            no=10*x+3;
            if(used[no%p] == false){
                used[no%p]=true;
                numbers.push(no);
                if( n == no%p){
                    break;
                }
            }
            no=10*x+5;
            if(used[no%p] == false){
                used[no%p]=true;
                numbers.push(no);
                if( n == no%p){
                    break;
                }
            }
            no=10*x+7;
            if(used[no%p] == false){
                used[no%p]=true;
                numbers.push(no);
                if( n == no%p){
                    break;
                }
            }
        }
        if(!numbers.empty()){
            fout << no << endl;
        }
        else{
            fout << -1 << endl;
        }


    }


    return 0;
}