hdu 5890 Eighty seven

本文介绍了一个有趣的数学挑战问题,通过给定的一组数字,在每轮移除不多于3个数字的情况下,判断是否能从剩余数字中选出10个数,使得这些数的总和等于87。文章提供了详细的解题思路和使用bitset优化后的DP算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Eighty seven

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 243    Accepted Submission(s): 74


Problem Description
Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare  N cards with numbers. The number on the  i -th card is  ai . In class, each turn he will remove no more than  3  cards and let students choose any ten cards, the sum of the numbers on which is  87 . After each turn the removed cards will be put back to their position. Now, he wants to know if there is at least one solution of each turn. Can you help him?
 

Input
The first line of input contains an integer  t (t5) , the number of test cases.  t  test cases follow.
For each test case, the first line consists an integer  N(N50) .
The second line contains  N  non-negative integers  a1,a2,...,aN . The  i -th number represents the number on the  i -th card. The third line consists an integer  Q(Q100000) . Each line of the next  Q  lines contains three integers  i,j,k , representing Mr.Fib will remove the  i -th,  j -th, and  k -th cards in this turn. A question may degenerate while  i=j i=k  or  j=k .
 

Output
For each turn of each case, output 'Yes' if there exists at least one solution, otherwise output 'No'.
 

Sample Input
  
  
1 12 1 2 3 4 5 6 7 8 9 42 21 22 10 1 2 3 3 4 5 2 3 2 10 10 10 10 11 11 10 1 1 1 2 10 1 11 12 1 10 10 11 11 12
 

Sample Output
  
  
No No No Yes No Yes No No Yes Yes
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5891  5889  5887  5886  5885 
题意:给你n个数,m次询问,每次询问给你三个数,问在给出的数里面存不存在任意取10个(不包含这三个数)的数的和==87。

思路:用bitset暴力dp,时间卡着过,暴力打表用dp[i][j][k]表示存不存在,然后就是用bitset优化0 1背包。剩下的看代码:

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
typedef long long LL;
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 55
typedef long long LL;
int a[maxn], dp[maxn][maxn][maxn];
bitset<90>s[11];
int n;
bool solve(int x, int y, int z){
	for (int i = 0; i<11; i++)
		s[i].reset();
	s[0][0] = 1;
	for (int i = 1; i <= n; i++){
		if (i == x || i == y || i == z || a[i]>87)
			continue;
		for (int j = 10; j>0; j--){
			s[j] |= s[j - 1] << a[i];
		}
	}
	if (s[10][87])
		return 1;
	else
		return 0;
}
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		scanf("%d", &n);
		for (int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
		}
		for (int i = 1; i <= n; i++){
			for (int j = i; j <= n; j++){
				for (int k = j; k <= n; k++){
					dp[i][j][k] = solve(i, j, k);
				}
			}
		}
		int q;
		scanf("%d", &q);
		while (q--){
			int b[3];
			scanf("%d%d%d", &b[0], &b[1], &b[2]);
			sort(b, b + 3);
			if (dp[b[0]][b[1]][b[2]])
				printf("Yes\n");
			else
				printf("No\n");
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值