Advertisement
coderchesser

construct

Jan 27th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int mod = 1000000007;
  5.  
  6. int power(int base, int exponent) {
  7.     int result = 1;
  8.     while (exponent > 0) {
  9.         if (exponent % 2 == 1) {
  10.             result = (result * 1LL * base) % mod;
  11.         }
  12.         base = (base * 1LL * base) % mod;
  13.         exponent /= 2;
  14.     }
  15.     return result;
  16. }
  17.  
  18. int main() {
  19.     int N, M, K;
  20.     cin >> N >> M >> K;
  21.    
  22.     if (K == 0) {
  23.         long long result = (1LL * power(N, M)) % mod;
  24.         cout << result << endl;
  25.     } else {
  26.         if (K == N) {
  27.             if (N <= 500) {
  28.                 vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
  29.                 vector<vector<bool>> blocked(N + 1, vector<bool>(N + 1, false));
  30.  
  31.                 for (int i = 0; i < K; ++i) {
  32.                     int a, b;
  33.                     cin >> a >> b;
  34.                     blocked[a][b] = true;
  35.                 }
  36.  
  37.                 for (int i = 1; i <= N; ++i) {
  38.                     dp[i][1] = 1;
  39.                 }
  40.  
  41.                 for (int step = 2; step <= M; ++step) {
  42.                     for (int curShop = 1; curShop <= N; ++curShop) {
  43.                         for (int prevShop = 1; prevShop <= N; ++prevShop) {
  44.                             if (!blocked[prevShop][curShop]) {
  45.                                 dp[curShop][step] = (dp[curShop][step] + dp[prevShop][step - 1]) % mod;
  46.                             }
  47.                         }
  48.                     }
  49.                 }
  50.  
  51.                 int result = 0;
  52.                 for (int i = 1; i <= N; ++i) {
  53.                     result = (result + dp[i][M]) % mod;
  54.                 }
  55.  
  56.                 cout << result << endl;
  57.             }
  58.             else{
  59.                 long long result = (1LL * N * power(N - 1, M - 1)) % mod;
  60.                 cout << result << endl;
  61.         }}
  62.         else{
  63.             vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
  64.             vector<vector<bool>> blocked(N + 1, vector<bool>(N + 1, false));
  65.  
  66.             for (int i = 0; i < K; ++i) {
  67.                 int a, b;
  68.                 cin >> a >> b;
  69.                 blocked[a][b] = true;
  70.             }
  71.  
  72.             for (int i = 1; i <= N; ++i) {
  73.                 dp[i][1] = 1;
  74.             }
  75.  
  76.             for (int step = 2; step <= M; ++step) {
  77.                 for (int curShop = 1; curShop <= N; ++curShop) {
  78.                     for (int prevShop = 1; prevShop <= N; ++prevShop) {
  79.                         if (!blocked[prevShop][curShop]) {
  80.                             dp[curShop][step] = (dp[curShop][step] + dp[prevShop][step - 1]) % mod;
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             int result = 0;
  87.             for (int i = 1; i <= N; ++i) {
  88.                 result = (result + dp[i][M]) % mod;
  89.             }
  90.  
  91.             cout << result << endl;
  92.         }
  93.     }
  94.     return 0;
  95. }
  96.  
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement