String of yuusaan
我们可以打表,我们会发现字符串无论重复多少次都会遵循这个规律
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a, b;
signed main() {
cin >> a >> b;
b--;
int x = b % 12;
if (x == 0) {
cout << "y";
}
else if (x == 1 || x == 2 || x == 7 || x == 8) {
cout << "u";
}
else if (x == 3 || x == 9) {
cout << "s";
}
else if (x == 6) {
cout << "n";
}
else cout << "a";
return 0;
}
NOT FOUND 404 Again
显然我们会发现这是一个数位 \(dp\) 那么我们就可以设计 \(dp_{i, 0/1/2, 0/1/2}\) 表示选到第 \(i\) 位,\(0/1/2\) 表示是否为 \(0\),是否为 \(4\),还是两者都不满足
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, mod = 998244353;
int n, dp[N][3][3][3], x[N];
string s;
int val(int e) {
if (e == 0) {
return 0;
}
else if (e == 4) {
return 1;
}
return 2;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
s = " " + s;
for (int i = 1; i <= n; i++) {
x[i] = (int)(s[i] - '0');
}
//0表示 = 0,1 表示 = 4, 2表示等于其他数
dp[0][0][0][1] = 1;
for (int i = 1; i <= n; i++) {
for (int a = 0; a <= 2; a++) {
for (int b = 0; b <= 2; b++) {
for (int c = 0; c <= 9; c++) {
if (c == 4 && b == 0 && a == 1) {
continue;
}
if (c < x[i]) {
dp[i][val(c)][b][0] = (dp[i][val(c)][b][0] + dp[i - 1][b][a][1]) % mod;
dp[i][val(c)][b][0] = (dp[i][val(c)][b][0] + dp[i - 1][b][a][0]) % mod;
}
else if (c == x[i]) {
dp[i][val(c)][b][1] = (dp[i][val(c)][b][1] + dp[i - 1][b][a][1]) % mod;
dp[i][val(c)][b][0] = (dp[i][val(c)][b][0] + dp[i - 1][b][a][0]) % mod;
}
else {
dp[i][val(c)][b][0] = (dp[i][val(c)][b][0] + dp[i - 1][b][a][0]) % mod;
}
}
}
}
}
int ans = 0;
for (int a = 0; a <= 2; a++) {
for (int b = 0; b <= 2; b++) {
ans += (dp[n][a][b][0] + dp[n][a][b][1]) % mod;
ans %= mod;
}
}
cout << ans - 1;
return 0;
}
yuusaan's Knapsacks
我们可以发现每个背包选的一定都是物品的一个子集,那么我们就可以枚举子集,然后状压,由于枚举子集的时间复杂度是 \(3^n\) 的,所以不会超时
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 17, M = (1 << 16), INF = 1e18;
int n, m, a[N], v[N], w[N], b[M], sumv[M], sumw[M], dp[N][M], pre[N][M];
inline int lowbit(int x) {
return x & (-x);
}
void print(int x, int y) {
if (!x) {
return ;
}
print(x - 1, y - pre[x][y]);
vector<int> ans;
for (int i = pre[x][y]; i; i -= lowbit(i)) {
ans.push_back(b[lowbit(i)]);
}
cout << ans.size() << " ";
for (auto cur : ans) {
cout << cur + 1 << " ";
}
cout << "\n";
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> v[i] >> w[i];
b[(1 << i)] = i;
}
for (int i = 1; i < (1 << m); i++) {
sumv[i] = sumv[i - lowbit(i)] + v[b[lowbit(i)]];
sumw[i] = sumw[i - lowbit(i)] + w[b[lowbit(i)]];
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < (1 << m); j++) {
dp[i][j] = -INF;
}
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
dp[i][0] = 0;
for (int j = 1; j < (1 << m); j++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
for (int k = j; k; k = (k - 1) & j) {
if (sumw[k] <= a[i] && dp[i - 1][j - k] + sumv[k] > dp[i][j]) {
dp[i][j] = dp[i - 1][j - k] + sumv[k];
pre[i][j] = k;
}
}
}
}
int maxi = -INF, p;
for (int i = 0; i < (1 << m); i++) {
if (maxi < dp[n][i]) {
maxi = dp[n][i];
p = i;
}
}
cout << maxi << "\n";
print(n, p);
return 0;
}
标签:20240912,namespace,cin,long,int,include,dp
From: https://www.cnblogs.com/libohan/p/18440735