光棍节的快乐
Time Limit:1000MS Memory Limit:65536K
Total Submit:125 Accepted:32
Description
光棍们,今天是光棍节。聪明的NS想到了一个活动来丰富这个光棍节。
规则如下:
每个光棍在一个纸条上写一个自己心仪女生的名字,然后把这些纸条装进一个盒子里,这些光 棍依次抽取一张纸条,如果上面的名字就是自己心仪的女生,那么主持人就在现场给该女生打电话,告诉这个光棍对她的爱慕之情,并让光棍当场表白,并得到现场所有人的祝福,没抽到的,嘿嘿就可以幸免了。
假设一共有N个光棍,其中有M个没有抽到自己的纸条,求发生这种情况一共有多少种可能.。
Input
每行包含两个整数N和M(1< M< = N< =20),以EOF结尾。
Output
对于每个测试实例,请输出一共有多少种发生这种情况的可能,每个实例的输出占一行。
Sample Input
2 2
3 2
Sample Output
1
3
Hint
原来的测试数据有问题,非常抱歉呀,现在改正了,应该是错位排列
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1149 {
class Program {
static long f(int n) {
if (n < 2)
return 1;
return f(n - 1) * n;
}
static void Main(string[] args) {
string st;
while ((st = Console.ReadLine()) != null) {
string[] ss = st.Split();
int n = int.Parse(ss[0]) , m = int.Parse(ss[1]);
long s = 0 , a = 1;
for (int i = 2 ; i < m + 1 ; i++) {
s += a * f(n) / f(i);
a *= -1;
}
Console.WriteLine(s);
}
}
}
}