2015 Sichuan Province Contest (Carries)

Carries

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main

Type:
None
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

Source

2015 Sichuan Province Contest

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
const double PI = acos(-1.0);
using namespace std;
#define esp  1e-8
const int inf = 99999999;
const int mod = 1000000007;
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中
int a[100005];
int main()
{
	int n, i, j;
	while (~scanf("%d", &n))
	{
		long long s = 0;
		for (i = 1; i <= n; ++i)
			scanf("%d", &a[i]);
		
		for (i = 1e9; i >= 10; i /= 10)//从高位开始每一位位判断是否会进位
		{
			for (j = 1; j <= n; ++j)
			{
				a[j] %= i;//提取当前位以下的值
			}
			sort(a + 1, a + 1 + n);
			int l = 1, r = n;
			while (l < r)//二分查找
			{
				if(a[l] + a[r] >= i && l < r)
				{
					s += r - l;
					r --;
				}
				while (a[l] + a[r] < i && l < r)
				{
					l++;
				}
			}
		}
		printf("%lld\n", s);
	}
}





你可能感兴趣的:(2015 Sichuan Province Contest (Carries))