fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  6. const ll mod = 1e9+7;
  7.  
  8. int main(){
  9. fast;
  10. int t=1, g =1;
  11. // freopen("input.txt","r",stdin);
  12. // freopen("ouput.txt","w",stdout);
  13. cin>>t;
  14. while(t--){
  15. string s;
  16. ll x,y;
  17. cin>>x>>y;
  18. ll xx = abs(x), yy = abs(y);
  19. int f = 0;
  20.  
  21. for(ll i=0;f==0 && (1ll<<i)<=xx || (1ll<<i)<=yy;++i){
  22. // cout<<"in"<<endl;
  23. ll one = (1ll<<i);
  24. if (((one & xx) && (one & yy)) || (!(one & xx) && !(one & yy))){
  25. s = "IMPOSSIBLE";f = 1;break;
  26. // cout<<"XXXXX";
  27. }
  28.  
  29. bool min = 0, plus = 0;
  30. for(ll j = i+1;(1ll<<j)<=xx || (1ll<<j)<=yy;++j){
  31. ll oo = (1ll<<j);
  32. if (!(oo & xx) && !(oo & yy))
  33. continue;
  34.  
  35. else if ((oo & xx) && (oo & yy)){
  36. plus = 1;
  37. break;
  38. }
  39. else{
  40. min = 1;break;
  41. }
  42. }
  43. if(!(one & xx) && (one & yy)){
  44. if(plus) {yy += one, s+="S";
  45. // cout<<"plus y "<<i<<" "<<xx<<" "<<yy<<endl;
  46. }
  47. else {yy -= one, s+="N";
  48. // cout<<"min y "<<i<<" "<<xx<<" "<<yy<<endl;
  49. }
  50. }
  51. else if((one & xx) && !(one & yy)){
  52. if(plus) {xx += one, s+="W";
  53. // cout<<"plus x "<<i<<" "<<xx<<" "<<yy<<endl;
  54. }
  55. else {xx-=one, s+= "E";
  56. // cout<<"min x "<<i<<" "<<xx<<" "<<yy<<endl;
  57. }
  58. }
  59. // }
  60. }
  61.  
  62. cout<<"Case #"<<(g++)<<": ";
  63. if(s=="IMPOSSIBLE") {cout<<s<<endl;continue;}
  64. // cout<<"orig: "<<s<<endl;
  65. for(int i=0;i<s.size();++i){
  66. if(s[i]=='S' || s[i]=='N'){
  67. char c;
  68. if(y<0) c = (s[i]=='N'?'S':'N');
  69. else c = s[i];
  70. cout<<c;
  71. }
  72. else if(s[i]=='E' || s[i]=='W'){
  73. char c;
  74. if(x<0) c = (s[i]=='W'?'E':'W');
  75. else c = s[i];
  76. cout<<c;
  77. }
  78.  
  79. }
  80. cout<<endl;
  81.  
  82. }
  83.  
  84. return 0;
  85. }
  86.  
Success #stdin #stdout 0s 4416KB
stdin
80
-4 -4
-4 -3
-4 -2
-4 -1
-4 0
-4 1
-4 2
-4 3
-4 4
-3 -4
-3 -3
-3 -2
-3 -1
-3 0
-3 1
-3 2
-3 3
-3 4
-2 -4
-2 -3
-2 -2
-2 -1
-2 0
-2 1
-2 2
-2 3
-2 4
-1 -4
-1 -3
-1 -2
-1 -1
-1 0
-1 1
-1 2
-1 3
-1 4
0 -4
0 -3
0 -2
0 -1
0 1
0 2
0 3
0 4
1 -4
1 -3
1 -2
1 -1
1 0
1 1
1 2
1 3
1 4
2 -4
2 -3
2 -2
2 -1
2 0
2 1
2 2
2 3
2 4
3 -4
3 -3
3 -2
3 -1
3 0
3 1
3 2
3 3
3 4
4 -4
4 -3
4 -2
4 -1
4 0
4 1
4 2
4 3
4 4
stdout
Case #1: IMPOSSIBLE
Case #2: SSW
Case #3: IMPOSSIBLE
Case #4: IMPOSSIBLE
Case #5: IMPOSSIBLE
Case #6: IMPOSSIBLE
Case #7: IMPOSSIBLE
Case #8: NNW
Case #9: IMPOSSIBLE
Case #10: WWS
Case #11: IMPOSSIBLE
Case #12: ESW
Case #13: IMPOSSIBLE
Case #14: WW
Case #15: IMPOSSIBLE
Case #16: ENW
Case #17: IMPOSSIBLE
Case #18: WWN
Case #19: IMPOSSIBLE
Case #20: NWS
Case #21: IMPOSSIBLE
Case #22: SW
Case #23: IMPOSSIBLE
Case #24: NW
Case #25: IMPOSSIBLE
Case #26: SWN
Case #27: IMPOSSIBLE
Case #28: IMPOSSIBLE
Case #29: IMPOSSIBLE
Case #30: WS
Case #31: IMPOSSIBLE
Case #32: W
Case #33: IMPOSSIBLE
Case #34: WN
Case #35: IMPOSSIBLE
Case #36: IMPOSSIBLE
Case #37: IMPOSSIBLE
Case #38: SS
Case #39: IMPOSSIBLE
Case #40: S
Case #41: N
Case #42: IMPOSSIBLE
Case #43: NN
Case #44: IMPOSSIBLE
Case #45: IMPOSSIBLE
Case #46: IMPOSSIBLE
Case #47: ES
Case #48: IMPOSSIBLE
Case #49: E
Case #50: IMPOSSIBLE
Case #51: EN
Case #52: IMPOSSIBLE
Case #53: IMPOSSIBLE
Case #54: IMPOSSIBLE
Case #55: NES
Case #56: IMPOSSIBLE
Case #57: SE
Case #58: IMPOSSIBLE
Case #59: NE
Case #60: IMPOSSIBLE
Case #61: SEN
Case #62: IMPOSSIBLE
Case #63: EES
Case #64: IMPOSSIBLE
Case #65: WSE
Case #66: IMPOSSIBLE
Case #67: EE
Case #68: IMPOSSIBLE
Case #69: WNE
Case #70: IMPOSSIBLE
Case #71: EEN
Case #72: IMPOSSIBLE
Case #73: SSE
Case #74: IMPOSSIBLE
Case #75: IMPOSSIBLE
Case #76: IMPOSSIBLE
Case #77: IMPOSSIBLE
Case #78: IMPOSSIBLE
Case #79: NNE
Case #80: IMPOSSIBLE