题目链接看这里传送门
RPG的错排
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19518 Accepted Submission(s): 7796
Problem Description
今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁。RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿,G是月野兔;第二次猜:R是草儿,P是月野兔,G是公主;第三次猜:R是草儿,P是公主,G是月野兔;…可怜的野骆驼第六次终于把RPG分清楚了。由于RPG的带动,做ACM的女生越来越多,我们的野骆驼想都知道她们,可现在有N多人,他要猜的次数可就多了,为了不为难野骆驼,女生们只要求他答对一半或以上就算过关,请问有多少组答案能使他顺利过关。
Input
输入的数据里有多个case,每个case包括一个n,代表有几个女生,(n<=25), n = 0输入结束。
Sample Input
1
2
0
Sample Output
1
1
Author
Rabbit
Source
RPG专场练习赛
题目思路:本题是一个简单的错排公式的使用,问题是在与当数据超过20的时候会爆long long,所以写组合的时候不能使用阶乘的公式。
而错排公式是
D(n)是指n个元素放在n个位子各不对应的有多少种情况。
具体操作间下面代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 30;
const int inf = 0x3f3f3f3f;
const int mod = 10000;
typedef long long ll;
ll s[maxn],n;
ll unit(ll x)
{
if(x==1)return s[1]=0;
if(x==2)return s[2]=1;
return s[x]=(x-1)*(unit(x-1)+unit(x-2));
}
long long c(int n,int m)
{
int i;
if(m<(n-m))m=n-m;
long long ans=1;
for(i=m+1; i<=n; i++)ans*=i;
for(i=1; i<=n-m; i++)ans/=i;
return ans;
}
int main()
{
unit(13);
while(~scanf("%lld",&n)){
if(n==0)break;
if(n==1){
printf("1\n");
continue;
}
if(n==2){
printf("1\n");
continue;
}
ll ans=1;
for(ll i=1;i<=n/2;i++){
ans+=(s[i]*c(n,i));
}
printf("%lld\n",ans);
}
return 0;
}