-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.cpp
94 lines (84 loc) · 2.38 KB
/
1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl;
#define vvprint(vv) for(auto v:vv)vprint(v);
ll msb(ll n){
ll count = 0;
while(n>0){
n >>= 1;
++count;
}
return max(count, 1ll);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(20);
ll T;
cin >> T;
rep(t, T){
ll X, Y;
cin >> X >> Y;
ll xsign = X<0? -1 : 1;
ll ysign = Y<0? -1 : 1;
X = abs(X);
Y = abs(Y);
ll xbits = msb(X), ybits = msb(Y);
ll bits = max(xbits, ybits);
bool found = false;
vll res;
for(ll tbits=bits; tbits<bits+2; ++tbits){
ll rx = X, ry = Y;
ll step = 1<<(tbits-1);
vll dirs;
while(step>0){
if(abs(rx)>abs(ry)){
if(rx<0){
dirs.push_back(0);
rx += step;
}
else{
dirs.push_back(2);
rx -= step;
}
}
else{
if(ry<0){
dirs.push_back(1);
ry += step;
}
else{
dirs.push_back(3);
ry -= step;
}
}
step >>= 1;
}
if(rx==0 && ry==0){
found = true;
if(res.size()==0 || res.size()>dirs.size()) res = dirs;
}
}
if(found){
cout << "Case #" << (t+1) << ": ";
irep(i, res.size()){
ll d = res[i];
if(d==0) cout << (xsign>0? 'W' : 'E');
else if(d==1) cout << (ysign>0? 'S' : 'N');
else if(d==2) cout << (xsign>0? 'E' : 'W');
else cout << (ysign>0? 'N' : 'S');
}
cout << endl;
}
else cout << "Case #" << (t+1) << ": " << "IMPOSSIBLE" << endl;
}
}