Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. int n, P;
  2. long long a;
  3. bool find(long long num) {
  4. if(num < P) return false;
  5. if(num == P || num == P + 1) return true;
  6. if(num % P != 0 && (num - 1) % P != 0) return false;
  7. return num % P == 0 ? find(num / P) : find((num - 1) / P);
  8. }
  9. int solve(long long num, int rem) {
  10. if(num < P) return 0;
  11. if(num == P && rem == 0) return 0;
  12. if(num == P && rem == 1) return 0;
  13. if(num == P + 1 && rem == 0) return 1;
  14. if(num == P + 1 && rem == 1) return 1;
  15. if(num == P + 2 && rem == 1) return 1;
  16. if(num == 2 * P + 1 && rem == 0) return 1;
  17. if(num == 2 * P + 1 && rem == 1) return 1;
  18. if(num == 2 * P && rem == 1) return 1;
  19. if(num == 2 * P + 2 && rem == 1) return 1;
  20. if(rem > 2) return 0;
  21. if((num - 1) % P == 0) {
  22. long long pp = (num - 1) / P;
  23. long long c = pp / 2;
  24. if(find(c) && ((c * P + c * P + 1 == num) || (((c + 1) * P + (c + 1) * P + 1) == num))) return 1;
  25. }
  26. if(num % P == 0) return find(num - 1) + solve(num / P, 0);
  27. if((num - 2) % P == 0) return find(num - 1) + solve((num - 2) / P, 0);
  28. if((num - 1) % P == 0) return find(num - 1) + 2 * solve((num - 1) / P, 1);
  29. return 0;
  30. }
  31.  
  32. int main() {
  33. scanf("%d %d", &n, &P);
  34. string ret = "";
  35. for(int i = 1; i <= n; ++i) {
  36. scanf("%lld", &a);
  37. int w = 0;
  38. if(a % 2 == 1 && find(a / 2) && find(a / 2 + 1)) w = 1;
  39. else w = solve(a, 0);
  40. if(w == 1) ret.push_back('1');
  41. else ret.push_back('0');
  42. }
  43. cout << ret << endl;
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement