#include <iostream>
#include <math.h>
#pragma GCC optimize("O3")
using namespace std;
int cnt = 0;
void generate(int max, int x, int* c, int len) {
if (x == 0) {
if(c[0] == c[max-1]) {
++cnt;
return;
} else {
for(auto i=1; i<max; ++i) {
if(c[0] == c[i]) {
for(auto j=max-2; j>0; --j) {
//if(j<i) return;
if(j-i==1 && c[max-1] == c[j]) {
++cnt;
return;
}
}
}
}
}
return;
}
for (int j = 0; j < len; ++j) {
c[max-x] = j;
generate(max, x - 1, c, len);
}
}
int main() {
int n, m;
cin >> n >> m;
if(m != 1) {
int c[n];
generate(n, n, c, m);
auto x = cnt % (10000000000 + 7);
cout << x;
} else {
cout << 1;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> #include <math.h> #pragma GCC optimize("O3") using namespace std; int cnt = 0; void generate(int max, int x, int* c, int len) { if (x == 0) { if(c[0] == c[max-1]) { ++cnt; return; } else { for(auto i=1; i<max; ++i) { if(c[0] == c[i]) { for(auto j=max-2; j>0; --j) { //if(j<i) return; if(j-i==1 && c[max-1] == c[j]) { ++cnt; return; } } } } } return; } for (int j = 0; j < len; ++j) { c[max-x] = j; generate(max, x - 1, c, len); } } int main() { int n, m; cin >> n >> m; if(m != 1) { int c[n]; generate(n, n, c, m); auto x = cnt % (10000000000 + 7); cout << x; } else { cout << 1; } return 0; } |
English