poj 2229&wustoj 1269划分数(简单dp)

1269: 划分数

Time Limit: 1 Sec   Memory Limit: 128 MB
[ Submit][ Status][ Web Board]

Problem Description

将整数n分成m份,求划分的种数,注意每份不为空,不考虑顺序。
比如整数4的划分,1 1 2 和 1 2 1 以及2 1 1 为同一种划分。

Input

多组测试样例,每组两个整数。n和m。(6<n<=200,2<=m<=6)

Output

每组测试样例输出一行。输出一个整数表示划分的种数。

Sample Input

7 3

Sample Output

4

HINT

7的划分有{1,1,5;1,2,4;1,3,3;2,2,3}

Source

Difficulty


题目意思就不多说了,只给一个dp方程: dp[i][j]=dp[i-1][j-1]+dp[i-j][j].自己慢慢yy吧,意思就是说把n分成m份这个状态可以看作是

1.将n-1分成m-1后面还有一个1

2.将dp[n][m]里面所有的元素都减去1,就是dp[n-m][m]

递推方程为dp[i][j]=dp[i-1][j-1]+dp[i-j][j]


   题目地址:1269: 划分数


AC代码:

#include<iostream>
#include<cstring>
using namespace std;

int dp[205][205];

int main()
{
    int i,j;
    memset(dp,0,sizeof(dp));
    dp[1][1]=1;
    for(i=2;i<=200;i++)
    {
        int mi=min(i,10);
        for(j=1;j<=mi;j++)
            dp[i][j]=dp[i-1][j-1]+dp[i-j][j];
    }
    int a,b;
    while(cin>>a>>b)
        cout<<dp[a][b]<<endl;
    return 0;
}


Sumsets
Time Limit: 2000MS Memory Limit: 200000K
Total Submissions: 11506 Accepted: 4631

Description

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 

1) 1+1+1+1+1+1+1 
2) 1+1+1+1+1+2 
3) 1+1+1+2+2 
4) 1+1+1+4 
5) 1+2+2+2 
6) 1+2+4 

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000). 

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6


题目大意:将一个数化成全部是2^i的个数,将两个题目放在一起,感觉有相似之处,都是划分的问题。

解题思路:肯定是动态规划,那我们想一下它的转移方程?

如果i是个奇数,那么dp[i]划分的时候当然会多出来一个1,那么dp[i]=dp[i-1]
如果i是个偶数,那么dp[i]划分的时候会出现两种情况,把一个2拆分为1和1,或者直接是2.那么dp[i]=dp[i-2]+dp[i/2]。

题目地址:Sumsets

AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e9;
int dp[1000005];

int main()
{
    int i;
    dp[1]=1,dp[2]=2;
    for(i=3;i<=1e6;i++)
    {
        if(i&1) dp[i]=dp[i-1];
        else
            dp[i]=(dp[i-2]+dp[i/2])%maxn;
    }

    int n;
    while(cin>>n)
        cout<<dp[n]<<endl;
    return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值