ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1120. Sum of Sequential Numbers

Why is this WA (#2)?
Posted by mbrc 7 Mar 2013 20:36
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;
typedef long long LL;

int main()
{
    LL N,A,P,up;
    cin>>N;
    up=(LL)ceil((1.00+sqrt(1.00+8.00*(double)N))/2.00);
    P=up;
    while (((2*N)%P)!=0)
    {
        P--;
    }

    A=(((2*N)/P)-P+1)/2;

    cout<<A<<" "<<P<<endl;
}


This is my code...but this is giving WA on #2. I can't understand the reason. It seems to give correct answer on the tests I do on my computer..?!
Re: Why is this WA (#2)?
Posted by jpinero 7 May 2014 13:12
At first sight, I strongly suggest you to avoid operating repeteadly with floating points, the little errors will accummulate, and WA comes to the order.

at second sight the logic is just not right. Look, I got the same algorithm that a dude with TLE at test9, but with a less complex break condition.

private static int sq(long A,long N){
    double aux= Math.sqrt( ((A*A)<<2) - (A<<2) + 1 + 8*N);
    int r = (int)aux;
    if ((double)r==aux)
        return r;
    else
        return -1;
    }
    public static void main (String[]args){
        Scanner s=new Scanner(System.in);
        int N= s.nextInt();
        long A=1,P=1,root=0;
        while (true){
            root= sq(A,N);
            if (root==-1)
                A++;
            else{
                P= (1-(A<<1)+root)/2;
                break;
            }
        }
        System.out.println(A+" "+(P));
    }