hdoj 5616 Jam's balance 【母函数】
来源:互联网 发布:linux服务器访问外网 编辑:程序博客网 时间:2024/06/05 05:36
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 148 Accepted Submission(s): 63
Problem Description
Jim has a balance and N weights. (1≤N≤20)
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
Input
The first line is a integer T(1≤T≤5) , means T test cases.
For each test case :
The first line isN , means the number of weights.
The second line areN number, i'th number wi(1≤wi≤100) means the i'th weight's weight is wi .
The third line is a numberM . M is the weight of the object being measured.
For each test case :
The first line is
The second line are
The third line is a number
Output
You should output the "YES"or"NO".
Sample Input
121 43245
Sample Output
NOYESYESHintFor the Case 1:Put the 4 weight aloneFor the Case 2:Put the 4 weight and 1 weight on both side
题意:有n个已知重量的砝码和一个没有游标的天平,给定m次查询,每次询问能否称出质量为k的物体。
思路:母函数,每次选取的系数可加可减,稍微变一点。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (100000+10)#define MAXM (300000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;int a[21];int c1[2010], c2[2010];int main(){ int t; Ri(t); W(t) { int n; Ri(n); int sum = 0; for(int i = 0; i < n; i++) scanf("%d", &a[i]), sum += a[i]; CLR(c1, 0); CLR(c2, 0); c1[a[0]] = c1[0] = 1; for(int i = 2; i <= n; i++) { for(int j = 0; j <= sum; j++) { for(int k = 0; k <= a[i-1] && k + j <= sum; k += a[i-1]) { c2[k+j] += c1[j]; c2[abs(k-j)] += c1[j]; } } for(int j = 0; j <= sum; j++) { c1[j] = c2[j]; c2[j] = 0; } } int m; Ri(m); W(m) { int k; Ri(k); printf(c1[k] ? "YES\n" : "NO\n"); } } return 0;}
0 0
- HDOJ 5616 Jam's balance(母函数)
- hdoj 5616 Jam's balance 【母函数】
- hdoj Jam's balance 5616 (母函数&暴力)
- hdoj-5616-Jam's balance
- HDU-5616 Jam's balance(母函数)
- HDOJ 5616-Jam's balance【模拟】
- HDOJ 5616 Jam's balance (暴力)
- 【HDU】5616 - Jam's balance(母函数变型)
- 母函数解决 Jam's balance hdu5616
- HDU 5616 Jam's balance
- hdu 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance(dp)
- hdu 5616 Jam's balance ( 动态规划 )
- hdu 5616 Jam's balance(简单dp)
- 一个不用密码的用户系统
- git-使用指南
- LeetCode #237 Delete Node in a Linked List
- 遍历新姿势
- opencv(2)
- hdoj 5616 Jam's balance 【母函数】
- 【Android】【笔记】《Android 入门经典》part 13 网络通信技术
- iOS swift sqlite使用,上传及定时更新
- C内存管理
- SACK信息
- 【Android】【笔记】《Android 入门经典》part 14 Service
- Codefroces 618D Hamiltonian Spanning Tree 树型动规
- TCP显示冲突通告(ECN)
- 集训队专题(2)1007 Evolution