ARC174D

· · 题解

怎么会有这么蠢的 ARC D。

赛时做法:打表看哪些数符合条件,发现对于 k\in \mathbb{N},满足以下两个之一的 x\in \mathbb{N} 符合条件:

枚举 k(代码中是 b=10^k),直接计算区间交即可,时间复杂度 O(T\log n)。

#include<bits/stdc++.h>
using namespace std;
int T;
typedef long long ll;
int main(){
    scanf("%d",&T);
    while(T--){
        ll n;
        scanf("%lld",&n);
        ll ans=1;
        for(ll b=10;b<=1000000000;b*=10){
            ll B=b*b;
            if(n>=B-b-b)ans++;
            if(n>=B-b)ans+=min(B+b-1,n)-B+b+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
    //quod erat demonstrandum
}

看起来很玄学,其实可以感性理解一下。首先题目中的性质比较稀有,大部分数都不满足这个条件,只有接近 100^k 的数才会满足。开头是 10^k 的对应 [100^k,100^k+10^k),开头是 10^k-1 的对应 [100^k-10^k,100^k),开头是 10^k-2 的对应 100^k-2\times10^k 一个。