The link for the problem AP2 - AP - Complete The Series (Easy) is:

http://www.spoj.com/problems/AP2/

The logic used in this problem AP2 - AP - Complete The Series (Easy) is:

This problem is used AP formulas.We have A[3] and A[N-3] and Sum of Series (S) in the given input.But we don't know about N and we need to calculate it.

We know the sum of Series of AP: ((A[1]+A[N]/2)*N

Given A[3] which is equal toA[3]=A[1]+2*D

So A[1] = A[3] - 2*D

and A[N] is given by A[N] = A[N-2] + 2*DSo ((A[1]+A[N]/2)*N becomes :

S = ((A[3]-2*D+A[N-2]+2*D)/2)*N

In this, we can know the value of N by:

N=2*S/(A[3]+A[N-2])

For Finding D in these formulas we have to use the thing:

suppose consider the series for example:

3 6 9 12 15 18 21 24

Suppose we know A[3] 9 and A[N-2] 18 So to find the D for this we must use the formula:

A[N-2]-A[3]/(No of terms in between them +1)

The following is CPP solution for this "AP2 - AP - Complete The Series (Easy)" is:

#include<iostream> #include<cstdio> #define LL long long int using namespace std; int main() { int T; cin>>T; while(T--) { LL a3,an3,sum,d,n,a; cin>>a3>>an3>>sum; n = (2*sum)/(a3+an3); d = (an3-a3)/(n-5); a = a3-(2*d); cout<<n<<endl; for(int i=0;i<n;i++) cout<<(a+(i*d))<<" "; cout<<endl; } return 0; }

Happy Coding........

## No comments:

## Post a comment