Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/tree_policy.hpp>
  7.  
  8. using namespace std;
  9. template <class T>
  10. using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
  11. template <class T>
  12. using ordered_multiset = __gnu_pbds::tree<T, __gnu_pbds::null_type, less_equal<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
  13.  
  14. #define PI atan2(0, -1)
  15. #define epsilon 1e-9
  16. #define INF 5e18
  17. #define MOD 1000000007
  18.  
  19. #define mp make_pair
  20. #define pb push_back
  21. #define f first
  22. #define s second
  23. #define lb lower_bound
  24. #define ub upper_bound
  25.  
  26. int kase, N;
  27. // Priority Queue State: ( ( Number of 1 Bits , Number of Total Bits ) , ( Current Residue , Previous Residue ) )
  28. priority_queue<pair<pair<int, int>, pair<int, int>>, vector<pair<pair<int, int>, pair<int, int>>>,
  29. greater<pair<pair<int, int>, pair<int, int>>>> pq;
  30. pair<int, int> dist [100010];
  31. vector<int> forAdj [100010], backAdj [100010];
  32. bool backReachable [100010];
  33.  
  34. int main(){
  35. //freopen("sort.in", "r", stdin); freopen("sort.out", "w", stdout);
  36. ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10);
  37. cin >> kase;
  38. for(int kk = 1; kk <= kase; kk++){
  39. cin >> N;
  40. // Edge case where N = 1
  41. if(N == 1){
  42. cout << "0\n";
  43. continue;
  44. }
  45. // Do the Dijkstra and Make the DAGs
  46. for(int i = 0; i < N; i++){
  47. dist[i] = {MOD, MOD};
  48. forAdj[i] = vector<int>(); backAdj[i] = vector<int>();
  49. backReachable[i] = false;
  50. }
  51. pq = priority_queue<pair<pair<int, int>, pair<int, int>>, vector<pair<pair<int, int>, pair<int, int>>>,
  52. greater<pair<pair<int, int>, pair<int, int>>>>();
  53. pq.push({{1, 0}, {1, 1}});
  54. while(pq.size() > 0){
  55. pair<pair<int, int>, pair<int, int>> state = pq.top(); pq.pop();
  56. int curr = state.s.f, par = state.s.s;
  57. if(dist[curr] < state.f) continue;
  58. forAdj[par].pb(curr);
  59. backAdj[curr].pb(par);
  60. if(dist[curr] == state.f) continue;
  61. else{
  62. dist[curr] = state.f;
  63. assert(backAdj[curr].size() == 1);
  64. }
  65. if(dist[(2*curr)%N] > mp(state.f.f, state.f.s+1)) pq.push({{state.f.f, state.f.s+1} ,{(2*curr)%N, curr}});
  66. if(dist[(2*curr+1)%N] > mp(state.f.f+1, state.f.s+1)) pq.push({{state.f.f+1, state.f.s+1}, {(2*curr+1)%N, curr}});
  67. }
  68. // Check if a path that leads to 0 exists from every node in the forward adjacency graph
  69. queue<int> q; backReachable[0] = true; q.push(0);
  70. while(q.size() > 0){
  71. int curr = q.front(); q.pop();
  72. for(int nexty : backAdj[curr])
  73. if(!backReachable[nexty]){
  74. backReachable[nexty] = true;
  75. q.push(nexty);
  76. }
  77. }
  78. // Greedily reconstruct the best solution
  79. vector<int> ret; ret.pb(dist[0].s);
  80. for(int bitPos = dist[0].s-1, curr = 1; bitPos > -1; bitPos--){
  81. if(find(forAdj[curr].begin(), forAdj[curr].end(), (curr*2)%N) != forAdj[curr].end() && backReachable[(curr*2)%N]){
  82. curr = (curr*2)%N;
  83. }
  84. else{
  85. curr = (curr*2+1)%N;
  86. ret.pb(bitPos);
  87. }
  88. }
  89. for(int i = ret.size()-1; i > -1; i--) cout << ret[i] << (i ? ' ' : '\n');
  90. }
  91. return 0;
  92. }
  93.  
  94. /******************************
  95. Kateba ii dake no hanashi darou
  96. Success is only a victory away
  97. - No Game No Life Opening
  98. ******************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement