题意:定义 h(a,b)为在十进制下 a + b 与 a 的位数差,求
题解:观察式子可以发现每个数要往找到b,计算h(a, b)不能每个都算,位数变化很少,所以应该按位数变化分类,计算多少个数位数变化为t可以用数状数组或者线段树维护一下
#include <bits/stdc++.h> #define maxn 101000 #define INF 0x3f3f3f3f typedef long long ll; using namespace std; ll c[maxn], ans; int n, a[maxn], b[maxn]; void update(int x,int d){ for(int i=x;i<maxn;i+=i&(-i)) c[i] += d; } ll sum(int x){ ll ans = 0; for(int i=x;i>=1;i-=i&(-i)) ans += c[i]; return ans; } int bit(ll a){ if(a == 0) return 1; else return (int )log10(a)+1; } int main(){ scanf("%d", &n); for(int i=1;i<=n;i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b+1, b+1+n); int num = unique(b+1, b+1+n)-(b+1); for(int i=1;i<=n;i++) update(lower_bound(b+1, b+1+num, a[i])-b, 1); for(int i=1;i<=n;i++){ update(lower_bound(b+1, b+1+num, a[i])-b, -1); for(ll j=10;j<=1e8;j*=10){ if(j < a[i]) continue; int t1 = lower_bound(b+1, b+1+num, j*10-a[i])-b; t1--; int t2 = lower_bound(b+1, b+1+num, j-a[i])-b; t2--; ans += (sum(t1)-sum(t2))*(bit(j)-bit(a[i])); } } printf("%lld ", ans); return 0; }