Pro.ID22042 TitleBrain Teasers Title链接http://10.20.2.8/oj/exercise/problem?problem_id=22042 AC2 Submit8 Ratio25.00% 时间&空间限制描述Alice and Bob love playing brain teasers with each other. One day, they decided to play brain teasers again. Then Alice asked Bob: "I have n different boxes and n different keys, each key can only open one unique box. Now all the keys are put into the n boxes and each box contains exactly one key. I will tell you which box contains which key, and which key can open which box, and you are allowed to open some boxes by violence. Then can you tell me what the minimum number of boxes that should be opened by violence is, so that you’re able to open every box?" And Bob answered: "Good question, but obviously is’s not hard for me. I can solve it in this way …." Then Alice say: "Hmm, yes, it’s not hard enough. So let me make some modifications: let’s call it n-key permutation to make each of the n boxes contain exactly one key. And you are only allowed to open at most k boxes by violence. Then how many n-key permutations are there, so that after I tell you the information about the key permutation and the correspondence between keys and boxes, you are able to open all the boxes?" "For example, suppose n=3, k=1, and we number the keys and the boxes from 1 to n respectively, so that key i can open box i , then permutation (2, 3, 1) denotes that box1 contains key2, box2 contains key3, and box3 contains key1. As you are allowed to open 1 box by violence, you can open box1 by violence, get key2, and open box2, get key3, and finally open box3. So under key permutation (2, 3, 1) you are able to open all the boxes with at most one chance of using violence. What's more, only permutation (3, 1, 2) is also satisfiable, so when n=3, k=1, only under 2 different permutations, you are able to open all the boxes." "Well, this problem is indeed not so easy. Maybe I should ask somebody to help me". Bob said. So he decide to turn to you, his good friend and a computer expert, to help him find out the answer. Could you help him please? 输入Input may contain multiple test cases. For each test case, there is one line, containing two integer, n and k, ( 1 ≤ n ≤ 100, 0 ≤ k ≤ n ). Input is terminated by a line containing two 0s, which should not be processed. 输出Description Alice and Bob love playing brain teasers with each other. One day, they decided to play brain teasers again. Then Alice asked Bob: "I have n different boxes and n different keys, each key can only open one unique box. Now all the keys are put into the n boxes and each box contains exactly one key. I will tell you which box contains which key, and which key can open which box, and you are allowed to open some boxes by violence. Then can you tell me what the minimum number of boxes that should be opened by violence is, so that you’re able to open every box?" And Bob answered: "Good question, but obviously is’s not hard for me. I can solve it in this way …." Then Alice say: "Hmm, yes, it’s not hard enough. So let me make some modifications: let’s call it n-key permutation to make each of the n boxes contain exactly one key. And you are only allowed to open at most k boxes by violence. Then how many n-key permutations are there, so that after I tell you the information about the key permutation and the correspondence between keys and boxes, you are able to open all the boxes?" "For example, suppose n=3, k=1, and we number the keys and the boxes from 1 to n respectively, so that key i can open box i , then permutation (2, 3, 1) denotes that box1 contains key2, box2 contains key3, and box3 contains key1. As you are allowed to open 1 box by violence, you can open box1 by violence, get key2, and open box2, get key3, and finally open box3. So under key permutation (2, 3, 1) you are able to open all the boxes with at most one chance of using violence. What's more, only permutation (3, 1, 2) is also satisfiable, so when n=3, k=1, only under 2 different permutations, you are able to open all the boxes." "Well, this problem is indeed not so easy. Maybe I should ask somebody to help me". Bob said. So he decide to turn to you, his good friend and a computer expert, to help him find out the answer. Could you help him please? Input Input may contain multiple test cases. For each test case, there is one line, containing two integer, n and k, ( 1 ≤ n ≤ 100, 0 ≤ k ≤ n ). Input is terminated by a line containing two 0s, which should not be processed. Output For each test case, just output the number of different permutation satisfying the requirement. Each test case should correspond to one line. No more bland is allowed. Sample Input 3 1 Sample Output 2 Source 样例输入3 1 样例输出2 作者 |