Bzoj2287 [POJ Challenge] disappears

Source: Internet
Author: User
Tags cmath

Bzoj2287 [POJ Challenge] disappears
Description

Ftiasch hasNItems, volume isW1,W2,...,WN. Due to her negligenceIItems are lost. "Use the remainingN-1 the full size of an item isXHow can I find a backpack ?" -- This is a classic problem. She recorded the answerCount (I, x)To obtain all the values of 1 <= I <= N, 1 <= x <= MCount (I, x)Table.

Input

Row 1st: two integersN(1 ≤N≤ 2 × 103) andM(1 ≤M≤ 2 × 103), the number of items and the maximum volume.

Row 3:NIntegersW1,W2,...,WNThe volume of the item.

Output

OneN×MMatrix,Count (I, x)The last digit.

Sample Input3 2
1 1 2
Sample Output11
11
21
HINT

If item 3 is lost, there is only one way to fill the backpack with a capacity of 2, that is, choose item 1 and item 2.

Source

I had a good idea about DP and didn't come up with a solution... QAQ

F [I] [j] indicates the number of solutions for the first I items filled with a backpack with a volume of j.

Obviously, the f array can be calculated using the O (n ^ 2) DP.

G [I] [j] indicates that the I-th item is not selected, and the number of solutions for a backpack filled with a volume of j is displayed.

If j

If j is ≥n, g [I] [j] = f [n] [j]-g [I] [j-w [I].

We can find that both f and g Arrays can be implemented in one dimension.

At the beginning, WA performed many times, because no modulo was obtained for each step.

#include
   
    #include
    
     #include
     
      #include
      
       #include
       
        #include#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define ll long long#define maxn 2005using namespace std;int n,m;int f[maxn],g[maxn],w[maxn];inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}int main(){n=read();m=read();F(i,1,n) w[i]=read();memset(f,0,sizeof(f));f[0]=1;F(i,1,n) D(j,m,w[i]) f[j]+=f[j-w[i]];F(i,1,n){F(j,0,w[i]-1) g[j]=f[j];F(j,w[i],m) g[j]=f[j]-g[j-w[i]];F(j,1,m) printf("%d",g[j]%10);printf("\n");}}
       
      
     
    
   

Related Article

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.