Carries SCU - 4437

Carries

frog has nn integers a1,a2,…,ana1,a2,…,an, and she wants to add them pairwise.

Unfortunately, frog is somehow afraid of carries (进位). She defines hardness h(x,y)h(x,y) for adding xxand yy the number of carries involved in the calculation. For example, h(1,9)=1,h(1,99)=2h(1,9)=1,h(1,99)=2.

Find the total hardness adding nn integers pairwise. In another word, find

∑1≤i<j≤nh(ai,aj)∑1≤i<j≤nh(ai,aj)

.

Input

The input consists of multiple tests. For each test:

The first line contains 11 integer nn (2≤n≤1052≤n≤105). The second line contains nn integersa1,a2,…,ana1,a2,…,an. (0≤ai≤1090≤ai≤109).

Output

For each test, write 11 integer which denotes the total hardness.

Sample Input

    2
5 5
10
0 1 2 3 4 5 6 7 8 9

Sample Output

    1
20
这题题目简单粗暴 ,给你一个长度为n的数组,求出这个数组中任意两个数相加进位的次数
这题是一个想法题,把一个数的每一位都分离出来,进行处理也就是取模运算
int cnt=lower_bound(b,b+n,temp)-b; lower_bound的返回值是一个元素的指针,b为头指针
两者相减就是这个元素与头元素之间有多少个元素。
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
long long a[],b[];
int main() {
int n;
while(scanf("%d",&n)!=EOF){
for (int i= ;i<n ;i++){
scanf("%lld",&a[i]);
}
long long s=,ans=;
for (int k= ;k< ;k++ ,s=s*){
for (int i= ;i<n ;i++ ){
b[i]=a[i]%s;
}
sort(b,b+n);
long long temp;
for (int i= ;i<n ;i++){
temp=s-b[i];
if (b[n-]>=temp){
int cnt=lower_bound(b,b+n,temp)-b;
if (cnt>i) ans+=n-cnt;
else ans+=n--i;
}
}
}
printf("%lld\n",ans);
}
return ;
}
上一篇:ProxySQL源码分析4-线程分析


下一篇:Oracle删除数据恢复方法