NOIP2001提高组复赛B 数的划分

题目链接:https://ac.nowcoder.com/acm/contest/249/B

题目大意:

  略

分析1(记忆化搜索):

  方法为减而治之,把n划分成k份的答案就相当于每次把n分成a,b两个数,再把a分成k-1份,然后把每次a分成k-1份的答案相加即可。注意点是每轮分出来的b要不大于上一轮分出来的b。

代码如下:

 #include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
const int maxN = 1e5 + ; int n, k;
// f[i][j][k]表示数i分成j分的分法总数,k为限制条件,每种分法每份的值不能超过k,用来排除重复
// f[i][j][k] = f[i-1][j-1][1] + f[i-2][j-1][2] + ……+ f[i-min(k, i-1)][j-1][min(k, i-1)]
int f[][][]; int solve(int x, int y, int z){
int ret = ;
if(x < y) return ;
if(y == ) return x <= z ? : ;
if(f[x][y][z]) return f[x][y][z]; For(i, , x-) {
if(x-i > z) continue;
ret += solve(i, y-, x-i);
}
f[x][y][z] = ret;
return ret;
} int main(){
scanf("%d%d", &n, &k);
printf("%d\n", solve(n, k, ));
return ;
}

分析2(DP):

  见代码内注释

代码如下:

 #include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
const int maxN = 1e5 + ; int n, k;
// f[i][j]表示数i分成j份的分法总数
/*
当i < j时,很明显没法分,所以f[i][j] = 0;
当i == j时,只有一种分法,所以f[i][j] = 1;
当i > j时,考虑从小到大分,第1个如果分1,那么f[i][j] = f[i-1][j-1];
第1个如果分大于1的数,可以对所有j份都减一,然后再分,即 f[i][j] = f[i-j][j];
根据加法原则,f[i][j] = f[i-1][j-1] + f[i-j][j];
*/
int f[][]; int main(){
scanf("%d%d", &n, &k);
For(i, , n) f[i][] = ; // 无论什么数,分成一份都只有一种
For(i, , k)
For(j, , n)
if(j >= i) f[j][i] = f[j-][i-] + f[j-i][i]; printf("%d\n", f[n][k]);
return ;
}
上一篇:Hello World 之 控制台版本(Console Application)


下一篇:C# 基础控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印