Friday, 17 July 2015

AP3

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;
}

4 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
  3. For testcase:
    250000000000000006 625000000000000003 4500000000000000036
    your solution prints:
    6 125000000000000006 250000000000000006 375000000000000006 500000000000000006 625000000000000006 750000000000000006 875000000000000006 1000000000000000006

    but the correct answer should be -
    8 125000000000000007 250000000000000006 375000000000000005 500000000000000004 625000000000000003 750000000000000002 875000000000000001 1000000000000000000

    ReplyDelete
    Replies
    1. I see. Thanks for informing. I will test and update it.

      Delete