I apologise in advance for a long post. I will include the essential part of multiple codes which I tried submitting for 785C once the contest got over. (In all my solutions I have defined int for long long beforehand so that's not an error) NOTE: If you haven't read the question, this is the link: QUESTION LINK
This is the code for the problem C which I submitted during the contest in GNU C++ 14 which got "Accepted".
int f(int x)
{
int d=8*x+1;
int x1=sqrt(d);
x1=(x1-1)/2;
if(x1*(x1+1)/2 == x)
return x1;
else
return x1+1;
}
int main()
{
int n,m;
cin>>n>>m;
if(n<=m)
{
cout<<n<<endl;
return 0;
}
int x=n-m;
cout<<m+f(x);
return 0;
}
Once the contest got over looking at the number of solutions which failed System Tests, I tried submitting different variations in different compiler versions. This is the same solution which doesn't use the function call on GNU C++14 which gets a wrong answer on test 16.
int main()
{
int n,m;
cin>>n>>m;
if(n<=m)
{
cout<<n<<endl;
return 0;
}
int x=n-m,ans;
int d=8*x+1;
int x1=sqrt(d);
x1=(x1-1)/2;
if((x1*(x1+1))/2 ==x)
ans= x1;
else
ans=x1+1;
cout<<m+ans;
return 0;
}
Now this is the solution in which I don't make any function call but gets "Accepted" on GNU C++.
int main()
{
int n,m;
cin>>n>>m;
if(n<=m)
{
cout<<n<<endl;
return 0;
}
int x=n-m,ans;
int d=8*x+1;
int x1=sqrt(d);
x1=(x1-1)/2;
if((x1*(x1+1))/2 >=x)
ans= x1;
else
ans=x1+1;
cout<<m+ans;
return 0;
}
Then again, the following code works on the right logic but gets a Wrong Answer on test 16 for using double instead of long long on C++.
int main()
{
ll n, m;
cin>>n>>m;
if(n <= m)
{
cout<<n<<endl;
}
else
{
ll x = n - m;
double y = sqrt(1+(8*x));
y-=1.0;
y/=2.0;
ll yd = ceil(y);
cout<<m+yd;
}
return 0;
}
The main problems are: 1. A code with a function call gets Accepted while the same logic without the function call and the instructions written inside main() function gets a wrong answer. 2. The same solution that I submit get different verdicts on GNU C++ and GNU C++14. 3. Using double in place of long long gives me wrong answer.
Thanks in advance to anyone who can help me with/explain these problems and sorry again for a long post.