进位

frog has n integers a1,a2,,an, and she wants to add them pairwise.
 
Unfortunately, frog is somehow afraid of carries (进位). She defines emph{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
1i<jnh(ai,aj)
.
 

Input

The input consists of multiple tests. For each test:
 
The first line contains 1 integer n (2n105). The second line contains n integers a1,a2,,an. (0ai109).
 

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2*1e5+5;
const int mod = 1000000007;
const double eps = 1e-7;
const double pi = 3.1415926;
const int e =  2.718281828;

int cont[15];
int arr[maxn];
int data[2][maxn];
int num[maxn];
void Init()
{
    cont[0] = 1;
    for(int i=1; i<10; i++)
        cont[i] = cont[i-1]*10;
}
int main()
{
    int m;
    Init();
    while(~scanf("%d",&m))
    {
        for(int i=0; i<m; i++)
            scanf("%d",&arr[i]);
        LL ans=0, ret=0;
        for(int i=1; i<10; i++)
        {
            int sum = 0;
            for(int j=0; j<m; j++)
            {
                data[0][j] = arr[j]%cont[i];
                data[1][j] = cont[i] -data[0][j];
                num[sum++] = data[0][j];
                if(data[0][j] >= data[1][j])
                    ans++;
            }
            sort(num, num+sum);
            for(int i=0; i<m; i++)
            {
                int temp = lower_bound(num, num+sum,data[1][i])-num;
                ret += sum - temp;
            }
        }
        cout<<(ret-ans)/2<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chen9510/p/4852072.html