A和B做法和官方题解一样
C题我是用背包+map,先把任务按最早开始的时间进行排序,然后去背包,dp[j]表示j时间能得到最大的得分,然后就过了。。
代码:
A:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int n, b; char str[205]; int ans[205]; int get(char c) { if (c >= '0' && c <= '9') return c - '0'; return c - 'a' + 10; } void print(int c) { if (c >= 0 && c <= 9) printf("%d", c); else printf("%c", c + 'a' - 10); } int main() { while (~scanf("%d%d", &n, &b)) { memset(ans, 0, sizeof(ans)); while (n--) { scanf("%s", str); int len = strlen(str); for (int i = len - 1; i >= 0; i--) { ans[len - i - 1] += get(str[i]); } } for (int i = 0; i < 204; i++) { ans[i] %= b; } int i; for (i = 204; i >= 0; i--) { if (ans[i]) break; } if (i == -1) i++; for (int j = i; j >= 0; j--) print(ans[j]); printf("\n"); } return 0; }
B:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; ll n, p; ll muti(ll a, ll n, ll p) { ll r = 0; while(n){ if(n&1){ r += a; if(r >= p) r -= p; } n >>= 1; a += a; if(a >= p) a -= p; } return r; } ll pow_mod(ll x, ll k) { ll ans = 1; x %= p; while (k) { if (k&1) ans = muti(ans, x, p); x = muti(x, x, p); k >>= 1; } return ans; } int main() { while (~scanf("%I64d%I64d", &n, &p)) { if (n == 1) printf("%I64d\n", n % p); else printf("%I64d\n", ((pow_mod(2LL, n) - 2) % p + p) % p); } return 0; }
C:
#include <cstdio> #include <cstring> #include <map> #include <algorithm> using namespace std; typedef long long ll; const int N = 35; int n; ll w; struct Q { int t, v, l, s; void read() { scanf("%d%d%d", &t, &v, &l); s = max(0, l - t); } } q[N]; map<int, ll> dp[2]; map<int, ll>::iterator it; bool cmp(Q a, Q b) { if (a.s == b.s) return a.t < b.t; return a.s < b.s; } int main() { while (~scanf("%d%I64d", &n, &w)) { ll sum = 0; for (int i = 0; i < n; i++) { q[i].read(); sum += q[i].v; } if (sum < w) { printf("zhx is naive!\n"); continue; } sort(q, q + n, cmp); int now = 0, pre = 1; dp[now].clear(); dp[now][0] = 0; for (int i = 0; i < n; i++) { swap(now, pre); dp[now].clear(); for (it = dp[pre].begin(); it != dp[pre].end(); it++) { int pt = it->first; ll pw = it->second; dp[now][pt] = max(dp[now][pt], pw); int ut = max(pt, q[i].s) + q[i].t; dp[now][ut] = max(dp[now][ut], pw + q[i].v); } } for (it = dp[now].begin(); it != dp[now].end(); it++) { if (it->second >= w) { int ans = it->first; printf("%d\n", ans); break; } } } return 0; }
时间: 2024-10-05 14:14:51