"Blue Bridge Cup" 2017 Provincial Tournament Buns Counting-Mathematical Sieve Method DP

『Title description』

问题描述

  小明几乎每天早晨都会在一家包子铺吃早餐。他发现这家包子铺有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

样例输入

2
4
6

样例输出

INF

样例说明

  对于样例1,凑不出的数目包括:1, 2, 3, 6, 7, 11。
  对于样例2,所有奇数都凑不出来,所以有无限多个。

"Solution"

  1. First solve the infinite case, if it is infinite, then only a part of the multiple is covered, and some are not covered, so if g = g c d { a [ i ] } , i = 1 , 2 , 3... g= gcd\{a[i]\},i=1,2,3... Is not 1 1 , then these numbers only cover all g g multiples of , there must be multiples of a certain number that cannot be covered.
  2. Use class idea similar to the linear sieve can be combined into the number of all screened out , a screen to set the upper limit of the number of first, a conclusion to use: for two prime numbers p, q, px + py not represent The maximum number is p * qpq. ( Proof link ) The maximum value of p and q is 100, then the upper limit can be determined as 1e4
  3. Consider the setting of the sieve method: if a number x x can be combined, then x a [ i ] x-a[i] must also be able to be combined. With the idea of ​​a dp, all the numbers can be sieved out. The initial situation should be 0, (it can be considered that 0 a [ i ] a[i] ). Many people write with complete backpacks, but the central idea of ​​the complete backpack in this question is justified.
/*****************************
*author:ccf
*source:POJ-
*topic:
*******************************/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define ll long long
using namespace std;

const int N = 107,LIM = 1e4;
int n,m,cas;
int a[N];
bool bk[N*N];
ll gcd(ll a,ll b){
	return b ? gcd(b,a%b) : a;
}
int main(){
	//freopen("data.in","r",stdin);
	scanf("%d",&n);
	for(int i = 1; i <= n; ++i) scanf("%d",a+i);
	int g = a[1];
	for(int i  =2; i <= n; ++i) g = gcd(a[i],g);
	if(g != 1){
		printf("INF\n");
		return 0;
	}
	bk[0] = 1;
	for(int i = 1; i <= n; ++i){
		for(int j = a[i]; j < LIM; j++){
			bk[j] = max(bk[j],bk[j-a[i]]);
		}
	}
	int ans = 0;
	for(int i = 1; i < LIM; ++i) 
		if(!bk[i]) ans++;
	printf("%d\n",ans);
	return 0;
}

Published 141 original articles · praised 71 · 60,000+ views

Guess you like

Origin blog.csdn.net/sinat_40872274/article/details/105314550