传送门:P10510 进制

更佳的阅读体验:洛谷 P10510 题解


简要题意:给定一个十进制正整数,将其转为三进制串后进行若干次操作,每次操作后输出当前三进制串对应的十进制数。

我们将给定十进制数 $V$ 转为三进制串,并用一个数组存储每一位的数,然后按题意执行每次操作即可。

每次操作结束后累加,将三进制串转为十进制数输出即可。

#include <iostream>
using namespace std;
using ll = long long;

const int N = 40;
ll v, ans, dig;
int q, op, x, a[N], cnt;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> v >> q;
    while (v) a[cnt++] = v % 3, v /= 3;
    while (q--) {
        ans = 0, dig = 1;
        cin >> op >> x;
        cnt = max(cnt, x + 1);
        if (op == 1) a[x] = (a[x] + 1) % 3;
        else if (op == 2) a[x] = (a[x] + 2) % 3;
        else a[x] = (3 - a[x]) % 3;
        for (int i = 0; i < cnt; ++i) ans += dig * a[i], dig *= 3;
        cout << ans << '\n';
    } return 0;
}
最后修改:2024 年 05 月 19 日
如果觉得我的文章对你有用,请随意赞赏