SUMMATH - Editorial

PROBLEM LINK:

Practice
Contest

Author: Chandan Boruah
Tester: Chandan Boruah
Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Maths

PROBLEM:

You need to print sum of all the numbers that are divisible by 10 and are less than and equal to given number N.

QUICK EXPLANATION:

Divide N by 10, then use formula (x*(x+1)*10)/2 to find the sum.

EXPLANATION:

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
	long long n,sum=0;
	cin>>n;
	long long x=n/10;
	sum=x*(x+1)*5;
	cout<<sum<<endl;
    }
}

c# code:

using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		int n=Int32.Parse(Console.ReadLine());
		for(int t=0;t<n;t++)
		{
			long nn=Int32.Parse(Console.ReadLine());
			if(nn<10)Console.WriteLine(0);
			else 
			{
				nn/=10;
				Console.WriteLine(nn*(nn+1)*5);
			}
		}
	}
}

lets say you have to print sum of numbers less than equal to number 20, divide 20 by 10 gives you 2.
This is a pattern. Everytime you divide it by 10 you get count of numbers less than or equal to N that are divisible by 10. Now, x*(x+1)/2 is the sum of first x positive integers. Since, there are 2 such numbers and we have 10+20=30, we divided by 10 so we have 1+2=3. We multiply that result by 10 to get 30. This, solution comes naturally to mind, because our psychology is to think step by step.

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found above (c# code).
Tester’s solution can be found above (c# code).