SCU 4437 Carries (2015四川省省赛B题)

题意:定义h(x,y)是x+y进位的次数,比如h(1,9)=1 , h(1,99)=2
现给定n个数,求B题截图

注意到0≤ai≤10^9这样只需要枚举在第10^k有多少个数产生进位就可以了。
注意到答案和顺序无关,只需要保证一对数不能计重就行,也就是说排序不影响结果。
那么枚举k,令s=10^k
每次对数组都mod s ,然后排序,现在已知a< s, b < s,现在已知a,求有多少个b使得a+b>=s


代码:

//author: CHC
//First Edit Time:    2015-06-18 23:00
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+5+1000;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int n;
LL A[MAXN],B[MAXN];
int main()
{
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            scanf("%lld",&A[i]);
        }
        sort(A,A+n);
        LL cnt=0;
        LL s=10;
        for(int k=1;k<=10;k++,s*=10){
            for(int i=0;i<n;i++) B[i]=A[i]%s;
            sort(B,B+n);
            for(int i=0;i<n;i++){
                LL t=s-B[i];
                //printf("s:%I64d t:%I64d bi:%I64d\n",s,t,B[i]);
                if(B[n-1]>=t){
                    int pos=lower_bound(B,B+n,t)-B;
                    //printf("pos:%d\n",pos);
                    if(pos>i)cnt+=n-pos;
                    else cnt+=n-(i+1);
                }
                //printf("cnt:%d\n",cnt);
            }
        }
        printf("%lld\n",cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值