蓝桥杯 PREV-8 买不到的数目

题目链接:

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;	
}
发布了356 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104329291
今日推荐