Solution of 10523 - Very Easy !!!

Problem Description
source:https://uva.onlinejudge.org/external/105/10523.html

Most of the times, the students of Computer Science & Engineering of BUET deal with bogus, tough and very complex formulae. That is why, sometimes, even for a easy problem they think very hard and make the problem much complex to solve. But, the team members of the team “BUET PESSIMISTIC” are the only exceptions. Just like the opposite manner, they treat every hard problem as easy and so cannot do well in any contest. Today, they try to solve a series but fail for treating it as hard. Let them help.


Input 

Just try to determine the answer for the following series ∑ N i=1 iAi You are given the value of integers N and A (1 ≤ N ≤ 150, 0 ≤ A ≤ 15).  

Output 

each line of the input, your correct program should output the integer value of the sum in separate lines for each pair of values of N and A

Sample Input 

3 3 4 4 

Sample Output 

102 1252

Solution

#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
void Multiply();
void MultiplyWithI(int value1);
void Addition();
int lena,lenb,lenResult,lenResult1,lenSum,result[1000],result1[1000],sum[1000],carry,temp,f;
char a[1000],b[1000],c[10];

int main()
{
    int n,i,j;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%s",&n,&b)==2)
    {
        lenSum=0;
        lena=1;
        lenb=strlen(b);
        a[0]='1';
        for(j=1;j<=n;j++)
        {
            Multiply();
            lena=strlen(a);
            MultiplyWithI(j);
            Addition();
        }
        if(strcmp(b,"0")==0)
            printf("0");
        else
        {
            for(i=f;i>=1;i--)
            {
                printf("%d",sum[i]);
            }
        }

        printf("\n");
        memset(a,'\0',sizeof(a));
        memset(b,'\0',sizeof(b));
        memset(c,'\0',sizeof(c));
        memset(result,0,sizeof(result));
        memset(result1,0,sizeof(result1));
        memset(sum,0,sizeof(sum));
    }
    return 0;
}

void Multiply()
{

    int i,j,k,m=0,l;
    k=0;
        for(i=lenb-1;i>=0;i--)
        {
            k+=1;
            l=k-1;
            carry=0;
            for(j=lena-1;j>=0;j--)
            {
                l+=1;
                if(i==lenb-1)
                    temp=(b[i]-48)*(a[j]-48)+carry;
                else
                    temp=(b[i]-48)*(a[j]-48)+result[l]+carry;
                result[l]=temp%10;
                carry=temp/10;
            }
            if(carry>0)
            {
                l+=1;
                result[l]=carry;
            }
        }
        lenResult=l;
        for(i=l;i>=1;i--)
        {
            a[m++]=result[i]+48;
        }
}

void MultiplyWithI(int value1)
{
    int i,j,k,m=0,x=0,lenc,l;
    k=0;
    while(value1!=0)
    {
        c[x++]=(value1%10)+48;
        value1/=10;
    }
    lenc=strlen(c);
        for(i=0;i<=lenc-1;i++)
        {
            k+=1;
            l=k-1;
            carry=0;
            for(j=1;j<=lenResult;j++)
            {
                l+=1;
                if(i==0)
                    temp=(c[i]-48)*result[j]+carry;
                else
                    temp=(c[i]-48)*result[j]+result1[l]+carry;
                result1[l]=temp%10;
                carry=temp/10;
            }
            if(carry>0)
            {
                l+=1;
                result1[l]=carry;
            }
        }
        lenResult1=l;
}
void Addition()
{
    int i,max1,tem,k=0;
    max1=lenSum;
    if(lenResult1>lenSum)
        max1=lenResult1;
    carry=0;
    for(i=1;i<=max1;i++)
    {
     tem=sum[i]+result1[i]+carry;
     k+=1;
     sum[k]=tem%10;
     carry=tem/10;
    }
    if(carry>0)
    {
        k+=1;
        sum[k]=carry;
    }
    lenSum=k;
    f=k;
    memset(result1,0,sizeof(result1));
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience