A wierd difference between c++14 and c++17

In past codeforces contest I was submitting code in C++17 ans it is showing WA while when same code is submitted in C++14 it got accepted can anyone tell me why this is happened
C++17 Wa code
C++14 Accepted code

2 Likes

I see you are using ceil. Can you try once without it and let us know the results?

I think its not because of the c++ version but its about floating point errors that arises during divisions and sometimes causes wrong answer.

These errors arises generally when you are doing some calculations like
(a/b) > k or something like that to get rid of such errors avoid division.
for example if you want to check if
(a/b) <= k
Then change this as (a <= k*b)

Try changing all your division operations into inverse multiplication and try submitting it again… because i used C++17 with almost same approach what you have done and got AC. so most probably that’s the reason for WA.

1 Like

Yes, you’re right, I faced the same problem and then changed float to double

no it is not working https://codeforces.com/contest/1476/submission/106007865

Here is my integer solution of the problem.

https://codeforces.com/contest/1476/submission/105964509

1 Like

lemme check :slight_smile:

Your Code That got AC see what i edited

#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
#define Fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define fo(i,s,n) for(int i=s;i<n;i++)
#define mod 1000000007
void fun();
ll digit(ll num,ll d);
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        fun();
    }
    return 0;
}
void fun()
{
    ll n,k;
    cin>>n>>k;
    ll arr[n];
    fo(i,0,n)
    {
        cin>>arr[i];
    }
    ll price=arr[0];
    ll ans=0;
    fo(i,1,n)
    {
        if(arr[i]*100 > price*k)
        {
            ll a=ceil(((double)arr[i]*100)/k);
            ans+=abs(a-price);
            price+=abs(a-price);
            //cout<<a<<" "<<ans<<" "<<price<<endl;
        }
        price+=arr[i];
    }
    cout<<ans<<endl;
    return;
}

okay thank you but why it got accepted in c++14 same code according to me it should also show WA

  • It may be because handling of floating numbers in c++14 differs from c++17.
  • I really don’t have any deep knowledge on this but to know you can refer this documentation :

Changes between C++14 and C++17