poj 3088 Push Botton Lock (dp+组合数学|斯特林数)

Push Botton Lock
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1526 Accepted: 967

Description

The Frobozz Magic Lock Company is in the business of manufacturing push button style combination door locks. A push button door lock consists of a number of push buttons B, (1 ≤ B ≤ 11), labeled “1” through “B”. The lock is opened by pressing the correct sequence of button combinations and then turning the doorknob. If the sequence of presses is correct, the door magically opens.

A combination consists of 1 or more buttons being pressed simultaneously. A sequence consists of a series of combinations. A sequence must have at least one combination. Once a button has been used in a combination, it may not be used again in the same sequence. In addition, it is not necessary to use all the buttons in a sequence. For example, for B = 8:

(1-2-3)(4)(7-8)

is a valid sequence with 3 combinations (1-2-3), (4), and (7-8). Note that buttons 5 and 6 are not used in this sequence.

(1-2-3)(2-4)(5-6)

is not a valid sequence, since button 2 appears in 2 combinations (1-2-3) and (2-4).

The CEO of Frobozz, J. Pierpont Flathead, wants you to write a program that determines the number of valid sequences possible for given values of B. The program must be able to process a list of lock orders (datasets) from customers and generate a report showing the order number, the value of B, and the number of valid sequences possible. This list will always contain at least one dataset, but no more than 100 datasets.

Reference Materials:


J. Pierpont Flathead

Input

The first line of input contains a single integer N, (1 ≤ N ≤ 100), representing the number of datasets that follow. Each dataset consists of a single line of data containing a single integer B, which is the number of buttons for the lock.

Output

For each dataset, display the dataset number, a blank, the value B, a blank, and the number of valid sequences.

Sample Input

3
3
4
3

Sample Output

1 3 25
2 4 149
3 3 25

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题解:dp+组合数学|斯特林数

f[i][1]=c[i][n] c表示组合数

f[i][i]=a[i][n] a表示排列数

f[i][j]=sigma f[i-k][j-1]*c[k][n-(i-k)]  我们考虑最后一个盒子放几个和放哪些。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 103
#define LL long long
using namespace std;
LL f[N][N];
int n;
LL calc(int x,int y)
{
	LL ans=1;
	for (int i=y;i>=y-x+1;i--)
	 ans*=(LL)i;
	return (LL)ans;
}
LL c(int x,int y)
{
	LL ans=1;
	for (int i=y;i>=y-x+1;i--)
	 ans*=(LL)i;
	for (int i=1;i<=x;i++) ans/=(LL)i;
	return ans;
}
int main()
{
   int t;
   scanf("%d",&t);
   for (int T=1;T<=t;T++){
   	  scanf("%d",&n);
   	  memset(f,0,sizeof(f));
   	  LL ans=0;
   	  f[1][1]=calc(1,n); ans+=f[1][1];
   	  for (int i=2;i<=n;i++)
   	  {
   	   f[i][1]=c(i,n); ans+=f[i][1];
   	   for (int j=2;j<=i-1;j++)
   	   {
   	     for (int k=1;k<=n;k++)
   	      	{
			 if (i-k==0) break;
   	      	 f[i][j]+=f[i-k][j-1]*c(k,n-(i-k)); 
   	        }
   	     ans+=f[i][j];
   	   }
   	   f[i][i]=calc(i,n),ans+=f[i][i];
      }
      printf("%d %d %lld\n",T,n,ans);
   }
}

斯特林数:斯特林数出现在许多组合枚举问题中. 对第一类斯特林数 StirlingS1[n,m], 给出恰包含 m 个圈的 n 个元素 的排列数目. 斯特林数满足母函数关系 . 注意某些 的定义与 Mathematica 中的不同,差别在于因子 . 第二类斯特林数 StirlingS2[n,m]给出把 n 个可区分小球分配到m个不可区分的的盒子,且盒子没有空盒子的方法的数量. 它们满足关系 . 划分函数 PartitionsP[n]给出把整数 n 写为正整数的和,不考虑顺序的方法的数目. PartitionsQ[n]给出把整数 n 写为正整数的和,并且和中的整数是互不相同的 写法的数目
  设S(p,k)是斯特林数
  S(p,k)的一个组合学解释是:将p个物体划分成k个非空的不可辨别的(可以理解为盒子没有编号)集合的方法数。
  S(p,k)的递推公式是:
   S(p,k) = k*S(p-1,k) + S(p-1,k-1) ,1<= k <=p-1
  边界条件:
  S(p,p) = 1 ,p>=0
  S(p,0) = 0 ,p>=1
  递推关系的说明:考虑第p个物品,p可以单独构成一个非空集合,此时前p-1个物品构成k-1个非空的不可辨别的集合,方法数为S(p-1,k-1);也可以前p-1种物品构成k个非空的不可辨别的集合,第p个物品放入任意一个中,这样有k*S(p-1,k)种方法。

从ATP神犇那里get到一个新的想法:

f[i][j]表示将i个不同的小球放到j个不同的盒子的方案数。

f[i][j]=f[i-1][j]*j+f[i-1][j-1]*(j+1)  f[i-1][j]*j表示把第i个小球放到已经分好的j个盒子中的任意一个中;f[i-1][j-1]*(j+1)表示第i个小球单独放在一个盒子中,这个盒子有(j+1)个不同的位置可以插入。

统计答案的时候ans+=f[i][j]*c(i,n)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值