码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-16 14:47:16      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:数学   算法   acm   

  其实这题只要想到这个结论就简单了。如果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;
}


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

标签:数学   算法   acm   

原文地址:http://blog.csdn.net/u012127882/article/details/46517099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!