scu oj 4437: Carries (2015年四川省程序ACM设计竞赛B题目 )

来源:互联网 发布:115网盘淘宝怎么搜 编辑:程序博客网 时间:2024/04/29 08:04

  其实这题只要想到这个结论就简单了。如果2个数a,b的第k位相加要进位,那么必须满足(a%10^k+b%10^k)>=10^k  .有了这个结论就很简单了,枚举没一位就好了。

#include<stdio.h>#include<string.h>#include<iostream>#include<string>#include<queue>#include<cmath>#include<map>#include<algorithm>#include<vector>#include<bitset>using namespace std;const int mmax = 100010;typedef long long LL;int a[mmax];int b[mmax];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)            scanf("%d",&a[i]);        int tmp=1;        LL ans=0;        for(int i=0;i<9;i++)        {            tmp*=10;            for(int j=0;j<n;j++)                b[j]=a[j]%tmp;            sort(b,b+n);            int r=n;            for(int j=0;j<n;j++)            {                while(  r && b[j]+b[r-1]>=tmp)                    r--;                if(r<=j)                    ans+=(n-r-1);                else                    ans+=(n-r);            }        }        ans/=2;        printf("%lld\n",ans);    }    return 0;}


1 0