You are not logged in. Please login at www.codechef.com to post your questions!

×

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).

asked 21 Apr '16, 11:55

chandubaba's gravatar image

2★chandubaba ♢
13311
accept rate: 0%

edited 20 May '16, 11:41

admin's gravatar image

0★admin ♦♦
19.8k350498541

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,852
×3,820
×1,220
×170
×19
×1

question asked: 21 Apr '16, 11:55

question was seen: 1,059 times

last updated: 20 May '16, 11:41