Thursday, July 28, 2016

UVA 10220 - I Love Big Numbers




This is one of the Big integer problem . Better to solve using java as Java has an built in datatype Biginteger. that can contain any range of int variable . here i have use C++ so i have to use 2D  int  array to solve. process is as follows :

Just calculate 1000! (from 1 to 1000) by easy using array product.
Use vector if you can as you can determine the  size of vector whenever you want whereas for int array you can't determine size using any function.

An accepted solution  is given below:
( 1st try yourself )

#include<bits/stdc++.h>
using namespace std;

vector<long long int> v[1005];

void result()
{
    v[0].push_back(1);
    v[1].push_back(1);
    int carry=0;

    for(int i=2;i<1001;i++)
    {
        int t=v[i-1].size();

        long long int s;

        for(int j=0;j<t;j++)
        {
            s=v[i-1][j]*i+carry;

            if(s>=10)
            {
                v[i].push_back(s%10);
                carry=s/10;
            }
            else
            {
                v[i].push_back(s);
                carry=0;
            }

        }

        if(carry>0)
        {
            while(carry!=0)
            {
                v[i].push_back(carry%10);
                carry=carry/10;
            }
        }
    }

}

int main()
{
    result();

    int n;

    while(cin>>n)
    {

        long long int s=0;

        for(int i=0;i<v[n].size();i++)
        {
            s=s+v[n][i];
        }

       cout<<s<<endl;
    }

    return 0;
}

Best of luck.........:D




No comments:

Post a Comment