fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define rep(i,a,b) for(int i=a;i<b;i++)
  5. #define rrep(i,b,a) for(int i=b;i>=a;i--)
  6. #define fori(i,a) for(auto i : a )
  7. #define all(a) begin(a), end(a)
  8. #define mset(a,b) memset(a,b,sizeof(a))
  9. #define mcopy(a,b) memcopy(a,b,sizeof(a))
  10. #define ll long long
  11. ll func(ll v){
  12. ll r = 0;
  13. while (v >>= 1) {
  14. r++;
  15. }
  16. return r;
  17. }
  18. void solve(){
  19. ll x,y;
  20. cin>>x>>y;
  21. int flag1=0,flag2=0;
  22. if(x<0) flag1=1;
  23. if(y<0) flag2=1;
  24. x=abs(x);
  25. y=abs(y);
  26. string ans="";
  27.  
  28. for(int i=0;i<64;i++){
  29. ll z=func(x),l=func(y);
  30. if(z<i && l<i) break;
  31. // if(x==0 && y==0) break;
  32. if(x<(1<<i) && (y<(1<<i))){
  33. cout<<"IMPOSSIBLE"<<endl;
  34. return ;
  35. }
  36.  
  37. if((x&(1<<i)== (y&(1<<i)))){
  38. cout<<"IMPOSSIBLE"<<endl;
  39. return ;
  40. }
  41. else if( (x&(1<<i))==0 && (y&(1<<i))==(1<<i) ){
  42.  
  43. if( (x &(1<<(i+1)))== (y & (1<<(i+1))) && z>i){
  44. y+=(1<<i);
  45. ans+="S";
  46. }
  47. else if(z>i){
  48. ans+="N";
  49. y-=(1<<i);
  50. }
  51. else if(z<=i){
  52. if((y & (1<<(i+1)))==0 && l>i){
  53. y+=(1<<i);
  54. ans+="S";
  55. }
  56. else{
  57. ans+="N";
  58. y-=(1<<i);
  59. }
  60. }
  61. }
  62. else if( (x&(1<<i))==(1<<i) && (y&(1<<i))==0 ){
  63. // cout<<1<<endl;
  64. if( (x&(1<<(i+1)))== (y&(1<<(i+1))) && l>i){
  65. x+=(1<<i);
  66. ans+="W";
  67. // cout<<1<<endl;
  68. }
  69. else if(l>i){
  70. x-=(1<<i);
  71. ans+="E";
  72. // cout<<2<<endl;
  73. }
  74. else if(l<=i){
  75. if((x&(1<<(i+1)))==0 && z>i){
  76. x+=(1<<i);
  77. ans+="W";
  78. // cout<<3<<endl;
  79. }
  80. else{
  81. x-=(1<<i);
  82. ans+="E";
  83. // cout<<4<<endl;
  84. }
  85. }
  86.  
  87. }
  88. // cout<<x<<" "<<y<<endl;
  89.  
  90. }
  91.  
  92. if(flag1){
  93. rep(i,0,ans.length()) {
  94. if(ans[i]=='S') ans[i]='N';
  95. else if(ans[i]=='N') ans[i]='S';
  96. }
  97. }
  98. if(flag2){
  99. rep(i,0,ans.length()) {
  100. if(ans[i]=='W') ans[i]='E';
  101. else if(ans[i]=='E') ans[i]='W';
  102. }
  103. }
  104. cout<<ans<<endl;
  105. }
  106.  
  107. int main(){
  108. ios_base::sync_with_stdio(false);
  109. cin.tie(0);
  110. cout.tie(0);
  111.  
  112. // freopen("input.txt", "r", stdin);
  113. // freopen("output.txt", "w", stdout);
  114.  
  115. int t,k=1;
  116. cin>>t;
  117. while(t--){
  118. cout<<"Case #"<<k++<<": ";
  119. solve();
  120. }
  121. }
  122.  
Success #stdin #stdout 0s 4284KB
stdin
4
2 3
-2 -3
3 0
-1 1
stdout
Case #1: SEN
Case #2: NWS
Case #3: EE
Case #4: IMPOSSIBLE