zoukankan      html  css  js  c++  java
  • 【dp】p1025数的划分

    题目描述】

    将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序)。

    例如:n=7,k=3,下面三种分法被认为是相同的。

    1,1,5; 1,5,1; 5,1,1;

    问有多少种不同的分法。 输出一个整数,即不同的分法。

    【输入】

    两个整数n,k(6<n≤200,2≤k≤6),中间用单个空格隔开。

    【输出】

    一个整数,即不同的分法。

    【输入样例】

    7 3

    【输出样例】

    4

    【提示】

    四种分法为:1,1,5;1,2,4;1,3,3;2,2,3。

    【思路】:

    我竟然一眼秒了一道dp题(虽然很简单),太开心了,O(∩_∩)O哈哈哈~

    用f[i][j]表示i分成j的方案数

    用样例来解释一下:

    7 3

    分成j=1的情况:
    7

    分成j=2的情况:

    2 5

    5 2

    分成j=3的情况:

    1 1 5=(1+1) +5

    5 1 1=5+(1+1)

    2 3 2=(2+3)+ 2

    3 2 2=2+(2+3)

    所以方程就一目了然了:

    f[i][j]=f[i-1][j-1]+f[i-j][j]

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<map>
    #include<string>
    #include<cstring>
    using namespace std;
    const int maxn=999999999;
    const int minn=-999999999;
    inline int read() {
        char c = getchar();
        int x = 0, f = 1;
        while(c < '0' || c > '9') {
            if(c == '-') f = -1;
            c = getchar();
        }
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int n,m;
    int f[520][520];//f[i][j]表示i分成j份的方案数 
    int main() {
        n=read();
        m=read();
        for(int i=1; i<=n; i++)
            f[i][1]=1;
        for(int i=2; i<=n; i++)
            for(int j=1; j<=m; j++)
                if(j<=i)
                    f[i][j]=f[i-1][j-1]+f[i-j][j];
        cout<<f[n][m];
        return 0;
    }

     

  • 相关阅读:
    1121 Django基本
    1121 爬虫简单面条版
    1118 DOM
    1114 CSS基础
    1116 前端的练习--博客界面
    1112 前端基础之标签
    仿优酷错误
    1107 python自定义实现ORM
    cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤
    ERROR:105: Unable to locate a modulefile for 'xxx'
  • 原文地址:https://www.cnblogs.com/pyyyyyy/p/10794630.html
Copyright © 2011-2022 走看看