Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define taskname "DAYCON"
- const int N = 25;
- int n, m, cnt = 0;
- int a[N], dp[N][N];
- void print() {
- ++cnt;
- /*cout << cnt << ": ";
- for (int i = 1; i <= n; i++) {
- cout << a[i];
- }
- cout << '\n';*/
- }
- void brute(int i) {
- for (int j = 1; j <= m; j++) {
- // kiem tra
- bool flag = false;
- for (int k = i - 1; k >= 1; k--) {
- if (a[k] == j) {
- int t = dp[k - 1][i - 1] + 1;
- if (t >= i - k) {
- flag = true;
- break;
- }
- }
- }
- if (flag) continue;
- // them
- for (int k = i - 1; k >= 1; k--) {
- if (a[k] == j) {
- int t = dp[k - 1][i - 1] + 1;
- dp[k][i] = t;
- }
- }
- a[i] = j;
- if (i == n) print();
- else brute(i + 1);
- // xoa
- for (int k = i - 1; k >= 1; k--) {
- if (a[k] == j) {
- dp[k][i] = 0;
- }
- }
- a[i] = 0;
- }
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- if (fopen(taskname".INP", "r")) {
- freopen(taskname".INP", "r", stdin);
- freopen(taskname".OUT", "w", stdout);
- }
- cin >> n >> m;
- brute(1);
- cout << cnt << '\n';
- }
Add Comment
Please, Sign In to add comment