Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was solving arithmatic progression question SPOJ.com - Problem AP2[^]
Logic-> first term + last term = third term + third last
sum=n/2(first term+last term)
I tried many test cases it is passing all ?Can any one help me whats wrong with my code?
Its look like it may be some kind of overflow?

What I have tried:

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int third;
        long long int thirdlast;
        long long int sum;
        cin>>third>>thirdlast>>sum;
        long long int n=(2*sum)/(third+thirdlast);
        long long int d= (thirdlast-third)/(n-5);  //(2*thirdlast-third*n+3*third)/(5-n);
        long long int a=(third-2*d);
        //cout<<n<<endl;
        //cout<<d<<endl;
        //int d=0;
        for(int i=1;i<=n;i++){
            long long int temp=d*(i-1);
          //  int t=a+d;
            cout<<a+temp<<" ";


        }
        cout<<endl;

    }
}
Posted
Updated 9-Jan-19 23:30pm

1 solution

Your code does not meet the specifications laid down:
Quote:
Output
For each input of the test case, you need to print 2 lines.

First line should have 1 value - the number of terms in the series.

2nd line of the output should print the series numbers separated by single space.
Your's does not do that: it prints a single line of n items separated by spaces.

Unless it provides exactly the output requested, it will fail automatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900