Material dispatching of ACM program design in Henan Province

Source: Internet
Author: User

Material Dispatch

earthquake in a certain area, the disaster area has been very difficult, the victims urgently need some tents, clothing, food and plasma and other supplies. The road to the disaster area is full of landslides,70% above the road damage, the bridge is all destroyed. The state immediately launched emergency preparedness plans, the history of the largest non-combat air transport operations, ready to drop urgently needed supplies to the disaster area.

one side has difficulty , p plus support. There are now known to have N places, respectively , A1,A2,...., an material for deployment. At present, the disaster area needs the quantity of M.

Now, ask your help to calculate the total number of material scheduling schemes.

Suppose a place is chosen to be leveled, the number of its material is taken away.

Input

First line: K indicates how many sets of test data are available.

Next, there are 2 rows for each set of test data, line 1 : N M

Line 2nd: A1 A2 ... an

2≤K≤8     1<n≤ -   1<m≤ +     1≤ Ai≤1 the 

All data is a positive integer. There is a space between the input data.

Assume that a given data has at least one scheduling scheme.

Output

for each set of test data, output one line: Total programme number of material dispatch

Sample Input
24 41 1 2 24 61 1 2 2
Sample Output
31

Idea: See this problem, began to think is dynamic planning, the result timed out, later found deep search very good. The code is as follows:
#include <stdio.h> #include <string.h>int s[101],sum,m,n,book[101]; void Dfs (int x,int i)//x represents the sum of the current dispatch material, I means the subscript {    if (x>=m) of the material and/or the end loop    {        if (x==m) when the sum of material is greater than or equal to the amount of material requested by the disaster area. When the sum of goods equals the amount of material needed, the scheme succeeds        {            sum++;//result +1            return;        }        return;//not satisfy end    }       for (int j=i;j<=n;j++)    {        if (book[i]==0)//material not used       {     x+=s[j];// The current dispatch material increases         book[j]=1;//material to be used         DFS (x,j+1);//Deep search         book[j]=0;  Backtracking         x-=s[j];//minus unsatisfied, re-searching       }}}int main () {    int k;    scanf ("%d", &k);    while (k--)    {        int i;        Memset (s,0,sizeof (s));//chushih        memset (book,0,sizeof (book));        sum=0;        scanf ("%d%d", &n,&m);        for (i=1;i<=n;i++)        scanf ("%d", &s[i]),//s array store material        DFS (0,1);//Deep Search        printf ("%d\n", sum);//output result    }    return 0;}



Material dispatching of ACM program design in Henan Province

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.