-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefixNum.cpp
47 lines (39 loc) · 1.02 KB
/
PrefixNum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<bits/stdc++.h>
using namespace std;
#define longnum 1000000000000000000 // 10^18
int main()
{
long long int T;
cin >> T;
while(T--)
{
long long int x , y;
cin >> x >> y;
long long int divider = 1; //Maintains the answer to our prob
long long int flag = 0; // Will Act as an indicator
long long int answer = -1; // Initializing our final answer
for(long long int i=x;i<=longnum;i=i*10)
{
if(flag!=0)break; // Breaking condition
else{
long long int same=i;
long long int begin = (same/y)*y; // Gives the lower bound
while(true)
{
if(begin!=0 && begin/divider == x && begin%y==0){
flag = 1;
answer = begin; // Here's the answer
break;
}
if(begin/divider!=x && begin>same){
break; // Look for next digit number
}
begin += y; //Moves to the next multiple of y for that i;
if(begin>longnum) break; //Condition for -1
}
}
divider = divider*10; // So that begin/divider less than 10 always
}
cout << answer << endl;
}
}