Try   HackMD

【CSES】1754. Coin Piles

題目連結

時間複雜度

  • O(1)

解題想法

這題的想法是先看目前的兩堆相加是否可以被 3 整除,除果可以的話就可以輸出 YES

但要記得保證

a<b
a2×b
上面的解法才會成立

完整程式

/* Question : CSES 1754. Coin Piles */ #include<bits/stdc++.h> using namespace std; #define opt ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pirq(type) priority_queue<type, vector<type>, greater<type>> #define mem(x, value) memset(x, value, sizeof(x)); #define pii pair<int, int> #define pdd pair<double, double> #define pb push_back #define f first #define s second #define int long long const auto dir = vector< pair<int, int> > { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; const int MAXN = 1e8 + 50; const int Mod = 1e9 + 7; int n, a, b, t; signed main(){ opt; cin >> n; while( n-- ){ cin >> a >> b; if( b > a ) swap(a, b); if( a > 2 * b ) { cout << "NO\n"; } else if( ( a + b ) % 3 == 0 ) { cout << "YES\n"; } else { cout << "NO\n"; } } }