Pagini recente » Cod sursa (job #1474603) | Cod sursa (job #2916723) | Cod sursa (job #55103) | Cod sursa (job #1705134) | Cod sursa (job #1705130)
#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;
}