SCU - 4437(思路)

这是一道关于计算加法过程中进位总数的问题。给定一个整数数组,需要找出所有可能的数对,计算它们相加时的进位次数,并累加这些进位次数。题解中提出了通过计算每个位上的进位次数来解决此问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 xx and 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

∑ 1 ≤ i &lt; j ≤ n h ( a i , a j ) ∑_{1≤i&lt;j≤n}h(ai,aj) 1i<jnh(ai,aj)

.

Input

The input consists of multiple tests. For each test:

The first line contains 1integer $ n, 2≤n≤10^5)$. The second line contains n n n integers a1,a2,…,ana1,a2,…,an. (​ 0 ≤ a i ≤ 1 0 9 0≤ai≤10^9 0ai109).

Output

For each test, write 1 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,然后下面的N个数, 统计任意两个数的进位之和。
例如h(35,82) = 1,h(1,99) = h(99,1) = 2;

思路:

​ 我们只要记录每个数字进个位的有多少个,进十位的有多少个,进百位的有多少个… 最后将统计结果相加即可。

假设我们现在求进百位的有多少个,那么就把所有数字全都取余100,假如这个数是164,算出大于等于100-64的数有多少个即可,其他进位的过程与百位相似。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll na[100010],tmp[100010];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    while(cin>>n)
    {
        for(ll i=0;i<n;++i) cin>>na[i];
        ll mod=1,ans=0;;
        for(ll k=1;k<=9;++k){
            mod*=10;
            for(ll i=0;i<n;++i) tmp[i]=na[i]%mod;
            sort(tmp,tmp+n);
            for(ll i=0;i<n;++i){
                ll th=lower_bound(tmp+i+1,tmp+n,mod-tmp[i])-tmp;
                ans+=n-th;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值