fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define fi first
  5. #define se second
  6. const int MAXN = 1e4 + 7;
  7. bool mark[MAXN];
  8. ll ds[] = {6, 8}, p, t;
  9. ll mul(ll x, ll y){
  10. return (x % p * y % p) % p;
  11. }
  12.  
  13. ll add(ll x, ll y){
  14. return (x % p + y % p) % p;
  15. }
  16.  
  17. void process(){
  18. queue <pair<ll, string>> q;
  19. q.push({6 % p, "6"});
  20. q.push({8 % p, "8"});
  21.  
  22. while(!q.empty()){
  23. ll u = q.front().fi;
  24. string las = q.front().se;
  25. q.pop();
  26. if(las.size() > 200) return void(cout << -1<< endl);
  27. if(u == 0) return void(cout << las << endl);
  28.  
  29. for(int i = 0; i < 2; i++){
  30. ll v = add(mul(u, 10), ds[i]);
  31. string num = las;
  32. num += (ds[i] + 48);
  33. if(!mark[v]){
  34. q.push({v, num});
  35. mark[v] = true;
  36. }
  37. }
  38. }
  39. cout << -1 << endl;
  40. }
  41.  
  42. int main(){
  43. ios_base::sync_with_stdio(0);
  44. cout.tie(0);
  45. cin.tie(0);
  46. cin >> t;
  47. while(t--){
  48. cin >> p;
  49. process();
  50. memset(mark, false, sizeof(mark));
  51. }
  52. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty