试了好多个终于搞定了
①[1,n](n >= 1) 1
②[2,1] 1
[2,2] 2
[2,n](n >= 2) 2
③[3,1] 1
[3,2]
{
[3,1] {(1,2)};
} 2
[3,3]
{
[3,2] {(1,1,1)}
} 3
[3,n](n >= 3) 3
···
我一直试到第7个···,发现
[i,j] = [i][j - 1] + [i - j][j]( j > 2 && j < i - 2);
j == 1,[i][j] = 1;
j == 2,[i][j] = i / 2 + 1;
j > i - 1 && j <= i,[i][j] = [i][j - 1] + 1;
j > i,[i][j] = [i][j - 1];
```
```
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define x first
#define y second
using namespace std;
const int N = 100;
int f[N][N];
void init()
{
memset(f,0,sizeof(f));
f[1][1] = 1;f[2][1] = 1;f[2][2] = 2;
for(int i = 1;i <= 10;i ++)
for(int j = 1;j <= 10;j ++)
{
if(j == 1)
f[i][j ] = 1;
else if(j == 2)
f[i][j] = i / 2 + 1;
else if(j < i - 1)
f[i][j] = f[i][j - 1] + f[i - j][j];
else if(j <= i)
f[i][j] = f[i][j - 1] + 1;
else
f[i][j] = f[i][j - 1];
}
/*for(int i = 1;i <= 10;i ++)
{
for(int j = 1;j <= 10;j ++)
cout<<f[i][j]<<" ";
cout<<endl;
}*/
}
int main()
{
int t;
scanf("%d",&t);
init();
while(t --)
{
int m,n;
scanf("%d%d",&m,&n);
cout<<f[m][n]<<endl;
}
return 0;
}
暴力搜索
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<malloc.h>
#include<climits>
#include<map>
#define LL long long
#define x first
#define y second
using namespace std;
const int N = 500;
int num[N];
bool st[N],flag[20][N];
int n,m;
int ans;
bool check()//判断当前类别是否枚举过
{
for(int i = 1;i <= m;i ++)
if(!flag[i][num[i]])
return true;
return false;
}
void dfs(int u,int v,int k)
{
if(v == m)
{
ans ++;//计数
//cout<<ans<<endl;
for(int i = 1;i < m;i ++)//标记当前种类
/*cout<<num[i]<<" ",*/flag[i][num[i]] = true;
num[m] = k/*,cout<<k<<endl*/;
flag[m][k] = true;
return;
}
for(int i = u;i <= (k - i)/(m - v);i ++)
{
num[v] = i;
if(num[v] >= num[v - 1])//按照递增顺序避免重复
dfs(u,v + 1,k - i);//第v + 1位为u时
if(!check())//看是否枚举过
continue;
if(num[v] >= num[v - 1])//按照递增顺序
dfs(u + 1,v,k);//第v位为u + 1时
}
}
int main()
{
int t;
scanf("%d",&t);
while(t --)
{
scanf("%d%d",&n,&m);
memset(flag,false,sizeof(flag));
ans = 0;
dfs(0,1,n);
cout<<ans<<endl;
}
return 0;
}