Sorry for misjudging the difficulty of problem D, but we still hope you enjoyed the problems!
Hint 1
$$$a \oplus 1$$$ is equivalent to $$$a - 1$$$ when $$$a$$$ is an odd number, otherwise it is equivalent to $$$a + 1$$$.
Hint 2
If you apply $$$a \gets a \oplus 1$$$ when $$$a$$$ is odd, then that operation should make $$$a = b$$$.
Tutorial
Tutorial is loading...
Implementation
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int t, a, b, x, y;
int main(){scanf("%d", &t);
while (t --){scanf("%d%d%d%d", &a, &b, &x, &y);
if (a > b) printf("%d\n", (a ^ 1) == b ? y : -1);
else {int c0 = b - a, c1 = (b + 1 >> 1) - (a + 1 >> 1);
printf("%lld\n", y > x ? 1ll * c0 * x : 1ll * (c0 - c1) * x + 1ll * c1 * y);}
}
}
Tutorial
Tutorial is loading...
Implementation
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int N = 5005;
int t, n, mod, f[N][N], ans;
inline void add(int& x, int y){x += y;if (x >= mod) x -= mod;}
int main(){scanf("%d", &t);
while (t --){scanf("%d%d", &n, &mod), f[0][0] = 1, ans = 0;
for (int i = 1;i <= n;i ++) for (int j = 0;j <= i;j ++) f[i][j] = 0;
for (int i = 1;i <= n;i ++)
for (int j = 0, now;j < i;j ++)
if (now = f[i - 1][j]) add(f[i][j + 1], now),
f[i][j] = (f[i][j] + (n - i + 1ll) * (j + 1) * now) % mod;
for (int j = 0;j <= n;j ++) add(ans, f[n][j]);printf("%d\n", ans);
}
}



