zoukankan      html  css  js  c++  java
  • 母函数

    以几个几克砝码为例

    1+x^2+x^4表示有2个2g砝码

    1+x^3+x^6表示2个3g砝码

    1+x^3+x^6+ x^9表示有3个3g砝码

    问这些砝码有几种不一样的重量

     f ( x ) = (1+x^2+x^4)×(1+x^3+x^6)×(1+x^3+x^6+ x^9)

    得出结果有几个x就有几种不一样的重量

    hdu2082典型的母函数问题

    代码如下

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long LL;
    
    int main() {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int N;
        scanf("%d",&N);
        LL a[60], b[60];//存放的是系数
        while(N--) {
            int num;
            int i, j, k;
            for(i = 0; i <= 60; i++) {
                a[i] = b[i] = 0;
            }
            a[0] = 1;
            for(i = 1; i <= 26; i++) {
                scanf("%d",&num);
                if(num == 0) continue;
                for(j = 0; j <= 50; j++)//这两个for比较关键k*i+j,j,i代表的是指数
                    for(k = 0; k <= num && k*i+j <= 50; k++)
                        b[k*i+j] += a[j];
                for(j = 0; j <= 50; j++) {
                    a[j] = b[j];
                    b[j] = 0;
                }
            }
            LL total = 0;
            for(int i = 1; i <= 50; i++) 
                total += a[i];
                printf("%lld
    ",total);
        }
        return 0;
    }

    整数的拆分的母函数代码

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long LL;
    
    int main() {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int N;
        while(scanf("%d",&N) != EOF ) {
            LL a[60], b[60];//存放的是系数
            int i, j, k;
            for(i = 0; i <= N; i++) {
                a[i] = 1;
                b[i] = 0;
            }
            for(i = 2; i <= N; i++) {
                for(j = 0; j <= N; j++)//这两个for比较关键k*i+j,j,i代表的是指数
                    for(k = 0; k*i+j <= N; k++)
                        b[k*i+j] += a[j];
                for(j = 0; j <= N; j++) {
                    a[j] = b[j];
                    b[j] = 0;
                }
            }
            printf("%lld
    ",a[N]);
        }
        return 0;
    }
  • 相关阅读:
    内存溢出
    Spring-bean的循环依赖以及解决方式
    JUC中Lock和ReentrantLock介绍及源码解析
    RocketMQ阅读注意
    RocketMQ环境搭建
    HSF源码阅读
    最近找工作,有招JAVA开发的可以联系我,如果不嫌弃我2年前用C,也可以联系我
    Spring Extensible XML
    MySQL主主+Keepalived架构安装部署
    记录一则数据库死锁故障的分析过程
  • 原文地址:https://www.cnblogs.com/creativepower/p/6802423.html
Copyright © 2011-2022 走看看