题解:P13672 [GCPC 2023] German Conference for Public Counting

· · 题解

题解:P13672 [GCPC 2023] German Conference for Public Counting

这题有点偏思维。

思路

对于给定的数 n,不妨假设它有 k 位,他的最高位是 s

不难得出,0 \sim 9 都至少要 k - 1 个牌子,若 n > \underbrace{a \, a \, \cdots \, a}_{k \text{个}},则 a 需要 k 个牌子。以此模拟即可。

代码

#include<bits/stdc++.h>
using namespace std;
long long n, ans;
int wei(long long x){
    int cnt = 0;
    while(x){
        x /= 10;
        cnt++;
    }
    return cnt;
}
long long shu(int a, int b){
    long long cnt = 0;
    while(a--){
        cnt = cnt * 10 + b;
    }
    return cnt;
}
int main(){
    scanf("%lld", &n);
    if(n < 10){
        printf("%lld", n + 1);
        return 0;
    }
    int w = wei(n), w10 = pow(10, w - 1), shou = n / w10, ans = 0;
    if(n < shu(w, shou))shou--;
    ans = shou * w + (10 - shou) * (w - 1);
    printf("%d", ans);
    return 0;
}