CDOUT2-Editorial

PROBLEM LINK
Practice
Contest

Author: Md Majid Jahangir

Tester : Md Majid Jahangir

Editorialist: Md Majid Jahangir

DIFFICULTY:

Cakewalk

PREREQUISITES:

Basics

PROBLEM:

Sourabh is a very good student, but he does a very weird mistake in multiplication. If he has to multiply two same single digits like 9x9 or 2x2, instead of multiplying the numbers like he is supposed to, he adds them. His teacher gives him two numbers to N and K and tells him to multiply the two numbers and Sourabh does the same mistake again. He then teaches Sourabh the correct process of multiplying and tells him to find the correct answer when N and K is multiplied. He also tells him to find the sum of the correct and wrong answer of the multiplication. As wants to go out and play, can you please help him to find the answer?

EXPLANATION:

This problem can easily be solved by Kid-Multiplication. Take the number N and store them in an array(Character or Integer). Starting from the right of the array, take individual digit in the ith position and multiply with K. If there is a previous carry, add it with it. Put the one's digit of the generated number and place it in the ith position of another array A[]. The ten's digit is stored as carry to be used in the (i-1)th digit. If the number is a single digit number, carry is 0. This process will give the correct answer of the multiplication. Repeat the above process again, except when ith digit is equal to K, instead of multiplying, add the ith digit and K. Store the answer generated number in another array B[]. Do a Kid addition of the two arrays A[] and B[], which is the answer

C++ Code:

#include<cstdio>
#include<cstring>

using namespace std;

void test();

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		test();
	}
	return 0;
}

void test()
{
	char num[20];
	long long num_act[20]={0},num_err[20]={0},s1;
	long long int num1=0,num2=0;
	int n,i,j=19,temp,carry=0;
	scanf("%s",num);
	scanf("%d",&n);
	for(i=strlen(num)-1 ; i>=0 ; i--)
	{	
		temp=(num[i]-48)*n;
		temp+=carry;
		if(i!=0)
		{
			carry=temp/10;
			num_act[j--]=temp%10;
		}
		else
			num_act[j]=temp;
	}
	s1=j;
	j=19;
	carry=0;
	for(i=strlen(num)-1 ; i>=0 ; i--)
	{	
		if(n==num[i]-48)
			temp=2*n;
		else
			temp=(num[i]-48)*n;
		temp+=carry;
		if(i!=0)
		{
			carry=temp/10;
			num_err[j--]=temp%10;
		}
		else
			num_err[j]=temp;
	}
	for(i=j ; i<20 ; i++)
		num2=(num2*10)+num_err[i];
	for(i=s1 ; i<20 ; i++)
		num1=(num1*10)+num_act[i];
	printf("%lld\n",num2+num1);
}