-
Notifications
You must be signed in to change notification settings - Fork 0
/
codechef - SHKNUM - Sheokand and Number.cpp
107 lines (101 loc) · 1.92 KB
/
codechef - SHKNUM - Sheokand and Number.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
95
96
97
98
99
100
101
102
103
104
105
106
107
/// YET TO BE SOLVED
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz 32
vector<ll> table[sz];
vector<ll> row, column;
ll x, y;
ll test;
ll num, N;
ll rowPos, colPos;
ll ans;
bool found;
ll getRow(ll num)
{
ll i;
for(i=1; i<sz; i++)
{
if(table[i][1] == num)
{
return i;
}
else if(table[i][1] > num)
{
return i-1;
}
}
}
ll getCol(ll n, ll num)
{
found = false;
for(ll i=1; i<table[n].size(); i++)
{
if(table[n][i] == num)
{
found = true;
return i;
}
else if(table[n][i] > num)
{
found = false;
return i;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
row.push_back(0);
column.push_back(0);
for(ll x=0; x<sz-1; x++)
{
row.push_back(1<<x);
column.push_back(1<<x);
}
for(ll i=0; i<sz; i++)
{
for(ll j=0; j<sz; j++)
{
table[i].push_back(row[i]+column[j]);
}
}
cin >> test;
while(test--)
{
cin >> N;
if(N==1)
{
cout << "1\n";
continue;
}
rowPos = getRow(N);
colPos = getCol(rowPos, N);
// cout << "ROW: " << rowPos << "\tCOL: " << colPos << endl;
if(found)
{
if(rowPos == colPos)
{
ans = min(
fabs(N - table[rowPos][colPos-1]),
fabs(table[rowPos][colPos+1] - N)
);
}
else
{
ans = 0;
}
}
else
{
ans = min(
fabs(N - table[rowPos][colPos-1]),
fabs(table[rowPos][colPos] - N)
);
}
cout << ans << "\n";
}
return 0;
}