Kaidul

B

Feb 20th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <map>
  8. #include <set>
  9. #include <list>
  10. #include <queue>
  11. #include <vector>
  12. #include <bitset>
  13. #include <complex>
  14. #include <cmath>
  15. #include <ctime>
  16. #include <cassert>
  17.  
  18. using namespace std;
  19.  
  20. #define pb push_back
  21. #define mp make_pair
  22. #define REP(i, n) for (int i = 0; i < (int)(n); i++)
  23. #define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
  24. typedef long long LL;
  25. typedef pair<int, int> PII;
  26.  
  27. int a[1000010], res[1000010];
  28.  
  29. void update(int n) {
  30.     for (int i = n-1; i >= 0; i--)
  31.         if (a[i] < res[i]) break;
  32.         else if (a[i] > res[i]) return;
  33.     memcpy(res, a, sizeof(a));
  34. }
  35.  
  36. int main() {
  37.     freopen("input.txt", "r", stdin);
  38.     //freopen("output.txt", "w", stdout);
  39.  
  40.     int n, m;
  41.     cin >> n >> m;
  42.     for (int i = 0; i < n; i++) res[i] = 10;
  43.     for (int dig = 1; dig <= 9; dig++) {
  44.         a[0] = dig;
  45.         int carry = 0;
  46.         for (int i = 0; i < n-1; i++) {
  47.             a[i + 1] = (a[i] * m + carry) % 10;
  48.             carry = (a[i] * m + carry) / 10;
  49.         }
  50.         if (a[n-1] == 0 or a[n-1]*m+carry != a[0] or a[n-1]*m+carry >= 10) continue;
  51.         update(n);
  52.     }
  53.     if (res[0] == 10) cout << "Impossible" << endl;
  54.     else {
  55.         for (int i = n-1; i >= 0; i--) cout << res[i];
  56.         cout << endl;
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment