以已知的“问题边界”为起点向“原问题”正向推到的扩展方式就是 递推
题意 :
思路 :
void dfs(int u, int state) {
if (u == n) {
for (int i = 0; i < n; ++ i) {
if (state >> i & 1)
cout << i + 1 << ' ';
}
cout << endl;
return ;
}
dfs(u + 1, state + (1 << u));
dfs(u + 1, state);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
dfs(0, 0);
}
题意 :
思路 :
剪枝
后时间复杂度从
2
n
2^n
2n降为
C
n
m
C^m_n
Cnm,剪枝:表示已选元素容器的vector如果size大于m或者加上剩下所有的数仍然小于m,则return剪枝int n, m;
void dfs(int u, int s, int state) {
if (s == m) {
for (int i = 0; i < n; ++ i)
if (state >> i & 1)
cout << i + 1 << ' ';
cout << endl;
return ;
}
if (u == n) return ;
for (int i = u; i < n; ++ i) {
dfs(i + 1, s + 1, state + (1 << i));
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
dfs(0, 0, 0);
}
题意 :
思路 :
int n;
vector<int> path;
void dfs(int u, int state) {
if (u == n) {
for (int i = 0; i < path.size(); ++ i)
cout << path[i] + 1 << ' ';
cout << endl;
return ;
}
for (int i = 0; i < n; ++ i) {
if (!(state >> i & 1)) {
path.push_back(i);
dfs(u + 1, state + (1 << i));
path.pop_back();
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
dfs(0, 0);
}
题意 :
思路 :
#include
#include
#define endl '\n'
#define _(a) cout << #a << ": " << (a) << " "
using namespace std;
typedef long long ll;
char g[10][10];
int dx[5] = {-1, 1, 0, 0, 0};
int dy[5] = {0, 0, 0, 1, -1};
void turn(int x, int y) {
for (int i = 0; i < 5; ++ i) {
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 5 && b >= 0 && b < 5) {
if (g[a][b] == '0') g[a][b] = '1';
else g[a][b] = '0';
}
}
}
int solve() {
int ans = 1e9;
for (int k = 0; k < (1 << 5); ++ k) {
char backup[10][10];
memcpy(backup, g, sizeof g);
int res = 0;
for (int i = 0; i < 5; ++ i) {
if (k >> i & 1) {
res ++ ;
turn(0, i);
}
}
for (int i = 0; i < 4; ++ i) {
for (int j = 0; j < 5; ++ j) {
if (g[i][j] == '0') {
res ++ ;
turn(i + 1, j);
}
}
}
for (int i = 0; i < 5; ++ i) {
if (g[4][i] == '0') {
res = 1e9;
}
}
ans = min(ans, res);
memcpy(g, backup, sizeof backup);
}
if (ans <= 6) return ans;
return -1;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int _; cin >> _;
while (_ -- ) {
for (int i = 0; i < 5; ++ i) cin >> g[i];
cout << solve() << endl;
}
}
题意 :
思路 :
#include
#include
#define endl '\n'
#define _(a) cout << #a << ": " << (a) << " "
using namespace std;
typedef long long ll;
const int N = 14;
int d[N], f[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
for (int i = 1; i <= 12; ++ i) d[i] = 2 * d[i - 1] + 1;
memset(f, 0x3f, sizeof f);
f[1] = 1;
for (int i = 1; i <= 12; ++ i) {
for (int j = 1; j < i; ++ j) {
f[i] = min(f[i], 2 * f[j] + d[i - j]);
}
cout << f[i] << endl;
}
}
题意 :
思路 :
#include
#define endl '\n'
#define _(a) cout << #a << ": " << (a) << " "
using namespace std;
typedef long long ll;
const int mod = 9901;
int qmi(int a, int b) {
a %= mod;
int res = 1 % mod;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int sum(int p, int c) {
if (!c) return 1;
if (c % 2 == 0) return ((1 + qmi(p, c / 2)) % mod * sum(p, c / 2 - 1) % mod + qmi(p, c)) % mod;
else return (1 + qmi(p, (c + 1) / 2)) % mod * sum(p, (c - 1) / 2) % mod;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int A, B;
cin >> A >> B;
int res = 1;
for (int i = 2; i <= A; ++ i) {
int cnt = 0;
while (A % i == 0) {
cnt ++ ;
A /= i;
}
if (cnt) res = (res * sum(i, B * cnt)) % mod;
}
if (!A) cout << 0;
else cout << res;
}