青蛙兔子这题C++用int数据溢出
如题,换成long long就通过了,看题目说明是10^9,压根没往这块想,提交了很多次总是不通过,最后试了下long long,瞬间AC。话说咱们出题也严谨点,而且不通过的case能不能给个case的数据,一直在这憋着也太难受了。附代码:
#include <iostream> #include <math.h> using namespace std; typedef long long ll; void gcd(ll a, ll b, ll& g, ll &x, ll &y) { if (b == 0) { g = a; x = 1; y = 0; return; } gcd(b, a % b, g, y, x); y -= x * (a / b); } int main() { int cnt; cin >> cnt; while(cnt-- > 0) { ll a, b, n, l, r; cin >> a >> b >> n >> l >> r; ll g, x, y; gcd(a, b, g, x, y); if (n % g != 0) { cout << "NO" << endl; continue; } x *= n / g; y *= n / g; ll b_ = b / g; ll a_ = a / g; ll minK = ceil((l - x) * 1.0 / b_); ll maxK = floor(min((r - x) * 1.0 / b_, y * 1.0 / a_)); if (maxK < minK) { cout << "NO" << endl; continue; } cout << "YES" << endl; } return 0; }
主要思路是套用二元一次方程整数解模板,利用通解公式施加条件,进而确定k的范围。附模板链接:https://www.luogu.com.cn/problem/solution/P5656