今年暑假杭電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輸入結束。
Output
1 1
Sample Input
1 2 0
Sample Output
1 1
題意:
問 1 ~ n 的排列中錯排一半以內的排列數
思路:
n 個數中最多有 n / 2個數可以錯排(題目規定),枚舉 i ~ [1,n / 2 ],從原有的 n 個數中選取 i 個數:,再乘以這 i 個數產生錯排的個數
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int N = 27;
const int mod = 998244353;
ll ans[N], a[N], com[N][N];
void init()
{
for(int i = 1; i < N; ++i)
{
com[i][0] = com[i][i] = 1;
for(int j = 1; j < i; ++j)
{
com[i][j] = com[i - 1][j] + com[i - 1][j - 1];
}
}
a[0] = 1;
a[1] = 0;
for(int i = 2; i < 15; ++i)
{
a[i] = (i - 1) * (a[i - 1] + a[i - 2]);
}
}
int main()
{
init();
int n;
while(~scanf("%d", &n) && n)
{
int tmp = n / 2;
ll ans = 0;
for(int i = 0; i <= tmp; ++i)
{
ans += a[i] * com[n][i];
}
cout<<ans<<'\n';
}
return 0;
}