POJ 1837Balance

来源:互联网 发布:点windows键没反应 编辑:程序博客网 时间:2024/06/07 15:01
Balance
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 13341 Accepted: 8352

Description

Gigel has a strange "balance" and he wants to poise(平衡) it. Actually, the device(装置) is different from any other ordinary balance.
It orders two arms of negligible(微不足道的) weight and each arm's length is 15. Some hooks are attached(依附) to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct(明显的) values in the range 1..25. Gigel may droop(下垂) any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.

Knowing the repartition(重新分配) of the hooks and the set of the weights write a program that calculates(计算) the number of possibilities to balance the device.
It is guaranteed(保证) that will exist at least one solution(解决方案) for each test case at the evaluation(评价).

Input

The input(投入) has the following structure(结构):
• the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20);
• the next line contains C integer(整数) numbers (these numbers are also distinct(明显的) and sorted in ascending(上升的) order) in the range -15..15 representing therepartition(重新分配) of the hooks; each number represents the position relative to the center of the balance on the X axis(轴) (when no weights are attached(依附) thedevice(装置) is balanced and lined up to the X axis; the absolute(绝对的) value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: '-' for the left arm and '+' for the right arm); 
• on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights' values. 

Output

The output(输出) contains the number M representing the number of possibilities to poise(平衡) the balance.

Sample Input

2 4-2 3 3 4 5 8

Sample Output

2

Source

Romania OI 2002
题意:给你一个天平的挂钩位置,负数和正数分别代表左右两边,然后给你勾码的重量,让你求所有让天平平衡的挂法种数。一开始想到的就是dfs暴力搜索,可是看了一下数据范围时间复杂度肯定得超时啊~,最后还是求助了网上大神的博客才想到了dp的解法。代码很好懂直接看代码吧。。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int cc[26],gg[26];int dp[26][15009];int main(){    int c,g; cin>>c>>g;    for(int i=1;i<=c;i++) scanf("%d",&cc[i]);    for(int i=1;i<=g;i++) scanf("%d",&gg[i]);    memset(dp,0,sizeof(dp));    dp[0][7500]=1;    for(int i=1;i<=g;i++)    for(int j=0;j<=15000;j++)    for(int k=1;k<=c;k++) dp[i][j+gg[i]*cc[k]]+=dp[i-1][j];    cout<<dp[g][7500]<<endl;;    return 0;}



0 0