题目链接:
PREV-8 买不到的数目
思路:
结论:a,b为质数,x,y为非负整数,则ax+by最大不能表示的数为ab-a-b,因此ab-a-b即为答案;
但是题目似乎并没有说a,b为质数?
所以严谨一点还是用动态规划吧;
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 99;
bool dp[maxn] = {true};
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
int a, b, ans = 0;
cin >> a >> b;
for(int i = 0; i < maxn - a - b; ++i) {
if(dp[i]) dp[i + a] = dp[i + b] = true;
else ans = i;
}
cout << ans;
return 0;
}