// 计算不同付款方式的数量 long long g[5]; for(int j=1;j<=4;j++){ g[j]=(long long)(d[j]+1)*c[j]; } long long da[6]; da[1]=ff(goal); da[2]=ff(goal-g[1])+ff(goal-g[2])+ff(goal-g[3])+ff(goal-g[4]); da[3]=ff(goal-g[1]-g[2])+ff(goal-g[1]-g[3])+ff(goal-g[1]-g[4])+ff(goal-g[2]-g[3])+ff(goal-g[2]-g[4])+ff(goal-g[3]-g[4]); da[4]=ff(goal-g[1]-g[2]-g[3])+ff(goal-g[1]-g[4]-g[3])+ff(goal-g[1]-g[2]-g[4])+ff(goal-g[4]-g[2]-g[3]); da[5]=ff(goal-g[1]-g[2]-g[3]-g[4]);
// 递归函数,计算必胜策略的情况 long long gui(long long a, long long b) { if (a % 2 == 1 && b % 2 == 1) return 0; // 如果两堆石子数都是奇数,返回0,表示无法必胜 if (a % 2 == 0 && b % 2 == 0) { return gui(a / 2, b / 2) + 1; // 如果两堆石子数都是偶数,递归调用gui函数,并加1 } else { if (a % 2 == 1) { return gui(a + 1, b); // 如果第一堆石子数是奇数,将其加1,继续递归调用gui函数 } else { return gui(a, b + 1); // 如果第二堆石子数是奇数,将其加1,继续递归调用gui函数 } } }
int main() { int t; cin >> t; // 输入游戏的轮数
for (int i = 1; i <= t; i++) { long long n, g, h, ans = 0; cin >> n; // 输入石子堆的数量
// 遍历每一组石子堆 for (int j = 1; j <= n / 2; j++) { cin >> g >> h; // 输入每组石子堆的石子数量 ans = (ans ^ gui(g, h)); // 异或运算,计算所有组的结果 }
Farmer John has $M$ cows, conveniently labeled $1 \ldots M$, who enjoy the occasional change of pace from eating grass. As a treat for the cows, Farmer John has baked $N$ pies ($1 \leq N \leq 300$), labeled $1 \ldots N$. Cow $i$ enjoys pies with labels in the range $[l_i, r_i]$ (from $l_i$ to $r_i$ inclusive), and no two cows enjoy the exact same range of pies. Cow $i$ also has a weight, $w_i$, which is an integer in the range $1 \ldots 10^6$.
Farmer John may choose a sequence of cows $c_1,c_2,\ldots, c_K,$ after which the selected cows will take turns eating in that order. Unfortunately, the cows don’t know how to share! When it is cow $c_i$’s turn to eat, she will consume all of the pies that she enjoys — that is, all remaining pies in the interval $[l_{c_i},r_{c_i}]$. Farmer John would like to avoid the awkward situation occurring when it is a cows turn to eat but all of the pies she enjoys have already been consumed. Therefore, he wants you to compute the largest possible total weight ($w_{c_1}+w_{c_2}+\ldots+w_{c_K}$) of a sequence $c_1,c_2,\ldots, c_K$ for which each cow in the sequence eats at least one pie.
#include <bits/stdc++.h> using namespace std; int dp[310][310]; int he[310][310][310]; // a, b, c: 对于区间bc,位置a处最大能吃的牛的重量 int main() { int n, m; cin >> n >> m; // 输入n和m的值
int w, l, r; for (int i = 1; i <= m; i++) { cin >> w >> l >> r; for (int i = l; i <= r; i++) { he[i][l][r] = w; // 记录每个位置和区间的最大能吃的牛的重量 } }
// 动态规划预处理最大能吃的牛的重量 for (int len = 0; len < n; len++) { for (int i = 1, j; i + len <= n; i++) { j = i + len; for (int k = i; k <= j; k++) { he[k][i - 1][j] = max(he[k][i - 1][j], he[k][i][j]); he[k][i][j + 1] = max(he[k][i][j + 1], he[k][i][j]); } } }
// 动态规划求解最优解 for (int len = 0; len < n; len++) { for (int i = 1, j; i + len <= n; i++) { j = i + len; for (int k = i; k < j; k++) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]); } for (int k = i; k <= j; k++) { dp[i][j] = max(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + he[k][i][j]); } } }