hdu 2069母関数(一)または暴力列挙


Coin Change
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6775    Accepted Submission(s): 2246
Problem Description
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.
 
Input
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
 
Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
 
Sample Input

   
   
   
   
11 26

 
Sample Output

   
   
   
   
4 13

 
まず暴力AC:
#include<iostream>
using namespace std;
int main(){
	int n,a,b,c,d,e;
	while(cin>>n){  
		int count=0;
	for(a=0;a<=n;a++){
		for(b=0;5*b<=n-a;b++){
			for(c=0;10*c<=n-a-5*b;c++){
				for(d=0;25*d<=n-a-5*b-10*c;d++){
					 e=n-a-5*b-10*c-25*d;          //   50           (    )          :
					if(e%50==0&&a+b+c+d+e/50<=100) //e%50==0    50    n      50   *    50      e/50 *

						count++;
					}
				}
			}
		}
	cout<<count<<endl;
	}
return 0;
}

次に、親関数を示します.
#include<iostream>
using namespace std;
	int c1[251][101],c2[251][101];
	int a[6]={0,1,5,10,25,50};
	int sum[251];
int main(){
	

	c1[0][0]=1;   
 /*             :
int n,a[5]={1,5,10,25,50},sum; 
    for(int i=0;i<=260;++i)
    for(int j=0;j<=101;++j) 
    {
        c1[i][j]=0;c2[i][j]=0;
    }
    for(int i=0;i<=100;++i)//    1        i(   100) 
    {
            c1[i][i]=1;*/

        for(int i=1;i<=5;i++){
		for(int j=0;j<=250;j++){
			for(int k=0;k*a[i]+j<=250;k++){
				for(int t=0;k+t<=100;t++){//  c1 coin  
					c2[k*a[i]+j][t+k]+=c1[j][t];
				}
			}
		}
		for(int i=0;i<251;i++){
			for(int j=0;j<101;j++){
				c1[i][j]=c2[i][j];
				c2[i][j]=0;
			}
		}
	}
	/*int n;
	while(cin>>n){
		int sum=0;
		for(int i=0;i<105;i++){
			sum+=c1[n][i];
		}
		cout<<sum<<endl;
	}
	*/
	 for ( int j = 0; j != 251; ++ j )
           {
                for ( int i = 0; i != 101; ++ i )
                {
                      sum[j] += c1[j][i] ;
                }
           }
           int N;
           while ( cin >> N )
           {
                  
                  cout << sum[N] << endl;
           }
return 0;
}