Guest User

Untitled

a guest
Feb 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. /*
  2. timing:
  3. first submision 36m
  4. bug fixing 10m
  5. */
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. typedef long long int lint;
  9. typedef long double ldouble;
  10.  
  11. const lint mod = 1e9 + 7;
  12.  
  13. vector<int> p;
  14. int n, k;
  15.  
  16. void genNext(int x) {
  17. for (int i = k - 1; i >= 0; --i, x /= k)
  18. p[i] = x % k;
  19. }
  20.  
  21. bool check() {
  22. for (int i = 0; i < k; ++i) {
  23. int c = p[i];
  24. int j = 0;
  25. while (c != 0 && j <= k) {
  26. c = p[c];
  27. ++j;
  28. }
  29. if (c != 0)
  30. return false;
  31. }
  32. return true;
  33. }
  34.  
  35. int powm(int x, int a) {
  36. if (a == 0)
  37. return 1;
  38.  
  39. lint res = x;
  40. lint y = a;
  41. y /= 2;
  42. int i = 0;
  43. while (y) {
  44. res = (res * res)%mod;
  45. y /= 2;
  46. ++i;
  47. }
  48. i = a - pow(2, i);
  49. while (i > 0) {
  50. res = (res * x)%mod;
  51. --i;
  52. }
  53. return res;
  54. }
  55.  
  56. int main(int, char**) {
  57. cin >> n >> k;
  58. p.resize(k);
  59.  
  60. lint count = 0LL;
  61. int ntuples = pow(k, k) - 1;
  62. for (int i = 0; i <= ntuples; ++i) {
  63. genNext(i);
  64. if (check()) {
  65. ++count;
  66. }
  67. }
  68.  
  69. cout << (count * powm(n-k,n-k))%mod << endl;
  70.  
  71. return 0;
  72. }
Add Comment
Please, Sign In to add comment