0-1 Backpack Re-revision

Source: Internet
Author: User

Title Description

Spend the money, so single, single, so over the double "11", over the double "11" so the money spent.

This year Nova June (third) still live his dark "buy to buy buy" double "11", but because the bag is shy, not be too willful. His shopping cart, filled with countless goods, a total of N, a lot of goods actually more than one __ (: 3"∠) _ now Nova June to make a difficult choice, he would like to pick out all the products m pieces of the order, how many kinds of ways to gather single? The remainder of the number of methods to M is obtained.

PS: The same product does not make a distinction.

Input

Multiple sets of test data (no more than 100 groups)

Each group of data two lines, the first behavior three positive integer n,m,m, the specific meaning is described in detail, the second action n positive integer a1,a2,,, an, representing the number of the first commodity

(1<=n,ai,m<=1000,2<=m<=10000)

Output

For each set of data, output a row representing the total number of methods

Input sample
3 3 100001 2 3
Output sample
6

题目来源:http://biancheng.love/contest/17/problem/G/index
For details, see the Code and comments:
1#include <bits/stdc++.h>2 3 using namespacestd;4 Const intn=1010;5 intA[n];6 intB[n][n];7 intMain ()8 {9     intn,m,m;Ten      while(SCANF ("%d%d%d", &n,&m,&m) = =3) One     { A          for(inti =0; I < n; i++) -scanf"%d", &a[i]);//number of pieces per product -  the          for(inti =0; I <= N; i++) -b[i][0] =1; -  -          for(inti =0; I < n; i++)//n Kinds of goods +              for(intj =1; J <= M; J + +)//pick out M-pieces -             { +                 if(J-1-A[i] >=0)//is greater than the number of items in Part I Ab[i+1][J] = (B[i][j] + b[i+1][j-1]-b[i][j-1-a[i]] +m)%M; at                 Else -b[i+1][J] = (B[i][j] + b[i+1][j-1])%M; -             } -printf"%d\n", B[n][m]); -     } -  in}
Some template functions attached to the backpack:
1 voidZoreonepack (intCost,intWeight//0-1 Backpack2 {3      for(inti = W; I >= weight; --i)4F[i] = max (f[i],f[i-weight]+Cost );5 }6 7 voidCompletepack (intCost,intWeight//Full Backpack8 {9      for(inti = weight; I <= W; ++i)TenF[i] = max (f[i],f[i-weight]+Cost ); One } A  - voidMultipack (intCost,intWeight,intNum//Multiple Backpack - { the     if(num*weight>=W) - Completepack (cost,weight); -     Else -     { +         intK =1 ; -          while(k<num) +         { AZoreonepack (cost*k,weight*k); atnum-=K; -K + =K; -         } -Zoreonepack (cost*num,weight*num); -     } - } in  - voidTwozoreonepack (intCost,intWeight,intmany) to { +      for(inti = W; I >= weight; --i) -          for(intj = M; J >= many; --j) theTWO[I][J] = max (two[i][j],two[i-weight][j-many]+Cost ); * } $ Panax Notoginseng voidTwocompletepack (intCost,intWeight,intmany) - { the      for(inti = weight; I <= W; ++i) +          for(intj = Many; J <= W; ++j) ATWO[I][J] = max (two[i][j],two[i-weight][j-many]+Cost ); the } +  - voidTwomultipack (intCost,intWeight,intMany,intnum) $ { $     if(num*weight>=w&&num*many>=M) - Twocompletepack (cost,weight,many); -     Else the     { -         intK =1 ;Wuyi          while(k<num) the         { -Twozoreonepack (cost*k,weight*k,many*k); Wunum-=K; -K + =K; About         } $Twozoreonepack (cost*num,weight*num,many*num); -     } -}



0-1 Backpack Re-revision

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.