Codeforces Round 863 (Div. 3) E. Living Sequence 题解

发布时间 2023-04-12 10:46:27作者: 1v7w

题意

Codeforces Round 863 (Div. 3) E. Living Sequence
如果正整数中不能存在 \(4\),那么新生成的数中的第 \(k\) 个数为多少?

思路

\(4\) 不能够选,也就是每一位只能选择 \(0,1,2,3,5,6,7,8,9\) 。可以发现,这就是一个九进制。

当需要第 \(k\) 个数的时候,也就是答案的九进制转化为十进制后为 \(k\)

程序要做的就是把十进制的 \(k\) ,转化为九进制的答案,注意数字的取值范围是\(0-3,5-9\)

代码

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
using ll = long long;

void solve() {
    ll k; cin >> k;
    vector<ll> res;
    while(k) {
        res.push_back(k%9);
        k /= 9;
    }
    reverse(res.begin(), res.end());
    for(auto &x:res)
        if(x >= 4) x++;  // 注意不能选4
    for(auto x:res) cout << x;
    puts("");
}

int main() {
    int t; cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}