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

1i<jnh(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 (2n1052≤n≤105). The second line contains nn integersa1,a2,,ana1,a2,…,an. (0ai1090≤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为头指针
两者相减就是这个元素与头元素之间有多少个元素。


 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 long long a[100010],b[100010];
 8 int main() {
 9     int n;
10     while(scanf("%d",&n)!=EOF){
11         for (int i=0 ;i<n ;i++){
12             scanf("%lld",&a[i]);
13         }
14         long long s=10,ans=0;
15         for (int k=0 ;k<10 ;k++ ,s=s*10){
16             for (int i=0 ;i<n ;i++ ){
17                 b[i]=a[i]%s;
18             }
19             sort(b,b+n);
20             long long temp;
21             for (int i=0 ;i<n ;i++){
22                 temp=s-b[i];
23                 if (b[n-1]>=temp){
24                     int cnt=lower_bound(b,b+n,temp)-b;
25                     if (cnt>i) ans+=n-cnt;
26                     else ans+=n-1-i;
27                 }
28             }
29         }
30         printf("%lld\n",ans);
31     }
32     return 0;
33 }

 



转载于:https://www.cnblogs.com/qldabiaoge/p/8516338.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值