Friday, 17 July 2015

AP3

Standard

AP - Complete The Series v2

 Link to the question : AP3

HINT :

Again a simple question based on the arithmetic progression. The problem becomes tricky if not taken care of the precisions.

RECOMMENDED QUESTION :

Try your hands on this question .

SOURCE CODE :

#include<stdio.h>
#include<math.h>

int main()
{
    int t; scanf("%d",&t);
    while(t--)
    {
       long long int a3,a3l,s,i,a,d,len; 
          
      long double n,sq,diff;
      scanf("%lld",&a3);
      scanf("%lld",&a3l);
      scanf("%lld",&s);
     
      sq=sqrtl(((5.0*a3l+7.0*a3+2.0*s)*(5.0*a3l+7.0*a3+2.0*s))-(48.0*(a3l+a3)*s));
      n=((5.0*a3l+7.0*a3+2.0*s)+sq)/(2.0*(a3l+a3));
     
      len=llrintl(n);
      printf("%lld\n",len);
     
      diff=(a3l-a3)/(len-6.0);
      d=llrintl(diff);
     
      a=(a3-(2*d));
       for(i=0;i<len;i++)
       {
                       printf("%lld ",a+i*d);
       }
        printf("\n");
      
      
       }  
     
     return 0;
}

2 comments:

  1. sq=sqrtl(((5.0*a3l+7.0*a3+2.0*s)*(5.0*a3l+7.0*a3+2.0*s))-(48.0*(a3l+a3)*s));
    n=((5.0*a3l+7.0*a3+2.0*s)+sq)/(2.0*(a3l+a3));

    what happening in above two lines ?

    ReplyDelete
  2. You are provided with the third term, third last term and sum of the series. So if use exploit the formulas of sum of the AP which is, s = n/2(2a + (n-1)d) and finding the T nth term of the series, T = a + (n-1)d , you will get some quadratic equation through which you can find n or length of the series and also the common difference.

    So the above two lines represent those calculations.

    ReplyDelete