51nod 1201 整数划分

1201 整数划分
基准时间限制:1 秒 空间限制:131072 KB
将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} {1,5} {2,4} {1,2,3},共4种。由于数据较大,输出Mod 10^9 + 7的结果即可。
 
Input
输入1个数N(1 <= N <= 50000)。
Output
输出划分的数量Mod 10^9 + 7。
Input示例
6
Output示例
4
 这题有点巧妙:
思考一个数的方案。
比如说7:{1,2,4},{1,3,3};
     {2,5},{2,2,3},{3,4},{7}。
上面的把1拿走在减一{1,3},{2,2};
后面的减一是{1,4},{1,1,2},{2,5},{6}。
感觉没什么东西,我们在按分成几个数分类:
就有:

dp[i][j]表示i这个数划分成j个数的情况数。

dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 int dp[50005][505],ans;
38 int main(){
39     int n=read();
40     dp[0][0]=1;
41     for (int i=1;i<=n;i++){
42         for (int j=1;j<=min(i,500);j++){
43             dp[i][j]=(dp[i-j][j]+dp[i-j][j-1])%pp;
44             if (i==n) ans=(ans+dp[i][j])%pp;
45         }
46     }
47     printf("%d",ans);
48     return 0;
49 } 
View Code
原文地址:https://www.cnblogs.com/SXia/p/7575797.html