题解:UVA11180 Base i-1

· · 题解

UVA11180 Base i-1

题目大意

有一种介于二进制和 (i-1) 进制的进制:

每位只有 0,1,但是第 w 位位权为 (i-1)^w,还不让用负号,且据题目所述似乎这种进制可以表示所有复数。

现在告诉你测试 N 次,每次给你个复数 (ai+b) 的 a,b,求对应的 (i-1) 进制表示。

分析

所以手算出 0-8 次方的 a,b 得到如下数组:

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;
    }
}

如此得到了所有八位内的 (i-1) 进制复数及与其相互对应的“从形式上二进制转十进制”的数。

我们立即把这些八位内复数打印出来看看成分:

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。

进而得到第二个结论:

由第一个结论稍加分析,又得到第三个结论:

另一方面,由上述得,八位 (i-1) 进制只能表示 256 个数,但是这 256 个数在复数坐标系所分布的最小矩形区域范围内的,实部虚部都为整数的复数有 20\times20=400 个左右,于是得到:

这意味着我们要到更高的八位“借 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,
};

对于输入的 a,b,从低到高算出每一个八位 (i-1) 进制值,每次对它俩模 16 再自除 16,模出的一组余数 t1,t2 在 a 里找,o 的第一组增量为 0,所以直接遍历 o 九次,前两个数是 t1,t2 的增量,如果加上这个增量终于在 a 找到,那就给 a,b 加上 o 中这行的第三第四个数作为增量,停止遍历去算更高位,实现的代码如下:

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);
}