poj1354(组合数学)

Placement of Keys
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1608 Accepted: 848

Description

Assume that there are n (3<=n<=200) boxes identified by A1, A2,..., An , and each box Ai is configured a lock which is different from the others. Now put n keys to the n locks into the n boxes, each box can only hold a key. After locking all the boxes, unclench the boxes named A1, A2 and fetch out the keys in them to unlock the locked boxes. If the two keys can open some box, fetch out the key in the box to unlock other locked boxes again. If we can open all the boxes finally, we call the placement of the n keys good placement. How many are there different good placements of the n keys?

Input

The input file, ending with -1, contains several data, each of which holds a line.

Output

According to every input datum, compute the number of different good placements. Each output data hold two lines, the first line is held by the input datum, followed by a colon, which follows an equal mark before which is an N; the second is held by the number of different good placement of the n keys.

Sample Input

6
8
-1

Sample Output

N=6:
240
N=8:
10080

/*
题意:有N个箱子,每个箱子里面都有一把钥匙,对应于N个箱子中的一个,现在撬开第一个和第二个,取得里面的钥匙,
问能打开所有箱子的钥匙放法有多少 
思路:有两种大的放法:1.所有钥匙形成一个环,以一个钥匙(1或2)为起点可以到达所有钥匙,所有钥匙环排列,方案数有(n-1)!
种。2. 以1和2为起点分别形成两个环,这样,先把其余n-2个节点全排列,然后将钥匙1加到最前面,钥匙2的方法有n-1种,
 序列就形成了以1和2为起点的两个环,有(n-1)*(n-2)!种。总共2*(n-1)!种。
 大数 
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long LL;
#define MAX 100
#define BASE 10000
int a[205][MAX];
void multiply(int a[],int Max,int b)  //大数乘法
{
	int array=0;
	for(int i=Max-1;i>=0;i--)
	{
		array+=b*a[i];
		a[i]=array%BASE;
		array/=BASE;
	}
}
void init()  
{
	memset(a,0,sizeof(a));
	a[2][MAX-1]=2;
	for(int i=3;i<205;i++)
	{
		memcpy(a[i],a[i-1],MAX*sizeof(int));
		multiply(a[i],MAX,i-1);
	}
}

int main()
{
	init();
	int n;
	while(~scanf("%d",&n)&&n!=-1)
	{
		printf("N=%d:\n",n);
		int i=0;
		for(;i<MAX;i++)
		{
			if(a[n][i]!=0)
			{
				printf("%d",a[n][i++]);
				break;
			}
		}
		for(;i<MAX;i++)printf("%04d",a[n][i]);
		printf("\n");
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值