A - Leading 1s
题意:设 \(f(x)\) 为 \(x\) 这个数的前导 1 的个数。给定一个数 \(n (1<=n<=10^{15})\)
code:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n;
cin >> n;
ll ans = 0;
for(int x = 1; x <= 16; ++x)
{
ll w = 0;
for(int j = 0; j < x; ++j)
w = w * 10 + 1;
ll p = w;
for(int y = x; y <= 16; ++y)
{
if(w <= n)
{
ans += min(n, p) - w + 1;
}
w *= 10;
p = 10 * p + 9;
}
}
cout << ans << endl;
return 0;
}