frog has n integers a1,a2,…,an, and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines hardness h(x,y) for adding x and y the number of carries involved in the calculation. For example, h(1,9)=1,h(1,99)=2.
Find the total hardness adding n integers pairwise. In another word, find
∑1≤i<j≤nh(ai,aj)
.
Input
The input consists of multiple tests. For each test:
The first line contains 1 integer n (2≤n≤105). The second line contains n integers a1,a2,…,an. (0≤ai≤109).
Output
For each test, write 1 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
思路:
从个位开始找一直到最大的位置,看相加大于等于10的位数平方,num就加上后面未加上的数有几个就加几。
代码:
#include<iostream>
#include <ostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
#define dd double
#define mes(x,y) memset(x,y,sizeof(y))
using namespace std;
int a[100010],b[100010];
int main(){
int n;
while(cin>>n){
for(int i=0;i<n;i++)cin>>a[i];
ll v=1,num=0;
for(int i=0;i<9;i++){
v*=10;
for(int j=0;j<n;j++)b[j]=a[j]%v;
sort(b,b+n);
int l=0;
for(int j=n-1;j>=0;j--){
for(;l<j;l++){
if(b[j]+b[l]>=v){
num+=j-l;break;
}
}
}
}
cout<<num<<endl;
}
}