Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Monday, September 2, 2013

SPOJ : Solution to Easy One For Cartman

Question :
Butters thinks he is really smart. So he gave gave three numbers to Cartman, A B and N such that A is the first term of an A.P. (Arithmetic Progression), B is the second term of that A.P. and N is the number of Terms in the A.P. Then he asked him to find the sum of all the elements in that A.P. Can you find the sum for him?

Inputs
First Line will contain "T" the number of Testcases. Each of the next T lines will contain three Integers A, B and N.

Outputs :
For each test case output the sum of all the elements of the A.P. in a separate line.

Constraints
1 <= N <=100000 0 <= Ai <= 10^9 1 <= Q <= 100000 0 <= L <= 10^9

Level : Medium

Solution to the Problem :


#include<stdio .h>
main()
{
      long long int t;
      scanf("%lld",&t);
      while(t--)
      {
                long long int n1,n2,n;
                scanf("%lld%lld%lld",&n1,&n2,&n);
                long long int d=n2-n1;
                long long int val=2*n1+(n-1)*d;
                val=val*n;
                val=val/2;
                printf("%lld\n",val);
      }







No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.