Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- timing:
- first submision 36m
- bug fixing 10m
- */
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int lint;
- typedef long double ldouble;
- const lint mod = 1e9 + 7;
- vector<int> p;
- int n, k;
- void genNext(int x) {
- for (int i = k - 1; i >= 0; --i, x /= k)
- p[i] = x % k;
- }
- bool check() {
- for (int i = 0; i < k; ++i) {
- int c = p[i];
- int j = 0;
- while (c != 0 && j <= k) {
- c = p[c];
- ++j;
- }
- if (c != 0)
- return false;
- }
- return true;
- }
- int powm(int x, int a) {
- if (a == 0)
- return 1;
- lint res = x;
- lint y = a;
- y /= 2;
- int i = 0;
- while (y) {
- res = (res * res)%mod;
- y /= 2;
- ++i;
- }
- i = a - pow(2, i);
- while (i > 0) {
- res = (res * x)%mod;
- --i;
- }
- return res;
- }
- int main(int, char**) {
- cin >> n >> k;
- p.resize(k);
- lint count = 0LL;
- int ntuples = pow(k, k) - 1;
- for (int i = 0; i <= ntuples; ++i) {
- genNext(i);
- if (check()) {
- ++count;
- }
- }
- cout << (count * powm(n-k,n-k))%mod << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment