题解:UVA11180 Base i-1
UVA11180 Base i-1
题目大意
有一种介于二进制和
每位只有
现在告诉你测试
分析
所以手算出
typedef array<ll,2>a2;
const a2 c[9]={
{1,0},
{-1,1},
{0,-2},
{2,2},
{-4,0},
{4,-4},
{0,8},
{-8,-8},
{16,0},
};
再打个表先~
map<a2,int>a; //复数->(i-1)进制01数从形式上转十进制
a2 b[N]; //反之
inline ll lowbit(ll x){return x&(-x);}//计算二进制最后一个1的权
void dfst() {
b[0] = { 0,0 };
for (int i = 1; i < 1 << 8; i++) {
b[i] = b[i - lowbit(i)] + c[__lg(lowbit(i))];
a[b[i]] = i;
}
}
如此得到了所有八位内的
我们立即把这些八位内复数打印出来看看成分:
dfst();
int cnt=0;
sort(b,b+N);
for(auto x:b){
cout << cnt++ << " " << x[0] <<" "<< x[1] << endl;
}
于是看到这些复数:
0 -13 -9//序号 a b
1 -13 -7
2 -13 -1
3 -13 1
4 -12 -10
5 -12 -9
6 -12 -8
7 -12 -7
8 -12 -2
9 -12 -1
...
251 6 -4
252 6 -3
253 6 -2
254 6 -1
255 6 4
256 6 5
257 6 6
258 6 7
259 7 -4
260 7 -2
261 7 4
262 7 6
//最后不是255是因为数组开大了,多了好几组0。
进而得到第二个结论:
- 八位内能被表示的复数的实部和虚部范围是
(-16,16) ;
由第一个结论稍加分析,又得到第三个结论:
- 复数在用
(i-1) 进制表示时只有第[8k+7,8k] 位及更高位有1 ,其他位为0 的必要条件是实部和虚部都可被16^k 整除;
另一方面,由上述得,八位
- 存在一些复数,在
(-16^k,16^k) 之间,但是却需要更高的位即第[8k+7,8k] 位有1 才能表示;
这意味着我们要到更高的八位“借
那么怎么借?再打个表:
const int o[9][4] = {
0,0,0,0,
16,0,-1,0,
-16,0,1,0,
0,16,0,-1,
16,16,-1,-1,
-16,16,1,-1,
0,-16,0,1,
16,-16,-1,1,
-16,-16,1,1,
};
对于输入的
if ((x || y) == 0) {//x=a,y=b
cout << 0 << endl; continue;
}
vector<int> ans;
int cnt = 0, t1, t2;
while ((x || y) != 0) {
t1 = x % 16;
t2 = y % 16;
cnt++;
x /= 16; y /= 16;
if ((t1 || t2) == 0);
else {
for (int k = 0; k < 9; k++) {
if (a[{ t1 + o[k][0], t2 + o[k][1] }]) {
t1 += o[k][0];
t2 += o[k][1];
x += o[k][2];
y += o[k][3];
break;
}
}
}
ans.push_back(a[{t1, t2}]);
}
while (cnt > 1 && ans[cnt - 1] == 0)ans.pop_back(), cnt--;
完整代码
#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 8) + 7;
typedef long long ll;
typedef array<ll, 2> a2;
a2 operator+ (const a2& x, const a2& y) {
return{ x[0] + y[0],x[1] + y[1] };
}
inline ll lowbit(ll x) { return x & (-x);}//计算二进制最后一个1的权
const a2 c[9] = {
{1,0},
{-1,1},
{0,-2},
{2,2},
{-4,0},
{4,-4},
{0,8},
{-8,-8},
};
map<a2,int>a;//复数->(i-1)进制01数从形式上转十进制
a2 b[N];//反之
void dfst() {
b[0] = { 0,0 };
for (int i = 1; i < 1 << 8; i++) {
b[i] = b[i - lowbit(i)] + c[__lg(lowbit(i))];
a[b[i]] = i;
}
}
const int o[9][4] = {
0,0,0,0,
16,0,-1,0,
-16,0,1,0,
0,16,0,-1,
16,16,-1,-1,
-16,16,1,-1,
0,-16,0,1,
16,-16,-1,1,
-16,-16,1,1,
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
dfst();
int x, y;
int i = 0;
for (int i = 1; i <= t; i++) {
cin >> x >> y;
cout << "Case #" << i << ": ";
if ((x || y) == 0) {
cout << 0 << endl; continue;
}
vector<int> ans;
int cnt = 0, t1, t2;
while ((x || y) != 0) {
t1 = x % 16;
t2 = y % 16;
cnt++;
x /= 16; y /= 16;
if ((t1 || t2) == 0);
else {
for (int k = 0; k < 9; k++) {
if (a[{ t1 + o[k][0], t2 + o[k][1] }]) {
t1 += o[k][0];
t2 += o[k][1];
x += o[k][2];
y += o[k][3];
break;
}
}
}
ans.push_back(a[{t1, t2}]);
}
while (cnt > 1 && ans[cnt - 1] == 0)ans.pop_back(), cnt--;
int bb = __lg(ans[cnt - 1]);
for (int i = 1 << bb; i; i >>= 1) {
cout << (i & ans[cnt - 1] ? 1 : 0);
}
for (int i = cnt - 2; i >= 0; i--) {
cout << bitset<8 >(ans[i]);
}
cout << endl;
}
return ~~(0 ^ 0);
}