zoukankan      html  css  js  c++  java
  • HDU 2069 Coin Change (母函数)

    问题描述:找硬币问题,有个限制条件,就是总数不能超过100。   思路:将母函数变形,加一维表示数量。   代码:  
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define MID(x,y) ((x+y)>>1)
    using namespace std;
    typedef long long LL;
    int a[5] = {1, 5, 10, 25, 50};
    /* generating_function */
    const int N = 300;
    struct generating_function{
        int c1[N][101], c2[N][101];
        int maxn;
        void init(int n){
            memset(c1, 0, sizeof(c1));
            memset(c2, 0, sizeof(c2));
            c1[0][0] = 1;
            maxn = n;
        }
        void cal(int p);
    }ef;
    void generating_function::cal(int p){
        int tmp[N][101];
        memset(tmp, 0, sizeof(tmp));
        for (int i = 0; i <= maxn; i ++){
            for (int j = 0; j <= maxn; j += a[p]){
                int k1 = j/a[p];
                if (i + j <= maxn && c2[j][k1])
                    for (int k2 = 0; k1+k2 <= 100; k2 ++)
                        tmp[i+j][k1+k2] += c1[i][k2] * c2[j][k1];
            }
        }
        memset(c1, 0, sizeof(c1));
        for (int i = 0; i <= maxn; i ++){
            for (int k = 0; k <= 100; k ++){
                c1[i][k] = tmp[i][k];
            }
        }
        return ;
    }
    int main(){
        int n;
        while(scanf("%d", &n) == 1){
            ef.init(n);
            for (int i = 0; i < 5; i ++){
                memset(ef.c2, 0, sizeof(ef.c2));
                for (int j = 0; j <= n; j += a[i]){
                    ef.c2[j][j/a[i]] = 1;
                }
                ef.cal(i);
            }
    
            int res = 0;
            for (int k = 0; k <= 100; k ++){
                res += ef.c1[n][k];
            }
            printf("%d\n", res);
        }
    	return 0;
    }
    
     
    举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG
  • 相关阅读:
    SSH框架中使用注解和xml配置的区别
    web项目中log4j的配置
    嵌入式—ASCII码
    MATLAB
    MATLAB
    MATLAB
    MATLAB
    CentOS 7将网卡名称eno16777736改为eth0
    图像增强处理
    Debussy与modelsim联仿时 do 文件脚本
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114224.html
Copyright © 2011-2022 走看看