The C++ operation changes the answer.

Asked 1 years ago, Updated 1 years ago, 317 views

Ask about the Panasonic 2020 contest C problem.
The AC codes are as follows:

#include<bits/stdc++.h>

using namespace std;
typeedef long long ll;
typeef pair <int,int>P;

// Macro
# define REP(i,n) for (li = 0; i<(ll)(n); i++)
# define REPD(i,n) for (li=(ll)(n)-1;i>=0;i--)
# define FOR(i, a, b) for(li=(a); i<=(b); i++)
#define FORD(i,a,b)for(li=(a);i>=(b);i--)
# I want to omit arguments such as define ALL(x)(x).begin(), (x).end() // sort
#define SIZE(x)(ll)(x).size()) // Change size from size_t to ll
# define MAX(x)*max_element(ALL(x))
#define INF 1000000000000//10^12
#define MOD 10000007//10^9+7
#define PB push_back
#define MP make_pair
#define First
# define second

int main(intargc, char const*argv[]){

  lla,b,c;cin>>a>b>c;
  ll right = 4*a*b, left, tmp = c-a-b;
  left = power(tmp,2);
  /* cout<<right<"<<left<<<endl;*/
  if (4*a*b<tmp*tmp>&tmp>0) cout<<"Yes"<<endl;
  else cout <<"No" <<endl;
  return 0;
}

The following code was WA for unknown reasons:

#include<bits/stdc++.h>

using namespace std;
typeedef long long ll;
typeef pair <int,int>P;

// Macro
# define REP(i,n) for (li = 0; i<(ll)(n); i++)
# define REPD(i,n) for (li=(ll)(n)-1;i>=0;i--)
# define FOR(i, a, b) for(li=(a); i<=(b); i++)
#define FORD(i,a,b)for(li=(a);i>=(b);i--)
# I want to omit arguments such as define ALL(x)(x).begin(), (x).end() // sort
#define SIZE(x)(ll)(x).size()) // Change size from size_t to ll
# define MAX(x)*max_element(ALL(x))
#define INF 1000000000000//10^12
#define MOD 10000007//10^9+7
#define PB push_back
#define MP make_pair
#define First
# define second

int main(intargc, char const*argv[]){

  lla,b,c;cin>>a>b>c;
  ll right = 4*a*b, left, tmp = c-a-b;
  left = power(tmp,2);
  /* cout<<right<"<<left<<<endl;*/
  if(right<left&&tmp>0)cout<<"Yes"<endl;
  else cout <<"No" <<endl;
  return 0;
}

I also feel that there is only a difference between adding the progress of the calculation to the variable or just including it in the if statement, but for some reason I end up with WA.

If you know the cause, please enjoy it.

c++

2022-09-30 21:49

2 Answers

std::power() returns the value of double as a return value for the argument. I think it's an error because long has almost 19 valid digits, while double has just over 15 valid digits.

#include<cmath>
# include <iostream>

int main() {
    long long tmp = 123456789;
    long long left = std::power(tmp,2);

    std::cout<<tmp*tmp<<'\n';
    std::cout<<left<<'\n';

}

The results are

15241578750190521
15241578750190520

You can see that the last digit is different.


2022-09-30 21:49

I checked and found that the power() function does not have an overload that accepts the long long type as an argument.
Because it is converted to double type and so on, if the argument is very large (approximately more than 16 digits), an error occurs.


2022-09-30 21:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.