【思维】SCU 4437 Carries

4437: Carries

Submit your solution     Discuss this problem     Best solutions

Carries

frog has  n n integers  a1,a2,,an 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 x and  y 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 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 1 integer  n n ( 2n105 2≤n≤105). The second line contains  n n integers  a1,a2,,an a1,a2,…,an. ( 0ai109 0≤ai≤109).

Output

For each test, write  1 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
 


题意:给定n个数的序列,问这个序列任意两个数相加需要进的位数的和是多少;

思路:

1.给定两个数若这两个数的第k位相加会进位,则满足:(a%10^k+b%10^k)>=10^k;

2.按位计算贡献(进位次数);

3.题目要求是每两个只选择一次,但排序后并不影响,并不会重复枚举两个数;

样例:

  97      56

  56      97

123    123  //更换顺序后不会影响统计贡献(进位次数);


代码:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn=100005;
int a[maxn],b[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);

        int mod=1;
        ll ans=0;
        for(int i=1;i<=9;i++)
        {
            mod*=10;
            for(int i=1;i<=n;i++)
                b[i]=a[i]%mod;
            sort(b+1,b+n+1);
            for(int i=1;i<n;i++)
                ans+=n-(lower_bound(b+i+1,b+n+1,mod-b[i])-b-1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值