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).
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
解题思路:肯定是动态规划,那我们想一下它的转移方程?
如果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;
}