tuki2501

DAYCON.CPP

Nov 18th, 2021 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define taskname "DAYCON"
  5.  
  6. const int N = 25;
  7.  
  8. int n, m, cnt = 0;
  9. int a[N], dp[N][N];
  10.  
  11. void print() {
  12.   ++cnt;
  13.   /*cout << cnt << ": ";
  14.   for (int i = 1; i <= n; i++) {
  15.     cout << a[i];
  16.   }
  17.   cout << '\n';*/
  18. }
  19.  
  20. void brute(int i) {
  21.   for (int j = 1; j <= m; j++) {
  22.     // kiem tra
  23.     bool flag = false;
  24.     for (int k = i - 1; k >= 1; k--) {
  25.       if (a[k] == j) {
  26.         int t = dp[k - 1][i - 1] + 1;
  27.         if (t >= i - k) {
  28.           flag = true;
  29.           break;
  30.         }
  31.       }
  32.     }
  33.     if (flag) continue;
  34.  
  35.     // them
  36.     for (int k = i - 1; k >= 1; k--) {
  37.       if (a[k] == j) {
  38.         int t = dp[k - 1][i - 1] + 1;
  39.         dp[k][i] = t;
  40.       }
  41.     }
  42.     a[i] = j;
  43.  
  44.     if (i == n) print();
  45.     else brute(i + 1);
  46.  
  47.     // xoa
  48.     for (int k = i - 1; k >= 1; k--) {
  49.       if (a[k] == j) {
  50.         dp[k][i] = 0;
  51.       }
  52.     }
  53.     a[i] = 0;
  54.   }
  55. }
  56.  
  57. int main() {
  58.   cin.tie(0)->sync_with_stdio(0);
  59.   if (fopen(taskname".INP", "r")) {
  60.     freopen(taskname".INP", "r", stdin);
  61.     freopen(taskname".OUT", "w", stdout);
  62.   }
  63.   cin >> n >> m;
  64.   brute(1);
  65.   cout << cnt << '\n';
  66. }
Add Comment
Please, Sign In to add comment