Poj1837balance (Group backpack)

Source: Internet
Author: User
Tags integer numbers

Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11042 Accepted: 6855

Description

Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any and ordinary balance.
It orders arms of negligible weight and each arm's length is 15. Some Hooks is attached to these arms and Gigel wants to hang up Some weights from his collection of G weights (1 <= g <=) Knowing that these weights has distinct values in the range 1..25. Gigel may droop any weight of any hooks but he's 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 Possibilitie s to balance the device.
It is guaranteed, that would 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 <=) and the number G (2 <= G <= 20);
? The next line contains C integer numbers (these numbers is also distinct and sorted in ascending order) in the range-15. . Representing the repartition of the hooks; Each number represents the position relative to the center of the balance in the X axis (when no weights is attached the Device 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 Nu Mbers determines the arm of the the balance to which the hook was attached: '-' for the left arm and ' + ' for the right arm);
? On the next line there is G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the W Eights ' 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

The main topic: there is a call, there is a C groove in different positions, can hang the code, there is G code can hang, ask all the code to hang up there are how many kinds of balance method.

Problem solving: Because all the code must be hung up, to be able to use the group backpack, DP[GN][V+H[I]*G[GN]] The composition is the first GN code, the first ditch to go with dp[gn-1][v] combination.

PS: If you encounter the topic said, must put all the things to use can be used in a group backpack.

#include <stdio.h> #include <string.h>int dp[25][15005];int main () {    int hn,gn,h[25],g[25],mid0=7500 ;//Indicates the overall translation of how much, negative numbers are positive, 0 becomes mid0 while    (scanf ("%d%d", &HN,&GN) >0)    {for        (int i=1;i<=hn; i++)            scanf ("%d", &h[i]);        for (int i=1;i<=gn; i++)            scanf ("%d", &g[i]);        Memset (Dp,0,sizeof (DP));        dp[0][mid0]=1;//Hang 0 items balance for        (int i=0;i<gn; i++) for            (int v=0;v<=15000;v++)            if (Dp[i][v])// The state of the first I-item is said to have a status of V, how many kinds of            {for                (int j=1;j<=hn; j + +)                    if (v+h[j]*g[i+1]>=0&&v+h[j]*g[i+1] <=15000)                        dp[i+1][v+h[j]*g[i+1]]+=dp[i][v];            }        printf ("%d\n", Dp[gn][mid0]);}    }


Poj1837balance (Group backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.