题意:定义h(x,y)是x+y进位的次数,比如h(1,9)=1 , h(1,99)=2
现给定n个数,求B题截图

注意到0≤ai≤109这样只需要枚举在第10k有多少个数产生进位就可以了。
注意到答案和顺序无关,只需要保证一对数不能计重就行,也就是说排序不影响结果。
那么枚举k,令s=10^k
每次对数组都mod s ,然后排序,现在已知a< s, b < s,现在已知a,求有多少个b使得a+b>=s


代码:

//author: CHC
//First Edit Time: 2015-06-18 23:00
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+5+1000;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int n;
LL A[MAXN],B[MAXN];
int main()
{
while(~scanf("%d",&n)){
for(int i=0;i<n;i++){
scanf("%lld",&A[i]);
}
sort(A,A+n);
LL cnt=0;
LL s=10;
for(int k=1;k<=10;k++,s*=10){
for(int i=0;i<n;i++) B[i]=A[i]%s;
sort(B,B+n);
for(int i=0;i<n;i++){
LL t=s-B[i];
//printf("s:%I64d t:%I64d bi:%I64d\n",s,t,B[i]);
if(B[n-1]>=t){
int pos=lower_bound(B,B+n,t)-B;
//printf("pos:%d\n",pos);
if(pos>i)cnt+=n-pos;
else cnt+=n-(i+1);
}
//printf("cnt:%d\n",cnt);
}
}
printf("%lld\n",cnt);
}
return 0;
}