思路
枚举d1和d2的四个组合,由于代码存在重复性,所以可以写成一个函数进行检验。
题目不难重在理清思路
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vl = vector<ll>;
using vvl = vector< vector<ll> >;
using vvi = vector< vector<int> >;
using vvvi = vector<vector< vector<int> >>;
const ll mod = 1e9 + 7;
const int maxn = 1e7 + 5;
template <typename T1, typename T2> inline void chmin(T1& x, T2 y) { if (x > y) x = y; }
template <typename T1, typename T2> inline void swapmin(T1& x, T2& y) { if (x > y) swap(x, y); }
template <typename T1, typename T2> inline void chmax(T1& x, T2 y) { if (x < y) x = y; }
template <typename T1, typename T2> inline void swapmax(T1& x, T2& y) { if (x < y) swap(x, y); }
ll n, k;
bool check(ll d1, ll d2)
{
if(n % 3 != 0 || (k - d1 - d2) % 3 != 0)
return false;
ll s1, s2, s3, delta;
s2 = (k - d1 - d2) / 3;
s1 = s2 + d1;
s3 = s2 + d2;
if(s1 > k || s1 < 0 || s2 > k || s2 < 0 || s3 > k || s3 < 0)
return false;
swapmax(s1, s2);
swapmax(s2, s3);//此时s1 >= s2 >= s3
delta = s1 - s2 + s1 -s3;//要弥补差距的最小分数
if(n - k >= delta && (n-k - delta) % 3 == 0)
return true;
return false;
}
int main()
{
ll q = 1, d1, d2;
cin.tie(0);
ios::sync_with_stdio(0);
cin >> q;
while (q--)
{
cin >> n >> k >> d1 >> d2;
if(check(d1,d2) || check(-d1,-d2) || check(d1,-d2) || check(-d1,d2))
printf("yes\n");
else
printf("no\n");
}
} 


京公网安备 11010502036488号