首页 > 代码库 > Lucky Number

Lucky Number

技术分享
 1 #include <bits/stdc++.h>
 2 #define _xx ios_base::sync_with_stdio(0);cin.tie(0);
 3 using namespace std;
 4 typedef long long ll;
 5 ll a, b;
 6 bool cmp(const string& s1, const string& s2)    //s1 <= s2 ? true : false
 7 {
 8     if(s1.size() == s2.size())
 9     {
10         for(int i = 0; i < s1.size(); i++)
11             if(s1[i] != s2[i]) return s1[i] < s2[i];
12         return true;
13     }
14     else s1.size() < s2.size();
15 }
16 bool isok(const string& s)
17 {
18     ll x = 0, y = 0;
19     for(int i = 0; i < s.size(); i++)
20     {
21         x = (x*10 + s[i] - 0)%a;
22         y = (y*10 + s[i] - 0)%b;
23     }
24     return x != 0 && y != 0;
25 }
26 void addone(string& s)
27 {
28     int i;
29     for(i = s.size() - 1; i >= 0; i--)
30         if(s[i] != 9) break;
31         else s[i] = 0;
32     s[i]++;
33 }
34 int main()
35 {_xx
36 //    freopen("in.txt", "r", stdin);
37 //    freopen("out.txt", "w", stdout);
38     string s1, s2, ans;
39     int t = 0;
40     while(cin >> a >> b >> s1 >> s2)
41     {
42         if(a == 1 || b == 1)
43         {
44             cout << -1 << endl;
45             continue;
46         }
47         s2.insert(0, "0");
48         while(s1.size() < s2.size()) s1.insert(0, "0");
49         ans = "-1";
50         while(cmp(s1, s2))
51         {
52             if(isok(s1))
53             {
54                 ans = s1;
55                 break;
56             }
57             else addone(s1);
58         }
59         ans.erase(0, ans.find_first_not_of("0"));
60         cout << ans << endl;
61     }
62     return 0;
63 }
View Code

 

Lucky Number