大数

1*A1+2*A2+......n*An  

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
const double clf =1e-8;
const double e= 2.718281828;
const double PI = 3.141592653589793;
const long int MAX=1e9;
#define ll long long
//const int mx=20000+1;
priority_queue<int > p;
priority_queue<int ,vector<int> , greater<int> > pq;
const int maxn=500;
const int maxs=5;
class bigNumber{
    private:
        int a[maxn];
        int len;
    public :
        bigNumber()
        {
            memset(a,0,sizeof(a));
            len=1;
        }
        int length()
        {
            return len;
        }
        int at(int k)
        {
            if(0<=k&&k<len) return a[k];
            return -1;
        }
        void setnum(char s[])
        {
            len=0;
            for(int i=strlen(s)-1;i>=0;i--)
                a[len++] = s[i] - '0';
        }
        bool isZero()
        {
            return len==1&&a[0]==0;
        }
        void add(bigNumber &x) //a <---a+x
        {
            for(int i=0;i<x.len;i++)
            {
                a[i]+=x.at(i);
                a[i+1]+=a[i]/10;
                a[i]%=10;
            }
            int k = x.len;
            while(a[k])
            {
                a[k+1]+=a[k]/10;a[k]%=10; k++;
            }
            len = len>k?len:k;
        }
        void  mul(bigNumber &x)//a<---a*x
        {
            if(x.isZero())
                setnum("0");
            int product[maxn];
            memset(product,0,sizeof(product));
            for(int i=0;i<len;i++)
                for(int j=0;j<x.length();j++)
                    product[i+j]+=a[i]*x.a[j];
            int k =0;
            while(k<len+x.length()-1)
            {
                product[k+1]+=product[k]/10;
                product[k++]%=10;
            }
            while(product[k])
            {
                product[k+1]+=product[k]/10;
                product[k++]%=10;
            }

            len =k;

            memcpy(a,product,sizeof(product));
        }

};
int main()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    int n;
    char s[maxs];
    while(~scanf("%d%s",&n,s))
    {
        bigNumber a,ap;
        a.setnum(s);
        ap.setnum("1");
        bigNumber sum;
        for(int i=1;i<=n;i++)
        {
            bigNumber num;
            sprintf(s,"%d",i);
            num.setnum(s);
            ap.mul(a);
            num.mul(ap);
            sum.add(num);
        }
        for(int i=sum.length()-1;i>=0;i--)
        {
            printf("%d",sum.at(i));
        }
        printf("\n");

    }
    return 0;
}

 

posted on 2017-02-08 16:53  acmtime  阅读(145)  评论(0编辑  收藏  举报

导航