c++ - Getting Wrong Answer in SPOJ AP3 (AP - Complete The Series)? -


i stucked in problem on spoj, here

i did maths it, , coded solution.

i right on ideone, spoj rejects answer "wa".

#include <iostream> #include <cmath> typedef long long ll;  using namespace std;  int main() {          ll t,x,y,sum,d,n,cd,a,i;          cin>>t;      while(t--)     {         cin >> x >> y >> sum;         d = 5*y + 7*x + 2*sum;          n = (d + sqrt(d*d - 48*sum*(x+y)))/(2*(x+y));          cd = ( y - x )/(n-6);          = x - 2 * cd;          cout<<n<<endl;         for(i=0;i<n;i++) {             cout<< + cd * i<<" ";         }         cout<<endl;     }     return 0; } 

input:

3

3 7 55

8 11 77

9 17 120

output:

10

1 2 3 4 5 6 7 8 9 10

7

2 5 8 11 14 17 20

8

1 5 9 13 17 21 25 29

where getting wrong. think problem might precision, not able check that. wrong data types or precision used or i/o optimization needed? input [ 5 1 25 ] valid? because gives output [ 7 6 5 4 3 2 1 0 -1 -2 ]

any appreciated.

hope not late..there precision loss have first take answer in long double , convert long long int type using llrint() function..i hadn't solved problem till because math calculations not yielding correct result..i used code check result , got ac..here code

#include <iostream> #include <cmath>  typedef long long int ll;  using namespace std;  int main() {          ll t,x,y,sum,i,a,tointeger,newdiff;         long double n,cd,d;          cin>>t;          while(t--) {             cin >> x >> y >> sum;             d = 5.0 * y + 7.0 * x + 2.0 * sum;              n = (d + sqrt(d * d - 48.0 * sum * (x + y))) / (2.0 * (x + y));             tointeger = llrint(n);              cd = (y - x) / (tointeger - 6.0);             newdiff = llrint(cd);             = x - 2 * newdiff;             cout<<tointeger<<endl;              for(i=0;i<tointeger;i++) {                 cout<< + newdiff * i<<" ";             }             cout<<endl;     }     return 0; } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -