本文涉及知识点
[蓝桥杯 2017 省 AB] 包子凑数
题目描述
小明几乎每天早晨都会在一家包子铺吃早餐。他发现这家包子铺有 N N N 种蒸笼,其中第 i i i 种蒸笼恰好能放 A i A_i Ai 个包子。每种蒸笼都有非常多笼,可以认为是无限笼。
每当有顾客想买 X X X 个包子,卖包子的大叔就会迅速选出若干笼包子来,使得这若干笼中恰好一共有 X X X 个包子。比如一共有 3 3 3 种蒸笼,分别能放 3 3 3 、 4 4 4 和 5 5 5 个包子。当顾客想买 11 11 11 个包子时,大叔就会选 2 2 2 笼 3 3 3 个的再加 1 1 1 笼 5 5 5 个的(也可能选出 1 1 1 笼 3 3 3 个的再加 2 2 2 笼 4 4 4 个的)。
当然有时包子大叔无论如何也凑不出顾客想买的数量。比如一共有 3 3 3 种蒸笼,分别能放 4 4 4 、 5 5 5 和 6 6 6 个包子。而顾客想买 7 7 7 个包子时,大叔就凑不出来了。
小明想知道一共有多少种数目是包子大叔凑不出来的。
输入格式
第一行包含一个整数 N N N。 ( 1 ≤ N ≤ 100 ) (1 \le N \le 100) (1≤N≤100)。
以下 N N N 行每行包含一个整数 A i A_i Ai。 ( 1 ≤ A i ≤ 100 ) (1 \le A_i \le 100) (1≤Ai≤100)。
输出格式
一个整数代表答案。如果凑不出的数目有无限多个,输出 INF
。
样例 #1
样例输入 #1
2
4
5
样例输出 #1
6
样例 #2
样例输入 #2
2
4
6
样例输出 #2
INF
提示
对于样例 1 1 1,凑不出的数目包括: 1 , 2 , 3 , 6 , 7 , 11 1,2,3,6,7,11 1,2,3,6,7,11。
对于样例 2 2 2,所有奇数都凑不出来,所以有无限多个。
蓝桥杯 2017 省赛 A 组 H 题。
数论 菲蜀定理 动态规划
e=gcd(a)如果不为1,如果x % e != 0 ,都无解。无限无解。
d = a所有元素的最小公倍数。
如果 dd >=d,一定有解。根据菲蜀定理,一定有整数解,需要证明的是一定有自然数解。
先证明 ax+by = dd 一定有自然数解。
令y最小自然数解的解(x1,y1),则by1
∈
\in
∈[0,d),如果by1大于 d,则by1-=d ,ax1 +=d。符合假设。
ax1 = dd - by1 ,dd >= d,by1 < d,故ax > 0,即x1 正整数。
下面用数学归纳法证明 多元菲蜀定理的正确性。
令 n元菲蜀定理 ,n元参数的最小公倍数是d1,则存在自然数解等于d1。
d1xy+byy = dd
由2元菲蜀定理可知dd >= d,一定存在自然数解xy1,yy2。
最终解是:(x1xy1,y1xy1
⋯
\cdots
⋯yy1)
如果e 是1,全部是e的倍数返回0,否则返回无限。
a,b,c的最小公倍数大于等于a,b的公倍数。故我们只需枚举2个数的公倍数,不需要枚举多个数的公倍数。由于ai小于等于100。故其公倍数一定小于等于10000。
has[i]表示i个包子能否达到。
从1到10000枚举
i的后续状态
has[i+a[j] ]=true
代码
核心代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include <bitset>
using namespace std;
template<class T = int>
vector<T> Read(int n,const char* pFormat = "%d") {
vector<T> ret;
T d ;
while (n--) {
scanf(pFormat, &d);
ret.emplace_back(d);
}
return ret;
}
template<class T = int>
vector<T> Read( const char* pFormat = "%d") {
int n;
scanf("%d", &n);
vector<T> ret;
T d;
while (n--) {
scanf(pFormat, &d);
ret.emplace_back(d);
}
return ret;
}
string ReadChar(int n) {
string str;
char ch;
while (n--) {
do
{
scanf("%c", &ch);
} while (('\n' == ch));
str += ch;
}
return str;
}
class Solution {
public:
int Ans(vector<int> a) {
int g = 0;
for (const auto& i : a) { g = gcd(g, i); }
if (1 != g) { return -1; }
const int N = 100 * 100;
vector<bool> has(N + 1);
has[0] = true;
for (int i = 0; i < N; i++) {
if (!has[i]) { continue; }
for (const auto& j : a) {
int k = i + j;
if (k <= N) { has[k] = true; }
}
}
return count(has.begin(), has.end(), false);
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
int n;
scanf("%d", &n);
auto a = Read<int>(n);
#ifdef _DEBUG
//Out(a, "a=");
#endif
auto res = Solution().Ans(a);
if (res < 0) {
cout << "INF" << std::endl;
}
else {
cout << res << std::endl;
}
return 0;
}
单元测试
vector<int> a;
TEST_METHOD(TestMethod1)
{
a = { 4,5 };
auto res = Solution().Ans(a);
AssertEx(6, res);
}
TEST_METHOD(TestMethod2)
{
a = { 4,6 };
auto res = Solution().Ans(a);
AssertEx(-1, res);
}
扩展阅读
我想对大家说的话 |
---|
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
如果程序是一条龙,那算法就是他的是睛 |
失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。