題目連接:https://codeforces.com/problemset/problem/1256/Aios
You have aa coins of value nn and bb coins of value 11. You always pay in exact change, so you want to know if there exist such xx and yy that if you take xx (0≤x≤a0≤x≤a) coins of value nn and yy (0≤y≤b0≤y≤b) coins of value 11, then the total value of taken coins will be SS.ide
You have to answer qq independent test cases.ui
The first line of the input contains one integer qq (1≤q≤1041≤q≤104) — the number of test cases. Then qq test cases follow.atom
The only line of the test case contains four integers aa, bb, nn and SS (1≤a,b,n,S≤1091≤a,b,n,S≤109) — the number of coins of value nn, the number of coins of value 11, the value nn and the required total value.spa
For the ii-th test case print the answer on it — YES (without quotes) if there exist such xx and yy that if you take xx coins of value nn and yy coins of value 11, then the total value of taken coins will be SS, and NO otherwise.code
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).xml
4 1 2 3 4 1 2 3 6 5 2 6 27 3 3 5 18
YES NO NO YES
思路:輸入a,b,n,s,每一個表明的意思爲:a個含有價值n的硬幣、b個含有價值1的硬幣、價值爲n的硬幣、由這些硬幣組成的目標數。先判斷b個爲1的硬幣是否能直接達到s,能的話則直接輸出,不能的話則進行下一步。
先判斷價值爲n的硬幣最多能取多少個,即s對n取整,再將s減去s/n,再判斷剩下的能不能由b個價值爲1的硬幣組成,能的話則知足,不能的話則不知足。
AC代碼
#include<iostream> #include<cmath> using namespace std; int main() { int q; cin >> q; while(q--) { int a = 0,b = 0,sum = 0,n = 0,s = 0,temp = 0,min1 = 0; cin >> a >> b >> n >> s; if(b >= s) { cout << "YES" << endl; continue; } temp = s / n; min1 = min(a,temp); sum = s - min1 * n; if(b >= sum) { cout << "YES" << endl; continue; } else { cout << "NO" << endl; continue; } } return 0; }
題目連接:https://codeforces.com/contest/1257/problem/Ablog