codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

288B.md (1833B)


      1 Source: https://codeforces.com/problemset/problem/288/B
      2 
      3 B. Polo the Penguin and Houses
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Little penguin Polo loves his home village. The village has *n* houses, indexed by integers from 1 to *n*. Each house has a plaque containing an integer, the *i*-th house has a plaque containing integer *p**i* (1 ≤ *p**i* ≤ *n*).
     22 
     23 Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number *x*. Then he goes to the house whose number is written on the plaque of house *x* (that is, to house *p**x*), then he goes to the house whose number is written on the plaque of house *p**x* (that is, to house *p**p**x*), and so on.
     24 
     25 We know that:
     26 
     27 1. When the penguin starts walking from any house indexed from 1 to *k*, inclusive, he can walk to house number 1.
     28 2. When the penguin starts walking from any house indexed from *k* + 1 to *n*, inclusive, he definitely cannot walk to house number 1.
     29 3. When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house.
     30 
     31 You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7).
     32 
     33 Input
     34 
     35 The single line contains two space-separated integers *n* and *k* (1 ≤ *n* ≤ 1000, 1 ≤ *k* ≤ *min*(8, *n*)) — the number of the houses and the number *k* from the statement.
     36 
     37 Output
     38 
     39 In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 5 2
     47 ```
     48 
     49 Output
     50 
     51 ```
     52 54
     53 ```
     54 
     55 Input
     56 
     57 ```
     58 7 4
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 1728
     65 ```