当前位置:网站首页>[蓝桥杯2017初赛]包子凑数
[蓝桥杯2017初赛]包子凑数
2022-07-06 09:14:00 【%xiao Q】
题目
题目描述
小明几乎每天早晨都会在一家包子铺吃早餐。这家包子铺有N种蒸笼,其中第i种蒸笼恰好能放Ai个包子
每种蒸笼都有非常多笼,可以认为是无限笼。
每当有顾客想买X个包子,卖包子的大叔就会选出若干笼包子来,使得这若干笼中恰好一共有X个包子。
比如一共有3种蒸笼,分别能放3、4和5个包子。当顾客想买11个包子时,大叔就会选2笼3个的再加1笼5个的(也可能选出1笼3个的再加2笼4个的)。
当然有时包子大叔无论如何也凑不出顾客想买的数量。
比如一共有3种蒸笼,分别能放4、5和6个包子。而顾客想买7个包子时,大叔就凑不出来了。
小明想知道一共有多少种数目是包子大叔凑不出来的。
输入格式
第一行包含一个整数N。(1 <= N <= 100)
以下N行每行包含一个整数Ai。(1 <= Ai <= 100)
输出格式
输出一行包含一个整数代表答案。如果凑不出的数目有无限多个,输出INF。
输入样例 复制
2
4
5
输出样例 复制
6
数据范围与提示
对于样例,凑不出的数目包括:1, 2, 3, 6, 7, 11。
分析:
这题得知道一个定理,即欧几里徳定理:对于不完全为0的整数a,b那么一定存在整数x,y使得gcd(x,y) = a * x + b * y。
所以我们可以假设a * x + b * y = d,要使该方程有解,那么d % gcd(a, b) = 0是必然的,如果gcd(a,b) = 1,那么d就能整除任何数,也就说a,b这2个数可以凑除任何数(但是里面不能有负数,所以存在一些数是凑不出来的),反之不能。
所以第一步我们得找出这些数的最大公约数,如果gcd = 1,那么在判断里面有多少个数凑不出来,如果gcd != 1,那么一定会有无数种数凑不出来,直接输出INF。
参考代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 10010, M = 110;
int f[N];
int a[N];
int main()
{
int n;
cin >> n;
int g = 0;
rep(i, 1, n)
{
cin >> a[i];
g = __gcd(g, a[i]);
}
if(g != 1)
{
puts("INF");
return 0;
}
rep(i, 1, n) f[a[i]] = 1;
rep(i, 1, n)
for(int j = 0; j + a[i]<= 10000; j++) // 这里到10000有点玄学(数学,不会呀)
{
if(f[j]) f[j + a[i]] = 1;
}
int ans = 0;
rep(i, 1, 10000)
if(!f[i]) ans++;
cout << ans << endl;
return 0;
}
边栏推荐
- PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
- QT creator test
- In the era of DFI dividends, can TGP become a new benchmark for future DFI?
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- Knowledge Q & A based on Apache Jena
- Introduction to the easy copy module
- error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
- Data dictionary in C #
- MySQL主從複制、讀寫分離
- 软件测试-面试题分享
猜你喜欢

MySQL master-slave replication, read-write separation

Solve the problem of installing failed building wheel for pilot

Machine learning -- census data analysis

Did you forget to register or load this tag

Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution

Basic use of redis

Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.

In the era of DFI dividends, can TGP become a new benchmark for future DFI?

Some problems in the development of unity3d upgraded 2020 VR

How to configure flymcu (STM32 serial port download software) is shown in super detail
随机推荐
引入了junit为什么还是用不了@Test注解
一键提取pdf中的表格
SSM整合笔记通俗易懂版
QT creator specifies dependencies
Installation and use of MySQL under MySQL 19 Linux
Install mysql5.5 and mysql8.0 under windows at the same time
Request object and response object analysis
Why is MySQL still slow to query when indexing is used?
自动机器学习框架介绍与使用(flaml、h2o)
Ansible实战系列一 _ 入门
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
A trip to Macao - > see the world from a non line city to Macao
Invalid global search in idea/pychar, etc. (win10)
Install mongdb tutorial and redis tutorial under Windows
One click extraction of tables in PDF
QT creator create button
Development of C language standard
Software testing - interview question sharing
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
机器学习笔记-Week02-卷积神经网络