output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7, M = 3e3 + 7, mod = 998244353; int f[M * 2], d[M][M], n, m, i, j, k, a[N], b[N], s[2], s0, s1, b0, b1; long long pow_mod(long long x, long long n) { x = (x % mod + mod) % mod; long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { for (scanf("%d%d", &n, &m), i = 1; i <= n; ++i) scanf("%d", a + i); for (i = 1; i <= n; ++i) scanf("%d", b + i), s[a[i]] += b[i]; for (i = -m; i <= m; ++i) f[i + m] = pow_mod(s[0] + s[1] + i, mod - 2); d[0][0] = 1; for (i = 0; i < m; ++i) for (j = 0; j <= i; ++j) if (d[i][j]) { k = f[2 * j - i + m]; d[i + 1][j] = (d[i + 1][j] + 1LL * (s[0] - (i - j)) * k % mod * d[i][j] % mod) % mod; d[i + 1][j + 1] = (d[i + 1][j + 1] + 1LL * (s[1] + j) * k % mod * d[i][j] % mod) % mod; } s0 = pow_mod(s[0], mod - 2); s1 = pow_mod(s[1], mod - 2); for (i = 0; i <= m; ++i) { b0 = (b0 + 1LL * i * d[m][m - i]) % mod; b1 = (b1 + 1LL * i * d[m][i]) % mod; } b0 = (mod - b0) % mod; for (i = 1; i <= n; ++i) printf("%lld\n", a[i] == 0 ? (b[i] + 1LL * b[i] * s0 % mod * b0 % mod) % mod : (b[i] + 1LL * b[i] * s1 % mod * b1 % mod) % mod); }
### Prompt Your challenge is to write a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7, M = 3e3 + 7, mod = 998244353; int f[M * 2], d[M][M], n, m, i, j, k, a[N], b[N], s[2], s0, s1, b0, b1; long long pow_mod(long long x, long long n) { x = (x % mod + mod) % mod; long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { for (scanf("%d%d", &n, &m), i = 1; i <= n; ++i) scanf("%d", a + i); for (i = 1; i <= n; ++i) scanf("%d", b + i), s[a[i]] += b[i]; for (i = -m; i <= m; ++i) f[i + m] = pow_mod(s[0] + s[1] + i, mod - 2); d[0][0] = 1; for (i = 0; i < m; ++i) for (j = 0; j <= i; ++j) if (d[i][j]) { k = f[2 * j - i + m]; d[i + 1][j] = (d[i + 1][j] + 1LL * (s[0] - (i - j)) * k % mod * d[i][j] % mod) % mod; d[i + 1][j + 1] = (d[i + 1][j + 1] + 1LL * (s[1] + j) * k % mod * d[i][j] % mod) % mod; } s0 = pow_mod(s[0], mod - 2); s1 = pow_mod(s[1], mod - 2); for (i = 0; i <= m; ++i) { b0 = (b0 + 1LL * i * d[m][m - i]) % mod; b1 = (b1 + 1LL * i * d[m][i]) % mod; } b0 = (mod - b0) % mod; for (i = 1; i <= n; ++i) printf("%lld\n", a[i] == 0 ? (b[i] + 1LL * b[i] * s0 % mod * b0 % mod) % mod : (b[i] + 1LL * b[i] * s1 % mod * b1 % mod) % mod); } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long dp[52][52][52]; long long inv[3010], sum0, sum1, sum; long long a[55], b[55], n, m; long long qpow(long long x, long long y) { long long ans = 1; for (; y; y >>= 1) { if (y & 1) ans = (ans * x) % mod; x = (x * x) % mod; } return ans; } long long solve1(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) continue; int now = sum + 2 * j - i; dp[i + 1][j + 1][k + 1] += dp[i][j][k] * (x + k) % mod * inv[now] % mod; dp[i + 1][j + 1][k + 1] %= mod; dp[i + 1][j + 1][k] += dp[i][j][k] * (sum1 + j - k - x) % mod * inv[now]; dp[i + 1][j + 1][k] %= mod; if (sum0 - i + j > 0) { dp[i + 1][j][k] += dp[i][j][k] * (sum0 - i + j) % mod * inv[now] % mod; dp[i + 1][j][k] %= mod; } } long long ans = 0; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) ans = (ans + dp[m][i][j] * (j + x)) % mod; return ans; } long long solve0(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) continue; int now = sum + i - 2 * j; if (sum0 - j > 0) { if (k < x) { dp[i + 1][j + 1][k + 1] += dp[i][j][k] * (x - k) % mod * inv[now] % mod; dp[i + 1][j + 1][k + 1] %= mod; } dp[i + 1][j + 1][k] += dp[i][j][k] * (sum0 - (j - k) - x) % mod * inv[now]; dp[i + 1][j + 1][k] %= mod; } dp[i + 1][j][k] += dp[i][j][k] * (sum1 + i - j) % mod * inv[now] % mod; dp[i + 1][j][k] %= mod; } long long ans = 0; for (int i = 0; i <= m; i++) { if (i >= sum0) continue; for (int j = 0; j <= m; j++) { if (j >= x) continue; ans = (ans + dp[m][i][j] * (x - j)) % mod; } } return ans; } int main() { for (int i = 1; i <= 3000; i++) inv[i] = qpow(i, mod - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (int i = 1; i <= n; i++) { scanf("%lld", &b[i]); sum += b[i]; if (a[i] == 0) sum0 += b[i]; else sum1 += b[i]; } for (int i = 1; i <= n; i++) { if (a[i] == 0) printf("%lld\n", solve0(b[i])); else printf("%lld\n", solve1(b[i])); } }
### Prompt Please formulate a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long dp[52][52][52]; long long inv[3010], sum0, sum1, sum; long long a[55], b[55], n, m; long long qpow(long long x, long long y) { long long ans = 1; for (; y; y >>= 1) { if (y & 1) ans = (ans * x) % mod; x = (x * x) % mod; } return ans; } long long solve1(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) continue; int now = sum + 2 * j - i; dp[i + 1][j + 1][k + 1] += dp[i][j][k] * (x + k) % mod * inv[now] % mod; dp[i + 1][j + 1][k + 1] %= mod; dp[i + 1][j + 1][k] += dp[i][j][k] * (sum1 + j - k - x) % mod * inv[now]; dp[i + 1][j + 1][k] %= mod; if (sum0 - i + j > 0) { dp[i + 1][j][k] += dp[i][j][k] * (sum0 - i + j) % mod * inv[now] % mod; dp[i + 1][j][k] %= mod; } } long long ans = 0; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) ans = (ans + dp[m][i][j] * (j + x)) % mod; return ans; } long long solve0(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) continue; int now = sum + i - 2 * j; if (sum0 - j > 0) { if (k < x) { dp[i + 1][j + 1][k + 1] += dp[i][j][k] * (x - k) % mod * inv[now] % mod; dp[i + 1][j + 1][k + 1] %= mod; } dp[i + 1][j + 1][k] += dp[i][j][k] * (sum0 - (j - k) - x) % mod * inv[now]; dp[i + 1][j + 1][k] %= mod; } dp[i + 1][j][k] += dp[i][j][k] * (sum1 + i - j) % mod * inv[now] % mod; dp[i + 1][j][k] %= mod; } long long ans = 0; for (int i = 0; i <= m; i++) { if (i >= sum0) continue; for (int j = 0; j <= m; j++) { if (j >= x) continue; ans = (ans + dp[m][i][j] * (x - j)) % mod; } } return ans; } int main() { for (int i = 1; i <= 3000; i++) inv[i] = qpow(i, mod - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (int i = 1; i <= n; i++) { scanf("%lld", &b[i]); sum += b[i]; if (a[i] == 0) sum0 += b[i]; else sum1 += b[i]; } for (int i = 1; i <= n; i++) { if (a[i] == 0) printf("%lld\n", solve0(b[i])); else printf("%lld\n", solve1(b[i])); } } ```
#include <bits/stdc++.h> using namespace std; long long fpw(long long n, long long p) { if (p == 0) return 1LL; long long tmp = fpw(n, p / 2); tmp *= tmp; tmp %= 998244353; if (p % 2 == 1) { tmp *= n; tmp %= 998244353; } return tmp; } long long inv(long long k) { return fpw(k, 998244353 - 2); } long long pre[3005]; long long inv2(long long k) { if (k > 3000) return inv(k); return pre[k]; } long long n, m; long long a[100]; long long w[100]; long long dp[55][105][105]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> w[i]; } for (int i = 1; i <= 3000; i++) { pre[i] = inv(i); } for (int i = 0; i < n; i++) { long long cntoth = 0, wall = 0, w1 = 0, w0 = 0; for (int j = 0; j < n; j++) { if (i != j) { cntoth += a[j]; if (a[j] == 1) w1 += w[j]; else w0 += w[j]; } wall += w[j]; } long long ans = 0; dp[0][51][51] = 1; for (int j = 1; j <= m; j++) { for (int k = 1; k <= 101; k++) { for (int l = 1; l <= 51; l++) { int l2 = 51 + (j - abs(51 - k) - abs(51 - l)); if (l2 < 51) continue; dp[j][k][l] = 0; if (a[i] == 0) dp[j][k][l] += ((dp[j - 1][k + 1][l] * (w[i] + (k + 1) - 51)) % 998244353) * inv2(wall + (k + 1) - 51 + l - 51 + l2 - 51); else dp[j][k][l] += ((dp[j - 1][k - 1][l] * (w[i] + (k - 1) - 51)) % 998244353) * inv2(wall + (k - 1) - 51 + l - 51 + l2 - 51); dp[j][k][l] += ((dp[j - 1][k][l] * (w1 + ((l2 - 1) - 51))) % 998244353) * inv2(wall + k - 51 + l - 51 + (l2 - 1) - 51); dp[j][k][l] += ((dp[j - 1][k][l + 1] * (w0 + ((l + 1) - 51))) % 998244353) * inv2(wall + k - 51 + (l + 1) - 51 + l2 - 51); dp[j][k][l] %= 998244353; if (j == m) ans += dp[j][k][l] * (w[i] + k - 51); ans %= 998244353; } } } printf("%lld\n", ans); } }
### Prompt Your task is to create a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long fpw(long long n, long long p) { if (p == 0) return 1LL; long long tmp = fpw(n, p / 2); tmp *= tmp; tmp %= 998244353; if (p % 2 == 1) { tmp *= n; tmp %= 998244353; } return tmp; } long long inv(long long k) { return fpw(k, 998244353 - 2); } long long pre[3005]; long long inv2(long long k) { if (k > 3000) return inv(k); return pre[k]; } long long n, m; long long a[100]; long long w[100]; long long dp[55][105][105]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> w[i]; } for (int i = 1; i <= 3000; i++) { pre[i] = inv(i); } for (int i = 0; i < n; i++) { long long cntoth = 0, wall = 0, w1 = 0, w0 = 0; for (int j = 0; j < n; j++) { if (i != j) { cntoth += a[j]; if (a[j] == 1) w1 += w[j]; else w0 += w[j]; } wall += w[j]; } long long ans = 0; dp[0][51][51] = 1; for (int j = 1; j <= m; j++) { for (int k = 1; k <= 101; k++) { for (int l = 1; l <= 51; l++) { int l2 = 51 + (j - abs(51 - k) - abs(51 - l)); if (l2 < 51) continue; dp[j][k][l] = 0; if (a[i] == 0) dp[j][k][l] += ((dp[j - 1][k + 1][l] * (w[i] + (k + 1) - 51)) % 998244353) * inv2(wall + (k + 1) - 51 + l - 51 + l2 - 51); else dp[j][k][l] += ((dp[j - 1][k - 1][l] * (w[i] + (k - 1) - 51)) % 998244353) * inv2(wall + (k - 1) - 51 + l - 51 + l2 - 51); dp[j][k][l] += ((dp[j - 1][k][l] * (w1 + ((l2 - 1) - 51))) % 998244353) * inv2(wall + k - 51 + l - 51 + (l2 - 1) - 51); dp[j][k][l] += ((dp[j - 1][k][l + 1] * (w0 + ((l + 1) - 51))) % 998244353) * inv2(wall + k - 51 + (l + 1) - 51 + l2 - 51); dp[j][k][l] %= 998244353; if (j == m) ans += dp[j][k][l] * (w[i] + k - 51); ans %= 998244353; } } } printf("%lld\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; long long MOD = 998244353; inline long long Msum(long long x) { return x; } template <typename... Rest> inline long long Msum(long long x, Rest... rest) { return (x + Msum(rest...)) % MOD; } inline long long Mprod(long long x) { return x; } template <typename... Rest> inline long long Mprod(long long x, Rest... rest) { return x * Mprod(rest...) % MOD; } inline long long Mnorm(long long x) { return (x % MOD + MOD) % MOD; } long long Msq(long long x) { return x * x % MOD; } long long Mpow(long long b, long long e) { return e ? Mprod(Msq(Mpow(b, e >> 1)), (e & 1 ? b : 1)) : 1; } long long Minv(long long x) { return Mpow(x, MOD - 2); } const int MAXN = 51; int sns[MAXN], w[MAXN]; int wi, sn; int sl, sd, S; long long dp[MAXN][MAXN][MAXN]; long long f(int h, int hl, int hd, int t) { if (!t) return 0; long long &ret = dp[h][hl][hd]; if (ret != -1) return ret; ret = 0; long long cS = S + hl - hd + sn * h; ret += Mprod(wi + sn * h, Minv(cS), 1 + f(h + 1, hl, hd, t - 1)); ret = Mnorm(ret); ret += Mprod(sl + hl, Minv(cS), f(h, hl + 1, hd, t - 1)); ret = Mnorm(ret); ret += Mprod(sd - hd, Minv(cS), f(h, hl, hd + 1, t - 1)); ret = Mnorm(ret); assert(sd - hd + sl + hl + wi + sn * h == cS); return ret; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &sns[i]); sns[i] = sns[i] * 2 - 1; } for (int i = 0; i < n; i++) { scanf("%d", &w[i]); } for (int i = 0; i < n; i++) { S = sl = sd = 0; wi = w[i]; sn = sns[i]; for (int j = 0; j < (int)n; j++) { S += w[j]; if (j != i) { if (sns[j] == 1) sl += w[j]; else sd += w[j]; } } memset(dp, -1, sizeof(dp)); printf("%lld\n", Mnorm(wi + sn * f(0, 0, 0, m))); } }
### Prompt Your challenge is to write a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long MOD = 998244353; inline long long Msum(long long x) { return x; } template <typename... Rest> inline long long Msum(long long x, Rest... rest) { return (x + Msum(rest...)) % MOD; } inline long long Mprod(long long x) { return x; } template <typename... Rest> inline long long Mprod(long long x, Rest... rest) { return x * Mprod(rest...) % MOD; } inline long long Mnorm(long long x) { return (x % MOD + MOD) % MOD; } long long Msq(long long x) { return x * x % MOD; } long long Mpow(long long b, long long e) { return e ? Mprod(Msq(Mpow(b, e >> 1)), (e & 1 ? b : 1)) : 1; } long long Minv(long long x) { return Mpow(x, MOD - 2); } const int MAXN = 51; int sns[MAXN], w[MAXN]; int wi, sn; int sl, sd, S; long long dp[MAXN][MAXN][MAXN]; long long f(int h, int hl, int hd, int t) { if (!t) return 0; long long &ret = dp[h][hl][hd]; if (ret != -1) return ret; ret = 0; long long cS = S + hl - hd + sn * h; ret += Mprod(wi + sn * h, Minv(cS), 1 + f(h + 1, hl, hd, t - 1)); ret = Mnorm(ret); ret += Mprod(sl + hl, Minv(cS), f(h, hl + 1, hd, t - 1)); ret = Mnorm(ret); ret += Mprod(sd - hd, Minv(cS), f(h, hl, hd + 1, t - 1)); ret = Mnorm(ret); assert(sd - hd + sl + hl + wi + sn * h == cS); return ret; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &sns[i]); sns[i] = sns[i] * 2 - 1; } for (int i = 0; i < n; i++) { scanf("%d", &w[i]); } for (int i = 0; i < n; i++) { S = sl = sd = 0; wi = w[i]; sn = sns[i]; for (int j = 0; j < (int)n; j++) { S += w[j]; if (j != i) { if (sns[j] == 1) sl += w[j]; else sd += w[j]; } } memset(dp, -1, sizeof(dp)); printf("%lld\n", Mnorm(wi + sn * f(0, 0, 0, m))); } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int mod = 998244353; long long qpow(long long a, long long b) { long long ans = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } long long gcd(long long a, long long b) { return b > 0 ? gcd(b, a % b) : a; } int a[maxn]; int w[maxn]; int one, zero, num, sum; long long dp[60][60][60][60]; long long ans[60]; int main() { int n, m, T; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i], num += a[i]; for (int i = 1; i <= n; i++) cin >> w[i], sum = (sum + w[i]) % mod, one = (one + a[i] * w[i]) % mod; zero = (sum - one + mod) % mod; for (int i = 1; i <= n; i++) { if (a[i] == 0) { dp[1][i][0][1] = 1ll * qpow(sum, mod - 2) * w[i] % mod; dp[1][i][0][0] = 1ll * qpow(sum, mod - 2) * (zero - w[i]) % mod; dp[1][i][1][0] = 1ll * qpow(sum, mod - 2) * (one) % mod; } else { dp[1][i][1][1] = 1ll * qpow(sum, mod - 2) * w[i] % mod; dp[1][i][1][0] = 1ll * qpow(sum, mod - 2) * (one - w[i]) % mod; dp[1][i][0][0] = 1ll * qpow(sum, mod - 2) * (zero) % mod; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int t = 0; t <= i; t++) { for (int k = 0; k <= i; k++) { int state = ((sum + t - (i - t)) % mod + mod) % mod; int o0 = zero - (i - t); int o1 = one + t; if (a[j] == 0) { dp[i + 1][j][t][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o0 - (w[j] - k)) % mod; dp[i + 1][j][t][k] %= mod; dp[i + 1][j][t][k + 1] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * ((w[j] - k)) % mod; dp[i + 1][j][t][k + 1] %= mod; dp[i + 1][j][t + 1][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o1) % mod; dp[i + 1][j][t + 1][k] %= mod; } else { dp[i + 1][j][t + 1][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o1 - (w[j] + k)) % mod; dp[i + 1][j][t + 1][k] %= mod; dp[i + 1][j][t + 1][k + 1] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * ((w[j] + k)) % mod; dp[i + 1][j][t + 1][k + 1] %= mod; dp[i + 1][j][t][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o0) % mod; dp[i + 1][j][t][k] %= mod; } } } } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { int temp = a[i] ? 1 : -1; ans[i] = (ans[i] + (w[i] + k * temp) * dp[m][i][j][k]) % mod; } } cout << (ans[i] + mod) % mod << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int mod = 998244353; long long qpow(long long a, long long b) { long long ans = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } long long gcd(long long a, long long b) { return b > 0 ? gcd(b, a % b) : a; } int a[maxn]; int w[maxn]; int one, zero, num, sum; long long dp[60][60][60][60]; long long ans[60]; int main() { int n, m, T; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i], num += a[i]; for (int i = 1; i <= n; i++) cin >> w[i], sum = (sum + w[i]) % mod, one = (one + a[i] * w[i]) % mod; zero = (sum - one + mod) % mod; for (int i = 1; i <= n; i++) { if (a[i] == 0) { dp[1][i][0][1] = 1ll * qpow(sum, mod - 2) * w[i] % mod; dp[1][i][0][0] = 1ll * qpow(sum, mod - 2) * (zero - w[i]) % mod; dp[1][i][1][0] = 1ll * qpow(sum, mod - 2) * (one) % mod; } else { dp[1][i][1][1] = 1ll * qpow(sum, mod - 2) * w[i] % mod; dp[1][i][1][0] = 1ll * qpow(sum, mod - 2) * (one - w[i]) % mod; dp[1][i][0][0] = 1ll * qpow(sum, mod - 2) * (zero) % mod; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int t = 0; t <= i; t++) { for (int k = 0; k <= i; k++) { int state = ((sum + t - (i - t)) % mod + mod) % mod; int o0 = zero - (i - t); int o1 = one + t; if (a[j] == 0) { dp[i + 1][j][t][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o0 - (w[j] - k)) % mod; dp[i + 1][j][t][k] %= mod; dp[i + 1][j][t][k + 1] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * ((w[j] - k)) % mod; dp[i + 1][j][t][k + 1] %= mod; dp[i + 1][j][t + 1][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o1) % mod; dp[i + 1][j][t + 1][k] %= mod; } else { dp[i + 1][j][t + 1][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o1 - (w[j] + k)) % mod; dp[i + 1][j][t + 1][k] %= mod; dp[i + 1][j][t + 1][k + 1] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * ((w[j] + k)) % mod; dp[i + 1][j][t + 1][k + 1] %= mod; dp[i + 1][j][t][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o0) % mod; dp[i + 1][j][t][k] %= mod; } } } } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { int temp = a[i] ? 1 : -1; ans[i] = (ans[i] + (w[i] + k * temp) * dp[m][i][j][k]) % mod; } } cout << (ans[i] + mod) % mod << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool debug = false; int n, m; int a[55], b[55]; int sigG, sigA, sigB; long long dp[55][55][55]; long long fp[1000005]; const long long mod = 998244353; long long qp(long long a, long long b) { if (b == 0) { return 1; } long long ans = qp(a, b / 2); if (b % 2) { return ans * ans % mod * a % mod; } else { return ans * ans % mod; } } void add(int ti, int tj, int tk, int fi, int fj, int fk, long long num, long long dem) { if (num < 0 || dem <= 0) { return; } dp[ti][tj][tk] += dp[fi][fj][fk] * num % mod * fp[dem] % mod; dp[ti][tj][tk] %= mod; } long long getAns(int pos) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) { continue; } add(i + 1, j, k, i, j, k, b[pos] + i, sigA + i + j - k); add(i, j + 1, k, i, j, k, sigG + j + i - b[pos] - i, sigA + i + j - k); add(i, j, k + 1, i, j, k, sigB - k, sigA + i + j - k); } } } long long tot = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (i + j + k == m) { tot += 1ll * (b[pos] + i) * dp[i][j][k] % mod; tot %= mod; } } } } return tot; } long long getAns2(int pos) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) { continue; } add(i + 1, j, k, i, j, k, b[pos] - i, sigA - i + j - k); add(i, j + 1, k, i, j, k, sigG + j, sigA - i + j - k); add(i, j, k + 1, i, j, k, sigB - k - i - b[pos] + i, sigA - i + j - k); } } } long long tot = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (i + j + k == m) { tot += 1ll * (b[pos] - i) * dp[i][j][k] % mod; tot %= mod; } } } } return tot; } int main(int argc, char* argv[]) { for (int i = 0; i <= 1000000; i++) { fp[i] = qp(i, mod - 2); } ios::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { sigA += b[i]; if (a[i]) { sigG += b[i]; } else { sigB += b[i]; } } for (int i = 0; i < n; i++) { if (a[i]) { cout << getAns(i) << endl; } else { cout << getAns2(i) << endl; } } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool debug = false; int n, m; int a[55], b[55]; int sigG, sigA, sigB; long long dp[55][55][55]; long long fp[1000005]; const long long mod = 998244353; long long qp(long long a, long long b) { if (b == 0) { return 1; } long long ans = qp(a, b / 2); if (b % 2) { return ans * ans % mod * a % mod; } else { return ans * ans % mod; } } void add(int ti, int tj, int tk, int fi, int fj, int fk, long long num, long long dem) { if (num < 0 || dem <= 0) { return; } dp[ti][tj][tk] += dp[fi][fj][fk] * num % mod * fp[dem] % mod; dp[ti][tj][tk] %= mod; } long long getAns(int pos) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) { continue; } add(i + 1, j, k, i, j, k, b[pos] + i, sigA + i + j - k); add(i, j + 1, k, i, j, k, sigG + j + i - b[pos] - i, sigA + i + j - k); add(i, j, k + 1, i, j, k, sigB - k, sigA + i + j - k); } } } long long tot = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (i + j + k == m) { tot += 1ll * (b[pos] + i) * dp[i][j][k] % mod; tot %= mod; } } } } return tot; } long long getAns2(int pos) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) { continue; } add(i + 1, j, k, i, j, k, b[pos] - i, sigA - i + j - k); add(i, j + 1, k, i, j, k, sigG + j, sigA - i + j - k); add(i, j, k + 1, i, j, k, sigB - k - i - b[pos] + i, sigA - i + j - k); } } } long long tot = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (i + j + k == m) { tot += 1ll * (b[pos] - i) * dp[i][j][k] % mod; tot %= mod; } } } } return tot; } int main(int argc, char* argv[]) { for (int i = 0; i <= 1000000; i++) { fp[i] = qp(i, mod - 2); } ios::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { sigA += b[i]; if (a[i]) { sigG += b[i]; } else { sigB += b[i]; } } for (int i = 0; i < n; i++) { if (a[i]) { cout << getAns(i) << endl; } else { cout << getAns2(i) << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, A, B, S, sl, fh, a[200010], w[200010], inv[3010 << 1], f[3010][3010], g[3010][3010]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int _pow(int k, int i) { int t = 1; while (i) { if (i & 1) t = 1ll * t * k % 998244353; k = 1ll * k * k % 998244353; i >>= 1; } return t; } int main() { n = rd(); m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) { w[i] = rd(); S += w[i]; if (a[i]) A += w[i]; else B += w[i]; } for (int i = 0; i <= m * 2; ++i) inv[i] = _pow(S + i - m, 998244353 - 2); for (int i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (int j = m - i - 1; ~j; --j) f[i][j] = (1ll * (A + i + 1) * f[i + 1][j] + 1ll * (B - j) * f[i][j + 1]) % 998244353 * inv[i - j + m] % 998244353, g[i][j] = (1ll * (B - j - 1) * g[i][j + 1] + 1ll * (A + i) * g[i + 1][j]) % 998244353 * inv[i - j + m] % 998244353; } A = f[0][0]; B = g[0][0]; for (int i = 1; i <= n; ++i) if (a[i]) printf("%lld\n", 1ll * A * w[i] % 998244353); else printf("%lld\n", 1ll * B * w[i] % 998244353); return 0; }
### Prompt Generate a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, A, B, S, sl, fh, a[200010], w[200010], inv[3010 << 1], f[3010][3010], g[3010][3010]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int _pow(int k, int i) { int t = 1; while (i) { if (i & 1) t = 1ll * t * k % 998244353; k = 1ll * k * k % 998244353; i >>= 1; } return t; } int main() { n = rd(); m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) { w[i] = rd(); S += w[i]; if (a[i]) A += w[i]; else B += w[i]; } for (int i = 0; i <= m * 2; ++i) inv[i] = _pow(S + i - m, 998244353 - 2); for (int i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (int j = m - i - 1; ~j; --j) f[i][j] = (1ll * (A + i + 1) * f[i + 1][j] + 1ll * (B - j) * f[i][j + 1]) % 998244353 * inv[i - j + m] % 998244353, g[i][j] = (1ll * (B - j - 1) * g[i][j + 1] + 1ll * (A + i) * g[i + 1][j]) % 998244353 * inv[i - j + m] % 998244353; } A = f[0][0]; B = g[0][0]; for (int i = 1; i <= n; ++i) if (a[i]) printf("%lld\n", 1ll * A * w[i] % 998244353); else printf("%lld\n", 1ll * B * w[i] % 998244353); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; }
### Prompt Create a solution in CPP for the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 10000; const int mod = 998244353; long long a[maxn], w[maxn], inv[maxn], dp[100][100][100], A, B, m, n; void add(long long& x, long long y) { x = (x + y) % mod; } long long mul(long long x, long long y) { return (x * y) % mod; } void mul2(long long& x, long long y) { x = (x * y) % mod; } void solve(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (long long i = 0; i <= m; i++) { for (long long j = 0; j <= i; j++) { for (long long k = 0; k <= j; k++) { if (a[x]) { long long wx = w[x] + k, wa = A + j, wb = B - (i - j); add(dp[i + 1][j + 1][k + 1], mul(mul(dp[i][j][k], wx), inv[wa + wb])); add(dp[i + 1][j + 1][k], mul(mul(dp[i][j][k], wa - wx), inv[wa + wb])); add(dp[i + 1][j][k], mul(mul(dp[i][j][k], wb), inv[wa + wb])); } else { long long wx = w[x] - k, wb = B - j, wa = A + (i - j); add(dp[i + 1][j + 1][k + 1], mul(mul(dp[i][j][k], wx), inv[wa + wb])); add(dp[i + 1][j + 1][k], mul(mul(dp[i][j][k], (wb - wx)), inv[wa + wb])); add(dp[i + 1][j][k], mul(mul(dp[i][j][k], wa), inv[wa + wb])); } } } } long long re = 0; for (long long i = 0; i <= m; i++) { for (long long j = 0; j <= i; j++) { if (a[x]) { long long val = w[x] + j; add(re, mul(val, dp[m][i][j])); } else { long long val = w[x] - j; add(re, mul(val, dp[m][i][j])); } } } printf("%I64d\n", re); } long long mypow(long long x, long long y) { long long re = 1; while (y) { if (y & 1LL) mul2(re, x); y >>= 1; mul2(x, x); } return re; } void prework() { for (long long i = 1; i <= 3000; i++) { inv[i] = mypow(i, mod - 2); } } int main() { prework(); scanf("%I64d%I64d", &n, &m); A = B = 0; for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (long long i = 1; i <= n; i++) { scanf("%I64d", &w[i]); if (a[i]) A += w[i]; else B += w[i]; } for (long long i = 1; i <= n; i++) solve(i); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 10000; const int mod = 998244353; long long a[maxn], w[maxn], inv[maxn], dp[100][100][100], A, B, m, n; void add(long long& x, long long y) { x = (x + y) % mod; } long long mul(long long x, long long y) { return (x * y) % mod; } void mul2(long long& x, long long y) { x = (x * y) % mod; } void solve(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (long long i = 0; i <= m; i++) { for (long long j = 0; j <= i; j++) { for (long long k = 0; k <= j; k++) { if (a[x]) { long long wx = w[x] + k, wa = A + j, wb = B - (i - j); add(dp[i + 1][j + 1][k + 1], mul(mul(dp[i][j][k], wx), inv[wa + wb])); add(dp[i + 1][j + 1][k], mul(mul(dp[i][j][k], wa - wx), inv[wa + wb])); add(dp[i + 1][j][k], mul(mul(dp[i][j][k], wb), inv[wa + wb])); } else { long long wx = w[x] - k, wb = B - j, wa = A + (i - j); add(dp[i + 1][j + 1][k + 1], mul(mul(dp[i][j][k], wx), inv[wa + wb])); add(dp[i + 1][j + 1][k], mul(mul(dp[i][j][k], (wb - wx)), inv[wa + wb])); add(dp[i + 1][j][k], mul(mul(dp[i][j][k], wa), inv[wa + wb])); } } } } long long re = 0; for (long long i = 0; i <= m; i++) { for (long long j = 0; j <= i; j++) { if (a[x]) { long long val = w[x] + j; add(re, mul(val, dp[m][i][j])); } else { long long val = w[x] - j; add(re, mul(val, dp[m][i][j])); } } } printf("%I64d\n", re); } long long mypow(long long x, long long y) { long long re = 1; while (y) { if (y & 1LL) mul2(re, x); y >>= 1; mul2(x, x); } return re; } void prework() { for (long long i = 1; i <= 3000; i++) { inv[i] = mypow(i, mod - 2); } } int main() { prework(); scanf("%I64d%I64d", &n, &m); A = B = 0; for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (long long i = 1; i <= n; i++) { scanf("%I64d", &w[i]); if (a[i]) A += w[i]; else B += w[i]; } for (long long i = 1; i <= n; i++) solve(i); return 0; } ```
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, A, B; int a[200010], w[200010]; int quick_pow(int x, int p) { int an = 1; int po = x; while (p) { if (p & 1) an = 1ll * an * po % 998244353; po = 1ll * po * po % 998244353; p >>= 1; } return an; } int f[3010][3010], g[3010][3010]; int inv[3010 * 2]; int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) w[i] = read(); for (int i = 1; i <= n; i++) { A += (a[i] == 1) * w[i]; A %= 998244353; B += (a[i] == 0) * w[i]; B %= 998244353; } for (int i = 1; i <= 2 * m; i++) inv[i] = quick_pow(A + B + i - m, 998244353 - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, B); j >= 0; j--) { f[i][j] = (1ll * f[i + 1][j] * (A + i + 1) % 998244353 * inv[i - j + m] % 998244353 + 1ll * f[i][j + 1] * (B - j) % 998244353 * inv[i - j + m] % 998244353) % 998244353; g[i][j] = (1ll * g[i + 1][j] * (A + i) % 998244353 * inv[i - j + m] % 998244353 + 1ll * g[i][j + 1] * (B - j - 1) % 998244353 * inv[i - j + m] % 998244353) % 998244353; } } for (int i = 1; i <= n; i++) { if (a[i] == 1) printf("%d\n", 1ll * w[i] * f[0][0] % 998244353); else printf("%d\n", 1ll * w[i] * g[0][0] % 998244353); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, A, B; int a[200010], w[200010]; int quick_pow(int x, int p) { int an = 1; int po = x; while (p) { if (p & 1) an = 1ll * an * po % 998244353; po = 1ll * po * po % 998244353; p >>= 1; } return an; } int f[3010][3010], g[3010][3010]; int inv[3010 * 2]; int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) w[i] = read(); for (int i = 1; i <= n; i++) { A += (a[i] == 1) * w[i]; A %= 998244353; B += (a[i] == 0) * w[i]; B %= 998244353; } for (int i = 1; i <= 2 * m; i++) inv[i] = quick_pow(A + B + i - m, 998244353 - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, B); j >= 0; j--) { f[i][j] = (1ll * f[i + 1][j] * (A + i + 1) % 998244353 * inv[i - j + m] % 998244353 + 1ll * f[i][j + 1] * (B - j) % 998244353 * inv[i - j + m] % 998244353) % 998244353; g[i][j] = (1ll * g[i + 1][j] * (A + i) % 998244353 * inv[i - j + m] % 998244353 + 1ll * g[i][j + 1] * (B - j - 1) % 998244353 * inv[i - j + m] % 998244353) % 998244353; } } for (int i = 1; i <= n; i++) { if (a[i] == 1) printf("%d\n", 1ll * w[i] * f[0][0] % 998244353); else printf("%d\n", 1ll * w[i] * g[0][0] % 998244353); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int NN = 200020, MM = 3030; const int mod = 998244353; int power(int a, int b, int m = mod, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } unordered_map<int, int> mp; int inv(int a) { if (a < 1) return 0; if (mp.count(a)) return mp[a]; return mp[a] = power(a, mod - 2); } inline void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int a[NN]; int wei[NN]; int dp[MM][MM]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", wei + i); int good = 0, bad = 0; for (int i = 1; i <= n; i++) { if (a[i]) good += wei[i]; else bad += wei[i]; } dp[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { if (bad - (i - j) >= 0) { add(dp[i + 1][j], (long long)dp[i][j] * (bad - (i - j)) % mod * inv(good + j + bad - (i - j)) % mod); add(dp[i + 1][j + 1], (long long)dp[i][j] * (good + j) % mod * inv(good + j + bad - (i - j)) % mod); } } } int Bad = 0, Good = 0; for (int i = 0; i <= m; i++) { add(Good, (long long)(good + i) * dp[m][i] % mod); add(Bad, (long long)(bad - (m - i)) * dp[m][i] % mod); } int igood = inv(good), ibad = inv(bad); for (int i = 1; i <= n; i++) { if (a[i]) { printf("%d ", (long long)Good * wei[i] % mod * igood % mod); } else { printf("%d ", (long long)Bad * wei[i] % mod * ibad % mod); } } puts(""); return 0; }
### Prompt Create a solution in Cpp for the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int NN = 200020, MM = 3030; const int mod = 998244353; int power(int a, int b, int m = mod, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } unordered_map<int, int> mp; int inv(int a) { if (a < 1) return 0; if (mp.count(a)) return mp[a]; return mp[a] = power(a, mod - 2); } inline void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int a[NN]; int wei[NN]; int dp[MM][MM]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", wei + i); int good = 0, bad = 0; for (int i = 1; i <= n; i++) { if (a[i]) good += wei[i]; else bad += wei[i]; } dp[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { if (bad - (i - j) >= 0) { add(dp[i + 1][j], (long long)dp[i][j] * (bad - (i - j)) % mod * inv(good + j + bad - (i - j)) % mod); add(dp[i + 1][j + 1], (long long)dp[i][j] * (good + j) % mod * inv(good + j + bad - (i - j)) % mod); } } } int Bad = 0, Good = 0; for (int i = 0; i <= m; i++) { add(Good, (long long)(good + i) * dp[m][i] % mod); add(Bad, (long long)(bad - (m - i)) * dp[m][i] % mod); } int igood = inv(good), ibad = inv(bad); for (int i = 1; i <= n; i++) { if (a[i]) { printf("%d ", (long long)Good * wei[i] % mod * igood % mod); } else { printf("%d ", (long long)Bad * wei[i] % mod * ibad % mod); } } puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 998244353; int64_t powmo(int64_t a, int64_t b) { int64_t r = 1; for (; b; b >>= 1) { if (b & 1) r = r * a % MOD; a = a * a % MOD; } return r; } int64_t mdiv(int64_t a, int64_t b) { return a * powmo(b, MOD - 2) % MOD; } bitset<300005> like; int64_t w[300005]; int64_t ans[300005]; int64_t dp[3005][3005]; int32_t main() { { cin.tie(0); ios_base::sync_with_stdio(false); }; int64_t n, m, t, add, sub, sum; add = sub = 0; cin >> n >> m; for (int64_t i = 0; i < n; i++) { cin >> t; like[i] = t; } for (int64_t i = 0; i < n; i++) { cin >> w[i]; if (like[i]) add += w[i]; else sub += w[i]; } dp[0][0] = 1; sum = add + sub; for (int64_t x = 1; x <= m; x++) { for (int64_t i = x, j = 0; i >= 0 && j <= sub; i--, j++) { int64_t nowsum = sum + i - j; int64_t nowadd = add + i; int64_t nowsub = sub - j; dp[i][j] = 0; if (i > 0) dp[i][j] = (dp[i][j] + dp[i - 1][j] * mdiv(nowadd - 1, nowsum - 1) % MOD) % MOD; if (j > 0) dp[i][j] = (dp[i][j] + dp[i][j - 1] * mdiv(nowsub + 1, nowsum + 1) % MOD) % MOD; } } for (int64_t i = m, j = 0; i >= 0 && j <= sub; i--, j++) { for (int64_t k = 0; k < n; k++) { if (like[k]) ans[k] = (ans[k] + (w[k] + i * mdiv(w[k], add)) % MOD * dp[i][j]) % MOD; else ans[k] = (ans[k] + (w[k] - j * mdiv(w[k], sub) % MOD + MOD) % MOD * dp[i][j]) % MOD; } } for (int64_t i = 0; i < n; i++) cout << ans[i] << '\n'; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int64_t MOD = 998244353; int64_t powmo(int64_t a, int64_t b) { int64_t r = 1; for (; b; b >>= 1) { if (b & 1) r = r * a % MOD; a = a * a % MOD; } return r; } int64_t mdiv(int64_t a, int64_t b) { return a * powmo(b, MOD - 2) % MOD; } bitset<300005> like; int64_t w[300005]; int64_t ans[300005]; int64_t dp[3005][3005]; int32_t main() { { cin.tie(0); ios_base::sync_with_stdio(false); }; int64_t n, m, t, add, sub, sum; add = sub = 0; cin >> n >> m; for (int64_t i = 0; i < n; i++) { cin >> t; like[i] = t; } for (int64_t i = 0; i < n; i++) { cin >> w[i]; if (like[i]) add += w[i]; else sub += w[i]; } dp[0][0] = 1; sum = add + sub; for (int64_t x = 1; x <= m; x++) { for (int64_t i = x, j = 0; i >= 0 && j <= sub; i--, j++) { int64_t nowsum = sum + i - j; int64_t nowadd = add + i; int64_t nowsub = sub - j; dp[i][j] = 0; if (i > 0) dp[i][j] = (dp[i][j] + dp[i - 1][j] * mdiv(nowadd - 1, nowsum - 1) % MOD) % MOD; if (j > 0) dp[i][j] = (dp[i][j] + dp[i][j - 1] * mdiv(nowsub + 1, nowsum + 1) % MOD) % MOD; } } for (int64_t i = m, j = 0; i >= 0 && j <= sub; i--, j++) { for (int64_t k = 0; k < n; k++) { if (like[k]) ans[k] = (ans[k] + (w[k] + i * mdiv(w[k], add)) % MOD * dp[i][j]) % MOD; else ans[k] = (ans[k] + (w[k] - j * mdiv(w[k], sub) % MOD + MOD) % MOD * dp[i][j]) % MOD; } } for (int64_t i = 0; i < n; i++) cout << ans[i] << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m; int a[55], w[55]; int A, B; int f[55][55][55]; int inv[3010]; int quick_pow(int x, int p) { int an = 1; int po = x; while (p) { if (p & 1) an = 1ll * an * po % 998244353; po = 1ll * po * po % 998244353; p >>= 1; } return an; } void solve(int id) { memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { if (!f[i][j][k]) continue; if (a[id]) { int nowA = A + k, nowB = B - (i - k), sum = A + B + k - (i - k), noww = w[id] + j; f[i + 1][j + 1][k + 1] += 1ll * f[i][j][k] * noww % 998244353 * inv[sum] % 998244353; f[i + 1][j + 1][k + 1] %= 998244353; f[i + 1][j][k + 1] += 1ll * f[i][j][k] * (nowA - noww) % 998244353 * inv[sum] % 998244353; f[i + 1][j][k + 1] %= 998244353; f[i + 1][j][k] += 1ll * f[i][j][k] * nowB % 998244353 * inv[sum] % 998244353; f[i + 1][j][k] %= 998244353; } else { int nowA = A + k, nowB = B - (i - k), sum = A + B + k - (i - k), noww = w[id] - j; f[i + 1][j + 1][k] += 1ll * f[i][j][k] * noww % 998244353 * inv[sum] % 998244353; f[i + 1][j + 1][k] %= 998244353; f[i + 1][j][k + 1] += 1ll * f[i][j][k] * nowA % 998244353 * inv[sum] % 998244353; f[i + 1][j][k + 1] %= 998244353; f[i + 1][j][k] += 1ll * f[i][j][k] * (nowB - noww) % 998244353 * inv[sum] % 998244353; f[i + 1][j][k] %= 998244353; } } } } int res = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (!f[m][j][k]) continue; if (a[id]) res = (res + 1ll * (w[id] + j) * f[m][j][k] % 998244353) % 998244353; else res = (res + 1ll * (w[id] - j) * f[m][j][k] % 998244353) % 998244353; } } printf("%d\n", res); } int main() { n = read(); m = read(); for (int i = 1; i <= 3000; i++) inv[i] = quick_pow(i, 998244353 - 2); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) w[i] = read(); for (int i = 1; i <= n; i++) A += w[i] * (a[i] == 1), B += w[i] * (a[i] == 0); for (int i = 1; i <= n; i++) solve(i); return 0; }
### Prompt Please formulate a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m; int a[55], w[55]; int A, B; int f[55][55][55]; int inv[3010]; int quick_pow(int x, int p) { int an = 1; int po = x; while (p) { if (p & 1) an = 1ll * an * po % 998244353; po = 1ll * po * po % 998244353; p >>= 1; } return an; } void solve(int id) { memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { if (!f[i][j][k]) continue; if (a[id]) { int nowA = A + k, nowB = B - (i - k), sum = A + B + k - (i - k), noww = w[id] + j; f[i + 1][j + 1][k + 1] += 1ll * f[i][j][k] * noww % 998244353 * inv[sum] % 998244353; f[i + 1][j + 1][k + 1] %= 998244353; f[i + 1][j][k + 1] += 1ll * f[i][j][k] * (nowA - noww) % 998244353 * inv[sum] % 998244353; f[i + 1][j][k + 1] %= 998244353; f[i + 1][j][k] += 1ll * f[i][j][k] * nowB % 998244353 * inv[sum] % 998244353; f[i + 1][j][k] %= 998244353; } else { int nowA = A + k, nowB = B - (i - k), sum = A + B + k - (i - k), noww = w[id] - j; f[i + 1][j + 1][k] += 1ll * f[i][j][k] * noww % 998244353 * inv[sum] % 998244353; f[i + 1][j + 1][k] %= 998244353; f[i + 1][j][k + 1] += 1ll * f[i][j][k] * nowA % 998244353 * inv[sum] % 998244353; f[i + 1][j][k + 1] %= 998244353; f[i + 1][j][k] += 1ll * f[i][j][k] * (nowB - noww) % 998244353 * inv[sum] % 998244353; f[i + 1][j][k] %= 998244353; } } } } int res = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (!f[m][j][k]) continue; if (a[id]) res = (res + 1ll * (w[id] + j) * f[m][j][k] % 998244353) % 998244353; else res = (res + 1ll * (w[id] - j) * f[m][j][k] % 998244353) % 998244353; } } printf("%d\n", res); } int main() { n = read(); m = read(); for (int i = 1; i <= 3000; i++) inv[i] = quick_pow(i, 998244353 - 2); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) w[i] = read(); for (int i = 1; i <= n; i++) A += w[i] * (a[i] == 1), B += w[i] * (a[i] == 0); for (int i = 1; i <= n; i++) solve(i); return 0; } ```
#include <bits/stdc++.h> using namespace std; int64_t mod = 998244353; int64_t mod1 = 1e9 + 5; int64_t power(int64_t a, int64_t b) { if (b == 0) return 1; else if (b % 2 == 0) return power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod; else return (((a) % mod) * (power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod)) % mod; } inline int64_t inverse(int64_t a) { a %= mod; if (a < 0) a += mod; int64_t b = mod, u = 0, v = 1; while (a) { int64_t t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += mod; return u; } const int64_t ce = 1e9 + 7; int64_t fast_mod(int64_t input) { return input < ce ? input : input % ce; } int64_t full, toti, toti1; int64_t n, m; int64_t x; int64_t ch; int64_t dp[50][101][51][51]; int64_t solve(int64_t i, int64_t we, int64_t fav, int64_t fav1) { if (i == m) { return we; } int64_t &res = dp[i][we][fav][fav1]; if (res != -1) return res; int64_t tot = full; tot += fav; tot -= fav1; tot -= x; tot += we; res = 0; if (toti > 0) res += ((toti + fav) * inverse(tot)) % mod * solve(i + 1, we, fav + 1, fav1); res %= mod; if (ch) res += (we * inverse(tot)) % mod * solve(i + 1, we + 1, fav, fav1); else if (we > 0) res += (we * inverse(tot)) % mod * solve(i + 1, we - 1, fav, fav1); res %= mod; if (toti1 - fav1 > 0) res += (max((toti1 - fav1), (int64_t)0) * inverse(tot)) % mod * solve(i + 1, we, fav, fav1 + 1); res %= mod; return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; int64_t a[n + 1]; for (int64_t i = 1; i <= n; i++) cin >> a[i]; full = 0; toti = 0; toti1 = 0; int64_t w[n + 1]; for (int64_t i = 1; i <= n; i++) { cin >> w[i]; full += w[i]; if (a[i]) toti += w[i]; else toti1 += w[i]; } for (int64_t i = 1; i <= n; i++) { if (a[i]) { toti -= w[i]; } else { toti1 -= w[i]; } if (a[i]) ch = 1; else ch = 0; x = w[i]; memset(dp, -1, sizeof(dp)); cout << solve(0, w[i], 0, 0) << '\n'; if (a[i]) { toti += w[i]; } else { toti1 += w[i]; } } }
### Prompt Generate a cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int64_t mod = 998244353; int64_t mod1 = 1e9 + 5; int64_t power(int64_t a, int64_t b) { if (b == 0) return 1; else if (b % 2 == 0) return power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod; else return (((a) % mod) * (power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod)) % mod; } inline int64_t inverse(int64_t a) { a %= mod; if (a < 0) a += mod; int64_t b = mod, u = 0, v = 1; while (a) { int64_t t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += mod; return u; } const int64_t ce = 1e9 + 7; int64_t fast_mod(int64_t input) { return input < ce ? input : input % ce; } int64_t full, toti, toti1; int64_t n, m; int64_t x; int64_t ch; int64_t dp[50][101][51][51]; int64_t solve(int64_t i, int64_t we, int64_t fav, int64_t fav1) { if (i == m) { return we; } int64_t &res = dp[i][we][fav][fav1]; if (res != -1) return res; int64_t tot = full; tot += fav; tot -= fav1; tot -= x; tot += we; res = 0; if (toti > 0) res += ((toti + fav) * inverse(tot)) % mod * solve(i + 1, we, fav + 1, fav1); res %= mod; if (ch) res += (we * inverse(tot)) % mod * solve(i + 1, we + 1, fav, fav1); else if (we > 0) res += (we * inverse(tot)) % mod * solve(i + 1, we - 1, fav, fav1); res %= mod; if (toti1 - fav1 > 0) res += (max((toti1 - fav1), (int64_t)0) * inverse(tot)) % mod * solve(i + 1, we, fav, fav1 + 1); res %= mod; return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; int64_t a[n + 1]; for (int64_t i = 1; i <= n; i++) cin >> a[i]; full = 0; toti = 0; toti1 = 0; int64_t w[n + 1]; for (int64_t i = 1; i <= n; i++) { cin >> w[i]; full += w[i]; if (a[i]) toti += w[i]; else toti1 += w[i]; } for (int64_t i = 1; i <= n; i++) { if (a[i]) { toti -= w[i]; } else { toti1 -= w[i]; } if (a[i]) ch = 1; else ch = 0; x = w[i]; memset(dp, -1, sizeof(dp)); cout << solve(0, w[i], 0, 0) << '\n'; if (a[i]) { toti += w[i]; } else { toti1 += w[i]; } } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int mod = 998244353; long long n, m, cnt[2], sum[2], a[55], p[55]; long long dp[55][55][55], val[55][55][55], vis[55][55][55]; long long qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = a * a % mod; } return ans; } long long inv(long long a) { return qpow(a, mod - 2); } long long dfs(long long c, long long p, long long i, long long j, long long k, long long x, long long y, long long z, long long ff) { if (!c) return p * x % mod; if (vis[i][j][k]) return val[i][j][k] * inv(dp[i][j][k]) % mod * p % mod; dp[i][j][k] = p; val[i][j][k] += dfs(c - 1, x * p % mod * inv(x + y + z) % mod, i + 1, j, k, max(0ll, x + ff), y, z, ff); val[i][j][k] += dfs(c - 1, y * p % mod * inv(x + y + z) % mod, i, j + 1, k, x, y + 1, z, ff); val[i][j][k] += dfs(c - 1, z * p % mod * inv(x + y + z) % mod, i, j, k + 1, x, y, max(0ll, z - 1), ff); val[i][j][k] %= mod; vis[i][j][k]++; return val[i][j][k]; } int main() { scanf("%lld", &n); scanf("%lld", &m); for (int i = 1; i < n + 1; ++i) scanf("%lld", &a[i]), cnt[a[i]]++; for (int i = 1; i < n + 1; ++i) scanf("%lld", &p[i]), sum[a[i]] += p[i]; for (int i = 1; i < n + 1; ++i) { memset(dp, 0, sizeof(dp)); memset(val, 0, sizeof(val)); memset(vis, 0, sizeof(vis)); printf("%lld\n", dfs(m, 1, 0, 0, 0, p[i], sum[1] - a[i] * p[i], sum[0] - (1 - a[i]) * p[i], a[i] * 2 - 1)); } }
### Prompt Please provide a CPP coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int mod = 998244353; long long n, m, cnt[2], sum[2], a[55], p[55]; long long dp[55][55][55], val[55][55][55], vis[55][55][55]; long long qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = a * a % mod; } return ans; } long long inv(long long a) { return qpow(a, mod - 2); } long long dfs(long long c, long long p, long long i, long long j, long long k, long long x, long long y, long long z, long long ff) { if (!c) return p * x % mod; if (vis[i][j][k]) return val[i][j][k] * inv(dp[i][j][k]) % mod * p % mod; dp[i][j][k] = p; val[i][j][k] += dfs(c - 1, x * p % mod * inv(x + y + z) % mod, i + 1, j, k, max(0ll, x + ff), y, z, ff); val[i][j][k] += dfs(c - 1, y * p % mod * inv(x + y + z) % mod, i, j + 1, k, x, y + 1, z, ff); val[i][j][k] += dfs(c - 1, z * p % mod * inv(x + y + z) % mod, i, j, k + 1, x, y, max(0ll, z - 1), ff); val[i][j][k] %= mod; vis[i][j][k]++; return val[i][j][k]; } int main() { scanf("%lld", &n); scanf("%lld", &m); for (int i = 1; i < n + 1; ++i) scanf("%lld", &a[i]), cnt[a[i]]++; for (int i = 1; i < n + 1; ++i) scanf("%lld", &p[i]), sum[a[i]] += p[i]; for (int i = 1; i < n + 1; ++i) { memset(dp, 0, sizeof(dp)); memset(val, 0, sizeof(val)); memset(vis, 0, sizeof(vis)); printf("%lld\n", dfs(m, 1, 0, 0, 0, p[i], sum[1] - a[i] * p[i], sum[0] - (1 - a[i]) * p[i], a[i] * 2 - 1)); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 55; const long long mod = 998244353; long long fpow(long long x, long long y) { x %= mod; long long ret = 1; while (y) { if (y & 1) ret = (long long)ret * x % mod; y >>= 1; x = (long long)x * x % mod; } return ret; } int n, m; int a[N]; int w[N]; long long f[N][N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } int totlike = 0, totdislike = 0; for (int i = 1; i <= n; ++i) { cin >> w[i]; if (a[i]) totlike += w[i]; else totdislike += w[i]; } for (int i = 1; i <= n; ++i) { memset(f, 0, sizeof f); int sumlike = totlike - a[i] * w[i]; int sumdislike = totdislike - (1 - a[i]) * w[i]; f[0][0][0] = 1; for (int d = 0; d < m; ++d) { for (int like = 0; like <= d; ++like) { for (int dislike = 0; dislike <= d - like; ++dislike) { if (!f[d][like][dislike]) continue; int delta = d - like - dislike; int sum = sumlike + like + sumdislike - dislike + (w[i] + (a[i] ? 1 : -1) * delta); int prob = fpow(sum, mod - 2); if (a[i] || delta < w[i]) f[d + 1][like][dislike] += (f[d][like][dislike] * (w[i] + (a[i] ? 1 : -1) * delta) % mod) * prob % mod; f[d + 1][like + 1][dislike] += (f[d][like][dislike] * (sumlike + like) % mod) * prob % mod; if (dislike < sumdislike) f[d + 1][like][dislike + 1] += (f[d][like][dislike] * prob % mod) * (sumdislike - dislike) % mod; } } } long long ret = 0; for (int like = 0; like <= m; ++like) { for (int dislike = 0; dislike <= m - like; ++dislike) { int delta = m - like - dislike; ret += (w[i] + (a[i] ? 1 : -1) * delta) * f[m][like][dislike] % mod; ret %= mod; } } cout << ret << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 55; const long long mod = 998244353; long long fpow(long long x, long long y) { x %= mod; long long ret = 1; while (y) { if (y & 1) ret = (long long)ret * x % mod; y >>= 1; x = (long long)x * x % mod; } return ret; } int n, m; int a[N]; int w[N]; long long f[N][N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } int totlike = 0, totdislike = 0; for (int i = 1; i <= n; ++i) { cin >> w[i]; if (a[i]) totlike += w[i]; else totdislike += w[i]; } for (int i = 1; i <= n; ++i) { memset(f, 0, sizeof f); int sumlike = totlike - a[i] * w[i]; int sumdislike = totdislike - (1 - a[i]) * w[i]; f[0][0][0] = 1; for (int d = 0; d < m; ++d) { for (int like = 0; like <= d; ++like) { for (int dislike = 0; dislike <= d - like; ++dislike) { if (!f[d][like][dislike]) continue; int delta = d - like - dislike; int sum = sumlike + like + sumdislike - dislike + (w[i] + (a[i] ? 1 : -1) * delta); int prob = fpow(sum, mod - 2); if (a[i] || delta < w[i]) f[d + 1][like][dislike] += (f[d][like][dislike] * (w[i] + (a[i] ? 1 : -1) * delta) % mod) * prob % mod; f[d + 1][like + 1][dislike] += (f[d][like][dislike] * (sumlike + like) % mod) * prob % mod; if (dislike < sumdislike) f[d + 1][like][dislike + 1] += (f[d][like][dislike] * prob % mod) * (sumdislike - dislike) % mod; } } } long long ret = 0; for (int like = 0; like <= m; ++like) { for (int dislike = 0; dislike <= m - like; ++dislike) { int delta = m - like - dislike; ret += (w[i] + (a[i] ? 1 : -1) * delta) * f[m][like][dislike] % mod; ret %= mod; } } cout << ret << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<long long> a, b; int cur = 0; long long su = 0; long long gpos = 0; long long gneg = 0; long long mod = 998244353; long long fpow(long long a, long long b, long long md) { long long res = 1; a = (a % md); while (b > 0) { if (b % 2 == 1) { res = (res * a) % md; } b = b / 2; a = (a * a) % md; } return res % md; } map<pair<pair<int, int>, pair<int, int> >, long long> dp; int n, m; long long f(int i, int my, int pos, int neg) { if (i == m) { return (b[cur] + my * a[cur]) % mod; } if (dp.count(make_pair(make_pair(i, my), make_pair(pos, neg)))) { return dp[make_pair(make_pair(i, my), make_pair(pos, neg))]; } long long denom = su + pos - neg; long long f1, f2, f3; f1 = f2 = f3 = 0; if (a[cur] == 1) { long long prob = b[cur] + my; if (prob > 0) { prob = (prob * fpow(denom, mod - 2, mod)) % mod; f1 = (prob * f(i + 1, my + 1, pos + 1, neg)) % mod; } long long probpos = gpos + pos - my - b[cur]; if (probpos > 0) { probpos = (probpos * fpow(denom, mod - 2, mod)) % mod; f2 = (probpos * f(i + 1, my, pos + 1, neg)) % mod; } long long probneg = gneg - neg; if (probneg > 0) { probneg = (probneg * fpow(denom, mod - 2, mod)) % mod; f3 = (probneg * f(i + 1, my, pos, neg + 1)) % mod; } dp[make_pair(make_pair(i, my), make_pair(pos, neg))] = (f1 + f2 + f3) % mod; return (f1 + f2 + f3) % mod; } else { long long prob = b[cur] - my; if (prob > 0) { prob = (prob * fpow(denom, mod - 2, mod)) % mod; f1 = (prob * f(i + 1, my + 1, pos, neg + 1)) % mod; } long long probpos = gpos + pos; if (probpos > 0) { probpos = (probpos * fpow(denom, mod - 2, mod)) % mod; f2 = (probpos * f(i + 1, my, pos + 1, neg)) % mod; } long long probneg = gneg - neg - b[cur] + my; if (probneg > 0) { probneg = (probneg * fpow(denom, mod - 2, mod)) % mod; f3 = (probneg * f(i + 1, my, pos, neg + 1)) % mod; } dp[make_pair(make_pair(i, my), make_pair(pos, neg))] = (f1 + f2 + f3) % mod; return (f1 + f2 + f3) % mod; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n >> m; a.resize(n + 1); b.resize(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; if (a[i] == 0) { a[i] = -1; gneg += b[i]; } else { gpos += b[i]; } su += b[i]; } for (int i = 0; i < n; i++) { dp.clear(); cur = i; cout << f(0, 0, 0, 0) << '\n'; } }
### Prompt Your challenge is to write a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<long long> a, b; int cur = 0; long long su = 0; long long gpos = 0; long long gneg = 0; long long mod = 998244353; long long fpow(long long a, long long b, long long md) { long long res = 1; a = (a % md); while (b > 0) { if (b % 2 == 1) { res = (res * a) % md; } b = b / 2; a = (a * a) % md; } return res % md; } map<pair<pair<int, int>, pair<int, int> >, long long> dp; int n, m; long long f(int i, int my, int pos, int neg) { if (i == m) { return (b[cur] + my * a[cur]) % mod; } if (dp.count(make_pair(make_pair(i, my), make_pair(pos, neg)))) { return dp[make_pair(make_pair(i, my), make_pair(pos, neg))]; } long long denom = su + pos - neg; long long f1, f2, f3; f1 = f2 = f3 = 0; if (a[cur] == 1) { long long prob = b[cur] + my; if (prob > 0) { prob = (prob * fpow(denom, mod - 2, mod)) % mod; f1 = (prob * f(i + 1, my + 1, pos + 1, neg)) % mod; } long long probpos = gpos + pos - my - b[cur]; if (probpos > 0) { probpos = (probpos * fpow(denom, mod - 2, mod)) % mod; f2 = (probpos * f(i + 1, my, pos + 1, neg)) % mod; } long long probneg = gneg - neg; if (probneg > 0) { probneg = (probneg * fpow(denom, mod - 2, mod)) % mod; f3 = (probneg * f(i + 1, my, pos, neg + 1)) % mod; } dp[make_pair(make_pair(i, my), make_pair(pos, neg))] = (f1 + f2 + f3) % mod; return (f1 + f2 + f3) % mod; } else { long long prob = b[cur] - my; if (prob > 0) { prob = (prob * fpow(denom, mod - 2, mod)) % mod; f1 = (prob * f(i + 1, my + 1, pos, neg + 1)) % mod; } long long probpos = gpos + pos; if (probpos > 0) { probpos = (probpos * fpow(denom, mod - 2, mod)) % mod; f2 = (probpos * f(i + 1, my, pos + 1, neg)) % mod; } long long probneg = gneg - neg - b[cur] + my; if (probneg > 0) { probneg = (probneg * fpow(denom, mod - 2, mod)) % mod; f3 = (probneg * f(i + 1, my, pos, neg + 1)) % mod; } dp[make_pair(make_pair(i, my), make_pair(pos, neg))] = (f1 + f2 + f3) % mod; return (f1 + f2 + f3) % mod; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n >> m; a.resize(n + 1); b.resize(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; if (a[i] == 0) { a[i] = -1; gneg += b[i]; } else { gpos += b[i]; } su += b[i]; } for (int i = 0; i < n; i++) { dp.clear(); cur = i; cout << f(0, 0, 0, 0) << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long pow_mod(long long a, long long p) { if (p == 0) return 1; long long ret = pow_mod(a, p / 2); ret = ret * ret % mod; if (p % 2 == 1) ret = ret * a % mod; return ret; } long long _inv(long long a) { return pow_mod(a, mod - 2); } long long dp[55][55][55], inv[3005]; int w[55], a[55]; int main() { inv[0] = 1; for (int i = 1; i < 3005; i++) inv[i] = _inv(i); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; int like = 0, dislike = 0, tot = 0; for (int i = 1; i <= n; i++) { if (a[i] == 0) dislike += w[i]; else like += w[i]; tot += w[i]; } for (int i = 1; i <= n; i++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int j = 0; j < m; j++) for (int k = 0; k <= j; k++) for (int l = 0; l <= j; l++) { int cur = tot + 2 * l - j; if (cur < 0) continue; if (a[i] == 1) { int p1 = w[i] + k; int p2 = like + l - w[i] - k; int p3 = cur - like - l; dp[j + 1][k + 1][l + 1] = (dp[j + 1][k + 1][l + 1] + dp[j][k][l] * p1 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l + 1] = (dp[j + 1][k][l + 1] + dp[j][k][l] * p2 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l] = (dp[j + 1][k][l] + dp[j][k][l] * p3 % mod * inv[cur] % mod) % mod; } else if (a[i] == 0 && k <= w[i] && (j - l) <= dislike) { int p1 = w[i] - k; int p2 = dislike - (j - l) - (w[i] - k); int p3 = cur - dislike + (j - l); dp[j + 1][k + 1][l] = (dp[j + 1][k + 1][l] + dp[j][k][l] * p1 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l] = (dp[j + 1][k][l] + dp[j][k][l] * p2 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l + 1] = (dp[j + 1][k][l + 1] + dp[j][k][l] * p3 % mod * inv[cur] % mod) % mod; } } long long ans = 0; for (int k = 0; k <= m; k++) for (int l = 0; l <= m; l++) { if (a[i] == 1) ans = (ans + dp[m][k][l] * (w[i] + k)) % mod; else if (k <= w[i]) ans = (ans + dp[m][k][l] * (w[i] - k)) % mod; } cout << ans << endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long pow_mod(long long a, long long p) { if (p == 0) return 1; long long ret = pow_mod(a, p / 2); ret = ret * ret % mod; if (p % 2 == 1) ret = ret * a % mod; return ret; } long long _inv(long long a) { return pow_mod(a, mod - 2); } long long dp[55][55][55], inv[3005]; int w[55], a[55]; int main() { inv[0] = 1; for (int i = 1; i < 3005; i++) inv[i] = _inv(i); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; int like = 0, dislike = 0, tot = 0; for (int i = 1; i <= n; i++) { if (a[i] == 0) dislike += w[i]; else like += w[i]; tot += w[i]; } for (int i = 1; i <= n; i++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int j = 0; j < m; j++) for (int k = 0; k <= j; k++) for (int l = 0; l <= j; l++) { int cur = tot + 2 * l - j; if (cur < 0) continue; if (a[i] == 1) { int p1 = w[i] + k; int p2 = like + l - w[i] - k; int p3 = cur - like - l; dp[j + 1][k + 1][l + 1] = (dp[j + 1][k + 1][l + 1] + dp[j][k][l] * p1 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l + 1] = (dp[j + 1][k][l + 1] + dp[j][k][l] * p2 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l] = (dp[j + 1][k][l] + dp[j][k][l] * p3 % mod * inv[cur] % mod) % mod; } else if (a[i] == 0 && k <= w[i] && (j - l) <= dislike) { int p1 = w[i] - k; int p2 = dislike - (j - l) - (w[i] - k); int p3 = cur - dislike + (j - l); dp[j + 1][k + 1][l] = (dp[j + 1][k + 1][l] + dp[j][k][l] * p1 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l] = (dp[j + 1][k][l] + dp[j][k][l] * p2 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l + 1] = (dp[j + 1][k][l + 1] + dp[j][k][l] * p3 % mod * inv[cur] % mod) % mod; } } long long ans = 0; for (int k = 0; k <= m; k++) for (int l = 0; l <= m; l++) { if (a[i] == 1) ans = (ans + dp[m][k][l] * (w[i] + k)) % mod; else if (k <= w[i]) ans = (ans + dp[m][k][l] * (w[i] - k)) % mod; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int rw = 998244353; int n, m; int a[52], w[52], sum1, sum2; int dp[52][52][52]; int ni[100005]; int ksm(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1LL * x * ans % rw; x = 1LL * x * x % rw; y >>= 1; } return ans; } int solve(int r) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { if (!dp[i][j][k]) continue; int v = dp[i][j][k]; if (a[r]) { dp[i + 1][j + 1][k + 1] = (dp[i + 1][j + 1][k + 1] + 1LL * v * (w[r] + j) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + 1LL * v * (sum1 + k - (w[r] + j)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k] = (dp[i + 1][j][k] + 1LL * v * (sum2 - (i - k)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; } else { dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + 1LL * v * (w[r] - j) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k] = (dp[i + 1][j][k] + 1LL * v * (sum2 - (i - k) - (w[r] - j)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + 1LL * v * (sum1 + k) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; } } } } int ans = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (!dp[m][j][k]) continue; int v; if (a[r]) v = w[r] + j; else v = w[r] - j; ans = (ans + 1LL * v * dp[m][j][k]) % rw; } } return (ans + rw) % rw; } int main() { for (int i = 1; i <= 100000; i++) ni[i] = ksm(i, rw - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); if (a[i]) (sum1 += w[i]) %= rw; else (sum2 += w[i]) %= rw; } for (int i = 1; i <= n; i++) { printf("%d\n", solve(i)); } }
### Prompt Develop a solution in CPP to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int rw = 998244353; int n, m; int a[52], w[52], sum1, sum2; int dp[52][52][52]; int ni[100005]; int ksm(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1LL * x * ans % rw; x = 1LL * x * x % rw; y >>= 1; } return ans; } int solve(int r) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { if (!dp[i][j][k]) continue; int v = dp[i][j][k]; if (a[r]) { dp[i + 1][j + 1][k + 1] = (dp[i + 1][j + 1][k + 1] + 1LL * v * (w[r] + j) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + 1LL * v * (sum1 + k - (w[r] + j)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k] = (dp[i + 1][j][k] + 1LL * v * (sum2 - (i - k)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; } else { dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + 1LL * v * (w[r] - j) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k] = (dp[i + 1][j][k] + 1LL * v * (sum2 - (i - k) - (w[r] - j)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + 1LL * v * (sum1 + k) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; } } } } int ans = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (!dp[m][j][k]) continue; int v; if (a[r]) v = w[r] + j; else v = w[r] - j; ans = (ans + 1LL * v * dp[m][j][k]) % rw; } } return (ans + rw) % rw; } int main() { for (int i = 1; i <= 100000; i++) ni[i] = ksm(i, rw - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); if (a[i]) (sum1 += w[i]) %= rw; else (sum2 += w[i]) %= rw; } for (int i = 1; i <= n; i++) { printf("%d\n", solve(i)); } } ```
#include <bits/stdc++.h> using namespace std; namespace FastIO { const int L = (1 << 20); char buf[L], *S, *T; inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, L, stdin); if (S == T) return EOF; } return *S++; } inline int read() { int s = 0, f = 1; char t = getchar(); while ('0' > t || t > '9') { if (t == '-') f = -1; t = getchar(); } while ('0' <= t && t <= '9') { s = (s << 1) + (s << 3) + t - '0'; t = getchar(); } return s * f; } } // namespace FastIO using FastIO::read; const int N = 55; const int Mod = 998244353; int dp[N][N][N * 2], sum, Sadd, Ssub; int w[N], a[N], n, m; int Inc(int x, int y) { return x + y >= Mod ? x + y - Mod : x + y; } int Dec(int x, int y) { return x - y < 0 ? x - y + Mod : x - y; } int Mul(int x, int y) { return (long long)x * y % Mod; } int Fpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = Mul(ans, a); b >>= 1; a = Mul(a, a); } return ans; } int Inv(int x) { return Fpow(x, Mod - 2); } void Dp(int c) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int k = 0; k <= i; k++) { int now = (a[c] ? w[c] + k : w[c] - k); if (now < 0) continue; for (int d = 0; d <= i; d++) { if (!dp[i][k][d]) continue; int Dw = sum + d - (i - d); Dw = Inv(Dw); int Up_add = Sadd + d; int Up_sub = Ssub - (i - d); if (a[c]) { dp[i + 1][k + 1][d + 1] = Inc(dp[i + 1][k + 1][d + 1], Mul(dp[i][k][d], Mul(now, Dw))); dp[i + 1][k][d + 1] = Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add - now, Dw))); dp[i + 1][k][d] = Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub, Dw))); } else { dp[i + 1][k + 1][d] = Inc(dp[i + 1][k + 1][d], Mul(dp[i][k][d], Mul(now, Dw))); dp[i + 1][k][d + 1] = Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add, Dw))); dp[i + 1][k][d] = Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub - now, Dw))); } } } } int Ans = 0; for (int k = 0; k <= m; k++) { int now = (a[c] ? w[c] + k : w[c] - k); if (now < 0) continue; for (int d = 0; d <= m; d++) { Ans = Inc(Ans, Mul(now, dp[m][k][d])); } } cout << Ans << '\n'; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) { w[i] = read(); if (a[i]) Sadd += w[i]; else Ssub += w[i]; sum += w[i]; } for (int i = 1; i <= n; i++) Dp(i); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; namespace FastIO { const int L = (1 << 20); char buf[L], *S, *T; inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, L, stdin); if (S == T) return EOF; } return *S++; } inline int read() { int s = 0, f = 1; char t = getchar(); while ('0' > t || t > '9') { if (t == '-') f = -1; t = getchar(); } while ('0' <= t && t <= '9') { s = (s << 1) + (s << 3) + t - '0'; t = getchar(); } return s * f; } } // namespace FastIO using FastIO::read; const int N = 55; const int Mod = 998244353; int dp[N][N][N * 2], sum, Sadd, Ssub; int w[N], a[N], n, m; int Inc(int x, int y) { return x + y >= Mod ? x + y - Mod : x + y; } int Dec(int x, int y) { return x - y < 0 ? x - y + Mod : x - y; } int Mul(int x, int y) { return (long long)x * y % Mod; } int Fpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = Mul(ans, a); b >>= 1; a = Mul(a, a); } return ans; } int Inv(int x) { return Fpow(x, Mod - 2); } void Dp(int c) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int k = 0; k <= i; k++) { int now = (a[c] ? w[c] + k : w[c] - k); if (now < 0) continue; for (int d = 0; d <= i; d++) { if (!dp[i][k][d]) continue; int Dw = sum + d - (i - d); Dw = Inv(Dw); int Up_add = Sadd + d; int Up_sub = Ssub - (i - d); if (a[c]) { dp[i + 1][k + 1][d + 1] = Inc(dp[i + 1][k + 1][d + 1], Mul(dp[i][k][d], Mul(now, Dw))); dp[i + 1][k][d + 1] = Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add - now, Dw))); dp[i + 1][k][d] = Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub, Dw))); } else { dp[i + 1][k + 1][d] = Inc(dp[i + 1][k + 1][d], Mul(dp[i][k][d], Mul(now, Dw))); dp[i + 1][k][d + 1] = Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add, Dw))); dp[i + 1][k][d] = Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub - now, Dw))); } } } } int Ans = 0; for (int k = 0; k <= m; k++) { int now = (a[c] ? w[c] + k : w[c] - k); if (now < 0) continue; for (int d = 0; d <= m; d++) { Ans = Inc(Ans, Mul(now, dp[m][k][d])); } } cout << Ans << '\n'; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) { w[i] = read(); if (a[i]) Sadd += w[i]; else Ssub += w[i]; sum += w[i]; } for (int i = 1; i <= n; i++) Dp(i); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a[55], w[55], dp[55][55][55]; long long fast(long long x, long long b, long long res = 1) { while (b) { if (b & 1) res = res * x % 998244353; x = x * x % 998244353; b >>= 1; } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; long long sum = 0, s[2] = {0, 0}; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> w[i], sum += w[i]; s[a[i]] += w[i]; } for (int q = 1; q <= n; q++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long ans = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= j; k++) { long long tmp = (a[q] == 1 ? 1 : -1); long long meow = fast(sum + tmp * (2 * j - i - 1), 998244353 - 2); if (s[1 - a[q]] > 0 && s[1 - a[q]] - tmp * (i - j - 1) > 0 && sum + tmp * (2 * j - i + 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k] * (s[1 - a[q]] - tmp * (i - j - 1)) % 998244353 * fast(sum + tmp * (2 * j - i + 1), 998244353 - 2) % 998244353) % 998244353; } if (s[a[q]] - w[q] > 0 && j > 0 && s[a[q]] + tmp * (j - 1 - k) - w[q] > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k] * (s[a[q]] + tmp * (j - 1 - k) - w[q]) % 998244353 * meow) % 998244353; } if (j > 0 && k > 0 && w[q] + tmp * (k - 1) > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k - 1] * (w[q] + tmp * (k - 1)) % 998244353 * meow) % 998244353; } if (i == m) ans = (ans + (w[q] + tmp * k) % 998244353 * dp[i][j][k] % 998244353) % 998244353; } } } cout << ans << '\n'; } return 0; }
### Prompt Generate a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[55], w[55], dp[55][55][55]; long long fast(long long x, long long b, long long res = 1) { while (b) { if (b & 1) res = res * x % 998244353; x = x * x % 998244353; b >>= 1; } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; long long sum = 0, s[2] = {0, 0}; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> w[i], sum += w[i]; s[a[i]] += w[i]; } for (int q = 1; q <= n; q++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long ans = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= j; k++) { long long tmp = (a[q] == 1 ? 1 : -1); long long meow = fast(sum + tmp * (2 * j - i - 1), 998244353 - 2); if (s[1 - a[q]] > 0 && s[1 - a[q]] - tmp * (i - j - 1) > 0 && sum + tmp * (2 * j - i + 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k] * (s[1 - a[q]] - tmp * (i - j - 1)) % 998244353 * fast(sum + tmp * (2 * j - i + 1), 998244353 - 2) % 998244353) % 998244353; } if (s[a[q]] - w[q] > 0 && j > 0 && s[a[q]] + tmp * (j - 1 - k) - w[q] > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k] * (s[a[q]] + tmp * (j - 1 - k) - w[q]) % 998244353 * meow) % 998244353; } if (j > 0 && k > 0 && w[q] + tmp * (k - 1) > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k - 1] * (w[q] + tmp * (k - 1)) % 998244353 * meow) % 998244353; } if (i == m) ans = (ans + (w[q] + tmp * k) % 998244353 * dp[i][j][k] % 998244353) % 998244353; } } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long rev(const long long x) { if (x == 0) return 0; if (x == 1) return 1; return (998244353LL - 998244353LL / x) * rev(998244353LL % x) % 998244353LL; } long long a[55LL], w[55LL], SA, SB, revv[8 * 55LL * 55LL]; map<long long, long long> f, g; int n, m; long long df(int j, int i, int k, int l) { if (f.count(j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l)) return f[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l]; long long c = 0; if (i == 0) c = j; else { if (j) c = j * revv[k + l] % 998244353LL * df(j + 1, i - 1, k + 1, l) % 998244353LL; if (k > j) c = c + (k - j) * revv[k + l] % 998244353LL * df(j, i - 1, k + 1, l) % 998244353LL; if (l) c = c + l * revv[k + l] % 998244353LL * df(j, i - 1, k, l - 1) % 998244353LL; } c %= 998244353LL; f[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l] = c; return c; } long long dg(int j, int i, int k, int l) { if (g.count(j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l)) return g[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l]; long long c = 0; if (i == 0) c = j; else { if (k) c = k * revv[k + l] % 998244353LL * dg(j, i - 1, k + 1, l) % 998244353LL; if (j) c = c + j * revv[k + l] % 998244353LL * dg(j - 1, i - 1, k, l - 1) % 998244353LL; if (l > j) c = c + (l - j) * revv[k + l] % 998244353LL * dg(j, i - 1, k, l - 1) % 998244353LL; } c %= 998244353LL; g[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l] = c; return c; } int main() { for (int i = 0; i < 8 * 55LL * 55LL; i++) revv[i] = rev(i); n = read(); m = read(); for (int i = 0; i < n; i++) a[i] = read(); for (int i = 0; i < n; i++) w[i] = read(); for (int i = 0; i < n; i++) if (a[i]) SA += w[i]; else SB += w[i]; for (int i = 0; i < n; i++) if (a[i]) printf("%lld\n", df(w[i], m, SA, SB)); else printf("%lld\n", dg(w[i], m, SA, SB)); return 0; }
### Prompt Generate a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long rev(const long long x) { if (x == 0) return 0; if (x == 1) return 1; return (998244353LL - 998244353LL / x) * rev(998244353LL % x) % 998244353LL; } long long a[55LL], w[55LL], SA, SB, revv[8 * 55LL * 55LL]; map<long long, long long> f, g; int n, m; long long df(int j, int i, int k, int l) { if (f.count(j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l)) return f[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l]; long long c = 0; if (i == 0) c = j; else { if (j) c = j * revv[k + l] % 998244353LL * df(j + 1, i - 1, k + 1, l) % 998244353LL; if (k > j) c = c + (k - j) * revv[k + l] % 998244353LL * df(j, i - 1, k + 1, l) % 998244353LL; if (l) c = c + l * revv[k + l] % 998244353LL * df(j, i - 1, k, l - 1) % 998244353LL; } c %= 998244353LL; f[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l] = c; return c; } long long dg(int j, int i, int k, int l) { if (g.count(j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l)) return g[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l]; long long c = 0; if (i == 0) c = j; else { if (k) c = k * revv[k + l] % 998244353LL * dg(j, i - 1, k + 1, l) % 998244353LL; if (j) c = c + j * revv[k + l] % 998244353LL * dg(j - 1, i - 1, k, l - 1) % 998244353LL; if (l > j) c = c + (l - j) * revv[k + l] % 998244353LL * dg(j, i - 1, k, l - 1) % 998244353LL; } c %= 998244353LL; g[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l] = c; return c; } int main() { for (int i = 0; i < 8 * 55LL * 55LL; i++) revv[i] = rev(i); n = read(); m = read(); for (int i = 0; i < n; i++) a[i] = read(); for (int i = 0; i < n; i++) w[i] = read(); for (int i = 0; i < n; i++) if (a[i]) SA += w[i]; else SB += w[i]; for (int i = 0; i < n; i++) if (a[i]) printf("%lld\n", df(w[i], m, SA, SB)); else printf("%lld\n", dg(w[i], m, SA, SB)); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int N = 200005, M = 3003; int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; inline long long qp(long long a, long long b) { if (b < 0) return b; if (b == 0) return 1; long long rt = 1; while (b) { if (b & 1) rt *= a; a = a * a; b >>= 1; rt %= mod, a %= mod; } return rt; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int i, j; cin >> n >> m; for (i = 1; i <= n; ++i) cin >> a[i]; for (i = 1; i <= n; ++i) { cin >> w[i]; sum[a[i]] += w[i]; sum[2] += w[i]; } for (i = max(0, m - sum[0]); i <= m * 2; ++i) inv[i] = qp(sum[2] + i - m, mod - 2); for (i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (j = min(m - i - 1, sum[0]); ~j; --j) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (i = 1; i <= n; ++i) cout << ((long long)w[i] * (a[i] ? f[0][0] : g[0][0])) % mod << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int N = 200005, M = 3003; int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; inline long long qp(long long a, long long b) { if (b < 0) return b; if (b == 0) return 1; long long rt = 1; while (b) { if (b & 1) rt *= a; a = a * a; b >>= 1; rt %= mod, a %= mod; } return rt; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int i, j; cin >> n >> m; for (i = 1; i <= n; ++i) cin >> a[i]; for (i = 1; i <= n; ++i) { cin >> w[i]; sum[a[i]] += w[i]; sum[2] += w[i]; } for (i = max(0, m - sum[0]); i <= m * 2; ++i) inv[i] = qp(sum[2] + i - m, mod - 2); for (i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (j = min(m - i - 1, sum[0]); ~j; --j) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (i = 1; i <= n; ++i) cout << ((long long)w[i] * (a[i] ? f[0][0] : g[0][0])) % mod << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; void READ(vector<long long> &v, long long n) { long long a; for (long long i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } } void PRINT(vector<long long> &v) { long long a; for (long long i = 0; i < v.size(); i = i + 1) { cout << v[i] << " "; } cout << "\n"; } long long power(long long k, long long n, long long m = 998244353) { long long res = 1; while (n) { if (n % 2 != 0) { res = (res * k) % m; } k = (k * k) % m; n = n / 2; } return (res); } long long fact(long long n, long long m = 998244353) { long long a = 1; if (n == 0 || n == 1) { return (1); } while (n > 0) { a = (a % m * n % m) % m; n--; } return (a); } long long n, m, k; long long good, bad; long long dp[3003][3003]; long long solve(long long i, long long j) { if (i == 0 && j == 0) { return (1); } if (i < 0 || j < 0) { return (0); } if (dp[i][j] != -1) { return (dp[i][j]); } dp[i][j] = ((solve(i - 1, j) % 998244353 * (good + i - 1) % 998244353 * power(good + bad + i - 1 - j, 998244353 - 2) % 998244353) % 998244353 + (solve(i, j - 1) % 998244353 * (bad - j + 1) % 998244353 * power(good + bad + i - j + 1, 998244353 - 2) % 998244353)) % 998244353; return (dp[i][j]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); std::cout.unsetf(std::ios::floatfield); std::cout.precision(6); long long a, b, c, d, e, x, i, j, t, y, w; double p, q, r, u; char ch, chr; cin >> n >> m; vector<long long> v; memset(dp, -1, sizeof(dp)); for (i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } good = bad = 0; vector<long long> v1; x = 0; y = 0; for (i = 0; i < n; i = i + 1) { cin >> a; if (v[i]) { good += a; } else { bad += a; } v1.push_back(a); } x = y = 0; for (i = 0; i <= m; i = i + 1) { x = (x % 998244353 + (solve(i, m - i) % 998244353 * i % 998244353) % 998244353) % 998244353; y = (y % 998244353 + 998244353 - (solve(i, m - i) % 998244353 * (m - i) % 998244353) % 998244353) % 998244353; } a = power(good, 998244353 - 2); b = power(bad, 998244353 - 2); for (i = 0; i < n; i = i + 1) { if (v[i]) { cout << (v1[i] + x % 998244353 * v1[i] % 998244353 * a % 998244353) % 998244353 << "\n"; } else { cout << (v1[i] + (y % 998244353 * v1[i] % 998244353 * b % 998244353) % 998244353) % 998244353 << "\n"; } } }
### Prompt In CPP, your task is to solve the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void READ(vector<long long> &v, long long n) { long long a; for (long long i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } } void PRINT(vector<long long> &v) { long long a; for (long long i = 0; i < v.size(); i = i + 1) { cout << v[i] << " "; } cout << "\n"; } long long power(long long k, long long n, long long m = 998244353) { long long res = 1; while (n) { if (n % 2 != 0) { res = (res * k) % m; } k = (k * k) % m; n = n / 2; } return (res); } long long fact(long long n, long long m = 998244353) { long long a = 1; if (n == 0 || n == 1) { return (1); } while (n > 0) { a = (a % m * n % m) % m; n--; } return (a); } long long n, m, k; long long good, bad; long long dp[3003][3003]; long long solve(long long i, long long j) { if (i == 0 && j == 0) { return (1); } if (i < 0 || j < 0) { return (0); } if (dp[i][j] != -1) { return (dp[i][j]); } dp[i][j] = ((solve(i - 1, j) % 998244353 * (good + i - 1) % 998244353 * power(good + bad + i - 1 - j, 998244353 - 2) % 998244353) % 998244353 + (solve(i, j - 1) % 998244353 * (bad - j + 1) % 998244353 * power(good + bad + i - j + 1, 998244353 - 2) % 998244353)) % 998244353; return (dp[i][j]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); std::cout.unsetf(std::ios::floatfield); std::cout.precision(6); long long a, b, c, d, e, x, i, j, t, y, w; double p, q, r, u; char ch, chr; cin >> n >> m; vector<long long> v; memset(dp, -1, sizeof(dp)); for (i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } good = bad = 0; vector<long long> v1; x = 0; y = 0; for (i = 0; i < n; i = i + 1) { cin >> a; if (v[i]) { good += a; } else { bad += a; } v1.push_back(a); } x = y = 0; for (i = 0; i <= m; i = i + 1) { x = (x % 998244353 + (solve(i, m - i) % 998244353 * i % 998244353) % 998244353) % 998244353; y = (y % 998244353 + 998244353 - (solve(i, m - i) % 998244353 * (m - i) % 998244353) % 998244353) % 998244353; } a = power(good, 998244353 - 2); b = power(bad, 998244353 - 2); for (i = 0; i < n; i = i + 1) { if (v[i]) { cout << (v1[i] + x % 998244353 * v1[i] % 998244353 * a % 998244353) % 998244353 << "\n"; } else { cout << (v1[i] + (y % 998244353 * v1[i] % 998244353 * b % 998244353) % 998244353) % 998244353 << "\n"; } } } ```
#include <bits/stdc++.h> using namespace std; constexpr int MOD = 998244353; int n, m, a[200001], w[200001], dp[3001][3001], sum[2], inv[10001], ans[2]; int ksm(int a, int b) { int s = 1; while (b) { if (b & 1) { s = s * static_cast<long long>(a) % MOD; } a = a * static_cast<long long>(a) % MOD; b >>= 1; } return s; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> w[i]; sum[a[i]] += w[i]; } for (int i = -m; i <= m; i++) { inv[i + m] = ksm((sum[0] + sum[1] + i + MOD) % MOD, MOD - 2); } dp[0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp[i][j + 1] = (dp[i][j + 1] + dp[i][j] * static_cast<long long>(sum[0] - j) % MOD * inv[i - j + m] % MOD) % MOD; dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * static_cast<long long>(sum[1] + i) % MOD * inv[i - j + m] % MOD) % MOD; } } for (int i = 0; i <= m; i++) { ans[0] = (ans[0] + dp[m - i][i] * static_cast<long long>(sum[0] - i) % MOD) % MOD; ans[1] = (ans[1] + dp[i][m - i] * static_cast<long long>(sum[1] + i) % MOD) % MOD; } ans[0] = ans[0] * static_cast<long long>(ksm(sum[0], MOD - 2)) % MOD; ans[1] = ans[1] * static_cast<long long>(ksm(sum[1], MOD - 2)) % MOD; for (int i = 1; i <= n; i++) { cout << w[i] * static_cast<long long>(ans[a[i]]) % MOD << '\n'; } return 0; }
### Prompt Please create a solution in CPP to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; constexpr int MOD = 998244353; int n, m, a[200001], w[200001], dp[3001][3001], sum[2], inv[10001], ans[2]; int ksm(int a, int b) { int s = 1; while (b) { if (b & 1) { s = s * static_cast<long long>(a) % MOD; } a = a * static_cast<long long>(a) % MOD; b >>= 1; } return s; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> w[i]; sum[a[i]] += w[i]; } for (int i = -m; i <= m; i++) { inv[i + m] = ksm((sum[0] + sum[1] + i + MOD) % MOD, MOD - 2); } dp[0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp[i][j + 1] = (dp[i][j + 1] + dp[i][j] * static_cast<long long>(sum[0] - j) % MOD * inv[i - j + m] % MOD) % MOD; dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * static_cast<long long>(sum[1] + i) % MOD * inv[i - j + m] % MOD) % MOD; } } for (int i = 0; i <= m; i++) { ans[0] = (ans[0] + dp[m - i][i] * static_cast<long long>(sum[0] - i) % MOD) % MOD; ans[1] = (ans[1] + dp[i][m - i] * static_cast<long long>(sum[1] + i) % MOD) % MOD; } ans[0] = ans[0] * static_cast<long long>(ksm(sum[0], MOD - 2)) % MOD; ans[1] = ans[1] * static_cast<long long>(ksm(sum[1], MOD - 2)) % MOD; for (int i = 1; i <= n; i++) { cout << w[i] * static_cast<long long>(ans[a[i]]) % MOD << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; long long w[51], dp[51][51][51]; bool a[51]; long long mul(long long a, long long b) { return (a * b) % mod; } long long add(long long a, long long b) { return (a + b) % mod; } long long quickpow(long long a, long long b) { if (b < 0) return 0; long long ret = 1; a %= mod; while (b) { if (b & 1) ret = (ret * a) % mod; b >>= 1; a = (a * a) % mod; } return ret; } long long inv(long long a) { return quickpow(a, mod - 2); } long long solve(int x) { long long pos = 0, neg = 0; for (int i = 1; i <= n; i++) { if (i == x) continue; if (a[i]) pos += w[i]; else neg += w[i]; } memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m - 1; i++) { for (int j = 0; j <= 50; j++) { for (int k = 0; k <= 50; k++) { if (dp[i][j][k] == 0) continue; long long me; if (a[x]) me = w[x] + (i - j - k); else me = w[x] - (i - j - k); long long pos_w = pos + j; long long neg_w = neg - k; long long total_inv = inv(add(me, add(pos_w, neg_w))); dp[i + 1][j + 1][k] = add(dp[i + 1][j + 1][k], mul(dp[i][j][k], mul(pos_w, total_inv))); dp[i + 1][j][k + 1] = add(dp[i + 1][j][k + 1], mul(dp[i][j][k], mul(neg_w, total_inv))); dp[i + 1][j][k] = add(dp[i + 1][j][k], mul(dp[i][j][k], mul(me, total_inv))); } } } long long res = 0; for (int j = 0; j <= 50; j++) { for (int k = 0; k <= 50; k++) { if (dp[m][j][k] == 0) continue; long long me; if (a[x]) me = w[x] + (m - j - k); else me = w[x] - (m - j - k); res = add(res, mul(me, dp[m][j][k])); } } return res; } int main(void) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i <= n; i++) { cout << solve(i) << endl; } }
### Prompt Generate a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; long long w[51], dp[51][51][51]; bool a[51]; long long mul(long long a, long long b) { return (a * b) % mod; } long long add(long long a, long long b) { return (a + b) % mod; } long long quickpow(long long a, long long b) { if (b < 0) return 0; long long ret = 1; a %= mod; while (b) { if (b & 1) ret = (ret * a) % mod; b >>= 1; a = (a * a) % mod; } return ret; } long long inv(long long a) { return quickpow(a, mod - 2); } long long solve(int x) { long long pos = 0, neg = 0; for (int i = 1; i <= n; i++) { if (i == x) continue; if (a[i]) pos += w[i]; else neg += w[i]; } memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m - 1; i++) { for (int j = 0; j <= 50; j++) { for (int k = 0; k <= 50; k++) { if (dp[i][j][k] == 0) continue; long long me; if (a[x]) me = w[x] + (i - j - k); else me = w[x] - (i - j - k); long long pos_w = pos + j; long long neg_w = neg - k; long long total_inv = inv(add(me, add(pos_w, neg_w))); dp[i + 1][j + 1][k] = add(dp[i + 1][j + 1][k], mul(dp[i][j][k], mul(pos_w, total_inv))); dp[i + 1][j][k + 1] = add(dp[i + 1][j][k + 1], mul(dp[i][j][k], mul(neg_w, total_inv))); dp[i + 1][j][k] = add(dp[i + 1][j][k], mul(dp[i][j][k], mul(me, total_inv))); } } } long long res = 0; for (int j = 0; j <= 50; j++) { for (int k = 0; k <= 50; k++) { if (dp[m][j][k] == 0) continue; long long me; if (a[x]) me = w[x] + (m - j - k); else me = w[x] - (m - j - k); res = add(res, mul(me, dp[m][j][k])); } } return res; } int main(void) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i <= n; i++) { cout << solve(i) << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; pair<int, int> v[55]; int dp[2][55][55][55], inv[3005], ans[2]; int liked, disliked; int exp(int a, int b) { int r = 1; while (b) { if (b & 1) r = ((long long)r * a) % MOD; b /= 2; a = ((long long)a * a) % MOD; } return r; } inline void add(int& a, int b) { a += b; if (a >= MOD) a -= MOD; } inline int mul(int a, int b) { return ((long long)a * b) % MOD; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &v[i].first); for (int i = 0; i < n; i++) { scanf("%d", &v[i].second); liked += v[i].first * v[i].second; disliked += (!v[i].first) * v[i].second; } for (int i = 0; i <= 3000; i++) inv[i] = exp(i, MOD - 2); dp[1][1][0][0] = dp[0][1][0][0] = 1; for (int w = 1; w < 51; w++) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[1][w][i + 1][j + 1], mul(mul(dp[1][w][i][j], (liked + j - w)), aux)); add(dp[1][w][i + 1][j], mul(mul(dp[1][w][i][j], (disliked - k)), aux)); add(dp[1][w + 1][i + 1][j + 1], mul(mul(dp[1][w][i][j], w), aux)); } for (int w = 1; w > 0; w--) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[0][w][i + 1][j], mul(mul(dp[0][w][i][j], (disliked - k - w)), aux)); add(dp[0][w][i + 1][j + 1], mul(mul(dp[0][w][i][j], (liked + j)), aux)); add(dp[0][w - 1][i + 1][j], mul(mul(dp[0][w][i][j], w), aux)); } for (int i = 0; i <= 1; i++) for (int w = 0; w <= 51; w++) for (int j = 0; j <= m; j++) add(ans[i], mul(w, dp[i][w][m][j])); for (int cur = 0; cur < n; cur++) printf("%d\n", mul(ans[v[cur].first], v[cur].second)); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 998244353; pair<int, int> v[55]; int dp[2][55][55][55], inv[3005], ans[2]; int liked, disliked; int exp(int a, int b) { int r = 1; while (b) { if (b & 1) r = ((long long)r * a) % MOD; b /= 2; a = ((long long)a * a) % MOD; } return r; } inline void add(int& a, int b) { a += b; if (a >= MOD) a -= MOD; } inline int mul(int a, int b) { return ((long long)a * b) % MOD; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &v[i].first); for (int i = 0; i < n; i++) { scanf("%d", &v[i].second); liked += v[i].first * v[i].second; disliked += (!v[i].first) * v[i].second; } for (int i = 0; i <= 3000; i++) inv[i] = exp(i, MOD - 2); dp[1][1][0][0] = dp[0][1][0][0] = 1; for (int w = 1; w < 51; w++) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[1][w][i + 1][j + 1], mul(mul(dp[1][w][i][j], (liked + j - w)), aux)); add(dp[1][w][i + 1][j], mul(mul(dp[1][w][i][j], (disliked - k)), aux)); add(dp[1][w + 1][i + 1][j + 1], mul(mul(dp[1][w][i][j], w), aux)); } for (int w = 1; w > 0; w--) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[0][w][i + 1][j], mul(mul(dp[0][w][i][j], (disliked - k - w)), aux)); add(dp[0][w][i + 1][j + 1], mul(mul(dp[0][w][i][j], (liked + j)), aux)); add(dp[0][w - 1][i + 1][j], mul(mul(dp[0][w][i][j], w), aux)); } for (int i = 0; i <= 1; i++) for (int w = 0; w <= 51; w++) for (int j = 0; j <= m; j++) add(ans[i], mul(w, dp[i][w][m][j])); for (int cur = 0; cur < n; cur++) printf("%d\n", mul(ans[v[cur].first], v[cur].second)); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return x; } const int maxn = 2e5 + 10; const int mod = 998244353; const double eps = 1e-9; long long f[51][51], g[51][51]; long long inv[3000]; long long qpow(int a, int b) { if (b == 0) return 1; long long now = qpow(a, b / 2); now = now * now % mod; if (b & 1) now = now * a % mod; return now; } int like[51], cnt[2], w[51]; int main() { inv[0] = inv[1] = 1; for (int i = 2; i < 3000; ++i) inv[i] = qpow(i, mod - 2); int n = read(), m = read(); for (int i = 1; i <= n; ++i) like[i] = read(); for (int i = 1; i <= n; ++i) { w[i] = read(); cnt[like[i]] += w[i]; } int sum = cnt[0] + cnt[1]; for (int j = 0; j <= m; ++j) { f[j][m - j] = 1; g[j][m - j] = 1; } for (int j = m - 1; j >= 0; --j) { for (int k = m - j - 1; k >= 0; --k) { f[j][k] = ((cnt[1] + j + 1) * inv[sum + j - k] % mod * f[j + 1][k] % mod + (cnt[0] - k) * inv[sum + j - k] % mod * f[j][k + 1]) % mod; g[j][k] = ((cnt[1] + j) * inv[sum + j - k] % mod * g[j + 1][k] % mod + (cnt[0] - k - 1) * inv[sum + j - k] % mod * g[j][k + 1]) % mod; } } for (int i = 1; i <= n; ++i) { if (like[i]) printf("%lld\n", w[i] * f[0][0] % mod); else printf("%lld\n", w[i] * g[0][0] % mod); } }
### Prompt Please create a solution in Cpp to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return x; } const int maxn = 2e5 + 10; const int mod = 998244353; const double eps = 1e-9; long long f[51][51], g[51][51]; long long inv[3000]; long long qpow(int a, int b) { if (b == 0) return 1; long long now = qpow(a, b / 2); now = now * now % mod; if (b & 1) now = now * a % mod; return now; } int like[51], cnt[2], w[51]; int main() { inv[0] = inv[1] = 1; for (int i = 2; i < 3000; ++i) inv[i] = qpow(i, mod - 2); int n = read(), m = read(); for (int i = 1; i <= n; ++i) like[i] = read(); for (int i = 1; i <= n; ++i) { w[i] = read(); cnt[like[i]] += w[i]; } int sum = cnt[0] + cnt[1]; for (int j = 0; j <= m; ++j) { f[j][m - j] = 1; g[j][m - j] = 1; } for (int j = m - 1; j >= 0; --j) { for (int k = m - j - 1; k >= 0; --k) { f[j][k] = ((cnt[1] + j + 1) * inv[sum + j - k] % mod * f[j + 1][k] % mod + (cnt[0] - k) * inv[sum + j - k] % mod * f[j][k + 1]) % mod; g[j][k] = ((cnt[1] + j) * inv[sum + j - k] % mod * g[j + 1][k] % mod + (cnt[0] - k - 1) * inv[sum + j - k] % mod * g[j][k + 1]) % mod; } } for (int i = 1; i <= n; ++i) { if (like[i]) printf("%lld\n", w[i] * f[0][0] % mod); else printf("%lld\n", w[i] * g[0][0] % mod); } } ```
#include <bits/stdc++.h> const int N = 55; const int P = 998244353; int n, m, a[N], w[N], inv[N * N], f[N][N][N][N]; void add(int &x, int y) { (x += y) >= P && (x -= P); } void init(int n) { inv[1] = 1; for (int i = 2; i <= n; i++) { inv[i] = 1LL * (P - P / i) * inv[P % i] % P; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] = (a[i] << 1) - 1; } int sumA = 0, sumB = 0; for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); (a[i] > 0 ? sumA : sumB) += w[i]; } init(sumA + sumB + m); for (int t = 1; t <= n; t++) { memset(f, 0, sizeof(f)); f[0][0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { for (int d = 0; d <= (a[t] > 0 ? j : k); d++) { if (!f[i][j][k][d]) continue; int x = 1LL * f[i][j][k][d] * inv[sumA + sumB + j - k] % P; if (a[t] > 0) { add(f[i + 1][j + 1][k][d + 1], 1LL * (w[t] + d) * x % P); add(f[i + 1][j + 1][k][d], 1LL * (sumA + j - w[t] - d) * x % P); add(f[i + 1][j][k + 1][d], 1LL * (sumB - k) * x % P); } else { add(f[i + 1][j][k + 1][d + 1], 1LL * (w[t] - d) * x % P); add(f[i + 1][j][k + 1][d], 1LL * (sumB - k - w[t] + d) * x % P); add(f[i + 1][j + 1][k][d], 1LL * (sumA + j) * x % P); } } } } } int ans = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { for (int d = 0; d <= (a[t] > 0 ? j : k); d++) { add(ans, 1LL * f[m][j][k][d] * (w[t] + a[t] * d) % P); } } } printf("%d\n", ans); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> const int N = 55; const int P = 998244353; int n, m, a[N], w[N], inv[N * N], f[N][N][N][N]; void add(int &x, int y) { (x += y) >= P && (x -= P); } void init(int n) { inv[1] = 1; for (int i = 2; i <= n; i++) { inv[i] = 1LL * (P - P / i) * inv[P % i] % P; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] = (a[i] << 1) - 1; } int sumA = 0, sumB = 0; for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); (a[i] > 0 ? sumA : sumB) += w[i]; } init(sumA + sumB + m); for (int t = 1; t <= n; t++) { memset(f, 0, sizeof(f)); f[0][0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { for (int d = 0; d <= (a[t] > 0 ? j : k); d++) { if (!f[i][j][k][d]) continue; int x = 1LL * f[i][j][k][d] * inv[sumA + sumB + j - k] % P; if (a[t] > 0) { add(f[i + 1][j + 1][k][d + 1], 1LL * (w[t] + d) * x % P); add(f[i + 1][j + 1][k][d], 1LL * (sumA + j - w[t] - d) * x % P); add(f[i + 1][j][k + 1][d], 1LL * (sumB - k) * x % P); } else { add(f[i + 1][j][k + 1][d + 1], 1LL * (w[t] - d) * x % P); add(f[i + 1][j][k + 1][d], 1LL * (sumB - k - w[t] + d) * x % P); add(f[i + 1][j + 1][k][d], 1LL * (sumA + j) * x % P); } } } } } int ans = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { for (int d = 0; d <= (a[t] > 0 ? j : k); d++) { add(ans, 1LL * f[m][j][k][d] * (w[t] + a[t] * d) % P); } } } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int MAXN = 50; const int MAXM = 50; const int MAXW = 100; bool likes[MAXN]; int weight[MAXN]; int dp[MAXW + 1][MAXM + 1][MAXM + 1][MAXM + 1]; int dp2[MAXW + 1][MAXM + 1][MAXM + 1][MAXM + 1]; void modInv(int a, int b, int& ainv, int& binv) { if (a == 0) { ainv = 0; binv = 1; return; } int x, y; modInv(b % a, a, x, y); ainv = (y - ((long long)x * (b / a)) % MOD + MOD) % MOD; binv = x % MOD; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> likes[i]; int good = 0, bad = 0; for (int i = 0; i < n; i++) { cin >> weight[i]; if (likes[i]) good += weight[i]; else bad += weight[i]; } int trash; for (int w = 1; w <= MAXW; w++) { for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp[w][0][i][j] = w; dp2[w][0][i][j] = w; } } } for (int v = 1; v <= m; v++) { for (int w = MAXW - v; w >= 0; w--) { for (int i = m - v; i >= 0; i--) { for (int j = m - v; j >= 0; j--) { if (w > good + i || j > bad) continue; int pInv; modInv(good + bad + i - j, MOD, pInv, trash); int r1 = (((long long)w * pInv % MOD) * dp[w + 1][v - 1][i + 1][j]) % MOD; int r2 = (((long long)(good + i - w) * pInv % MOD) * dp[w][v - 1][i + 1][j]) % MOD; int r3 = (((long long)(bad - j) * pInv % MOD) * dp[w][v - 1][i][j + 1]) % MOD; dp[w][v][i][j] = ((long long)r1 + r2 + r3) % MOD; assert(dp[w][v][i][j] >= 0); } } } } for (int v = 1; v <= m; v++) { for (int w = 1; w <= MAXW - v; w++) { for (int i = m - v; i >= 0; i--) { for (int j = m - v; j >= 0; j--) { if (w > bad - j || j > bad) continue; int pInv; modInv(good + bad + i - j, MOD, pInv, trash); int r1 = (((long long)w * pInv % MOD) * dp2[w - 1][v - 1][i][j + 1]) % MOD; int r2 = (((long long)(bad - j - w) * pInv % MOD) * dp2[w][v - 1][i][j + 1]) % MOD; int r3 = (((long long)(good + i) * pInv % MOD) * dp2[w][v - 1][i + 1][j]) % MOD; dp2[w][v][i][j] = ((long long)r1 + r2 + r3) % MOD; assert(dp2[w][v][i][j] >= 0); } } } } for (int i = 0; i < n; i++) { if (likes[i]) { cout << dp[weight[i]][m][0][0] << endl; } else { cout << dp2[weight[i]][m][0][0] << endl; } } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int MAXN = 50; const int MAXM = 50; const int MAXW = 100; bool likes[MAXN]; int weight[MAXN]; int dp[MAXW + 1][MAXM + 1][MAXM + 1][MAXM + 1]; int dp2[MAXW + 1][MAXM + 1][MAXM + 1][MAXM + 1]; void modInv(int a, int b, int& ainv, int& binv) { if (a == 0) { ainv = 0; binv = 1; return; } int x, y; modInv(b % a, a, x, y); ainv = (y - ((long long)x * (b / a)) % MOD + MOD) % MOD; binv = x % MOD; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> likes[i]; int good = 0, bad = 0; for (int i = 0; i < n; i++) { cin >> weight[i]; if (likes[i]) good += weight[i]; else bad += weight[i]; } int trash; for (int w = 1; w <= MAXW; w++) { for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp[w][0][i][j] = w; dp2[w][0][i][j] = w; } } } for (int v = 1; v <= m; v++) { for (int w = MAXW - v; w >= 0; w--) { for (int i = m - v; i >= 0; i--) { for (int j = m - v; j >= 0; j--) { if (w > good + i || j > bad) continue; int pInv; modInv(good + bad + i - j, MOD, pInv, trash); int r1 = (((long long)w * pInv % MOD) * dp[w + 1][v - 1][i + 1][j]) % MOD; int r2 = (((long long)(good + i - w) * pInv % MOD) * dp[w][v - 1][i + 1][j]) % MOD; int r3 = (((long long)(bad - j) * pInv % MOD) * dp[w][v - 1][i][j + 1]) % MOD; dp[w][v][i][j] = ((long long)r1 + r2 + r3) % MOD; assert(dp[w][v][i][j] >= 0); } } } } for (int v = 1; v <= m; v++) { for (int w = 1; w <= MAXW - v; w++) { for (int i = m - v; i >= 0; i--) { for (int j = m - v; j >= 0; j--) { if (w > bad - j || j > bad) continue; int pInv; modInv(good + bad + i - j, MOD, pInv, trash); int r1 = (((long long)w * pInv % MOD) * dp2[w - 1][v - 1][i][j + 1]) % MOD; int r2 = (((long long)(bad - j - w) * pInv % MOD) * dp2[w][v - 1][i][j + 1]) % MOD; int r3 = (((long long)(good + i) * pInv % MOD) * dp2[w][v - 1][i + 1][j]) % MOD; dp2[w][v][i][j] = ((long long)r1 + r2 + r3) % MOD; assert(dp2[w][v][i][j] >= 0); } } } } for (int i = 0; i < n; i++) { if (likes[i]) { cout << dp[weight[i]][m][0][0] << endl; } else { cout << dp2[weight[i]][m][0][0] << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int64_t MAX = 55, MOD = 998244353; int64_t n, m, sumW = 0, sumL = 0, C[MAX], W[MAX], F[MAX][MAX][2 * MAX][2]; bool Free[MAX][MAX][2 * MAX][2]; int64_t Mul(int64_t x, int64_t k) { if (k == 0) return 1; int64_t tmp = Mul(x, k / 2); if (k % 2 == 0) return tmp * tmp % MOD; return tmp * tmp % MOD * x % MOD; } int64_t DP(int64_t step, int64_t like, int64_t curW, int64_t type) { if (step == m) return curW; if (Free[step][like][curW][type] == true) return F[step][like][curW][type]; Free[step][like][curW][type] = true; int64_t cursumW = sumW + like - (step - like); int64_t cursumL = sumL + like; int64_t rs = 0; int64_t invcursumW = Mul(cursumW, MOD - 2); if (cursumL - curW * type > 0) rs = (rs + (cursumL - curW * type) * DP(step + 1, like + 1, curW, type) % MOD * invcursumW) % MOD; if (cursumW - cursumL - curW * (1 - type) > 0) rs = (rs + (cursumW - cursumL - curW * (1 - type)) * DP(step + 1, like, curW, type) % MOD * invcursumW) % MOD; if (curW > 0) rs = (rs + curW * DP(step + 1, like + type, curW + (type == 1 ? 1 : -1), type) % MOD * invcursumW) % MOD; return F[step][like][curW][type] = rs; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int64_t i = 1; i <= n; i++) cin >> C[i]; for (int64_t i = 1; i <= n; i++) { cin >> W[i]; sumW += W[i]; sumL += W[i] * C[i]; } for (int64_t i = 1; i <= n; i++) cout << DP(0, 0, W[i], C[i]) << "\n"; }
### Prompt Develop a solution in Cpp to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int64_t MAX = 55, MOD = 998244353; int64_t n, m, sumW = 0, sumL = 0, C[MAX], W[MAX], F[MAX][MAX][2 * MAX][2]; bool Free[MAX][MAX][2 * MAX][2]; int64_t Mul(int64_t x, int64_t k) { if (k == 0) return 1; int64_t tmp = Mul(x, k / 2); if (k % 2 == 0) return tmp * tmp % MOD; return tmp * tmp % MOD * x % MOD; } int64_t DP(int64_t step, int64_t like, int64_t curW, int64_t type) { if (step == m) return curW; if (Free[step][like][curW][type] == true) return F[step][like][curW][type]; Free[step][like][curW][type] = true; int64_t cursumW = sumW + like - (step - like); int64_t cursumL = sumL + like; int64_t rs = 0; int64_t invcursumW = Mul(cursumW, MOD - 2); if (cursumL - curW * type > 0) rs = (rs + (cursumL - curW * type) * DP(step + 1, like + 1, curW, type) % MOD * invcursumW) % MOD; if (cursumW - cursumL - curW * (1 - type) > 0) rs = (rs + (cursumW - cursumL - curW * (1 - type)) * DP(step + 1, like, curW, type) % MOD * invcursumW) % MOD; if (curW > 0) rs = (rs + curW * DP(step + 1, like + type, curW + (type == 1 ? 1 : -1), type) % MOD * invcursumW) % MOD; return F[step][like][curW][type] = rs; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int64_t i = 1; i <= n; i++) cin >> C[i]; for (int64_t i = 1; i <= n; i++) { cin >> W[i]; sumW += W[i]; sumL += W[i] * C[i]; } for (int64_t i = 1; i <= n; i++) cout << DP(0, 0, W[i], C[i]) << "\n"; } ```
#include <bits/stdc++.h> using namespace std; inline long long Getint() { char ch = getchar(); long long x = 0, fh = 1; while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { (x *= 10) += ch ^ 48; ch = getchar(); } return x * fh; } const int Mod = 998244353; const int N = 55; const int M = 3005; int n, m, su, A[N], W[N]; int nw, cnt, top[2]; int vis[M][N * 2]; long long f[2][M][N * 2]; struct nod { int x, y; nod() {} nod(int a, int b) { x = a; y = b; } } q[2][M * N * 2]; inline void Up(int x, int y, long long w) { if (vis[x][y] == cnt) (f[nw][x][y] += w) %= Mod; else vis[x][y] = cnt, f[nw][x][y] = w % Mod, q[nw][++top[nw]] = nod(x, y); } inline void Clear() { cnt++; top[nw] = 0; } long long inv[M * N]; long long Ans[N]; inline long long Solve(int x) { nw = 0; Clear(); int bsu = 0; for (int i = 1; i <= n; i++) { if (A[i] == 0) bsu += W[i]; } Up(bsu, W[x], 1); for (int t = 1; t <= m; t++) { nw ^= 1; Clear(); for (int i = 1; i <= top[nw ^ 1]; i++) { nod w = q[nw ^ 1][i]; int gd = t - 1 - (bsu - w.x); int wsu = su + gd - (bsu - w.x); long long p = w.y * inv[wsu] % Mod; if (A[x] == 0) { long long badres = w.x - w.y; long long godres = wsu - w.x; long long p1 = badres * inv[wsu] % Mod; long long p2 = godres * inv[wsu] % Mod; if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod); if (w.y) Up(w.x - 1, w.y - 1, f[nw ^ 1][w.x][w.y] * p % Mod); Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod); } else { long long badres = w.x; long long godres = wsu - w.x - w.y; long long p1 = badres * inv[wsu] % Mod; long long p2 = godres * inv[wsu] % Mod; if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod); Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod); Up(w.x, w.y + 1, f[nw ^ 1][w.x][w.y] * p % Mod); } } } for (int i = 1; i <= top[nw]; i++) { nod w = q[nw][i]; Ans[x] += w.y * f[nw][w.x][w.y] % Mod; } return (Ans[x] % Mod + Mod) % Mod; } int main() { n = Getint(); m = Getint(); inv[0] = inv[1] = 1; for (int i = 2; i <= 5000; i++) { inv[i] = (Mod - Mod / i) * inv[Mod % i] % Mod; } for (int i = 1; i <= n; i++) A[i] = Getint(); for (int i = 1; i <= n; i++) W[i] = Getint(), su += W[i]; for (int i = 1; i <= n; i++) cout << Solve(i) << '\n'; return 0; }
### Prompt Create a solution in CPP for the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline long long Getint() { char ch = getchar(); long long x = 0, fh = 1; while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { (x *= 10) += ch ^ 48; ch = getchar(); } return x * fh; } const int Mod = 998244353; const int N = 55; const int M = 3005; int n, m, su, A[N], W[N]; int nw, cnt, top[2]; int vis[M][N * 2]; long long f[2][M][N * 2]; struct nod { int x, y; nod() {} nod(int a, int b) { x = a; y = b; } } q[2][M * N * 2]; inline void Up(int x, int y, long long w) { if (vis[x][y] == cnt) (f[nw][x][y] += w) %= Mod; else vis[x][y] = cnt, f[nw][x][y] = w % Mod, q[nw][++top[nw]] = nod(x, y); } inline void Clear() { cnt++; top[nw] = 0; } long long inv[M * N]; long long Ans[N]; inline long long Solve(int x) { nw = 0; Clear(); int bsu = 0; for (int i = 1; i <= n; i++) { if (A[i] == 0) bsu += W[i]; } Up(bsu, W[x], 1); for (int t = 1; t <= m; t++) { nw ^= 1; Clear(); for (int i = 1; i <= top[nw ^ 1]; i++) { nod w = q[nw ^ 1][i]; int gd = t - 1 - (bsu - w.x); int wsu = su + gd - (bsu - w.x); long long p = w.y * inv[wsu] % Mod; if (A[x] == 0) { long long badres = w.x - w.y; long long godres = wsu - w.x; long long p1 = badres * inv[wsu] % Mod; long long p2 = godres * inv[wsu] % Mod; if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod); if (w.y) Up(w.x - 1, w.y - 1, f[nw ^ 1][w.x][w.y] * p % Mod); Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod); } else { long long badres = w.x; long long godres = wsu - w.x - w.y; long long p1 = badres * inv[wsu] % Mod; long long p2 = godres * inv[wsu] % Mod; if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod); Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod); Up(w.x, w.y + 1, f[nw ^ 1][w.x][w.y] * p % Mod); } } } for (int i = 1; i <= top[nw]; i++) { nod w = q[nw][i]; Ans[x] += w.y * f[nw][w.x][w.y] % Mod; } return (Ans[x] % Mod + Mod) % Mod; } int main() { n = Getint(); m = Getint(); inv[0] = inv[1] = 1; for (int i = 2; i <= 5000; i++) { inv[i] = (Mod - Mod / i) * inv[Mod % i] % Mod; } for (int i = 1; i <= n; i++) A[i] = Getint(); for (int i = 1; i <= n; i++) W[i] = Getint(), su += W[i]; for (int i = 1; i <= n; i++) cout << Solve(i) << '\n'; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int inf = 0x3f3f3f3f; const int mod = 998244353; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; inline int read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == 45) f ^= 1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x; } int qp(int x, int n) { return !n ? 1 : 1LL * qp(1LL * x * x % mod, n >> 1) * (n & 1 ? x : 1) % mod; } int inv(int x) { return qp(x, mod - 2); } int n, m; int a[55], w[55]; int dp[55][55][55][55]; int main() { n = read(), m = read(); for (int i = 0; i < (int)(n); ++i) a[i] = read(); int sum = 0, like = 0; for (int i = 0; i < (int)(n); ++i) w[i] = read(), sum += w[i], like += w[i] * a[i]; for (int i = 0; i < (int)(n); ++i) dp[0][0][i][0] = 1; for (int i = 0; i < (int)(m); ++i) for (int j = 0; j < (int)(i + 1); ++j) { int nwsum = sum + j - (i - j); int invnwsum = inv(nwsum); for (int k = 0; k < (int)(n); ++k) for (int l = 0; l < (int)(i + 1); ++l) { if (!dp[i][j][k][l]) continue; int nwlike = like + j; for (int sel = 0; sel < (int)(2); ++sel) { int nj = j + sel; int ful = sel ? nwlike : nwsum - nwlike; int nwk = l * (a[k] ? 1 : -1) + w[k]; if (nwk <= 0) continue; if (sel == a[k]) ful -= nwk; (dp[i + 1][nj][k][l] += 1LL * dp[i][j][k][l] * ful % mod * invnwsum % mod) %= mod; if (sel == a[k]) (dp[i + 1][nj][k][l + 1] += 1LL * dp[i][j][k][l] * nwk % mod * invnwsum % mod) %= mod; } } } for (int i = 0; i < (int)(n); ++i) { int ans = 0; for (int j = 0; j < (int)(m + 1); ++j) for (int l = 0; l < (int)(m + 1); ++l) { (ans += 1LL * (l * (a[i] ? 1 : -1) + w[i]) * dp[m][j][i][l] % mod) %= mod; } printf("%d\n", ans); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int inf = 0x3f3f3f3f; const int mod = 998244353; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; inline int read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == 45) f ^= 1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x; } int qp(int x, int n) { return !n ? 1 : 1LL * qp(1LL * x * x % mod, n >> 1) * (n & 1 ? x : 1) % mod; } int inv(int x) { return qp(x, mod - 2); } int n, m; int a[55], w[55]; int dp[55][55][55][55]; int main() { n = read(), m = read(); for (int i = 0; i < (int)(n); ++i) a[i] = read(); int sum = 0, like = 0; for (int i = 0; i < (int)(n); ++i) w[i] = read(), sum += w[i], like += w[i] * a[i]; for (int i = 0; i < (int)(n); ++i) dp[0][0][i][0] = 1; for (int i = 0; i < (int)(m); ++i) for (int j = 0; j < (int)(i + 1); ++j) { int nwsum = sum + j - (i - j); int invnwsum = inv(nwsum); for (int k = 0; k < (int)(n); ++k) for (int l = 0; l < (int)(i + 1); ++l) { if (!dp[i][j][k][l]) continue; int nwlike = like + j; for (int sel = 0; sel < (int)(2); ++sel) { int nj = j + sel; int ful = sel ? nwlike : nwsum - nwlike; int nwk = l * (a[k] ? 1 : -1) + w[k]; if (nwk <= 0) continue; if (sel == a[k]) ful -= nwk; (dp[i + 1][nj][k][l] += 1LL * dp[i][j][k][l] * ful % mod * invnwsum % mod) %= mod; if (sel == a[k]) (dp[i + 1][nj][k][l + 1] += 1LL * dp[i][j][k][l] * nwk % mod * invnwsum % mod) %= mod; } } } for (int i = 0; i < (int)(n); ++i) { int ans = 0; for (int j = 0; j < (int)(m + 1); ++j) for (int l = 0; l < (int)(m + 1); ++l) { (ans += 1LL * (l * (a[i] ? 1 : -1) + w[i]) * dp[m][j][i][l] % mod) %= mod; } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int maxn = 105; const long long int modd = 998244353; long long int dp[maxn][maxn][maxn], n, m, i; long long int a[maxn], w[maxn]; void count(long long int a, long long int b, long long int &x, long long int &y) { if (a == 1 && b == 0) { x = 1; y = 1; return; } else { count(b, a % b, y, x); y = (y - (a / b) * x % modd) % modd; return; } } long long int qny(long long int a) { long long int x, y; count(modd, a, x, y); y = (y + modd) % modd; return y; } void do_it(long long int x) { long long int i, j, k, p, ta = 0, tb = 0, tm = 0, tt; if (a[x] == 0) tt = -1; else tt = 1; for (i = 1; i <= n; i++) { if (i == x) tm += w[i]; else { if (a[i] == 1) ta += w[i]; if (a[i] == 0) tb += w[i]; } } dp[0][0][0] = 1; for (k = 0; k <= m; k++) { for (i = 0; i <= k; i++) { for (j = 0; j <= k - i; j++) { if (dp[k][i][j] == 0) continue; p = k - i - j; dp[k + 1][i + 1][j] = (dp[k + 1][i + 1][j] + dp[k][i][j] * (ta + i) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; dp[k + 1][i][j + 1] = (dp[k + 1][i][j + 1] + dp[k][i][j] * (tb - j) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; dp[k + 1][i][j] = (dp[k + 1][i][j] + dp[k][i][j] * (tm + tt * p) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; } } } long long int ans = 0; for (i = 0; i <= m; i++) { for (j = 0; j <= m; j++) { if (dp[m][i][j] == 0) continue; p = m - i - j; ans = (ans + (dp[m][i][j] * ((w[x] + p * tt) % modd)) % modd) % modd; } } ans = (ans + modd) % modd; printf("%lld\n", ans); return; } int main() { scanf("%lld%lld", &n, &m); long long int i; for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) scanf("%lld", &w[i]); for (i = 1; i <= n; i++) { memset(dp, 0, sizeof(dp)); do_it(i); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int maxn = 105; const long long int modd = 998244353; long long int dp[maxn][maxn][maxn], n, m, i; long long int a[maxn], w[maxn]; void count(long long int a, long long int b, long long int &x, long long int &y) { if (a == 1 && b == 0) { x = 1; y = 1; return; } else { count(b, a % b, y, x); y = (y - (a / b) * x % modd) % modd; return; } } long long int qny(long long int a) { long long int x, y; count(modd, a, x, y); y = (y + modd) % modd; return y; } void do_it(long long int x) { long long int i, j, k, p, ta = 0, tb = 0, tm = 0, tt; if (a[x] == 0) tt = -1; else tt = 1; for (i = 1; i <= n; i++) { if (i == x) tm += w[i]; else { if (a[i] == 1) ta += w[i]; if (a[i] == 0) tb += w[i]; } } dp[0][0][0] = 1; for (k = 0; k <= m; k++) { for (i = 0; i <= k; i++) { for (j = 0; j <= k - i; j++) { if (dp[k][i][j] == 0) continue; p = k - i - j; dp[k + 1][i + 1][j] = (dp[k + 1][i + 1][j] + dp[k][i][j] * (ta + i) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; dp[k + 1][i][j + 1] = (dp[k + 1][i][j + 1] + dp[k][i][j] * (tb - j) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; dp[k + 1][i][j] = (dp[k + 1][i][j] + dp[k][i][j] * (tm + tt * p) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; } } } long long int ans = 0; for (i = 0; i <= m; i++) { for (j = 0; j <= m; j++) { if (dp[m][i][j] == 0) continue; p = m - i - j; ans = (ans + (dp[m][i][j] * ((w[x] + p * tt) % modd)) % modd) % modd; } } ans = (ans + modd) % modd; printf("%lld\n", ans); return; } int main() { scanf("%lld%lld", &n, &m); long long int i; for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) scanf("%lld", &w[i]); for (i = 1; i <= n; i++) { memset(dp, 0, sizeof(dp)); do_it(i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, a[55], w[55]; long long f[55][55][55]; const int mod = 998244353; int ksm(int x, int k) { int ret = 1, tmp = x; while (k) { if (k & 1) ret = 1ll * ret * tmp % mod; tmp = 1ll * tmp * tmp % mod; k >>= 1; } return ret; } int inv(int x) { return ksm(x, mod - 2); } int main() { int sum = 0, sum0 = 0, sum1 = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i], sum += w[i], sum1 += a[i] == 1 ? w[i] : 0, sum0 += a[i] == 0 ? w[i] : 0; for (int i = 1; i <= n; i++) { memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int j = 0; j < m; j++) { for (int k = 0; k <= j; k++) { int cursum = sum + k - (j - k); for (int l = 0; l <= j; l++) { if (!f[j][k][l]) continue; int curw = w[i] + (a[i] ? l : -l), curp = 1ll * curw * inv(cursum) % mod; if (a[i]) { int cur0 = 1ll * (sum0 - (j - k)) * inv(cursum) % mod; int cur1 = 1ll * (sum1 + k - curw) * inv(cursum) % mod; (f[j + 1][k + 1][l + 1] += 1ll * curp * f[j][k][l]) %= mod; (f[j + 1][k + 1][l] += 1ll * cur1 * f[j][k][l]) %= mod; (f[j + 1][k][l] += 1ll * cur0 * f[j][k][l]) %= mod; } else { int cur0 = 1ll * (sum0 - (j - k) - curw) * inv(cursum) % mod; int cur1 = 1ll * (sum1 + k) * inv(cursum) % mod; (f[j + 1][k][l + 1] += 1ll * curp * f[j][k][l]) %= mod; (f[j + 1][k + 1][l] += 1ll * cur1 * f[j][k][l]) %= mod; (f[j + 1][k][l] += 1ll * cur0 * f[j][k][l]) %= mod; } } } } int ans = 0; for (int k = 0; k <= m; k++) for (int l = 0; l <= m; l++) ans = (ans + 1ll * f[m][k][l] * (w[i] + (a[i] ? l : -l))) % mod; cout << (ans + mod) % mod << "\n"; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, a[55], w[55]; long long f[55][55][55]; const int mod = 998244353; int ksm(int x, int k) { int ret = 1, tmp = x; while (k) { if (k & 1) ret = 1ll * ret * tmp % mod; tmp = 1ll * tmp * tmp % mod; k >>= 1; } return ret; } int inv(int x) { return ksm(x, mod - 2); } int main() { int sum = 0, sum0 = 0, sum1 = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i], sum += w[i], sum1 += a[i] == 1 ? w[i] : 0, sum0 += a[i] == 0 ? w[i] : 0; for (int i = 1; i <= n; i++) { memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int j = 0; j < m; j++) { for (int k = 0; k <= j; k++) { int cursum = sum + k - (j - k); for (int l = 0; l <= j; l++) { if (!f[j][k][l]) continue; int curw = w[i] + (a[i] ? l : -l), curp = 1ll * curw * inv(cursum) % mod; if (a[i]) { int cur0 = 1ll * (sum0 - (j - k)) * inv(cursum) % mod; int cur1 = 1ll * (sum1 + k - curw) * inv(cursum) % mod; (f[j + 1][k + 1][l + 1] += 1ll * curp * f[j][k][l]) %= mod; (f[j + 1][k + 1][l] += 1ll * cur1 * f[j][k][l]) %= mod; (f[j + 1][k][l] += 1ll * cur0 * f[j][k][l]) %= mod; } else { int cur0 = 1ll * (sum0 - (j - k) - curw) * inv(cursum) % mod; int cur1 = 1ll * (sum1 + k) * inv(cursum) % mod; (f[j + 1][k][l + 1] += 1ll * curp * f[j][k][l]) %= mod; (f[j + 1][k + 1][l] += 1ll * cur1 * f[j][k][l]) %= mod; (f[j + 1][k][l] += 1ll * cur0 * f[j][k][l]) %= mod; } } } } int ans = 0; for (int k = 0; k <= m; k++) for (int l = 0; l <= m; l++) ans = (ans + 1ll * f[m][k][l] * (w[i] + (a[i] ? l : -l))) % mod; cout << (ans + mod) % mod << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } long long dp[3100][3100]; int a[200010]; int w[200010]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) gn(w[i]); for (int i = 1; i <= n; i++) gn(a[i]); long long tot_dis = 0, tot = 0, tot_like = 0; for (int i = 1; i <= n; i++) { if (w[i]) tot_like += a[i]; else tot_dis += a[i]; tot += a[i]; } dp[0][0] = 1; for (int i = 1; i <= m; i++) { dp[i][0] = dp[i - 1][0] * (tot_like + i - 1) % 998244353 * power(tot + i - 1, 998244353 - 2, 998244353) % 998244353; for (int j = 1; j <= i; j++) if (tot_dis >= j) { long long first = 0, second = 0; first = dp[i - 1][j - 1] * power(tot + i + 1 - 2 * j, 998244353 - 2, 998244353) % 998244353 * (tot_dis - j + 1) % 998244353; if (i != j) second = dp[i - 1][j] * power(tot + i - 1 - 2 * j, 998244353 - 2, 998244353) % 998244353 * (tot_like + i - 1 - j) % 998244353; dp[i][j] = (first + second) % 998244353; } } long long sum = 0; for (int j = 0; j <= m; j++) sum += j * dp[m][j] % 998244353 * power(tot_dis, 998244353 - 2, 998244353) % 998244353; sum %= 998244353; long long s = 0; for (int j = 0; j <= m; j++) s += j * dp[m][m - j] % 998244353 * power(tot_like, 998244353 - 2, 998244353) % 998244353; for (int i = 1; i <= n; i++) { if (w[i]) { long long first = s * a[i] % 998244353; first = (first + a[i]) % 998244353; printf("%I64d\n", first); continue; } long long first = sum * a[i] % 998244353; first = a[i] - first; first = (first % 998244353 + 998244353) % 998244353; printf("%I64d\n", first); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } long long dp[3100][3100]; int a[200010]; int w[200010]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) gn(w[i]); for (int i = 1; i <= n; i++) gn(a[i]); long long tot_dis = 0, tot = 0, tot_like = 0; for (int i = 1; i <= n; i++) { if (w[i]) tot_like += a[i]; else tot_dis += a[i]; tot += a[i]; } dp[0][0] = 1; for (int i = 1; i <= m; i++) { dp[i][0] = dp[i - 1][0] * (tot_like + i - 1) % 998244353 * power(tot + i - 1, 998244353 - 2, 998244353) % 998244353; for (int j = 1; j <= i; j++) if (tot_dis >= j) { long long first = 0, second = 0; first = dp[i - 1][j - 1] * power(tot + i + 1 - 2 * j, 998244353 - 2, 998244353) % 998244353 * (tot_dis - j + 1) % 998244353; if (i != j) second = dp[i - 1][j] * power(tot + i - 1 - 2 * j, 998244353 - 2, 998244353) % 998244353 * (tot_like + i - 1 - j) % 998244353; dp[i][j] = (first + second) % 998244353; } } long long sum = 0; for (int j = 0; j <= m; j++) sum += j * dp[m][j] % 998244353 * power(tot_dis, 998244353 - 2, 998244353) % 998244353; sum %= 998244353; long long s = 0; for (int j = 0; j <= m; j++) s += j * dp[m][m - j] % 998244353 * power(tot_like, 998244353 - 2, 998244353) % 998244353; for (int i = 1; i <= n; i++) { if (w[i]) { long long first = s * a[i] % 998244353; first = (first + a[i]) % 998244353; printf("%I64d\n", first); continue; } long long first = sum * a[i] % 998244353; first = a[i] - first; first = (first % 998244353 + 998244353) % 998244353; printf("%I64d\n", first); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 200010; const int M = 3010; const int mod = 998244353; int qpow(int x, int y) { int out = 1; while (y) { if (y & 1) out = (long long)out * x % mod; x = (long long)x * x % mod; y >>= 1; } return out; } int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; int main() { int i, j; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", a + i); for (i = 1; i <= n; ++i) { scanf("%d", w + i); sum[a[i]] += w[i]; sum[2] += w[i]; } for (i = max(0, m - sum[0]); i <= 2 * m; ++i) inv[i] = qpow(sum[2] + i - m, mod - 2); for (i = m; i >= 0; --i) { f[i][m - i] = g[i][m - i] = 1; for (j = min(m - i - 1, sum[0]); j >= 0; --j) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (i = 1; i <= n; ++i) printf("%d\n", int((long long)w[i] * (a[i] ? f[0][0] : g[0][0]) % mod)); return 0; }
### Prompt Develop a solution in CPP to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 200010; const int M = 3010; const int mod = 998244353; int qpow(int x, int y) { int out = 1; while (y) { if (y & 1) out = (long long)out * x % mod; x = (long long)x * x % mod; y >>= 1; } return out; } int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; int main() { int i, j; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", a + i); for (i = 1; i <= n; ++i) { scanf("%d", w + i); sum[a[i]] += w[i]; sum[2] += w[i]; } for (i = max(0, m - sum[0]); i <= 2 * m; ++i) inv[i] = qpow(sum[2] + i - m, mod - 2); for (i = m; i >= 0; --i) { f[i][m - i] = g[i][m - i] = 1; for (j = min(m - i - 1, sum[0]); j >= 0; --j) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (i = 1; i <= n; ++i) printf("%d\n", int((long long)w[i] * (a[i] ? f[0][0] : g[0][0]) % mod)); return 0; } ```
#include <bits/stdc++.h> using namespace std; using INT = long long; const int mod = 998244353; int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } const int N = 50; int a[110], w[110], dp[110][110][110], tmp[110][110][110]; void add(int &x, int y) { y = (y + mod) % mod; x += y; if (x >= mod) x -= mod; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; int sum = 0, ss = 0; for (int j = 1; j <= n; j++) { cin >> w[j]; sum += w[j]; if (a[j] == 0) ss += w[j]; } for (int i = 1; i <= n; i++) { dp[i][0][0] = 1; } for (int k = 1; k <= m; k++) { for (int i = 1; i <= n; i++) { for (int j = 0; j <= k - 1; j++) { for (int l = 0; l <= k - 1; l++) { int op = (a[i] == 0); int f = a[i]; if (f == 0) f = -1; int tot = sum + k - 1 - 2 * l; int pb = (INT)power(tot, mod - 2, mod) * (w[i] + f * j) % mod; int t = ss - l; if (a[i] == 0) t -= (w[i] - j); int qb = (INT)power(tot, mod - 2, mod) * t % mod; qb = (qb + mod) % mod; pb = (pb + mod) % mod; add(tmp[i][j + 1][l + op], (INT)pb * dp[i][j][l] % mod); add(tmp[i][j][l + 1], (INT)qb * dp[i][j][l] % mod); add(tmp[i][j][l], (INT)dp[i][j][l] * (1 - pb - qb) % mod); } } } for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) for (int l = 0; l <= k; l++) { dp[i][j][l] = tmp[i][j][l]; tmp[i][j][l] = 0; } } for (int i = 1; i <= n; i++) { int ans = 0; int f = a[i]; if (f == 0) f = -1; for (int j = 0; j <= m; j++) { for (int l = 0; l <= m; l++) { add(ans, (INT)(w[i] + f * j) * dp[i][j][l] % mod); } } printf("%d\n", ans); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using INT = long long; const int mod = 998244353; int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } const int N = 50; int a[110], w[110], dp[110][110][110], tmp[110][110][110]; void add(int &x, int y) { y = (y + mod) % mod; x += y; if (x >= mod) x -= mod; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; int sum = 0, ss = 0; for (int j = 1; j <= n; j++) { cin >> w[j]; sum += w[j]; if (a[j] == 0) ss += w[j]; } for (int i = 1; i <= n; i++) { dp[i][0][0] = 1; } for (int k = 1; k <= m; k++) { for (int i = 1; i <= n; i++) { for (int j = 0; j <= k - 1; j++) { for (int l = 0; l <= k - 1; l++) { int op = (a[i] == 0); int f = a[i]; if (f == 0) f = -1; int tot = sum + k - 1 - 2 * l; int pb = (INT)power(tot, mod - 2, mod) * (w[i] + f * j) % mod; int t = ss - l; if (a[i] == 0) t -= (w[i] - j); int qb = (INT)power(tot, mod - 2, mod) * t % mod; qb = (qb + mod) % mod; pb = (pb + mod) % mod; add(tmp[i][j + 1][l + op], (INT)pb * dp[i][j][l] % mod); add(tmp[i][j][l + 1], (INT)qb * dp[i][j][l] % mod); add(tmp[i][j][l], (INT)dp[i][j][l] * (1 - pb - qb) % mod); } } } for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) for (int l = 0; l <= k; l++) { dp[i][j][l] = tmp[i][j][l]; tmp[i][j][l] = 0; } } for (int i = 1; i <= n; i++) { int ans = 0; int f = a[i]; if (f == 0) f = -1; for (int j = 0; j <= m; j++) { for (int l = 0; l <= m; l++) { add(ans, (INT)(w[i] + f * j) * dp[i][j][l] % mod); } } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int MAXM = 3010; const int MOD = 998244353; int qpow(int a, int b) { int base = 1; while (b) { if (b & 1) base = 1ll * base * a % MOD; a = 1ll * a * a % MOD; b >>= 1; } return base; } int n, m, A[MAXN], W[MAXN], F[MAXM][MAXM], G[MAXM][MAXM], Inv[MAXM << 1], sum[10]; namespace io { inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } char buf[1 << 21]; inline void write(int x) { if (x == 0) { putchar('0'); return; } int tmp = x < 0 ? -x : x; if (x < 0) putchar('-'); int cnt = 0; while (tmp > 0) { buf[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0) putchar(buf[--cnt]); } } // namespace io using namespace io; int main() { n = read(), m = read(); for (register int i = 1; i <= n; ++i) A[i] = read(); for (register int i = 1; i <= n; ++i) W[i] = read(), sum[A[i]] += W[i], sum[2] += W[i]; for (register int i = 0 > m - sum[0] ? 0 : m - sum[0]; i <= 2 * m; ++i) Inv[i] = qpow(sum[2] + i - m, MOD - 2); for (register int i = m; i >= 0; --i) { F[i][m - i] = G[i][m - i] = 1; for (register int j = m - i - 1 < sum[0] ? m - i - 1 : sum[0]; j >= 0; --j) { F[i][j] = (1ll * (sum[1] + i + 1) * F[i + 1][j] + 1ll * (sum[0] - j) * F[i][j + 1]) % MOD * Inv[i - j + m] % MOD; G[i][j] = (1ll * (sum[1] + i) * G[i + 1][j] + 1ll * (sum[0] - j - 1) * G[i][j + 1]) % MOD * Inv[i - j + m] % MOD; } } for (register int i = 1; i <= n; ++i) write(int(1ll * W[i] * (A[i] ? F[0][0] : G[0][0]) % MOD)), putchar('\n'); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int MAXM = 3010; const int MOD = 998244353; int qpow(int a, int b) { int base = 1; while (b) { if (b & 1) base = 1ll * base * a % MOD; a = 1ll * a * a % MOD; b >>= 1; } return base; } int n, m, A[MAXN], W[MAXN], F[MAXM][MAXM], G[MAXM][MAXM], Inv[MAXM << 1], sum[10]; namespace io { inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } char buf[1 << 21]; inline void write(int x) { if (x == 0) { putchar('0'); return; } int tmp = x < 0 ? -x : x; if (x < 0) putchar('-'); int cnt = 0; while (tmp > 0) { buf[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0) putchar(buf[--cnt]); } } // namespace io using namespace io; int main() { n = read(), m = read(); for (register int i = 1; i <= n; ++i) A[i] = read(); for (register int i = 1; i <= n; ++i) W[i] = read(), sum[A[i]] += W[i], sum[2] += W[i]; for (register int i = 0 > m - sum[0] ? 0 : m - sum[0]; i <= 2 * m; ++i) Inv[i] = qpow(sum[2] + i - m, MOD - 2); for (register int i = m; i >= 0; --i) { F[i][m - i] = G[i][m - i] = 1; for (register int j = m - i - 1 < sum[0] ? m - i - 1 : sum[0]; j >= 0; --j) { F[i][j] = (1ll * (sum[1] + i + 1) * F[i + 1][j] + 1ll * (sum[0] - j) * F[i][j + 1]) % MOD * Inv[i - j + m] % MOD; G[i][j] = (1ll * (sum[1] + i) * G[i + 1][j] + 1ll * (sum[0] - j - 1) * G[i][j + 1]) % MOD * Inv[i - j + m] % MOD; } } for (register int i = 1; i <= n; ++i) write(int(1ll * W[i] * (A[i] ? F[0][0] : G[0][0]) % MOD)), putchar('\n'); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; int mod = MOD; struct ModInt { unsigned val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} ModInt pow(long long exponent) { ModInt tmp = *this, res = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } ModInt &operator+=(const ModInt &rhs) { if ((val += rhs.val) >= mod) val -= mod; return *this; } ModInt &operator-=(const ModInt &rhs) { if ((val += mod - rhs.val) >= mod) val -= mod; return *this; } ModInt &operator*=(const ModInt &rhs) { val = (unsigned long long)val * rhs.val % mod; return *this; } ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } bool operator==(const ModInt &rhs) const { return val == rhs.val; } bool operator!=(const ModInt &rhs) const { return val != rhs.val; } bool operator<(const ModInt &rhs) const { return val < rhs.val; } bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } bool operator>(const ModInt &rhs) const { return val > rhs.val; } bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } ModInt operator-() const { return ModInt(-val); } ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; } friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; } private: ModInt inv() const { unsigned a = val, b = mod; int x = 1, y = 0; while (b) { unsigned tmp = a / b; a -= tmp * b; swap(a, b); x -= tmp * y; swap(x, y); } return ModInt(x); } }; ModInt abs(const ModInt &x) { return x.val; } struct Combinatorics { Combinatorics(int MAX = 5000000) { MAX <<= 1; fact.resize(MAX + 1); fact_inv.resize(MAX + 1); fact[0] = 1; for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i; fact_inv[MAX] = ModInt(1) / fact[MAX]; for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; } ModInt nCk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[k] * fact_inv[n - k]; } ModInt nPk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[n - k]; } ModInt nHk(int n, int k) { if (n < 0 || k < 0) return ModInt(0); return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); } private: vector<ModInt> fact, fact_inv; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; vector<int> w(n); for (int i = (0); i < (n); ++i) cin >> w[i]; int dislike = 0, like = 0; for (int i = (0); i < (n); ++i) { if (a[i] == 0) dislike += w[i]; else like += w[i]; } for (int i = (0); i < (n); ++i) { vector<vector<vector<ModInt> > > dp( m + 1, vector<vector<ModInt> >(m + 1, vector<ModInt>(m + 1, 0))); dp[0][0][0] = 1; int dl = dislike, l = like; if (a[i] == 0) dl -= w[i]; else l -= w[i]; for (int x = (0); x < (m); ++x) { for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { int sum = like + dislike - z + (x - y - z) + (a[i] == 0 ? -y : y); if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > x || sum <= 0) continue; dp[x + 1][y + 1][z] += dp[x][y][z] * (w[i] + (a[i] == 0 ? -y : y)) / sum; dp[x + 1][y][z + 1] += dp[x][y][z] * (dl - z) / sum; dp[x + 1][y][z] += dp[x][y][z] * (l + (x - y - z)) / sum; } } ModInt ans = 0; for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > m) continue; ans += dp[m][y][z] * (w[i] + (a[i] == 0 ? -y : y)); } cout << ans << '\n'; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; int mod = MOD; struct ModInt { unsigned val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} ModInt pow(long long exponent) { ModInt tmp = *this, res = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } ModInt &operator+=(const ModInt &rhs) { if ((val += rhs.val) >= mod) val -= mod; return *this; } ModInt &operator-=(const ModInt &rhs) { if ((val += mod - rhs.val) >= mod) val -= mod; return *this; } ModInt &operator*=(const ModInt &rhs) { val = (unsigned long long)val * rhs.val % mod; return *this; } ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } bool operator==(const ModInt &rhs) const { return val == rhs.val; } bool operator!=(const ModInt &rhs) const { return val != rhs.val; } bool operator<(const ModInt &rhs) const { return val < rhs.val; } bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } bool operator>(const ModInt &rhs) const { return val > rhs.val; } bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } ModInt operator-() const { return ModInt(-val); } ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; } friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; } private: ModInt inv() const { unsigned a = val, b = mod; int x = 1, y = 0; while (b) { unsigned tmp = a / b; a -= tmp * b; swap(a, b); x -= tmp * y; swap(x, y); } return ModInt(x); } }; ModInt abs(const ModInt &x) { return x.val; } struct Combinatorics { Combinatorics(int MAX = 5000000) { MAX <<= 1; fact.resize(MAX + 1); fact_inv.resize(MAX + 1); fact[0] = 1; for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i; fact_inv[MAX] = ModInt(1) / fact[MAX]; for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; } ModInt nCk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[k] * fact_inv[n - k]; } ModInt nPk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[n - k]; } ModInt nHk(int n, int k) { if (n < 0 || k < 0) return ModInt(0); return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); } private: vector<ModInt> fact, fact_inv; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; vector<int> w(n); for (int i = (0); i < (n); ++i) cin >> w[i]; int dislike = 0, like = 0; for (int i = (0); i < (n); ++i) { if (a[i] == 0) dislike += w[i]; else like += w[i]; } for (int i = (0); i < (n); ++i) { vector<vector<vector<ModInt> > > dp( m + 1, vector<vector<ModInt> >(m + 1, vector<ModInt>(m + 1, 0))); dp[0][0][0] = 1; int dl = dislike, l = like; if (a[i] == 0) dl -= w[i]; else l -= w[i]; for (int x = (0); x < (m); ++x) { for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { int sum = like + dislike - z + (x - y - z) + (a[i] == 0 ? -y : y); if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > x || sum <= 0) continue; dp[x + 1][y + 1][z] += dp[x][y][z] * (w[i] + (a[i] == 0 ? -y : y)) / sum; dp[x + 1][y][z + 1] += dp[x][y][z] * (dl - z) / sum; dp[x + 1][y][z] += dp[x][y][z] * (l + (x - y - z)) / sum; } } ModInt ans = 0; for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > m) continue; ans += dp[m][y][z] * (w[i] + (a[i] == 0 ? -y : y)); } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using namespace std; int n, m; long long bm(long long base, int power) { if (power == 0) { return 1; } if (power % 2 == 0) { long long ret = bm(base, power / 2); return (ret * ret) % 998244353; } else { return (base * bm(base, power - 1)) % 998244353; } } long long mod_inv(long long num) { return bm(num, 998244353 - 2); } int dp[2560][110][60]; bool foo; int init_pos_wei = 0; int init_neg_wei = 0; long long func_pos(long long pos_wei, long long cur_wei, int vis) { long long pos_used = (pos_wei + cur_wei - init_pos_wei); long long neg_used = vis - pos_used; long long neg_wei = init_neg_wei - neg_used; if (vis == m) { return cur_wei; } if (dp[pos_wei][cur_wei][vis] != -1) { return dp[pos_wei][cur_wei][vis]; } long long ans = 0; long long all = pos_wei + neg_wei + cur_wei; if (cur_wei) { ans += (((cur_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei, cur_wei + 1, vis + 1)) % 998244353; } if (pos_wei) { ans += (((pos_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei + 1, cur_wei, vis + 1)) % 998244353; } if (neg_wei) { ans += (((neg_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei, cur_wei, vis + 1)) % 998244353; } ans %= 998244353; return dp[pos_wei][cur_wei][vis] = ans; } long long func_neg(long long neg_wei, long long cur_wei, int vis) { long long neg_used = init_neg_wei - (neg_wei + cur_wei); long long pos_used = vis - neg_used; long long pos_wei = init_pos_wei + pos_used; if (vis == m) { return cur_wei; } if (dp[neg_wei][cur_wei][vis] != -1) { return dp[neg_wei][cur_wei][vis]; } long long ans = 0; long long all = pos_wei + neg_wei + cur_wei; if (cur_wei) { ans += (((cur_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei, cur_wei - 1, vis + 1)) % 998244353; } if (pos_wei) { ans += (((pos_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei, cur_wei, vis + 1)) % 998244353; } if (neg_wei) { ans += (((neg_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei - 1, cur_wei, vis + 1)) % 998244353; } ans %= 998244353; return dp[neg_wei][cur_wei][vis] = ans; } int res[60]; bool is_like[60]; int wei[60]; int main() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) { cin >> is_like[i]; } for (int i = 0; i <= n - 1; i++) { cin >> wei[i]; if (is_like[i]) { init_pos_wei += wei[i]; } else { init_neg_wei += wei[i]; } } memset((dp), -1, sizeof(dp)); for (int i = 0; i <= n - 1; i++) { if (is_like[i]) { res[i] = func_pos(init_pos_wei - wei[i], wei[i], 0); } } memset((dp), -1, sizeof(dp)); for (int i = 0; i <= n - 1; i++) { if (!is_like[i]) { res[i] = func_neg(init_neg_wei - wei[i], wei[i], 0); } } for (int i = 0; i <= n - 1; i++) { cout << res[i] << "\n"; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std; int n, m; long long bm(long long base, int power) { if (power == 0) { return 1; } if (power % 2 == 0) { long long ret = bm(base, power / 2); return (ret * ret) % 998244353; } else { return (base * bm(base, power - 1)) % 998244353; } } long long mod_inv(long long num) { return bm(num, 998244353 - 2); } int dp[2560][110][60]; bool foo; int init_pos_wei = 0; int init_neg_wei = 0; long long func_pos(long long pos_wei, long long cur_wei, int vis) { long long pos_used = (pos_wei + cur_wei - init_pos_wei); long long neg_used = vis - pos_used; long long neg_wei = init_neg_wei - neg_used; if (vis == m) { return cur_wei; } if (dp[pos_wei][cur_wei][vis] != -1) { return dp[pos_wei][cur_wei][vis]; } long long ans = 0; long long all = pos_wei + neg_wei + cur_wei; if (cur_wei) { ans += (((cur_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei, cur_wei + 1, vis + 1)) % 998244353; } if (pos_wei) { ans += (((pos_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei + 1, cur_wei, vis + 1)) % 998244353; } if (neg_wei) { ans += (((neg_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei, cur_wei, vis + 1)) % 998244353; } ans %= 998244353; return dp[pos_wei][cur_wei][vis] = ans; } long long func_neg(long long neg_wei, long long cur_wei, int vis) { long long neg_used = init_neg_wei - (neg_wei + cur_wei); long long pos_used = vis - neg_used; long long pos_wei = init_pos_wei + pos_used; if (vis == m) { return cur_wei; } if (dp[neg_wei][cur_wei][vis] != -1) { return dp[neg_wei][cur_wei][vis]; } long long ans = 0; long long all = pos_wei + neg_wei + cur_wei; if (cur_wei) { ans += (((cur_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei, cur_wei - 1, vis + 1)) % 998244353; } if (pos_wei) { ans += (((pos_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei, cur_wei, vis + 1)) % 998244353; } if (neg_wei) { ans += (((neg_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei - 1, cur_wei, vis + 1)) % 998244353; } ans %= 998244353; return dp[neg_wei][cur_wei][vis] = ans; } int res[60]; bool is_like[60]; int wei[60]; int main() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) { cin >> is_like[i]; } for (int i = 0; i <= n - 1; i++) { cin >> wei[i]; if (is_like[i]) { init_pos_wei += wei[i]; } else { init_neg_wei += wei[i]; } } memset((dp), -1, sizeof(dp)); for (int i = 0; i <= n - 1; i++) { if (is_like[i]) { res[i] = func_pos(init_pos_wei - wei[i], wei[i], 0); } } memset((dp), -1, sizeof(dp)); for (int i = 0; i <= n - 1; i++) { if (!is_like[i]) { res[i] = func_neg(init_neg_wei - wei[i], wei[i], 0); } } for (int i = 0; i <= n - 1; i++) { cout << res[i] << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 60; const long long INF = 0x3f3f3f3f; const long long iinf = 1 << 30; const long long linf = 2e18; const long long MOD = 998244353; const double eps = 1e-7; void douout(double x) { printf("%lf\n", x + 0.0000000001); } template <class T> void print(T a) { cout << a << endl; exit(0); } template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } void upd(long long &a, long long b) { a = (long long)(a + b) % MOD; } template <class T> void mul(T &a, T b) { a = (long long)a * b % MOD; } template <class T> T read() { long long f = 1; T x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, m, suma, sumb; long long a[N], w[N], inv[N * N]; long long f[N][N][N][N]; void init(long long x) { inv[1] = 1; for (long long i = (2); i <= (x); i++) inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD; } signed main() { scanf("%lld%lld", &n, &m); for (long long i = (1); i <= (n); i++) scanf("%lld", &a[i]), a[i] = (a[i] * 2) - 1; for (long long i = (1); i <= (n); i++) scanf("%lld", &w[i]); for (long long i = (1); i <= (n); i++) { if (a[i] > 0) upd(suma, w[i]); else upd(sumb, w[i]); } init(suma + sumb + m); for (long long t = (1); t <= (n); t++) { memset(f, 0, sizeof(f)); f[0][0][0][0] = 1; for (long long i = (0); i <= (m - 1); i++) for (long long j = (0); j <= (i); j++) for (long long k = (0); k <= (i); k++) for (long long d = (0); d <= ((a[t] > 0 ? j : k)); d++) { if (!f[i][j][k][d]) continue; long long x = 1ll * f[i][j][k][d] * inv[suma + sumb + j - k] % MOD; if (a[t] > 0) { upd(f[i + 1][j + 1][k][d + 1], 1ll * (w[t] + d) * x % MOD); upd(f[i + 1][j + 1][k][d], 1ll * (suma + j - w[t] - d) * x % MOD); upd(f[i + 1][j][k + 1][d], 1ll * (sumb - k) * x % MOD); } else { upd(f[i + 1][j][k + 1][d + 1], 1ll * (w[t] - d) * x % MOD); upd(f[i + 1][j][k + 1][d], 1ll * (sumb - k - w[t] + d) * x % MOD); upd(f[i + 1][j + 1][k][d], 1ll * (suma + j) * x % MOD); } } long long ans = 0; for (long long j = (0); j <= (m); j++) for (long long k = (0); k <= (m); k++) for (long long d = (0); d <= ((a[t] > 0 ? j : k)); d++) upd(ans, 1ll * (f[m][j][k][d] * (w[t] + a[t] * d) % MOD)); printf("%lld\n", ans); } return 0; }
### Prompt Generate a cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 60; const long long INF = 0x3f3f3f3f; const long long iinf = 1 << 30; const long long linf = 2e18; const long long MOD = 998244353; const double eps = 1e-7; void douout(double x) { printf("%lf\n", x + 0.0000000001); } template <class T> void print(T a) { cout << a << endl; exit(0); } template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } void upd(long long &a, long long b) { a = (long long)(a + b) % MOD; } template <class T> void mul(T &a, T b) { a = (long long)a * b % MOD; } template <class T> T read() { long long f = 1; T x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, m, suma, sumb; long long a[N], w[N], inv[N * N]; long long f[N][N][N][N]; void init(long long x) { inv[1] = 1; for (long long i = (2); i <= (x); i++) inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD; } signed main() { scanf("%lld%lld", &n, &m); for (long long i = (1); i <= (n); i++) scanf("%lld", &a[i]), a[i] = (a[i] * 2) - 1; for (long long i = (1); i <= (n); i++) scanf("%lld", &w[i]); for (long long i = (1); i <= (n); i++) { if (a[i] > 0) upd(suma, w[i]); else upd(sumb, w[i]); } init(suma + sumb + m); for (long long t = (1); t <= (n); t++) { memset(f, 0, sizeof(f)); f[0][0][0][0] = 1; for (long long i = (0); i <= (m - 1); i++) for (long long j = (0); j <= (i); j++) for (long long k = (0); k <= (i); k++) for (long long d = (0); d <= ((a[t] > 0 ? j : k)); d++) { if (!f[i][j][k][d]) continue; long long x = 1ll * f[i][j][k][d] * inv[suma + sumb + j - k] % MOD; if (a[t] > 0) { upd(f[i + 1][j + 1][k][d + 1], 1ll * (w[t] + d) * x % MOD); upd(f[i + 1][j + 1][k][d], 1ll * (suma + j - w[t] - d) * x % MOD); upd(f[i + 1][j][k + 1][d], 1ll * (sumb - k) * x % MOD); } else { upd(f[i + 1][j][k + 1][d + 1], 1ll * (w[t] - d) * x % MOD); upd(f[i + 1][j][k + 1][d], 1ll * (sumb - k - w[t] + d) * x % MOD); upd(f[i + 1][j + 1][k][d], 1ll * (suma + j) * x % MOD); } } long long ans = 0; for (long long j = (0); j <= (m); j++) for (long long k = (0); k <= (m); k++) for (long long d = (0); d <= ((a[t] > 0 ? j : k)); d++) upd(ans, 1ll * (f[m][j][k][d] * (w[t] + a[t] * d) % MOD)); printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int P = 998244353, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 1e6 + 10; int n, m, a[N], w[N], s1, s0; int in[N]; int dp[55][55][55]; int solve(int a, int w) { memset(dp, 0, sizeof dp); dp[0][0][0] = 1; int ans = 0; if (a) { for (int i = 1; i <= m + 1; ++i) for (int j = 0; j <= i - 1; ++j) for (int k = 0; k <= j; ++k) if (dp[i - 1][j][k]) { int &r = dp[i - 1][j][k]; int now = w + k, tot = s1 + s0 + j - (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + (long long)p * r) % P; now = s1 - w + j - k, p = (long long)now * in[tot] % P; dp[i][j + 1][k] = (dp[i][j + 1][k] + (long long)p * r) % P; now = s0 - (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j][k] = (dp[i][j][k] + (long long)p * r) % P; if (i - 1 == m) ans = (ans + (long long)(w + k) * r) % P; } return ans; } for (int i = 1; i <= m + 1; ++i) for (int j = 0; j <= i - 1; ++j) for (int k = 0; k <= min(w, j); ++k) if (dp[i - 1][j][k]) { int &r = dp[i - 1][j][k]; int now = w - k, tot = s0 + s1 - j + (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + (long long)p * r) % P; now = s1 + (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j][k] = (dp[i][j][k] + (long long)p * r) % P; now = s0 - w - j + k, p = (long long)now * in[tot] % P; dp[i][j + 1][k] = (dp[i][j + 1][k] + (long long)p * r) % P; if (i - 1 == m) ans = (ans + (long long)(w - k) * r) % P; } return ans; } int main() { in[0] = 1; for (int i = 1; i <= N - 1; ++i) in[i] = inv(i); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) { scanf("%d", w + i); if (a[i]) s1 += w[i]; else s0 += w[i]; } for (int i = 1; i <= n; ++i) printf("%d\n", solve(a[i], w[i])); }
### Prompt Please formulate a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int P = 998244353, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 1e6 + 10; int n, m, a[N], w[N], s1, s0; int in[N]; int dp[55][55][55]; int solve(int a, int w) { memset(dp, 0, sizeof dp); dp[0][0][0] = 1; int ans = 0; if (a) { for (int i = 1; i <= m + 1; ++i) for (int j = 0; j <= i - 1; ++j) for (int k = 0; k <= j; ++k) if (dp[i - 1][j][k]) { int &r = dp[i - 1][j][k]; int now = w + k, tot = s1 + s0 + j - (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + (long long)p * r) % P; now = s1 - w + j - k, p = (long long)now * in[tot] % P; dp[i][j + 1][k] = (dp[i][j + 1][k] + (long long)p * r) % P; now = s0 - (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j][k] = (dp[i][j][k] + (long long)p * r) % P; if (i - 1 == m) ans = (ans + (long long)(w + k) * r) % P; } return ans; } for (int i = 1; i <= m + 1; ++i) for (int j = 0; j <= i - 1; ++j) for (int k = 0; k <= min(w, j); ++k) if (dp[i - 1][j][k]) { int &r = dp[i - 1][j][k]; int now = w - k, tot = s0 + s1 - j + (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + (long long)p * r) % P; now = s1 + (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j][k] = (dp[i][j][k] + (long long)p * r) % P; now = s0 - w - j + k, p = (long long)now * in[tot] % P; dp[i][j + 1][k] = (dp[i][j + 1][k] + (long long)p * r) % P; if (i - 1 == m) ans = (ans + (long long)(w - k) * r) % P; } return ans; } int main() { in[0] = 1; for (int i = 1; i <= N - 1; ++i) in[i] = inv(i); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) { scanf("%d", w + i); if (a[i]) s1 += w[i]; else s0 += w[i]; } for (int i = 1; i <= n; ++i) printf("%d\n", solve(a[i], w[i])); } ```
#include <bits/stdc++.h> using namespace std; const int N = 55; const long long mod = 998244353; long long a[N], w[N], dp[N][N][N], pre[N][N][N]; long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long r = exgcd(b, a % b, x, y); long long t = x; x = y; y = t - a / b * y; return r; } long long get_inv(long long a, long long b) { long long x, y; long long d = exgcd(b, mod, x, y); x = (x + mod) % mod; return x * a % mod; } long long solve(long long a, long long b, long long c, long long m, int type) { memset(dp, 0, sizeof(dp)); memset(pre, 0, sizeof(pre)); dp[0][0][0] = 1; for (int t = 1; t <= m; t++) { for (int i = 0; i <= t; i++) { for (int j = 0; j <= t; j++) { for (int k = 0; k <= t; k++) { if (dp[i][j][k] == 0) continue; long long sum = a + b + c + j - k; if (type == 0) sum -= i; else sum += i; if (type == 0 && a - i > 0) { pre[i + 1][j][k] = (pre[i + 1][j][k] + dp[i][j][k] * get_inv(a - i, sum) % mod) % mod; } else if (type == 1) { pre[i + 1][j][k] = (pre[i + 1][j][k] + dp[i][j][k] * get_inv(a + i, sum) % mod) % mod; } pre[i][j + 1][k] = (pre[i][j + 1][k] + dp[i][j][k] * get_inv(b + j, sum) % mod) % mod; if (c - k > 0) pre[i][j][k + 1] = (pre[i][j][k + 1] + dp[i][j][k] * get_inv(c - k, sum) % mod) % mod; } } } for (int i = 0; i <= t; i++) { for (int j = 0; j <= t; j++) { for (int k = 0; k <= t; k++) { dp[i][j][k] = pre[i][j][k]; pre[i][j][k] = 0; } } } } long long res = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { long long v; if (type == 0) v = a - i; else v = a + i; if (v < 0 || c - k < 0 || dp[i][j][k] == 0) continue; res = (res + v * dp[i][j][k] % mod) % mod; } } return res; } int main() { int n, m, one = 0, zero = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } for (int i = 1; i <= n; i++) { scanf("%d", w + i); if (a[i] == 1) one += w[i]; else zero += w[i]; } for (int i = 1; i <= n; i++) { long long sum1 = one, sum2 = zero; if (a[i] == 1) sum1 -= w[i]; else sum2 -= w[i]; printf("%lld\n", solve(w[i], sum1, sum2, m, a[i])); } }
### Prompt Please create a solution in Cpp to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 55; const long long mod = 998244353; long long a[N], w[N], dp[N][N][N], pre[N][N][N]; long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long r = exgcd(b, a % b, x, y); long long t = x; x = y; y = t - a / b * y; return r; } long long get_inv(long long a, long long b) { long long x, y; long long d = exgcd(b, mod, x, y); x = (x + mod) % mod; return x * a % mod; } long long solve(long long a, long long b, long long c, long long m, int type) { memset(dp, 0, sizeof(dp)); memset(pre, 0, sizeof(pre)); dp[0][0][0] = 1; for (int t = 1; t <= m; t++) { for (int i = 0; i <= t; i++) { for (int j = 0; j <= t; j++) { for (int k = 0; k <= t; k++) { if (dp[i][j][k] == 0) continue; long long sum = a + b + c + j - k; if (type == 0) sum -= i; else sum += i; if (type == 0 && a - i > 0) { pre[i + 1][j][k] = (pre[i + 1][j][k] + dp[i][j][k] * get_inv(a - i, sum) % mod) % mod; } else if (type == 1) { pre[i + 1][j][k] = (pre[i + 1][j][k] + dp[i][j][k] * get_inv(a + i, sum) % mod) % mod; } pre[i][j + 1][k] = (pre[i][j + 1][k] + dp[i][j][k] * get_inv(b + j, sum) % mod) % mod; if (c - k > 0) pre[i][j][k + 1] = (pre[i][j][k + 1] + dp[i][j][k] * get_inv(c - k, sum) % mod) % mod; } } } for (int i = 0; i <= t; i++) { for (int j = 0; j <= t; j++) { for (int k = 0; k <= t; k++) { dp[i][j][k] = pre[i][j][k]; pre[i][j][k] = 0; } } } } long long res = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { long long v; if (type == 0) v = a - i; else v = a + i; if (v < 0 || c - k < 0 || dp[i][j][k] == 0) continue; res = (res + v * dp[i][j][k] % mod) % mod; } } return res; } int main() { int n, m, one = 0, zero = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } for (int i = 1; i <= n; i++) { scanf("%d", w + i); if (a[i] == 1) one += w[i]; else zero += w[i]; } for (int i = 1; i <= n; i++) { long long sum1 = one, sum2 = zero; if (a[i] == 1) sum1 -= w[i]; else sum2 -= w[i]; printf("%lld\n", solve(w[i], sum1, sum2, m, a[i])); } } ```
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int M = 2e5 + 10; const int N = 3e3 + 10; int n, m, a[M], w[M], sum[3], f[N][N], g[N][N], inv[N << 1]; int qpow(int a, int b) { int y = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) y = (long long)y * a % mod; return y; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &w[i]), sum[a[i]] += w[i]; for (int i = max(0, m - sum[0]); i <= 2 * m; i++) inv[i] = qpow(sum[0] + sum[1] + i - m, mod - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, sum[0]); j >= 0; j--) { f[i][j] = ((long long)f[i + 1][j] * (sum[1] + i + 1) % mod + (long long)f[i][j + 1] * (sum[0] - j)) % mod * inv[m + i - j] % mod; g[i][j] = ((long long)g[i][j + 1] * (sum[0] - j - 1) % mod + (long long)g[i + 1][j] * (sum[1] + i)) % mod * inv[m + i - j] % mod; } } for (int i = 1; i <= n; i++) printf("%d\n", int((long long)w[i] * (a[i] ? f[0][0] : g[0][0]) % mod)); return 0; }
### Prompt Develop a solution in cpp to the problem described below: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int M = 2e5 + 10; const int N = 3e3 + 10; int n, m, a[M], w[M], sum[3], f[N][N], g[N][N], inv[N << 1]; int qpow(int a, int b) { int y = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) y = (long long)y * a % mod; return y; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &w[i]), sum[a[i]] += w[i]; for (int i = max(0, m - sum[0]); i <= 2 * m; i++) inv[i] = qpow(sum[0] + sum[1] + i - m, mod - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, sum[0]); j >= 0; j--) { f[i][j] = ((long long)f[i + 1][j] * (sum[1] + i + 1) % mod + (long long)f[i][j + 1] * (sum[0] - j)) % mod * inv[m + i - j] % mod; g[i][j] = ((long long)g[i][j + 1] * (sum[0] - j - 1) % mod + (long long)g[i + 1][j] * (sum[1] + i)) % mod * inv[m + i - j] % mod; } } for (int i = 1; i <= n; i++) printf("%d\n", int((long long)w[i] * (a[i] ? f[0][0] : g[0][0]) % mod)); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; int a[66], w[66]; long long dp[66][66][66]; int sa, sb, sum; long long inv[10010]; void solve1(int x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long zi, mu; for (int i = 0; i < m; ++i) for (int j = 0; j <= i; ++j) for (int k = j; k <= i; ++k) { if (!dp[i][j][k]) continue; mu = inv[sum + k * 2 - i]; zi = (w[x] + j) % mod; (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sa + k - j - w[x] + mod) % mod; (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sb - i + k + mod) % mod; (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod; } long long r = 0; for (int j = 0; j <= m; ++j) for (int k = j; k <= m; ++k) { if (!dp[m][j][k]) continue; r += dp[m][j][k] * (w[x] + j) % mod; r %= mod; } printf("%lld\n", r); return; } void solve2(int x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long zi, mu; for (int i = 0; i < m; ++i) for (int j = 0; j <= i; ++j) for (int k = j; k <= i; ++k) { if (!dp[i][j][k]) continue; mu = inv[sum - k * 2 + i]; zi = (w[x] - j) % mod; (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sb - w[x] - k + j + mod) % mod; (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sa + i - k + mod) % mod; (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod; } long long r = 0; for (int j = 0; j <= m; ++j) for (int k = j; k <= m; ++k) { if (!dp[m][j][k]) continue; r += dp[m][j][k] * max(0, w[x] - j) % mod; r %= mod; } printf("%lld\n", r); return; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { scanf("%d", &w[i]); sa += a[i] * w[i]; sb += (a[i] ^ 1) * w[i]; } sum = sa + sb; inv[1] = 1; for (int i = 2; i <= 10000; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= n; ++i) if (a[i]) solve1(i); else solve2(i); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; int a[66], w[66]; long long dp[66][66][66]; int sa, sb, sum; long long inv[10010]; void solve1(int x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long zi, mu; for (int i = 0; i < m; ++i) for (int j = 0; j <= i; ++j) for (int k = j; k <= i; ++k) { if (!dp[i][j][k]) continue; mu = inv[sum + k * 2 - i]; zi = (w[x] + j) % mod; (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sa + k - j - w[x] + mod) % mod; (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sb - i + k + mod) % mod; (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod; } long long r = 0; for (int j = 0; j <= m; ++j) for (int k = j; k <= m; ++k) { if (!dp[m][j][k]) continue; r += dp[m][j][k] * (w[x] + j) % mod; r %= mod; } printf("%lld\n", r); return; } void solve2(int x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long zi, mu; for (int i = 0; i < m; ++i) for (int j = 0; j <= i; ++j) for (int k = j; k <= i; ++k) { if (!dp[i][j][k]) continue; mu = inv[sum - k * 2 + i]; zi = (w[x] - j) % mod; (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sb - w[x] - k + j + mod) % mod; (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sa + i - k + mod) % mod; (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod; } long long r = 0; for (int j = 0; j <= m; ++j) for (int k = j; k <= m; ++k) { if (!dp[m][j][k]) continue; r += dp[m][j][k] * max(0, w[x] - j) % mod; r %= mod; } printf("%lld\n", r); return; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { scanf("%d", &w[i]); sa += a[i] * w[i]; sb += (a[i] ^ 1) * w[i]; } sum = sa + sb; inv[1] = 1; for (int i = 2; i <= 10000; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= n; ++i) if (a[i]) solve1(i); else solve2(i); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long dpf[110][100][60]; long long dpg[110][100][60]; int diff; int likes_init = 0; const long long mod = 998244353; long long fast_exp(long long b, int e) { b = b % mod; if (e == 0) return 1; if (e == 1) return b; if (e % 2 == 1) return (b * fast_exp(b, e - 1)) % mod; return fast_exp((b * b) % mod, e / 2); } long long modinv(long long x) { assert(x % mod != 0); return fast_exp(x, mod - 2); } long long f(int curr, int likes, int runs) { if (dpf[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1, tmp2, tmp3; tmp1 = (curr == 0 ? 0 : curr * f(curr + 1, likes + 1, runs - 1)); tmp2 = (likes - curr == 0 ? 0 : (likes - curr) * f(curr, likes + 1, runs - 1)); tmp3 = (dislike == 0 ? 0 : dislike * f(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpf[curr][likes - likes_init][runs] = ans; } else { return dpf[curr][likes - likes_init][runs]; } } long long g(int curr, int likes, int runs) { if (dpg[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1 = (curr == 0 ? 0 : curr * g(curr - 1, likes, runs - 1)); long long tmp2 = (likes == 0 ? 0 : likes * g(curr, likes + 1, runs - 1)); long long tmp3 = (dislike - curr == 0 ? 0 : (dislike - curr) * g(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpg[curr][likes - likes_init][runs] = ans; } else { return dpg[curr][likes - likes_init][runs]; } } int main() { int n, m; cin >> n >> m; diff = m; int a[n]; for (int i = 0; i < 110; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 60; k++) { dpf[i][j][k] = 1LL << 40; dpg[i][j][k] = 1LL << 40; } } } bool good[n]; int tl = 0; for (int i = 0; i < n; i++) { cin >> good[i]; } for (int i = 0; i < n; i++) { cin >> a[i]; if (good[i]) { diff += a[i]; tl += a[i]; } else diff -= a[i]; } likes_init = tl; for (int i = 0; i < n; i++) { if (good[i]) { cout << (f(a[i], tl, m) % mod + mod) % mod << "\n"; } else { cout << (g(a[i], tl, m) % mod + mod) % mod << "\n"; } } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long dpf[110][100][60]; long long dpg[110][100][60]; int diff; int likes_init = 0; const long long mod = 998244353; long long fast_exp(long long b, int e) { b = b % mod; if (e == 0) return 1; if (e == 1) return b; if (e % 2 == 1) return (b * fast_exp(b, e - 1)) % mod; return fast_exp((b * b) % mod, e / 2); } long long modinv(long long x) { assert(x % mod != 0); return fast_exp(x, mod - 2); } long long f(int curr, int likes, int runs) { if (dpf[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1, tmp2, tmp3; tmp1 = (curr == 0 ? 0 : curr * f(curr + 1, likes + 1, runs - 1)); tmp2 = (likes - curr == 0 ? 0 : (likes - curr) * f(curr, likes + 1, runs - 1)); tmp3 = (dislike == 0 ? 0 : dislike * f(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpf[curr][likes - likes_init][runs] = ans; } else { return dpf[curr][likes - likes_init][runs]; } } long long g(int curr, int likes, int runs) { if (dpg[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1 = (curr == 0 ? 0 : curr * g(curr - 1, likes, runs - 1)); long long tmp2 = (likes == 0 ? 0 : likes * g(curr, likes + 1, runs - 1)); long long tmp3 = (dislike - curr == 0 ? 0 : (dislike - curr) * g(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpg[curr][likes - likes_init][runs] = ans; } else { return dpg[curr][likes - likes_init][runs]; } } int main() { int n, m; cin >> n >> m; diff = m; int a[n]; for (int i = 0; i < 110; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 60; k++) { dpf[i][j][k] = 1LL << 40; dpg[i][j][k] = 1LL << 40; } } } bool good[n]; int tl = 0; for (int i = 0; i < n; i++) { cin >> good[i]; } for (int i = 0; i < n; i++) { cin >> a[i]; if (good[i]) { diff += a[i]; tl += a[i]; } else diff -= a[i]; } likes_init = tl; for (int i = 0; i < n; i++) { if (good[i]) { cout << (f(a[i], tl, m) % mod + mod) % mod << "\n"; } else { cout << (g(a[i], tl, m) % mod + mod) % mod << "\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; char a[3][3]; int p[3]; int main() { int i; cin >> a[0] >> a[1] >> a[2]; int x; for (i = 0; i < 3; i++) { x = 0; switch (a[i][1]) { case 's': x = 0; break; case 'm': x = 10; break; case 'p': x = 20; break; } p[i] = x + (a[i][0] - '0'); } sort(p, p + 3); if (p[0] == p[1] && p[1] == p[2]) { cout << 0; return 0; } if (p[2] - p[1] == 1 && p[1] - p[0] == 1) { cout << 0; return 0; } if (p[2] / 10 == p[1] / 10 && p[2] - p[1] <= 2) { cout << 1; return 0; } if (p[1] / 10 == p[0] / 10 && p[1] - p[0] <= 2) { cout << 1; return 0; } cout << 2; }
### Prompt Please formulate a Cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char a[3][3]; int p[3]; int main() { int i; cin >> a[0] >> a[1] >> a[2]; int x; for (i = 0; i < 3; i++) { x = 0; switch (a[i][1]) { case 's': x = 0; break; case 'm': x = 10; break; case 'p': x = 20; break; } p[i] = x + (a[i][0] - '0'); } sort(p, p + 3); if (p[0] == p[1] && p[1] == p[2]) { cout << 0; return 0; } if (p[2] - p[1] == 1 && p[1] - p[0] == 1) { cout << 0; return 0; } if (p[2] / 10 == p[1] / 10 && p[2] - p[1] <= 2) { cout << 1; return 0; } if (p[1] / 10 == p[0] / 10 && p[1] - p[0] <= 2) { cout << 1; return 0; } cout << 2; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; const int seed = 1333331; long long qm(long long a, long long b, long long res = 1) { for (a %= mod; b; b >>= 1, a = a * a % mod) if (b & 1) res = res * a % mod; return res; } int n, m; long long a[maxn], res, lim[maxn]; vector<int> vec; int val[110]; string s; int main() { int tmp, res = 3; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; for (int i = 1; i <= 9; i++) { int tmp = 3; if (i > 1 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 9 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } for (int i = 11; i <= 19; i++) { int tmp = 3; if (i > 11 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 19 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } for (int i = 21; i <= 29; i++) { int tmp = 3; if (i > 21 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 29 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } cout << res << endl; }
### Prompt In Cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; const int seed = 1333331; long long qm(long long a, long long b, long long res = 1) { for (a %= mod; b; b >>= 1, a = a * a % mod) if (b & 1) res = res * a % mod; return res; } int n, m; long long a[maxn], res, lim[maxn]; vector<int> vec; int val[110]; string s; int main() { int tmp, res = 3; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; for (int i = 1; i <= 9; i++) { int tmp = 3; if (i > 1 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 9 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } for (int i = 11; i <= 19; i++) { int tmp = 3; if (i > 11 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 19 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } for (int i = 21; i <= 29; i++) { int tmp = 3; if (i > 21 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 29 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } cout << res << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; if (a == b && a == c) cout << "0"; else if ((a == b && a != c)) cout << "1"; else if ((a == c && a != b)) cout << "1"; else if (b == c && b != a) cout << "1"; else if (a[1] == b[1] && b[1] == c[1]) { if ((a[0] + 1 == b[0]) && (b[0] + 1 == c[0]) || ((b[0] + 1 == a[0]) && (a[0] + 1 == c[0])) || ((a[0] + 1 == c[0]) && (c[0] + 1 == b[0])) || ((a[0] + 1 == b[0]) && (c[0] + 1 == a[0])) || ((b[0] + 1 == c[0]) && (c[0] + 1 == a[0]))) cout << "0"; else if ((a[0] + 1 == b[0]) || (b[0] + 1 == c[0]) || (a[0] + 1 == c[0]) || (c[0] + 1 == a[0]) || (c[0] + 1 == b[0])) cout << "1"; else if ((abs(a[0] - b[0]) == 2) || (abs(b[0] - c[0]) == 2) || (abs(a[0] - c[0]) == 2)) cout << "1"; else if (a == b && b != c) cout << "1"; else if (b == c && b != a) cout << "1"; else cout << "2"; } else if (a[1] == b[1]) { if (a[0] + 1 == b[0] || b[0] + 1 == a[0]) cout << "1"; else if (abs(b[0] - a[0]) == 2) cout << "1"; else cout << "2"; } else if (b[1] == c[1]) { if (b[0] + 1 == c[0] || c[0] + 1 == b[0]) cout << "1"; else if (abs(b[0] - c[0]) == 2) cout << "1"; else cout << "2"; } else if (a[1] == c[1]) { if (a[0] + 1 == c[0] || c[0] + 1 == a[0]) cout << "1"; else if (abs(a[0] - c[0]) == 2) cout << "1"; else cout << "2"; } else if ((a[1] != b[1] && a[1] != c[1]) && (b[1] != c[1])) cout << "2"; }
### Prompt Create a solution in CPP for the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; if (a == b && a == c) cout << "0"; else if ((a == b && a != c)) cout << "1"; else if ((a == c && a != b)) cout << "1"; else if (b == c && b != a) cout << "1"; else if (a[1] == b[1] && b[1] == c[1]) { if ((a[0] + 1 == b[0]) && (b[0] + 1 == c[0]) || ((b[0] + 1 == a[0]) && (a[0] + 1 == c[0])) || ((a[0] + 1 == c[0]) && (c[0] + 1 == b[0])) || ((a[0] + 1 == b[0]) && (c[0] + 1 == a[0])) || ((b[0] + 1 == c[0]) && (c[0] + 1 == a[0]))) cout << "0"; else if ((a[0] + 1 == b[0]) || (b[0] + 1 == c[0]) || (a[0] + 1 == c[0]) || (c[0] + 1 == a[0]) || (c[0] + 1 == b[0])) cout << "1"; else if ((abs(a[0] - b[0]) == 2) || (abs(b[0] - c[0]) == 2) || (abs(a[0] - c[0]) == 2)) cout << "1"; else if (a == b && b != c) cout << "1"; else if (b == c && b != a) cout << "1"; else cout << "2"; } else if (a[1] == b[1]) { if (a[0] + 1 == b[0] || b[0] + 1 == a[0]) cout << "1"; else if (abs(b[0] - a[0]) == 2) cout << "1"; else cout << "2"; } else if (b[1] == c[1]) { if (b[0] + 1 == c[0] || c[0] + 1 == b[0]) cout << "1"; else if (abs(b[0] - c[0]) == 2) cout << "1"; else cout << "2"; } else if (a[1] == c[1]) { if (a[0] + 1 == c[0] || c[0] + 1 == a[0]) cout << "1"; else if (abs(a[0] - c[0]) == 2) cout << "1"; else cout << "2"; } else if ((a[1] != b[1] && a[1] != c[1]) && (b[1] != c[1])) cout << "2"; } ```
#include <bits/stdc++.h> using namespace std; int s[15][5]; int k[300]; int main() { int a, b, c; char x, y, z; cin >> a >> x >> b >> y >> c >> z; k['m'] = 1; k['p'] = 2; k['s'] = 3; s[a][k[x]]++, s[b][k[y]]++, s[c][k[z]]++; int ans = 3; for (int i = 1; i <= 9; i++) for (int j = 1; j <= 3; j++) ans = min(ans, 3 - s[i][j]); for (int i = 1; i <= 7; i++) for (int j = 1; j <= 3; j++) ans = min(ans, 3 - (s[i][j] >= 1) - (s[i + 1][j] >= 1) - (s[i + 2][j] >= 1)); cout << ans << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int s[15][5]; int k[300]; int main() { int a, b, c; char x, y, z; cin >> a >> x >> b >> y >> c >> z; k['m'] = 1; k['p'] = 2; k['s'] = 3; s[a][k[x]]++, s[b][k[y]]++, s[c][k[z]]++; int ans = 3; for (int i = 1; i <= 9; i++) for (int j = 1; j <= 3; j++) ans = min(ans, 3 - s[i][j]); for (int i = 1; i <= 7; i++) for (int j = 1; j <= 3; j++) ans = min(ans, 3 - (s[i][j] >= 1) - (s[i + 1][j] >= 1) - (s[i + 2][j] >= 1)); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { map<char, int> m; m['s'] = 0; m['m'] = 1; m['p'] = 2; string a, b, c; cin >> a >> b >> c; if (a == b && b == c) { cout << 0; return 0; } if (a == b || b == c || c == a) { cout << 1; return 0; } bool p[3][10]; for (int i = 0; i < 10; i++) { p[1][i] = 0; p[2][i] = 0; p[0][i] = 0; } p[m[a[1]]][(int(a[0]) - '0')] = true; p[m[b[1]]][(int(b[0]) - '0')] = true; p[m[c[1]]][(int(c[0]) - '0')] = true; int ans = 2; int temp = 0; for (int j = 0; j <= 2; j++) { for (int i = 1; i <= 7; i++) { temp = 0; if (p[j][i]) temp++; if (p[j][i + 1]) temp++; if (p[j][i + 2]) temp++; if (temp == 3) { cout << "0"; return 0; } if (temp == 2) ans = min(ans, 1); } } cout << ans; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { map<char, int> m; m['s'] = 0; m['m'] = 1; m['p'] = 2; string a, b, c; cin >> a >> b >> c; if (a == b && b == c) { cout << 0; return 0; } if (a == b || b == c || c == a) { cout << 1; return 0; } bool p[3][10]; for (int i = 0; i < 10; i++) { p[1][i] = 0; p[2][i] = 0; p[0][i] = 0; } p[m[a[1]]][(int(a[0]) - '0')] = true; p[m[b[1]]][(int(b[0]) - '0')] = true; p[m[c[1]]][(int(c[0]) - '0')] = true; int ans = 2; int temp = 0; for (int j = 0; j <= 2; j++) { for (int i = 1; i <= 7; i++) { temp = 0; if (p[j][i]) temp++; if (p[j][i + 1]) temp++; if (p[j][i + 2]) temp++; if (temp == 3) { cout << "0"; return 0; } if (temp == 2) ans = min(ans, 1); } } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; using namespace std; string a[4]; int main() { cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0][1] == a[1][1] && a[1][1] == a[2][1]) { if (a[0][0] == a[1][0] && a[1][0] == a[2][0]) { printf("0\n"); return 0; } if (a[0][0] + 1 == a[1][0] && a[1][0] + 1 == a[2][0]) { printf("0\n"); return 0; } } if (a[0] == a[1] || a[0] == a[2] || a[1] == a[2]) { printf("1\n"); return 0; } if ((a[0][0] + 1 == a[1][0] && a[0][1] == a[1][1]) || (a[0][0] + 1 == a[2][0] && a[0][1] == a[2][1]) || (a[1][0] + 1 == a[2][0] && a[1][1] == a[2][1])) { printf("1\n"); return 0; } if ((a[0][0] + 2 == a[1][0] && a[0][1] == a[1][1]) || (a[0][0] + 2 == a[2][0] && a[0][1] == a[2][1]) || (a[1][0] + 2 == a[2][0] && a[1][1] == a[2][1])) { printf("1\n"); return 0; } printf("2\n"); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std; string a[4]; int main() { cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0][1] == a[1][1] && a[1][1] == a[2][1]) { if (a[0][0] == a[1][0] && a[1][0] == a[2][0]) { printf("0\n"); return 0; } if (a[0][0] + 1 == a[1][0] && a[1][0] + 1 == a[2][0]) { printf("0\n"); return 0; } } if (a[0] == a[1] || a[0] == a[2] || a[1] == a[2]) { printf("1\n"); return 0; } if ((a[0][0] + 1 == a[1][0] && a[0][1] == a[1][1]) || (a[0][0] + 1 == a[2][0] && a[0][1] == a[2][1]) || (a[1][0] + 1 == a[2][0] && a[1][1] == a[2][1])) { printf("1\n"); return 0; } if ((a[0][0] + 2 == a[1][0] && a[0][1] == a[1][1]) || (a[0][0] + 2 == a[2][0] && a[0][1] == a[2][1]) || (a[1][0] + 2 == a[2][0] && a[1][1] == a[2][1])) { printf("1\n"); return 0; } printf("2\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int num, m = 0, p = 0, s = 0; char alpha; vector<pair<int, char>> tiles; for (int i = 0; i < 3; i++) { cin >> num >> alpha; pair<int, char> temp = make_pair(num, alpha); tiles.push_back(temp); if (alpha == 'm') { m++; } else if (alpha == 'p') { p++; } else { s++; } } sort(tiles.begin(), tiles.end()); int one = abs(tiles[0].first - tiles[1].first); int two = abs(tiles[1].first - tiles[2].first); int three = abs(tiles[0].first - tiles[2].first); if (tiles[0].second == tiles[1].second && tiles[1].second == tiles[2].second) { if (one == 0 && two == 0) { cout << 0 << endl; return 0; } else if (one == 0 || two == 0 || three == 0) { cout << 1 << endl; return 0; } else { if (one == 1 && two == 1) { cout << 0 << endl; } else if (one > 1 && two == 1 || one == 1 && two > 1 || one == 2 || two == 2) { cout << 1 << endl; } else { cout << 2 << endl; } } return 0; } else if (tiles[0].second == tiles[1].second) { if (one <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else if (tiles[1].second == tiles[2].second) { if (two <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else if (tiles[0].second == tiles[2].second) { if (three <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else { cout << 2 << endl; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int num, m = 0, p = 0, s = 0; char alpha; vector<pair<int, char>> tiles; for (int i = 0; i < 3; i++) { cin >> num >> alpha; pair<int, char> temp = make_pair(num, alpha); tiles.push_back(temp); if (alpha == 'm') { m++; } else if (alpha == 'p') { p++; } else { s++; } } sort(tiles.begin(), tiles.end()); int one = abs(tiles[0].first - tiles[1].first); int two = abs(tiles[1].first - tiles[2].first); int three = abs(tiles[0].first - tiles[2].first); if (tiles[0].second == tiles[1].second && tiles[1].second == tiles[2].second) { if (one == 0 && two == 0) { cout << 0 << endl; return 0; } else if (one == 0 || two == 0 || three == 0) { cout << 1 << endl; return 0; } else { if (one == 1 && two == 1) { cout << 0 << endl; } else if (one > 1 && two == 1 || one == 1 && two > 1 || one == 2 || two == 2) { cout << 1 << endl; } else { cout << 2 << endl; } } return 0; } else if (tiles[0].second == tiles[1].second) { if (one <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else if (tiles[1].second == tiles[2].second) { if (two <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else if (tiles[0].second == tiles[2].second) { if (three <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else { cout << 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> b(9); for (int i = 0; i < 9; i++) for (int j = 0; j < 3; j++) b[i].push_back(0); string s; for (int i = 0; i < 3; i++) { cin >> s; switch (s[1]) { case 'm': b[s[0] - '1'][0]++; break; case 'p': b[s[0] - '1'][1]++; break; case 's': b[s[0] - '1'][2]++; break; } } int m = 0; for (int i = 0; i < 9; i++) { int s1 = b[i][0]; int s2 = b[i][1]; int s3 = b[i][2]; m = min(max(max(max(s1, s2), s3), m), 3); } for (int i = 0; i < 7; i++) { int s1 = (b[i][0] != 0) + (b[i + 1][0] != 0) + (b[i + 2][0] != 0); int s2 = (b[i][1] != 0) + (b[i + 1][1] != 0) + (b[i + 2][1] != 0); int s3 = (b[i][2] != 0) + (b[i + 1][2] != 0) + (b[i + 2][2] != 0); m = max(max(max(s1, s2), s3), m); } cout << 3 - m; }
### Prompt Construct a CPP code solution to the problem outlined: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> b(9); for (int i = 0; i < 9; i++) for (int j = 0; j < 3; j++) b[i].push_back(0); string s; for (int i = 0; i < 3; i++) { cin >> s; switch (s[1]) { case 'm': b[s[0] - '1'][0]++; break; case 'p': b[s[0] - '1'][1]++; break; case 's': b[s[0] - '1'][2]++; break; } } int m = 0; for (int i = 0; i < 9; i++) { int s1 = b[i][0]; int s2 = b[i][1]; int s3 = b[i][2]; m = min(max(max(max(s1, s2), s3), m), 3); } for (int i = 0; i < 7; i++) { int s1 = (b[i][0] != 0) + (b[i + 1][0] != 0) + (b[i + 2][0] != 0); int s2 = (b[i][1] != 0) + (b[i + 1][1] != 0) + (b[i + 2][1] != 0); int s3 = (b[i][2] != 0) + (b[i + 1][2] != 0) + (b[i + 2][2] != 0); m = max(max(max(s1, s2), s3), m); } cout << 3 - m; } ```
#include <bits/stdc++.h> using namespace std; int num[4]; template <class T> int check_mod(T a, T b) { return (a % b) ? 1 : 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<string> cards(3); for (int i = 0; i < 3; i++) cin >> cards[i]; sort(cards.begin(), cards.end()); if ((cards[0] == cards[1] && cards[1] == cards[2]) || ((cards[0][1] == cards[1][1] && cards[1][1] == cards[2][1]) && (cards[1][0] == cards[0][0] + 1 && cards[2][0] == cards[1][0] + 1))) { cout << 0 << endl; return 0; } if (cards[0] == cards[1] || cards[1] == cards[2] || cards[0] == cards[2]) { cout << 1 << endl; return 0; } if (cards[0][1] == cards[1][1] && (abs(cards[0][0] - cards[1][0]) == 1 || abs(cards[0][0] - cards[1][0]) == 2)) { cout << 1 << endl; return 0; } if (cards[0][1] == cards[2][1] && (abs(cards[0][0] - cards[2][0]) == 1 || abs(cards[0][0] - cards[2][0]) == 2)) { cout << 1 << endl; return 0; } if (cards[2][1] == cards[1][1] && (abs(cards[2][0] - cards[1][0]) == 1 || abs(cards[2][0] - cards[1][0]) == 2)) { cout << 1 << endl; return 0; } cout << 2 << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int num[4]; template <class T> int check_mod(T a, T b) { return (a % b) ? 1 : 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<string> cards(3); for (int i = 0; i < 3; i++) cin >> cards[i]; sort(cards.begin(), cards.end()); if ((cards[0] == cards[1] && cards[1] == cards[2]) || ((cards[0][1] == cards[1][1] && cards[1][1] == cards[2][1]) && (cards[1][0] == cards[0][0] + 1 && cards[2][0] == cards[1][0] + 1))) { cout << 0 << endl; return 0; } if (cards[0] == cards[1] || cards[1] == cards[2] || cards[0] == cards[2]) { cout << 1 << endl; return 0; } if (cards[0][1] == cards[1][1] && (abs(cards[0][0] - cards[1][0]) == 1 || abs(cards[0][0] - cards[1][0]) == 2)) { cout << 1 << endl; return 0; } if (cards[0][1] == cards[2][1] && (abs(cards[0][0] - cards[2][0]) == 1 || abs(cards[0][0] - cards[2][0]) == 2)) { cout << 1 << endl; return 0; } if (cards[2][1] == cards[1][1] && (abs(cards[2][0] - cards[1][0]) == 1 || abs(cards[2][0] - cards[1][0]) == 2)) { cout << 1 << endl; return 0; } cout << 2 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int buc[101][101], ans = 0x3f3f3f3f; char ch1, ch2; int hash_(char c) { if (c == 'm') return 1; if (c == 'p') return 2; if (c == 's') return 3; } int main() { while (~scanf("%c%c%*c", &ch1, &ch2)) { ++buc[hash_(ch2)][ch1 - '0']; } for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= 3; ++j) { ans = min(ans, 3 - buc[j][i]); } } for (int j = 1; j <= 3; ++j) { for (int i = 1; i <= 7; ++i) { ans = min( ans, 3 - (bool)buc[j][i] - (bool)buc[j][i + 1] - (bool)buc[j][i + 2]); } } printf("%d\n", ans); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int buc[101][101], ans = 0x3f3f3f3f; char ch1, ch2; int hash_(char c) { if (c == 'm') return 1; if (c == 'p') return 2; if (c == 's') return 3; } int main() { while (~scanf("%c%c%*c", &ch1, &ch2)) { ++buc[hash_(ch2)][ch1 - '0']; } for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= 3; ++j) { ans = min(ans, 3 - buc[j][i]); } } for (int j = 1; j <= 3; ++j) { for (int i = 1; i <= 7; ++i) { ans = min( ans, 3 - (bool)buc[j][i] - (bool)buc[j][i + 1] - (bool)buc[j][i + 2]); } } printf("%d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int s[15], p[15], m[15], ans, l, i, j, minim = 9; string s1[5]; int main() { for (int i = 0; i < 3; ++i) { cin >> s1[i]; if (s1[i][1] == 's') s[s1[i][0] - '0']++; if (s1[i][1] == 'p') p[s1[i][0] - '0']++; if (s1[i][1] == 'm') m[s1[i][0] - '0']++; } for (i = 1; i <= 9; i++) { if (s[i] == 3 || p[i] == 3 || m[i] == 3) ans = 0; else if ((s[i] == 1 && s[i + 1] == 1 && s[i + 2] == 1) || (m[i] == 1 && m[i + 1] == 1 && m[i + 2] == 1) || (p[i] == 1 && p[i + 1] == 1 && p[i + 2] == 1)) ans = 0; else if (((s[i] == 1 && s[i + 1] == 1) || (s[i] == 1 && s[i + 2] == 1)) || ((m[i] == 1 && m[i + 1] == 1) || (m[i] == 1 && m[i + 2] == 1)) || ((p[i] == 1 && p[i + 1] == 1) || (p[i] == 1 && p[i + 2] == 1)) || s[i] == 2 || m[i] == 2 || p[i] == 2) ans = 1; else ans = 2; if (ans < minim) minim = ans; } cout << minim; return 0; }
### Prompt Create a solution in cpp for the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int s[15], p[15], m[15], ans, l, i, j, minim = 9; string s1[5]; int main() { for (int i = 0; i < 3; ++i) { cin >> s1[i]; if (s1[i][1] == 's') s[s1[i][0] - '0']++; if (s1[i][1] == 'p') p[s1[i][0] - '0']++; if (s1[i][1] == 'm') m[s1[i][0] - '0']++; } for (i = 1; i <= 9; i++) { if (s[i] == 3 || p[i] == 3 || m[i] == 3) ans = 0; else if ((s[i] == 1 && s[i + 1] == 1 && s[i + 2] == 1) || (m[i] == 1 && m[i + 1] == 1 && m[i + 2] == 1) || (p[i] == 1 && p[i + 1] == 1 && p[i + 2] == 1)) ans = 0; else if (((s[i] == 1 && s[i + 1] == 1) || (s[i] == 1 && s[i + 2] == 1)) || ((m[i] == 1 && m[i + 1] == 1) || (m[i] == 1 && m[i + 2] == 1)) || ((p[i] == 1 && p[i + 1] == 1) || (p[i] == 1 && p[i + 2] == 1)) || s[i] == 2 || m[i] == 2 || p[i] == 2) ans = 1; else ans = 2; if (ans < minim) minim = ans; } cout << minim; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string a, b, c; cin >> a >> b >> c; vector<int> m(10, 0); vector<int> p(10, 0); vector<int> s(10, 0); if (a[1] == 'm') { m[a[0] - 48]++; } else if (a[1] == 'p') { p[a[0] - 48]++; } else if (a[1] == 's') { s[a[0] - 48]++; } if (b[1] == 'm') { m[b[0] - 48]++; } else if (b[1] == 'p') { p[b[0] - 48]++; } else if (b[1] == 's') { s[b[0] - 48]++; } if (c[1] == 'm') { m[c[0] - 48]++; } else if (c[1] == 'p') { p[c[0] - 48]++; } else if (c[1] == 's') { s[c[0] - 48]++; } bool flag0 = false; bool flag1 = false; for (int i = 1; i < 8; i++) { if ((m[i] > 0 && m[i + 1] > 0 && m[i + 2]) || (p[i] > 0 && p[i + 1] > 0 && p[i + 2]) || (s[i] > 0 && s[i + 1] > 0 && s[i + 2])) { flag0 = true; } if ((m[i] > 0 && m[i + 2] > 0) || (p[i] > 0 && p[i + 2] > 0) || (s[i] > 0 && s[i + 2] > 0)) { flag1 = true; } } for (int i = 1; i < 9; i++) { if ((m[i] > 0 && m[i + 1] > 0) || (p[i] > 0 && p[i + 1] > 0) || (s[i] > 0 && s[i + 1] > 0)) { flag1 = true; } } for (int i = 1; i < 10; i++) { if (m[i] == 3 || p[i] == 3 || s[i] == 3) flag0 = true; if (m[i] == 2 || p[i] == 2 || s[i] == 2) flag1 = true; } if (flag0) { cout << 0; return 0; } else if (flag1) { cout << 1; return 0; } else { cout << 2; return 0; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string a, b, c; cin >> a >> b >> c; vector<int> m(10, 0); vector<int> p(10, 0); vector<int> s(10, 0); if (a[1] == 'm') { m[a[0] - 48]++; } else if (a[1] == 'p') { p[a[0] - 48]++; } else if (a[1] == 's') { s[a[0] - 48]++; } if (b[1] == 'm') { m[b[0] - 48]++; } else if (b[1] == 'p') { p[b[0] - 48]++; } else if (b[1] == 's') { s[b[0] - 48]++; } if (c[1] == 'm') { m[c[0] - 48]++; } else if (c[1] == 'p') { p[c[0] - 48]++; } else if (c[1] == 's') { s[c[0] - 48]++; } bool flag0 = false; bool flag1 = false; for (int i = 1; i < 8; i++) { if ((m[i] > 0 && m[i + 1] > 0 && m[i + 2]) || (p[i] > 0 && p[i + 1] > 0 && p[i + 2]) || (s[i] > 0 && s[i + 1] > 0 && s[i + 2])) { flag0 = true; } if ((m[i] > 0 && m[i + 2] > 0) || (p[i] > 0 && p[i + 2] > 0) || (s[i] > 0 && s[i + 2] > 0)) { flag1 = true; } } for (int i = 1; i < 9; i++) { if ((m[i] > 0 && m[i + 1] > 0) || (p[i] > 0 && p[i + 1] > 0) || (s[i] > 0 && s[i + 1] > 0)) { flag1 = true; } } for (int i = 1; i < 10; i++) { if (m[i] == 3 || p[i] == 3 || s[i] == 3) flag0 = true; if (m[i] == 2 || p[i] == 2 || s[i] == 2) flag1 = true; } if (flag0) { cout << 0; return 0; } else if (flag1) { cout << 1; return 0; } else { cout << 2; return 0; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int check(int req, string s1, string s2, string s3) { int new_req = 3; vector<int> list; if (s1[1] == s2[1] && s1[1] == s3[1]) { list.push_back(s1[0] - 48); list.push_back(s2[0] - 48); list.push_back(s3[0] - 48); sort(list.begin(), list.end()); if (abs(list[0] - list[1]) == 1 && abs(list[1] - list[2]) == 1) new_req = 0; else if (abs(list[0] - list[1]) == 1 || abs(list[1] - list[2]) == 1 || abs(list[0] - list[1]) == 2 || abs(list[1] - list[2]) == 2) new_req = 1; else new_req = 2; } else if ((s1[1] == s2[1] && (abs(s1[0] - s2[0]) == 1 || abs(s1[0] - s2[0]) == 2))) new_req = 1; else if ((s3[1] == s2[1] && (abs(s3[0] - s2[0]) == 1 || abs(s3[0] - s2[0]) == 2))) new_req = 1; else if ((s3[1] == s1[1] && (abs(s3[0] - s1[0]) == 1 || abs(s3[0] - s1[0]) == 2))) new_req = 1; else new_req = 2; if (new_req < req) return new_req; else return req; } int main() { int req = 0; string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 == s2 && s2 == s3) req = 0; else if (s1 == s2 || s2 == s3 || s1 == s3) req = 1; else { req = 2; } req = check(req, s1, s2, s3); cout << req; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int check(int req, string s1, string s2, string s3) { int new_req = 3; vector<int> list; if (s1[1] == s2[1] && s1[1] == s3[1]) { list.push_back(s1[0] - 48); list.push_back(s2[0] - 48); list.push_back(s3[0] - 48); sort(list.begin(), list.end()); if (abs(list[0] - list[1]) == 1 && abs(list[1] - list[2]) == 1) new_req = 0; else if (abs(list[0] - list[1]) == 1 || abs(list[1] - list[2]) == 1 || abs(list[0] - list[1]) == 2 || abs(list[1] - list[2]) == 2) new_req = 1; else new_req = 2; } else if ((s1[1] == s2[1] && (abs(s1[0] - s2[0]) == 1 || abs(s1[0] - s2[0]) == 2))) new_req = 1; else if ((s3[1] == s2[1] && (abs(s3[0] - s2[0]) == 1 || abs(s3[0] - s2[0]) == 2))) new_req = 1; else if ((s3[1] == s1[1] && (abs(s3[0] - s1[0]) == 1 || abs(s3[0] - s1[0]) == 2))) new_req = 1; else new_req = 2; if (new_req < req) return new_req; else return req; } int main() { int req = 0; string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 == s2 && s2 == s3) req = 0; else if (s1 == s2 || s2 == s3 || s1 == s3) req = 1; else { req = 2; } req = check(req, s1, s2, s3); cout << req; return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); solve(); return 0; } void solve() { string a, b, c; cin >> a >> b >> c; vector<int> arr; arr.push_back(a[0]); arr.push_back(b[0]); arr.push_back(c[0]); if (a[1] == b[1] && a[1] == c[1]) { if (arr[0] == arr[1] && arr[0] == arr[2]) cout << 0 << endl; else if (arr[0] == arr[1] || arr[1] == arr[2] || arr[0] == arr[2]) { cout << 1 << endl; } else { int min = 4; sort(arr.begin(), arr.end()); for (int i = 0; i < 2; ++i) { if ((arr[i + 1] - arr[i]) < min) min = arr[i + 1] - arr[i]; } if (min == 3 || min == 4) cout << 2 << endl; else if (min == 2) cout << 1 << endl; else { if (arr[1] == (arr[0] + 1) && arr[2] == (arr[1] + 1)) cout << 0 << endl; else cout << 1 << endl; } } } else if (a[1] == b[1] || a[1] == c[1] || c[1] == b[1]) { if (a[1] == b[1]) { int temp = abs(a[0] - b[0]); if (temp == 0 || temp == 1 || temp == 2) cout << 1 << endl; else cout << 2 << endl; } else if (a[1] == c[1]) { int temp = abs(a[0] - c[0]); if (temp == 0 || temp == 1 || temp == 2) cout << 1 << endl; else cout << 2 << endl; } else { int temp = abs(b[0] - c[0]); if (temp == 0 || temp == 1 || temp == 2) cout << 1 << endl; else cout << 2 << endl; } } else { cout << 2 << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); solve(); return 0; } void solve() { string a, b, c; cin >> a >> b >> c; vector<int> arr; arr.push_back(a[0]); arr.push_back(b[0]); arr.push_back(c[0]); if (a[1] == b[1] && a[1] == c[1]) { if (arr[0] == arr[1] && arr[0] == arr[2]) cout << 0 << endl; else if (arr[0] == arr[1] || arr[1] == arr[2] || arr[0] == arr[2]) { cout << 1 << endl; } else { int min = 4; sort(arr.begin(), arr.end()); for (int i = 0; i < 2; ++i) { if ((arr[i + 1] - arr[i]) < min) min = arr[i + 1] - arr[i]; } if (min == 3 || min == 4) cout << 2 << endl; else if (min == 2) cout << 1 << endl; else { if (arr[1] == (arr[0] + 1) && arr[2] == (arr[1] + 1)) cout << 0 << endl; else cout << 1 << endl; } } } else if (a[1] == b[1] || a[1] == c[1] || c[1] == b[1]) { if (a[1] == b[1]) { int temp = abs(a[0] - b[0]); if (temp == 0 || temp == 1 || temp == 2) cout << 1 << endl; else cout << 2 << endl; } else if (a[1] == c[1]) { int temp = abs(a[0] - c[0]); if (temp == 0 || temp == 1 || temp == 2) cout << 1 << endl; else cout << 2 << endl; } else { int temp = abs(b[0] - c[0]); if (temp == 0 || temp == 1 || temp == 2) cout << 1 << endl; else cout << 2 << endl; } } else { cout << 2 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { char s[10], s1[10], s2[10]; scanf("%s", s); scanf("%s", s1); scanf("%s", s2); vector<int> v, v1, v2; for (int i = 0; i < 5; i++) { v.push_back(100); v1.push_back(100); v2.push_back(100); } if (s[1] == 'm') v.push_back((char)s[0] - 48); if (s[1] == 's') v1.push_back((char)s[0] - 48); if (s[1] == 'p') v2.push_back((char)s[0] - 48); if (s1[1] == 'm') v.push_back((char)s1[0] - 48); if (s1[1] == 's') v1.push_back((char)s1[0] - 48); if (s1[1] == 'p') v2.push_back((char)s1[0] - 48); if (s2[1] == 'm') v.push_back((char)s2[0] - 48); if (s2[1] == 's') v1.push_back((char)s2[0] - 48); if (s2[1] == 'p') v2.push_back((char)s2[0] - 48); sort(v.begin(), v.end()); sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); int ans = 10000; for (int i = 0; i < v.size(); i++) { if (v[i] == 100) break; if (v[i] == v[i + 1] && v[i + 1] == v[i + 2]) { ans = 0; break; } if (v[i] == v[i + 1] - 1 && v[i] == v[i + 2] - 2) { ans = 0; break; } } for (int i = 0; i < v1.size(); i++) { if (v1[i] == 100) break; if (v1[i] == v1[i + 1] && v1[i + 1] == v1[i + 2]) { ans = 0; break; } if (v1[i] == v1[i + 1] - 1 && v1[i] == v1[i + 2] - 2) { ans = 0; break; } } for (int i = 0; i < v2.size(); i++) { if (v2[i] == 100) break; if (v2[i] == v2[i + 1] && v2[i + 1] == v2[i + 2]) { ans = 0; break; } if (v2[i] == v2[i + 1] - 1 && v2[i] == v2[i + 2] - 2) { ans = 0; break; } } if (!ans) { cout << 0; return 0; } for (int i = 0; i < v.size(); i++) { if (v[i] == 100) break; if (v[i] == v[i + 1] || v[i] == v[i + 1] - 1 || v[i] == v[i + 1] - 2) { cout << 1; return 0; } } for (int i = 0; i < v1.size(); i++) { if (v1[i] == 100) break; if (v1[i] == v1[i + 1] || v1[i] == v1[i + 1] - 1 || v1[i] == v1[i + 1] - 2) { cout << 1; return 0; } } for (int i = 0; i < v2.size(); i++) { if (v2[i] == 100) break; if (v2[i] == v2[i + 1] || v2[i] == v2[i + 1] - 1 || v2[i] == v2[i + 1] - 2) { cout << 1; return 0; } } cout << 2; return 0; }
### Prompt Generate a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char s[10], s1[10], s2[10]; scanf("%s", s); scanf("%s", s1); scanf("%s", s2); vector<int> v, v1, v2; for (int i = 0; i < 5; i++) { v.push_back(100); v1.push_back(100); v2.push_back(100); } if (s[1] == 'm') v.push_back((char)s[0] - 48); if (s[1] == 's') v1.push_back((char)s[0] - 48); if (s[1] == 'p') v2.push_back((char)s[0] - 48); if (s1[1] == 'm') v.push_back((char)s1[0] - 48); if (s1[1] == 's') v1.push_back((char)s1[0] - 48); if (s1[1] == 'p') v2.push_back((char)s1[0] - 48); if (s2[1] == 'm') v.push_back((char)s2[0] - 48); if (s2[1] == 's') v1.push_back((char)s2[0] - 48); if (s2[1] == 'p') v2.push_back((char)s2[0] - 48); sort(v.begin(), v.end()); sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); int ans = 10000; for (int i = 0; i < v.size(); i++) { if (v[i] == 100) break; if (v[i] == v[i + 1] && v[i + 1] == v[i + 2]) { ans = 0; break; } if (v[i] == v[i + 1] - 1 && v[i] == v[i + 2] - 2) { ans = 0; break; } } for (int i = 0; i < v1.size(); i++) { if (v1[i] == 100) break; if (v1[i] == v1[i + 1] && v1[i + 1] == v1[i + 2]) { ans = 0; break; } if (v1[i] == v1[i + 1] - 1 && v1[i] == v1[i + 2] - 2) { ans = 0; break; } } for (int i = 0; i < v2.size(); i++) { if (v2[i] == 100) break; if (v2[i] == v2[i + 1] && v2[i + 1] == v2[i + 2]) { ans = 0; break; } if (v2[i] == v2[i + 1] - 1 && v2[i] == v2[i + 2] - 2) { ans = 0; break; } } if (!ans) { cout << 0; return 0; } for (int i = 0; i < v.size(); i++) { if (v[i] == 100) break; if (v[i] == v[i + 1] || v[i] == v[i + 1] - 1 || v[i] == v[i + 1] - 2) { cout << 1; return 0; } } for (int i = 0; i < v1.size(); i++) { if (v1[i] == 100) break; if (v1[i] == v1[i + 1] || v1[i] == v1[i + 1] - 1 || v1[i] == v1[i + 1] - 2) { cout << 1; return 0; } } for (int i = 0; i < v2.size(); i++) { if (v2[i] == 100) break; if (v2[i] == v2[i + 1] || v2[i] == v2[i + 1] - 1 || v2[i] == v2[i + 1] - 2) { cout << 1; return 0; } } cout << 2; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 == s2 && s2 == s3) { cout << "0"; return 0; } else if (s1 == s2 || s1 == s3 || s2 == s3) { cout << "1"; return 0; } int a = s1[0] - '0', b = s2[0] - '0', c = s3[0] - '0'; char x = s1[1], y = s2[1], z = s3[1]; if (x == y && x == z) { int k = min(a, min(b, c)); int l = max(a, max(b, c)); if ((a + b + c) * 2 == (min(a, min(b, c)) + max(a, max(b, c))) * 3 && k == l - 2) { cout << "0"; } else if (abs(a - b) == 1) cout << "1"; else if (abs(c - b) == 1) cout << "1"; else if (abs(a - c) == 1) cout << "1"; else if (abs(a - c) == 2 || abs(a - b) == 2 || abs(b - c) == 2) cout << "1"; else cout << "2"; } else if (x == y) { if (abs(a - b) == 1 || abs(a - b) == 2) cout << "1"; else cout << "2"; } else if (x == z) { if (abs(c - a) == 1 || abs(a - c) == 2) cout << "1"; else cout << "2"; } else if (y == z) { if (abs(c - b) == 1 || abs(c - b) == 2) cout << "1"; else cout << "2"; } else { cout << "2"; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 == s2 && s2 == s3) { cout << "0"; return 0; } else if (s1 == s2 || s1 == s3 || s2 == s3) { cout << "1"; return 0; } int a = s1[0] - '0', b = s2[0] - '0', c = s3[0] - '0'; char x = s1[1], y = s2[1], z = s3[1]; if (x == y && x == z) { int k = min(a, min(b, c)); int l = max(a, max(b, c)); if ((a + b + c) * 2 == (min(a, min(b, c)) + max(a, max(b, c))) * 3 && k == l - 2) { cout << "0"; } else if (abs(a - b) == 1) cout << "1"; else if (abs(c - b) == 1) cout << "1"; else if (abs(a - c) == 1) cout << "1"; else if (abs(a - c) == 2 || abs(a - b) == 2 || abs(b - c) == 2) cout << "1"; else cout << "2"; } else if (x == y) { if (abs(a - b) == 1 || abs(a - b) == 2) cout << "1"; else cout << "2"; } else if (x == z) { if (abs(c - a) == 1 || abs(a - c) == 2) cout << "1"; else cout << "2"; } else if (y == z) { if (abs(c - b) == 1 || abs(c - b) == 2) cout << "1"; else cout << "2"; } else { cout << "2"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int s = 0, p = 0, m = 0, i, j, k, l; string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 > s2) swap(s1, s2); if (s2 > s3) swap(s2, s3); if (s1 > s2) swap(s1, s2); if (s1 == s2 && s2 == s3) { cout << "0"; return 0; } if (s1[0] + 1 == s2[0] && s2[0] + 1 == s3[0] && (s1[1] == s2[1] && s2[1] == s3[1])) { cout << "0"; return 0; } if (s1 == s2 || s2 == s3) { cout << "1"; return 0; } if (s1[1] == s2[1] && (s1[0] + 1 == s2[0] || s1[0] + 2 == s2[0])) { cout << "1"; return 0; } if (s2[1] == s3[1] && (s2[0] + 1 == s3[0] || s2[0] + 2 == s3[0])) { cout << "1"; return 0; } if (s1[1] == s3[1] && (s1[0] + 1 == s3[0] || s1[0] + 2 == s3[0])) { cout << "1"; return 0; } cout << "2"; return 0; }
### Prompt Generate a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int s = 0, p = 0, m = 0, i, j, k, l; string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 > s2) swap(s1, s2); if (s2 > s3) swap(s2, s3); if (s1 > s2) swap(s1, s2); if (s1 == s2 && s2 == s3) { cout << "0"; return 0; } if (s1[0] + 1 == s2[0] && s2[0] + 1 == s3[0] && (s1[1] == s2[1] && s2[1] == s3[1])) { cout << "0"; return 0; } if (s1 == s2 || s2 == s3) { cout << "1"; return 0; } if (s1[1] == s2[1] && (s1[0] + 1 == s2[0] || s1[0] + 2 == s2[0])) { cout << "1"; return 0; } if (s2[1] == s3[1] && (s2[0] + 1 == s3[0] || s2[0] + 2 == s3[0])) { cout << "1"; return 0; } if (s1[1] == s3[1] && (s1[0] + 1 == s3[0] || s1[0] + 2 == s3[0])) { cout << "1"; return 0; } cout << "2"; return 0; } ```
#include <bits/stdc++.h> using namespace std; void ECHO(string _s) { cout << endl; (void)_s; } template <typename T, typename... Args> void ECHO(string _s, T x, Args... args) { unsigned _i; string _s2 = ""; for (_i = 0; _i < _s.length(); ++_i) { if (_s[_i] == ',') break; if (_s[_i] != ' ') _s2 += _s[_i]; } if (_i == _s.length()) { --_i; } cout << " (" << _s2 << "):" << x; ECHO(_s.substr(_i + 1, _s.length() - _i - 1), args...); } template <typename T0, typename T1> inline ostream& operator<<(ostream& os, pair<T0, T1>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& v) { for (unsigned i = 0; i < v.size(); ++i) { cout << v[i] << "_"; } cout << endl; return os; } template <typename T> inline T _min(T x1, T x2, T x3) { return min(x1, min(x2, x3)); } template <typename T> inline T _min(T x1, T x2, T x3, T x4) { return min(min(x1, x2), min(x2, x3)); } inline int _gcd(int a, int b) { while (b) b %= a ^= b ^= a ^= b; return a; } int main() { ios::sync_with_stdio(false); string s[3]; cin >> s[0] >> s[1] >> s[2]; int aux, aux2; if (s[0] == s[1] && s[1] == s[2]) aux = 0; else if (s[0] == s[1] || s[1] == s[2] || s[2] == s[0]) aux = 1; else aux = 2; sort(s, s + 3); if ((s[1][0] - s[0][0] == 1) && (s[2][0] - s[1][0] == 1) && s[0][1] == s[1][1] && s[2][1] == s[1][1]) aux2 = 0; else if ((s[1][1] == s[0][1] && s[1][0] - s[0][0] <= 2) || (s[1][1] == s[2][1] && s[2][0] - s[1][0] <= 2) || (s[0][1] == s[2][1] && s[2][0] - s[0][0] <= 2)) aux2 = 1; else aux2 = 2; cout << min(aux, aux2); return 0; }
### Prompt In Cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void ECHO(string _s) { cout << endl; (void)_s; } template <typename T, typename... Args> void ECHO(string _s, T x, Args... args) { unsigned _i; string _s2 = ""; for (_i = 0; _i < _s.length(); ++_i) { if (_s[_i] == ',') break; if (_s[_i] != ' ') _s2 += _s[_i]; } if (_i == _s.length()) { --_i; } cout << " (" << _s2 << "):" << x; ECHO(_s.substr(_i + 1, _s.length() - _i - 1), args...); } template <typename T0, typename T1> inline ostream& operator<<(ostream& os, pair<T0, T1>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& v) { for (unsigned i = 0; i < v.size(); ++i) { cout << v[i] << "_"; } cout << endl; return os; } template <typename T> inline T _min(T x1, T x2, T x3) { return min(x1, min(x2, x3)); } template <typename T> inline T _min(T x1, T x2, T x3, T x4) { return min(min(x1, x2), min(x2, x3)); } inline int _gcd(int a, int b) { while (b) b %= a ^= b ^= a ^= b; return a; } int main() { ios::sync_with_stdio(false); string s[3]; cin >> s[0] >> s[1] >> s[2]; int aux, aux2; if (s[0] == s[1] && s[1] == s[2]) aux = 0; else if (s[0] == s[1] || s[1] == s[2] || s[2] == s[0]) aux = 1; else aux = 2; sort(s, s + 3); if ((s[1][0] - s[0][0] == 1) && (s[2][0] - s[1][0] == 1) && s[0][1] == s[1][1] && s[2][1] == s[1][1]) aux2 = 0; else if ((s[1][1] == s[0][1] && s[1][0] - s[0][0] <= 2) || (s[1][1] == s[2][1] && s[2][0] - s[1][0] <= 2) || (s[0][1] == s[2][1] && s[2][0] - s[0][0] <= 2)) aux2 = 1; else aux2 = 2; cout << min(aux, aux2); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v1, v2, v3; string s; string a[3]; for (int i = 0; i < 3; i++) { cin >> s; a[i] = s; if (s[1] == 's') { v1.push_back(int(s[0]) - 48); } if (s[1] == 'p') { v2.push_back(int(s[0]) - 48); } if (s[1] == 'm') { v3.push_back(int(s[0]) - 48); } } int eqcount = 1; for (int i = 0; i < 2; i++) { if (a[i] == a[i + 1]) { eqcount++; } } if (eqcount == 3) { cout << "0"; exit(0); } if ((a[0] == a[1]) || a[1] == a[2] || a[2] == a[0]) { eqcount = 2; cout << "1"; exit(0); } if (eqcount == 1) { int eqcount2 = 0; if (v1.size() > 1) { sort(v1.begin(), v1.end()); for (int i = 0; i < v1.size() - 1; i++) { if (v1[i + 1] - v1[i] == 1) { eqcount2++; } else if (v1[i + 1] - v1[i] == 2) { cout << "1"; exit(0); } } cout << 2 - eqcount2; exit(0); } if (v2.size() > 1) { sort(v2.begin(), v2.end()); for (int i = 0; i < v2.size() - 1; i++) { if (v2[i + 1] - v2[i] == 1) { eqcount2++; } else if (v2[i + 1] - v2[i] == 2) { cout << "1"; exit(0); } } cout << 2 - eqcount2; exit(0); } if (v3.size() > 1) { sort(v3.begin(), v3.end()); for (int i = 0; i < v3.size() - 1; i++) { if (v3[i + 1] - v3[i] == 1) { eqcount2++; } else if (v3[i + 1] - v3[i] == 2) { cout << "1"; exit(0); } } cout << 2 - eqcount2; exit(0); } } cout << "2"; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1, v2, v3; string s; string a[3]; for (int i = 0; i < 3; i++) { cin >> s; a[i] = s; if (s[1] == 's') { v1.push_back(int(s[0]) - 48); } if (s[1] == 'p') { v2.push_back(int(s[0]) - 48); } if (s[1] == 'm') { v3.push_back(int(s[0]) - 48); } } int eqcount = 1; for (int i = 0; i < 2; i++) { if (a[i] == a[i + 1]) { eqcount++; } } if (eqcount == 3) { cout << "0"; exit(0); } if ((a[0] == a[1]) || a[1] == a[2] || a[2] == a[0]) { eqcount = 2; cout << "1"; exit(0); } if (eqcount == 1) { int eqcount2 = 0; if (v1.size() > 1) { sort(v1.begin(), v1.end()); for (int i = 0; i < v1.size() - 1; i++) { if (v1[i + 1] - v1[i] == 1) { eqcount2++; } else if (v1[i + 1] - v1[i] == 2) { cout << "1"; exit(0); } } cout << 2 - eqcount2; exit(0); } if (v2.size() > 1) { sort(v2.begin(), v2.end()); for (int i = 0; i < v2.size() - 1; i++) { if (v2[i + 1] - v2[i] == 1) { eqcount2++; } else if (v2[i + 1] - v2[i] == 2) { cout << "1"; exit(0); } } cout << 2 - eqcount2; exit(0); } if (v3.size() > 1) { sort(v3.begin(), v3.end()); for (int i = 0; i < v3.size() - 1; i++) { if (v3[i + 1] - v3[i] == 1) { eqcount2++; } else if (v3[i + 1] - v3[i] == 2) { cout << "1"; exit(0); } } cout << 2 - eqcount2; exit(0); } } cout << "2"; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> v[5]; int a, b, c, flag[5], flag1[5], flag2[5], mx; char x, y, z; int main() { for (int i = 0; i < 3; i++) { scanf("%d%c%c", &a, &x, &y); if (x == 'm') { v[0].push_back(a); } if (x == 'p') { v[1].push_back(a); } if (x == 's') { v[2].push_back(a); } } sort(v[0].begin(), v[0].end()); sort(v[1].begin(), v[1].end()); sort(v[2].begin(), v[2].end()); for (int i = 0; i < 3; i++) { flag[i] = 1; flag1[i] = 1; flag2[i] = 1; for (int j = 1; j < v[i].size(); j++) { if ((v[i][j - 1] + 1) == v[i][j]) { flag1[i]++; } if ((v[i][j - 1]) == v[i][j]) { flag2[i]++; } if ((v[i][j - 1] + 2) == v[i][j]) { flag[i] = 2; } } } int f = 1, f1 = 1, f2 = 1; for (int i = 0; i < 3; i++) { f = max(f, flag[i]); f1 = max(f1, flag1[i]); f2 = max(f2, flag2[i]); } mx = max(f1, f2); mx = max(mx, f); if (mx > 3) mx = 3; printf("%d\n", 3 - mx); }
### Prompt Generate a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v[5]; int a, b, c, flag[5], flag1[5], flag2[5], mx; char x, y, z; int main() { for (int i = 0; i < 3; i++) { scanf("%d%c%c", &a, &x, &y); if (x == 'm') { v[0].push_back(a); } if (x == 'p') { v[1].push_back(a); } if (x == 's') { v[2].push_back(a); } } sort(v[0].begin(), v[0].end()); sort(v[1].begin(), v[1].end()); sort(v[2].begin(), v[2].end()); for (int i = 0; i < 3; i++) { flag[i] = 1; flag1[i] = 1; flag2[i] = 1; for (int j = 1; j < v[i].size(); j++) { if ((v[i][j - 1] + 1) == v[i][j]) { flag1[i]++; } if ((v[i][j - 1]) == v[i][j]) { flag2[i]++; } if ((v[i][j - 1] + 2) == v[i][j]) { flag[i] = 2; } } } int f = 1, f1 = 1, f2 = 1; for (int i = 0; i < 3; i++) { f = max(f, flag[i]); f1 = max(f1, flag1[i]); f2 = max(f2, flag2[i]); } mx = max(f1, f2); mx = max(mx, f); if (mx > 3) mx = 3; printf("%d\n", 3 - mx); } ```
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) { uniform_int_distribution<int64_t> generator(l, r); return generator(rng); } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long long inf = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } const int N = 2e6 + 1; void solve() { string s, t, v; cin >> s >> t >> v; vector<int> ve; ve.push_back(s[0] - '0'); ve.push_back(t[0] - '0'); ve.push_back(v[0] - '0'); ; sort((ve).begin(), (ve).end()); if ((s == t && t == v) || (s[1] == t[1] && t[1] == v[1] && ve[1] == ve[0] + 1 && ve[2] == ve[1] + 1)) { cout << 0 << "\n"; return; } if ((s == t) || (t == v) || (v == s)) { cout << 1 << "\n"; return; } if (s[1] == t[1] && (abs(s[0] - t[0]) == 1 || abs(s[0] - t[0]) == 2)) { cout << 1 << "\n"; return; } if (t[1] == v[1] && (abs(v[0] - t[0]) == 1 || abs(v[0] - t[0]) == 2)) { cout << 1 << "\n"; return; } if (s[1] == v[1] && (abs(s[0] - v[0]) == 1 || abs(s[0] - v[0]) == 2)) { cout << 1 << "\n"; return; } cout << 2 << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) { uniform_int_distribution<int64_t> generator(l, r); return generator(rng); } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long long inf = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } const int N = 2e6 + 1; void solve() { string s, t, v; cin >> s >> t >> v; vector<int> ve; ve.push_back(s[0] - '0'); ve.push_back(t[0] - '0'); ve.push_back(v[0] - '0'); ; sort((ve).begin(), (ve).end()); if ((s == t && t == v) || (s[1] == t[1] && t[1] == v[1] && ve[1] == ve[0] + 1 && ve[2] == ve[1] + 1)) { cout << 0 << "\n"; return; } if ((s == t) || (t == v) || (v == s)) { cout << 1 << "\n"; return; } if (s[1] == t[1] && (abs(s[0] - t[0]) == 1 || abs(s[0] - t[0]) == 2)) { cout << 1 << "\n"; return; } if (t[1] == v[1] && (abs(v[0] - t[0]) == 1 || abs(v[0] - t[0]) == 2)) { cout << 1 << "\n"; return; } if (s[1] == v[1] && (abs(s[0] - v[0]) == 1 || abs(s[0] - v[0]) == 2)) { cout << 1 << "\n"; return; } cout << 2 << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string str; int m, s, p; m = s = p = 0; vector<pair<char, int>> hai; for (int i = 0; i < 3; i++) { cin >> str; hai.push_back(make_pair(str[1], str[0] - '0')); if (str[1] == 'm') m++; else if (str[1] == 's') s++; else p++; } sort(hai.begin(), hai.end()); if (hai[0] == hai[1] && hai[1] == hai[2]) cout << 0 << endl; else if (hai[0] == hai[1] || hai[1] == hai[2] || hai[2] == hai[0]) cout << 1 << endl; else { if (m == 3 || p == 3 || s == 3) { if (hai[2].second - hai[0].second == 2) cout << 0 << endl; else if (hai[1].second - hai[0].second <= 2 || hai[2].second - hai[1].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (m == 2) { if (hai[1].second - hai[0].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (m == 1 && p == 2) { if (hai[2].second - hai[1].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (p == 2 && s == 1) { if (hai[1].second - hai[0].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (s == 2) { if (hai[2].second - hai[1].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else { cout << 2 << endl; } } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string str; int m, s, p; m = s = p = 0; vector<pair<char, int>> hai; for (int i = 0; i < 3; i++) { cin >> str; hai.push_back(make_pair(str[1], str[0] - '0')); if (str[1] == 'm') m++; else if (str[1] == 's') s++; else p++; } sort(hai.begin(), hai.end()); if (hai[0] == hai[1] && hai[1] == hai[2]) cout << 0 << endl; else if (hai[0] == hai[1] || hai[1] == hai[2] || hai[2] == hai[0]) cout << 1 << endl; else { if (m == 3 || p == 3 || s == 3) { if (hai[2].second - hai[0].second == 2) cout << 0 << endl; else if (hai[1].second - hai[0].second <= 2 || hai[2].second - hai[1].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (m == 2) { if (hai[1].second - hai[0].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (m == 1 && p == 2) { if (hai[2].second - hai[1].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (p == 2 && s == 1) { if (hai[1].second - hai[0].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (s == 2) { if (hai[2].second - hai[1].second <= 2) cout << 1 << endl; else cout << 2 << endl; } else { cout << 2 << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int t, id, n, m, x, y, k, c, p, dif, ans, pre, rem, cur, tmp, tot, r, l, u, d, xx, yy; vector<int> v, adj[N]; bool fl, ok; long long res, sum; char ch; string s, z; map<string, int> ide; map<char, set<int>> sui; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ; for (int i = 0; i < (int)3; ++i) { cin >> s; ++ide[s]; sui[s[1]].insert(s[0] - '0'); } x = y = 0x3f3f3f3f; for (auto it : ide) { x = min(x, 3 - it.second); } for (auto it : sui) { set<int> v = it.second; if (v.size() == 1) y = min(y, 2); else if (v.size() == 2) { if (abs(*v.begin() - *v.rbegin()) <= 2) y = min(y, 1); } else if (v.size() == 3) { int arr[3]; auto it = v.begin(); for (int i = 0; i < (int)3; ++i) { arr[i] = *it; ++it; } if (abs(arr[0] - arr[2]) == 2) y = min(y, 0); if (abs(arr[0] - arr[1]) <= 2 || abs(arr[1] - arr[2]) <= 2) y = min(y, 1); } } printf("%d\n", min(x, y)); return 0; }
### Prompt Generate a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int t, id, n, m, x, y, k, c, p, dif, ans, pre, rem, cur, tmp, tot, r, l, u, d, xx, yy; vector<int> v, adj[N]; bool fl, ok; long long res, sum; char ch; string s, z; map<string, int> ide; map<char, set<int>> sui; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ; for (int i = 0; i < (int)3; ++i) { cin >> s; ++ide[s]; sui[s[1]].insert(s[0] - '0'); } x = y = 0x3f3f3f3f; for (auto it : ide) { x = min(x, 3 - it.second); } for (auto it : sui) { set<int> v = it.second; if (v.size() == 1) y = min(y, 2); else if (v.size() == 2) { if (abs(*v.begin() - *v.rbegin()) <= 2) y = min(y, 1); } else if (v.size() == 3) { int arr[3]; auto it = v.begin(); for (int i = 0; i < (int)3; ++i) { arr[i] = *it; ++it; } if (abs(arr[0] - arr[2]) == 2) y = min(y, 0); if (abs(arr[0] - arr[1]) <= 2 || abs(arr[1] - arr[2]) <= 2) y = min(y, 1); } } printf("%d\n", min(x, y)); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); map<char, vector<int>> check; check['m'] = vector<int>(10); check['p'] = vector<int>(10); check['s'] = vector<int>(10); for (int i = 0; i < 3; i++) { string cur; cin >> cur; check[cur[1]][cur[0] - '0']++; } vector<char> chars = {'m', 'p', 's'}; for (int i = 0; i < 3; i++) { char c = chars[i]; for (int j = 1; j < 10; j++) { bool flag = false; if (check[c][j] > 2) flag = true; if (j <= 7 && check[c][j] > 0 && check[c][j + 1] > 0 && check[c][j + 2] > 0) flag = true; if (flag) { cout << 0 << endl; return 0; } } } for (int i = 0; i < 3; i++) { char c = chars[i]; for (int j = 1; j < 10; j++) { bool flag = false; if (check[c][j] > 1) flag = true; if (j <= 8 && check[c][j] > 0 && check[c][j + 1] > 0) flag = true; if (j <= 7 && check[c][j] > 0 && check[c][j + 2] > 0) flag = true; if (flag) { cout << 1 << endl; return 0; } } } cout << 2 << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); map<char, vector<int>> check; check['m'] = vector<int>(10); check['p'] = vector<int>(10); check['s'] = vector<int>(10); for (int i = 0; i < 3; i++) { string cur; cin >> cur; check[cur[1]][cur[0] - '0']++; } vector<char> chars = {'m', 'p', 's'}; for (int i = 0; i < 3; i++) { char c = chars[i]; for (int j = 1; j < 10; j++) { bool flag = false; if (check[c][j] > 2) flag = true; if (j <= 7 && check[c][j] > 0 && check[c][j + 1] > 0 && check[c][j + 2] > 0) flag = true; if (flag) { cout << 0 << endl; return 0; } } } for (int i = 0; i < 3; i++) { char c = chars[i]; for (int j = 1; j < 10; j++) { bool flag = false; if (check[c][j] > 1) flag = true; if (j <= 8 && check[c][j] > 0 && check[c][j + 1] > 0) flag = true; if (j <= 7 && check[c][j] > 0 && check[c][j + 2] > 0) flag = true; if (flag) { cout << 1 << endl; return 0; } } } cout << 2 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; char x, y, z; cin >> a >> x >> b >> y >> c >> z; if (x != y && x != z && y != z) { cout << 2 << endl; } else if (x == y && y != z) { if (abs(a - b) == 1 || abs(a - b) == 2) cout << 1 << endl; else if (a == b) cout << 1 << endl; else cout << 2 << endl; } else if (x == z && z != y) { if (abs(a - c) == 1 || abs(a - c) == 2) cout << 1 << endl; else if (a == c) cout << 1 << endl; else cout << 2 << endl; } else if (z == y && x != z) { if (abs(c - b) == 1 || abs(c - b) == 2) cout << 1 << endl; else if (c == b) cout << 1 << endl; else cout << 2 << endl; } else { int temp; if (a > b) { temp = a; a = b; b = temp; } if (a > c) { temp = a; a = c; c = temp; } if (b > c) { temp = b; b = c; c = temp; } if ((a + c == 2 * b && b - a == 1) || (a == b && b == c)) cout << "0" << endl; else if (abs(b - a) == 2 || abs(b - c) == 2 || abs(b - a) == 1 || abs(b - c) == 1) cout << 1 << endl; else if (a == b || b == c) cout << 1 << endl; else cout << 2 << endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; char x, y, z; cin >> a >> x >> b >> y >> c >> z; if (x != y && x != z && y != z) { cout << 2 << endl; } else if (x == y && y != z) { if (abs(a - b) == 1 || abs(a - b) == 2) cout << 1 << endl; else if (a == b) cout << 1 << endl; else cout << 2 << endl; } else if (x == z && z != y) { if (abs(a - c) == 1 || abs(a - c) == 2) cout << 1 << endl; else if (a == c) cout << 1 << endl; else cout << 2 << endl; } else if (z == y && x != z) { if (abs(c - b) == 1 || abs(c - b) == 2) cout << 1 << endl; else if (c == b) cout << 1 << endl; else cout << 2 << endl; } else { int temp; if (a > b) { temp = a; a = b; b = temp; } if (a > c) { temp = a; a = c; c = temp; } if (b > c) { temp = b; b = c; c = temp; } if ((a + c == 2 * b && b - a == 1) || (a == b && b == c)) cout << "0" << endl; else if (abs(b - a) == 2 || abs(b - c) == 2 || abs(b - a) == 1 || abs(b - c) == 1) cout << 1 << endl; else if (a == b || b == c) cout << 1 << endl; else cout << 2 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main(void) { vector<string> s(3); cin >> s[0] >> s[1] >> s[2]; reverse(s[0].begin(), s[0].end()); reverse(s[1].begin(), s[1].end()); reverse(s[2].begin(), s[2].end()); sort(s.begin(), s.end()); if (s[0] == s[1] && s[1] == s[2]) { cout << 0 << endl; } else if (s[0] == s[1] || s[1] == s[2]) { cout << 1 << endl; } else if (s[0][0] == s[1][0] && s[2][0] == s[1][0] && (int)(s[2][1]) - (int)(s[0][1]) == 2) { cout << 0 << endl; } else if (s[0][0] == s[1][0] && (int)(s[1][1]) - (int)(s[0][1]) <= 2) { cout << 1 << endl; } else if (s[1][0] == s[2][0] && (int)(s[2][1]) - (int)(s[1][1]) <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { vector<string> s(3); cin >> s[0] >> s[1] >> s[2]; reverse(s[0].begin(), s[0].end()); reverse(s[1].begin(), s[1].end()); reverse(s[2].begin(), s[2].end()); sort(s.begin(), s.end()); if (s[0] == s[1] && s[1] == s[2]) { cout << 0 << endl; } else if (s[0] == s[1] || s[1] == s[2]) { cout << 1 << endl; } else if (s[0][0] == s[1][0] && s[2][0] == s[1][0] && (int)(s[2][1]) - (int)(s[0][1]) == 2) { cout << 0 << endl; } else if (s[0][0] == s[1][0] && (int)(s[1][1]) - (int)(s[0][1]) <= 2) { cout << 1 << endl; } else if (s[1][0] == s[2][0] && (int)(s[2][1]) - (int)(s[1][1]) <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long num, m, tc, t = 1; int arr[3]; for (long long i = 0; i < 3; i++) { char ch; cin >> num >> ch; if (ch == 'm') arr[i] = num + 100; else if (ch == 'p') arr[i] = num + 200; else arr[i] = num + 300; } sort(arr, arr + 3); if (arr[0] == arr[1] && arr[1] == arr[2]) { cout << "0" << endl; } else if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) { cout << "0" << endl; } else if (arr[0] == arr[1] || arr[1] == arr[2] || arr[0] + 1 == arr[1] || arr[1] + 1 == arr[2] || arr[0] + 2 == arr[1] || arr[1] + 2 == arr[2]) { cout << "1" << endl; } else cout << "2" << endl; }
### Prompt In Cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long num, m, tc, t = 1; int arr[3]; for (long long i = 0; i < 3; i++) { char ch; cin >> num >> ch; if (ch == 'm') arr[i] = num + 100; else if (ch == 'p') arr[i] = num + 200; else arr[i] = num + 300; } sort(arr, arr + 3); if (arr[0] == arr[1] && arr[1] == arr[2]) { cout << "0" << endl; } else if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) { cout << "0" << endl; } else if (arr[0] == arr[1] || arr[1] == arr[2] || arr[0] + 1 == arr[1] || arr[1] + 1 == arr[2] || arr[0] + 2 == arr[1] || arr[1] + 2 == arr[2]) { cout << "1" << endl; } else cout << "2" << endl; } ```
#include <bits/stdc++.h> using namespace std; struct data { int v; char a; }; bool compare(data c, data d) { if (c.a > d.a) return true; else if (c.a == d.a) { if (c.v < d.v) return true; else return false; } return false; } int main() { int i, j, k, l; data arr[3]; cin >> arr[0].v; cin >> arr[0].a; cin >> arr[1].v; cin >> arr[1].a; cin >> arr[2].v; cin >> arr[2].a; sort(arr, arr + 3, compare); if (arr[0].a == arr[1].a && arr[1].a == arr[2].a && arr[0].v == arr[1].v && arr[1].v == arr[2].v) cout << 0 << endl; else if (arr[0].a == arr[1].a && arr[1].a == arr[2].a && arr[0].v == arr[1].v - 1 && arr[1].v == arr[2].v - 1) cout << 0 << endl; else if (arr[0].a == arr[1].a && arr[0].v == arr[1].v || arr[0].a == arr[2].a && arr[0].v == arr[2].v || arr[2].a == arr[1].a && arr[2].v == arr[1].v) cout << 1; else if ((arr[0].a == arr[1].a && (abs(arr[0].v - arr[1].v) <= 2)) || (arr[0].a == arr[2].a && (abs(arr[0].v - arr[2].v) <= 2)) || (arr[2].a == arr[1].a && (abs(arr[2].v - arr[1].v) <= 2))) cout << 1; else cout << 2; }
### Prompt Generate a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct data { int v; char a; }; bool compare(data c, data d) { if (c.a > d.a) return true; else if (c.a == d.a) { if (c.v < d.v) return true; else return false; } return false; } int main() { int i, j, k, l; data arr[3]; cin >> arr[0].v; cin >> arr[0].a; cin >> arr[1].v; cin >> arr[1].a; cin >> arr[2].v; cin >> arr[2].a; sort(arr, arr + 3, compare); if (arr[0].a == arr[1].a && arr[1].a == arr[2].a && arr[0].v == arr[1].v && arr[1].v == arr[2].v) cout << 0 << endl; else if (arr[0].a == arr[1].a && arr[1].a == arr[2].a && arr[0].v == arr[1].v - 1 && arr[1].v == arr[2].v - 1) cout << 0 << endl; else if (arr[0].a == arr[1].a && arr[0].v == arr[1].v || arr[0].a == arr[2].a && arr[0].v == arr[2].v || arr[2].a == arr[1].a && arr[2].v == arr[1].v) cout << 1; else if ((arr[0].a == arr[1].a && (abs(arr[0].v - arr[1].v) <= 2)) || (arr[0].a == arr[2].a && (abs(arr[0].v - arr[2].v) <= 2)) || (arr[2].a == arr[1].a && (abs(arr[2].v - arr[1].v) <= 2))) cout << 1; else cout << 2; } ```
#include <bits/stdc++.h> using namespace std; char s[3][10]; int cnt[1000]; int a[10]; set<int> ss; struct node { int ch, val; node(int ch = 0, int val = 0) { this->ch = ch; this->val = val; } friend bool operator<(const node& mm, const node& gg) { if (mm.ch != gg.ch) return mm.ch < gg.ch; else return mm.val < gg.val; } } mmp[10]; int main() { cin >> s[0] >> s[1] >> s[2]; if (s[0][1] == s[1][1] && s[0][1] == s[2][1]) { a[0] = s[0][0] - '0'; a[1] = s[1][0] - '0'; a[2] = s[2][0] - '0'; sort(a, a + 3); if (a[0] == a[2] || (a[0] + 1 == a[1] && a[1] + 1 == a[2])) { cout << 0 << endl; return 0; } if (a[0] == a[1] || a[1] == a[2] || a[1] - a[0] == 1 || a[2] - a[1] == 1 || a[2] - a[1] == 2 || a[1] - a[0] == 2) { cout << 1 << endl; return 0; } cout << 2 << endl; return 0; } ss.insert(s[0][1]); ss.insert(s[1][1]); ss.insert(s[2][1]); if (ss.size() == 3) cout << 2 << endl; else { mmp[0] = node(s[0][1], s[0][0] - '0'); mmp[1] = node(s[1][1], s[1][0] - '0'); mmp[2] = node(s[2][1], s[2][0] - '0'); sort(mmp, mmp + 3); if (mmp[0].ch == mmp[1].ch) { if (mmp[0].val == mmp[1].val || mmp[1].val - mmp[0].val == 1 || mmp[1].val - mmp[0].val == 2) cout << 1 << endl; else cout << 2 << endl; } else { if (mmp[1].val == mmp[2].val || mmp[2].val - mmp[1].val == 1 || mmp[2].val - mmp[1].val == 2) cout << 1 << endl; else cout << 2 << endl; } } return 0; }
### Prompt Create a solution in CPP for the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[3][10]; int cnt[1000]; int a[10]; set<int> ss; struct node { int ch, val; node(int ch = 0, int val = 0) { this->ch = ch; this->val = val; } friend bool operator<(const node& mm, const node& gg) { if (mm.ch != gg.ch) return mm.ch < gg.ch; else return mm.val < gg.val; } } mmp[10]; int main() { cin >> s[0] >> s[1] >> s[2]; if (s[0][1] == s[1][1] && s[0][1] == s[2][1]) { a[0] = s[0][0] - '0'; a[1] = s[1][0] - '0'; a[2] = s[2][0] - '0'; sort(a, a + 3); if (a[0] == a[2] || (a[0] + 1 == a[1] && a[1] + 1 == a[2])) { cout << 0 << endl; return 0; } if (a[0] == a[1] || a[1] == a[2] || a[1] - a[0] == 1 || a[2] - a[1] == 1 || a[2] - a[1] == 2 || a[1] - a[0] == 2) { cout << 1 << endl; return 0; } cout << 2 << endl; return 0; } ss.insert(s[0][1]); ss.insert(s[1][1]); ss.insert(s[2][1]); if (ss.size() == 3) cout << 2 << endl; else { mmp[0] = node(s[0][1], s[0][0] - '0'); mmp[1] = node(s[1][1], s[1][0] - '0'); mmp[2] = node(s[2][1], s[2][0] - '0'); sort(mmp, mmp + 3); if (mmp[0].ch == mmp[1].ch) { if (mmp[0].val == mmp[1].val || mmp[1].val - mmp[0].val == 1 || mmp[1].val - mmp[0].val == 2) cout << 1 << endl; else cout << 2 << endl; } else { if (mmp[1].val == mmp[2].val || mmp[2].val - mmp[1].val == 1 || mmp[2].val - mmp[1].val == 2) cout << 1 << endl; else cout << 2 << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int minimum(int x, int y, int z) { return min(x, min(y, z)); } int main() { unordered_map<char, vector<int> > m; unordered_map<char, vector<int> >::iterator itr; string a, b, c; cin >> a >> b >> c; itr = m.find(a[1]); if (itr == m.end()) { vector<int> v; v.push_back(a[0] - '0'); m.insert({a[1], v}); } else itr->second.push_back(a[0] - '0'); itr = m.find(b[1]); if (itr == m.end()) { vector<int> f; f.push_back(b[0] - '0'); m.insert({b[1], f}); } else itr->second.push_back(b[0] - '0'); itr = m.find(c[1]); if (itr == m.end()) { vector<int> g; g.push_back(c[0] - '0'); m.insert({c[1], g}); } else itr->second.push_back(c[0] - '0'); itr = m.find('s'); int x, y, z; if (itr == m.end()) x = 3; else if (itr->second.size() == 1) x = 2; else if (itr->second.size() == 2) { if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[0] - itr->second[1]) == 2) x = 1; else x = 2; } else { sort(itr->second.begin(), itr->second.end()); if (abs(itr->second[0] - itr->second[1]) == 1 && abs(itr->second[1] - itr->second[2]) == 1 || (itr->second[0] == itr->second[1] && itr->second[1] == itr->second[2])) x = 0; else if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[1] - itr->second[2]) == 1 || abs(itr->second[1] - itr->second[2]) == 2 || abs(itr->second[0] - itr->second[1]) == 2 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[1] - itr->second[2]) == 0) x = 1; else x = 2; } itr = m.find('m'); if (itr == m.end()) y = 3; else if (itr->second.size() == 1) y = 2; else if (itr->second.size() == 2) { if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[0] - itr->second[1]) == 2) y = 1; else y = 2; } else { sort(itr->second.begin(), itr->second.end()); if (abs(itr->second[0] - itr->second[1]) == 1 && abs(itr->second[1] - itr->second[2]) == 1 || (itr->second[0] == itr->second[1] && itr->second[1] == itr->second[2])) y = 0; else if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[1] - itr->second[2]) == 1 || abs(itr->second[1] - itr->second[2]) == 2 || abs(itr->second[0] - itr->second[1]) == 2 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[1] - itr->second[2]) == 0) y = 1; else y = 2; } itr = m.find('p'); if (itr == m.end()) z = 3; else if (itr->second.size() == 1) z = 2; else if (itr->second.size() == 2) { if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[0] - itr->second[1]) == 2) z = 1; else z = 2; } else { sort(itr->second.begin(), itr->second.end()); if (abs(itr->second[0] - itr->second[1]) == 1 && abs(itr->second[1] - itr->second[2]) == 1 || (itr->second[0] == itr->second[1] && itr->second[1] == itr->second[2])) z = 0; else if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[1] - itr->second[2]) == 1 || abs(itr->second[1] - itr->second[2]) == 2 || abs(itr->second[0] - itr->second[1]) == 2 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[1] - itr->second[2]) == 0) z = 1; else z = 2; } cout << minimum(x, y, z); }
### Prompt Please formulate a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int minimum(int x, int y, int z) { return min(x, min(y, z)); } int main() { unordered_map<char, vector<int> > m; unordered_map<char, vector<int> >::iterator itr; string a, b, c; cin >> a >> b >> c; itr = m.find(a[1]); if (itr == m.end()) { vector<int> v; v.push_back(a[0] - '0'); m.insert({a[1], v}); } else itr->second.push_back(a[0] - '0'); itr = m.find(b[1]); if (itr == m.end()) { vector<int> f; f.push_back(b[0] - '0'); m.insert({b[1], f}); } else itr->second.push_back(b[0] - '0'); itr = m.find(c[1]); if (itr == m.end()) { vector<int> g; g.push_back(c[0] - '0'); m.insert({c[1], g}); } else itr->second.push_back(c[0] - '0'); itr = m.find('s'); int x, y, z; if (itr == m.end()) x = 3; else if (itr->second.size() == 1) x = 2; else if (itr->second.size() == 2) { if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[0] - itr->second[1]) == 2) x = 1; else x = 2; } else { sort(itr->second.begin(), itr->second.end()); if (abs(itr->second[0] - itr->second[1]) == 1 && abs(itr->second[1] - itr->second[2]) == 1 || (itr->second[0] == itr->second[1] && itr->second[1] == itr->second[2])) x = 0; else if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[1] - itr->second[2]) == 1 || abs(itr->second[1] - itr->second[2]) == 2 || abs(itr->second[0] - itr->second[1]) == 2 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[1] - itr->second[2]) == 0) x = 1; else x = 2; } itr = m.find('m'); if (itr == m.end()) y = 3; else if (itr->second.size() == 1) y = 2; else if (itr->second.size() == 2) { if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[0] - itr->second[1]) == 2) y = 1; else y = 2; } else { sort(itr->second.begin(), itr->second.end()); if (abs(itr->second[0] - itr->second[1]) == 1 && abs(itr->second[1] - itr->second[2]) == 1 || (itr->second[0] == itr->second[1] && itr->second[1] == itr->second[2])) y = 0; else if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[1] - itr->second[2]) == 1 || abs(itr->second[1] - itr->second[2]) == 2 || abs(itr->second[0] - itr->second[1]) == 2 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[1] - itr->second[2]) == 0) y = 1; else y = 2; } itr = m.find('p'); if (itr == m.end()) z = 3; else if (itr->second.size() == 1) z = 2; else if (itr->second.size() == 2) { if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[0] - itr->second[1]) == 2) z = 1; else z = 2; } else { sort(itr->second.begin(), itr->second.end()); if (abs(itr->second[0] - itr->second[1]) == 1 && abs(itr->second[1] - itr->second[2]) == 1 || (itr->second[0] == itr->second[1] && itr->second[1] == itr->second[2])) z = 0; else if (abs(itr->second[0] - itr->second[1]) == 1 || abs(itr->second[1] - itr->second[2]) == 1 || abs(itr->second[1] - itr->second[2]) == 2 || abs(itr->second[0] - itr->second[1]) == 2 || abs(itr->second[0] - itr->second[1]) == 0 || abs(itr->second[1] - itr->second[2]) == 0) z = 1; else z = 2; } cout << minimum(x, y, z); } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); vector<string> ar(3); for (int i = 0; i < 3; ++i) cin >> ar[i]; sort(ar.begin(), ar.end()); char a = ar[0][0]; char b = ar[1][0]; char c = ar[2][0]; if (a - '0' + 1 == b - '0' && b - '0' + 1 == c - '0' && ar[0][1] == ar[1][1] && ar[1][1] == ar[2][1]) { cout << 0; } else if (ar[0] == ar[1] && ar[1] == ar[2]) { cout << 0; } else if (ar[0] == ar[1] || ar[1] == ar[2] || ar[0] == ar[2]) { cout << 1; } else if (b - a <= 2 && ar[1][1] == ar[0][1] || c - b <= 2 && ar[2][1] == ar[1][1] || c - a <= 2 && ar[2][1] == ar[0][1]) { cout << 1; } else { cout << 2; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); vector<string> ar(3); for (int i = 0; i < 3; ++i) cin >> ar[i]; sort(ar.begin(), ar.end()); char a = ar[0][0]; char b = ar[1][0]; char c = ar[2][0]; if (a - '0' + 1 == b - '0' && b - '0' + 1 == c - '0' && ar[0][1] == ar[1][1] && ar[1][1] == ar[2][1]) { cout << 0; } else if (ar[0] == ar[1] && ar[1] == ar[2]) { cout << 0; } else if (ar[0] == ar[1] || ar[1] == ar[2] || ar[0] == ar[2]) { cout << 1; } else if (b - a <= 2 && ar[1][1] == ar[0][1] || c - b <= 2 && ar[2][1] == ar[1][1] || c - a <= 2 && ar[2][1] == ar[0][1]) { cout << 1; } else { cout << 2; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); map<char, vector<long long>> mp; string s[3]; bool flag; for (long long i = 0; i < 3; i++) { cin >> s[i]; mp[s[i][1]].push_back(s[i][0] - '0'); } if (s[0] == s[1] && s[1] == s[2]) { cout << 0; return 0; } for (auto it : mp) { sort(it.second.begin(), it.second.end()); flag = ((long long)it.second.size() == 3); for (long long i = 1; i < (long long)it.second.size(); i++) { if (it.second[i] - it.second[i - 1] != 1) { flag = 0; break; } } if (flag) { cout << 0; return 0; } } if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) { cout << 1; return 0; } for (auto it : mp) { sort(it.second.begin(), it.second.end()); flag = 0; for (long long i = 1; i < (long long)it.second.size(); i++) { if (it.second[i] - it.second[i - 1] < 3) { flag = 1; break; } } if (flag) { cout << 1; return 0; } } cout << 2; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); map<char, vector<long long>> mp; string s[3]; bool flag; for (long long i = 0; i < 3; i++) { cin >> s[i]; mp[s[i][1]].push_back(s[i][0] - '0'); } if (s[0] == s[1] && s[1] == s[2]) { cout << 0; return 0; } for (auto it : mp) { sort(it.second.begin(), it.second.end()); flag = ((long long)it.second.size() == 3); for (long long i = 1; i < (long long)it.second.size(); i++) { if (it.second[i] - it.second[i - 1] != 1) { flag = 0; break; } } if (flag) { cout << 0; return 0; } } if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) { cout << 1; return 0; } for (auto it : mp) { sort(it.second.begin(), it.second.end()); flag = 0; for (long long i = 1; i < (long long)it.second.size(); i++) { if (it.second[i] - it.second[i - 1] < 3) { flag = 1; break; } } if (flag) { cout << 1; return 0; } } cout << 2; return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[10][4]; int main() { scanf("%2s %2s %2s", s[0], s[1], s[2]); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3 - i - 1; j++) { if (strcmp(s[j], s[j + 1]) == 1) { swap(s[j], s[j + 1]); } } } int cot = 0; if (strcmp(s[0], s[1]) == 0 && strcmp(s[0], s[2]) == 0) { cot = 3; } else if (strcmp(s[0], s[1]) == 0 || strcmp(s[0], s[2]) == 0 || strcmp(s[1], s[2]) == 0) { cot = 2; } else { cot = 1; } if ((s[0][1] == s[1][1] && s[0][1] == s[2][1]) && ((char)s[0][0] + 1 == s[1][0] && (char)s[1][0] + 1 == s[2][0])) { cot = 3; } else if ((s[0][1] == s[1][1] && (char)s[0][0] + 1 == s[1][0]) || (s[0][1] == s[2][1] && s[0][0] + 1 == s[2][0]) || (s[1][1] == s[2][1] && s[1][0] + 1 == s[2][0]) || (s[1][1] == s[2][1] && s[1][0] + 2 == s[2][0]) || (s[0][1] == s[2][1] && s[0][0] + 2 == s[2][0]) || (s[0][1] == s[1][1] && s[0][0] + 2 == s[1][0])) { cot = max(cot, 2); } else { cot = max(cot, 1); } printf("%d\n", 3 - cot); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[10][4]; int main() { scanf("%2s %2s %2s", s[0], s[1], s[2]); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3 - i - 1; j++) { if (strcmp(s[j], s[j + 1]) == 1) { swap(s[j], s[j + 1]); } } } int cot = 0; if (strcmp(s[0], s[1]) == 0 && strcmp(s[0], s[2]) == 0) { cot = 3; } else if (strcmp(s[0], s[1]) == 0 || strcmp(s[0], s[2]) == 0 || strcmp(s[1], s[2]) == 0) { cot = 2; } else { cot = 1; } if ((s[0][1] == s[1][1] && s[0][1] == s[2][1]) && ((char)s[0][0] + 1 == s[1][0] && (char)s[1][0] + 1 == s[2][0])) { cot = 3; } else if ((s[0][1] == s[1][1] && (char)s[0][0] + 1 == s[1][0]) || (s[0][1] == s[2][1] && s[0][0] + 1 == s[2][0]) || (s[1][1] == s[2][1] && s[1][0] + 1 == s[2][0]) || (s[1][1] == s[2][1] && s[1][0] + 2 == s[2][0]) || (s[0][1] == s[2][1] && s[0][0] + 2 == s[2][0]) || (s[0][1] == s[1][1] && s[0][0] + 2 == s[1][0])) { cot = max(cot, 2); } else { cot = max(cot, 1); } printf("%d\n", 3 - cot); return 0; } ```
#include <bits/stdc++.h> using namespace std; string s[3]; int main() { while (cin >> s[0] >> s[1] >> s[2]) { if (s[0] == s[1] && s[1] == s[2]) { puts("0"); } else if (s[0] == s[1] || s[0] == s[2] || s[1] == s[2]) { puts("1"); } else { int cntm = 0, cntp = 0, cnts = 0; for (int i = 0; i < 3; i++) { if (s[i][1] == 'm') cntm++; else if (s[i][1] == 'p') cntp++; else cnts++; } if (cntm == 3 || cntp == 3 || cnts == 3) { sort(s, s + 3); int lx = 0; if (abs(s[0][0] - s[1][0]) == 1) lx++; if (abs(s[1][0] - s[2][0]) == 1) lx++; if (lx) printf("%d\n", 2 - lx); else { if (abs(s[0][0] - s[1][0]) == 2) lx++; if (abs(s[1][0] - s[2][0]) == 2) lx++; puts(lx ? "1" : "2"); } } else if (cntm == 1 && cntp == 1 && cnts == 1) { puts("2"); } else { char c = 'm'; if (cntp == 2) c = 'p'; else if (cnts == 2) c = 's'; vector<string> vs; for (int i = 0; i < 3; i++) { if (s[i][1] == c) vs.push_back(s[i]); } if (abs(vs[0][0] - vs[1][0]) <= 2) { puts("1"); } else { puts("2"); } } } } }
### Prompt In Cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s[3]; int main() { while (cin >> s[0] >> s[1] >> s[2]) { if (s[0] == s[1] && s[1] == s[2]) { puts("0"); } else if (s[0] == s[1] || s[0] == s[2] || s[1] == s[2]) { puts("1"); } else { int cntm = 0, cntp = 0, cnts = 0; for (int i = 0; i < 3; i++) { if (s[i][1] == 'm') cntm++; else if (s[i][1] == 'p') cntp++; else cnts++; } if (cntm == 3 || cntp == 3 || cnts == 3) { sort(s, s + 3); int lx = 0; if (abs(s[0][0] - s[1][0]) == 1) lx++; if (abs(s[1][0] - s[2][0]) == 1) lx++; if (lx) printf("%d\n", 2 - lx); else { if (abs(s[0][0] - s[1][0]) == 2) lx++; if (abs(s[1][0] - s[2][0]) == 2) lx++; puts(lx ? "1" : "2"); } } else if (cntm == 1 && cntp == 1 && cnts == 1) { puts("2"); } else { char c = 'm'; if (cntp == 2) c = 'p'; else if (cnts == 2) c = 's'; vector<string> vs; for (int i = 0; i < 3; i++) { if (s[i][1] == c) vs.push_back(s[i]); } if (abs(vs[0][0] - vs[1][0]) <= 2) { puts("1"); } else { puts("2"); } } } } } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const double EPS = 1e-9; int mods(int a, int b) { return (b + (a % b)) % b; } bool compare(string a, string b) { if (a[1] == b[1]) return a[0] < b[0]; return a[1] < b[1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s[3]; cin >> s[0] >> s[1] >> s[2]; sort(s, s + 3, compare); if (s[0] == s[1] && s[1] == s[2]) cout << "0\n"; else if (s[0][1] == s[1][1] && s[1][1] == s[2][1] && (int)s[1][0] == s[0][0] + 1 && (int)s[2][0] == s[1][0] + 1) cout << "0\n"; else if ((s[0] == s[1] && s[1] != s[2]) || (s[1] == s[2] && s[0] != s[1])) cout << "1\n"; else if ((abs(s[0][0] - s[1][0]) <= 2 && s[1][1] == s[0][1]) || (abs(s[2][0] - s[1][0]) <= 2) && s[2][1] == s[1][1]) cout << "1\n"; else cout << "2\n"; return 0; }
### Prompt Please create a solution in CPP to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const double EPS = 1e-9; int mods(int a, int b) { return (b + (a % b)) % b; } bool compare(string a, string b) { if (a[1] == b[1]) return a[0] < b[0]; return a[1] < b[1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s[3]; cin >> s[0] >> s[1] >> s[2]; sort(s, s + 3, compare); if (s[0] == s[1] && s[1] == s[2]) cout << "0\n"; else if (s[0][1] == s[1][1] && s[1][1] == s[2][1] && (int)s[1][0] == s[0][0] + 1 && (int)s[2][0] == s[1][0] + 1) cout << "0\n"; else if ((s[0] == s[1] && s[1] != s[2]) || (s[1] == s[2] && s[0] != s[1])) cout << "1\n"; else if ((abs(s[0][0] - s[1][0]) <= 2 && s[1][1] == s[0][1]) || (abs(s[2][0] - s[1][0]) <= 2) && s[2][1] == s[1][1]) cout << "1\n"; else cout << "2\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename S37, typename... Args> void err(istream_iterator<string> it, S37 a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } const long long N = 300100, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353, sq = 450, base = 37, lg = 25, inf = 1e18 + 10, del = 79589; long long n, m, X, Y, Z, W, t, k, q, ans, a[N]; string x, y, z; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> x >> y >> z; if (x > y) swap(x, y); if (y > z) swap(y, z); if (y < x) swap(x, y); if (x[1] == y[1] && y[1] == z[1] && y[0] - x[0] == z[0] - y[0] && y[0] - x[0] <= 1) return cout << 0, 0; if ((x[1] == y[1] && abs(y[0] - x[0]) <= 2) || (x[1] == z[1] && abs(z[0] - x[0]) <= 2) || (z[1] == y[1] && abs(y[0] - z[0]) <= 2)) return cout << 1, 0; cout << 2; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename S37, typename... Args> void err(istream_iterator<string> it, S37 a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } const long long N = 300100, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353, sq = 450, base = 37, lg = 25, inf = 1e18 + 10, del = 79589; long long n, m, X, Y, Z, W, t, k, q, ans, a[N]; string x, y, z; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> x >> y >> z; if (x > y) swap(x, y); if (y > z) swap(y, z); if (y < x) swap(x, y); if (x[1] == y[1] && y[1] == z[1] && y[0] - x[0] == z[0] - y[0] && y[0] - x[0] <= 1) return cout << 0, 0; if ((x[1] == y[1] && abs(y[0] - x[0]) <= 2) || (x[1] == z[1] && abs(z[0] - x[0]) <= 2) || (z[1] == y[1] && abs(y[0] - z[0]) <= 2)) return cout << 1, 0; cout << 2; return 0; } ```
#include <bits/stdc++.h> long long power(long long a, long long b, long long c) { long long ans = 1; a = (a % c); while (b > 0) { if (b & 1) { ans = (ans * a) % c; } b = b >> 1; a = (a * a) % c; } return ans; } using namespace std; int main() { vector<string> s; for (int i = 0; i < 3; i++) { string a; cin >> a; s.push_back(a); } vector<int> n; set<int> ns; set<char> nc; vector<char> c; vector<pair<char, int>> p; for (int i = 0; i < 3; i++) { n.push_back(s[i][0] - '0'); c.push_back(s[i][1]); ns.insert(s[i][0] - '0'); nc.insert(s[i][1]); p.push_back({s[i][1], s[i][0] - '0'}); } if (nc.size() == 3) cout << 2; else if (nc.size() == 1) { if (ns.size() == 1) cout << 0; else if (ns.size() == 2) cout << 1; else { sort(n.begin(), n.end()); int c = 1; int g = n[1] - n[0]; int h = n[2] - n[1]; if (g == h && g == 1) cout << 0; else if (g == 1 || h == 1) cout << 1; else if (g == 2 || h == 2) cout << 1; else cout << 2; } } else if (nc.size() == 2) { if (ns.size() == 1) cout << 1; else { sort(p.begin(), p.end()); if (p[0].first == p[1].first) { if (abs(p[0].second - p[1].second) < 3) cout << 1; else cout << 2; } else if (p[1].first == p[2].first) { if (abs(p[1].second - p[2].second) < 3) cout << 1; else cout << 2; } else if (p[0].first == p[2].first) { if (abs(p[0].second - p[2].second) < 3) cout << 1; else cout << 2; } } } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> long long power(long long a, long long b, long long c) { long long ans = 1; a = (a % c); while (b > 0) { if (b & 1) { ans = (ans * a) % c; } b = b >> 1; a = (a * a) % c; } return ans; } using namespace std; int main() { vector<string> s; for (int i = 0; i < 3; i++) { string a; cin >> a; s.push_back(a); } vector<int> n; set<int> ns; set<char> nc; vector<char> c; vector<pair<char, int>> p; for (int i = 0; i < 3; i++) { n.push_back(s[i][0] - '0'); c.push_back(s[i][1]); ns.insert(s[i][0] - '0'); nc.insert(s[i][1]); p.push_back({s[i][1], s[i][0] - '0'}); } if (nc.size() == 3) cout << 2; else if (nc.size() == 1) { if (ns.size() == 1) cout << 0; else if (ns.size() == 2) cout << 1; else { sort(n.begin(), n.end()); int c = 1; int g = n[1] - n[0]; int h = n[2] - n[1]; if (g == h && g == 1) cout << 0; else if (g == 1 || h == 1) cout << 1; else if (g == 2 || h == 2) cout << 1; else cout << 2; } } else if (nc.size() == 2) { if (ns.size() == 1) cout << 1; else { sort(p.begin(), p.end()); if (p[0].first == p[1].first) { if (abs(p[0].second - p[1].second) < 3) cout << 1; else cout << 2; } else if (p[1].first == p[2].first) { if (abs(p[1].second - p[2].second) < 3) cout << 1; else cout << 2; } else if (p[0].first == p[2].first) { if (abs(p[0].second - p[2].second) < 3) cout << 1; else cout << 2; } } } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s1, s2, s3; cin >> s1 >> s2 >> s3; long long ans = 3; set<string> s; s.insert(s1); s.insert(s2); s.insert(s3); if (s.size() == 1) ans = 0; else if (s.size() == 2) ans = 1; else ans = 2; set<long long> sa, ma, pa; if (s1[1] == 's') sa.insert((long long)(s1[0] - '0')); else if (s1[1] == 'm') ma.insert((long long)(s1[0] - '0')); else pa.insert((long long)(s1[0] - '0')); if (s2[1] == 's') sa.insert((long long)(s2[0] - '0')); else if (s2[1] == 'm') ma.insert((long long)(s2[0] - '0')); else pa.insert((long long)(s2[0] - '0')); if (s3[1] == 's') sa.insert((long long)(s3[0] - '0')); else if (s3[1] == 'm') ma.insert((long long)(s3[0] - '0')); else pa.insert((long long)(s3[0] - '0')); long long c = 0; long long prev = -1; for (auto x : sa) { if (c == 0) c++; else if (x - 1 == prev) c++; else if (x - 2 == prev) { c = 2; if (3 - c < ans) ans = 3 - c; c = 1; } else { c = 1; } prev = x; if (3 - c < ans) ans = 3 - c; } c = 0; prev = -1; for (auto x : ma) { if (c == 0) c++; else if (x - 1 == prev) c++; else if (x - 2 == prev) { c = 2; if (3 - c < ans) ans = 3 - c; c = 1; } else { c = 1; } prev = x; if (3 - c < ans) ans = 3 - c; } c = 0; prev = -1; for (auto x : pa) { if (c == 0) c++; else if (x - 1 == prev) c++; else if (x - 2 == prev) { c = 2; if (3 - c < ans) ans = 3 - c; c = 1; } else { c = 1; } prev = x; if (3 - c < ans) ans = 3 - c; } cout << ans; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s1, s2, s3; cin >> s1 >> s2 >> s3; long long ans = 3; set<string> s; s.insert(s1); s.insert(s2); s.insert(s3); if (s.size() == 1) ans = 0; else if (s.size() == 2) ans = 1; else ans = 2; set<long long> sa, ma, pa; if (s1[1] == 's') sa.insert((long long)(s1[0] - '0')); else if (s1[1] == 'm') ma.insert((long long)(s1[0] - '0')); else pa.insert((long long)(s1[0] - '0')); if (s2[1] == 's') sa.insert((long long)(s2[0] - '0')); else if (s2[1] == 'm') ma.insert((long long)(s2[0] - '0')); else pa.insert((long long)(s2[0] - '0')); if (s3[1] == 's') sa.insert((long long)(s3[0] - '0')); else if (s3[1] == 'm') ma.insert((long long)(s3[0] - '0')); else pa.insert((long long)(s3[0] - '0')); long long c = 0; long long prev = -1; for (auto x : sa) { if (c == 0) c++; else if (x - 1 == prev) c++; else if (x - 2 == prev) { c = 2; if (3 - c < ans) ans = 3 - c; c = 1; } else { c = 1; } prev = x; if (3 - c < ans) ans = 3 - c; } c = 0; prev = -1; for (auto x : ma) { if (c == 0) c++; else if (x - 1 == prev) c++; else if (x - 2 == prev) { c = 2; if (3 - c < ans) ans = 3 - c; c = 1; } else { c = 1; } prev = x; if (3 - c < ans) ans = 3 - c; } c = 0; prev = -1; for (auto x : pa) { if (c == 0) c++; else if (x - 1 == prev) c++; else if (x - 2 == prev) { c = 2; if (3 - c < ans) ans = 3 - c; c = 1; } else { c = 1; } prev = x; if (3 - c < ans) ans = 3 - c; } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; int judge(string a1, string a2, string a3) { int ans1 = 2, ans2 = 2, ans3 = 2, ans4 = 2, ans5 = 2, ans6 = 2, ans7 = 2; if (a1 == a2) ans1--; if (a1 == a3) ans1--; string a4, a5; a4 += (((a1[0] - '0') + 1) + '0'); a5 += (((a1[0] - '0') - 1) + '0'); a4 += a1[1]; a5 += a1[1]; if (a2 == a4) ans2--; if (a3 == a5) ans2--; if (a2 == a5) ans3--; if (a3 == a4) ans3--; string a6, a7; a6 += (((a1[0] - '0') + 2) + '0'); a6 += a1[1]; a7 += (((a1[0] - '0') - 2) + '0'); a7 += a1[1]; if (a2 == a4) ans4--; if (a3 == a6) ans4--; if (a2 == a6) ans6--; if (a3 == a4) ans6--; if (a2 == a5) ans7--; if (a3 == a7) ans7--; if (a3 == a5) ans5--; if (a2 == a7) ans5--; return min(ans1, min(ans2, min(ans3, min(ans4, min(ans5, min(ans6, ans7)))))); } int main() { string a1, a2, a3; cin >> a1 >> a2 >> a3; int re = min(judge(a1, a2, a3), min(judge(a2, a1, a3), min(judge(a2, a3, a1), min(judge(a1, a3, a2), min(judge(a3, a1, a2), judge(a3, a2, a1)))))); cout << re; return 0; }
### Prompt Please formulate a cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int judge(string a1, string a2, string a3) { int ans1 = 2, ans2 = 2, ans3 = 2, ans4 = 2, ans5 = 2, ans6 = 2, ans7 = 2; if (a1 == a2) ans1--; if (a1 == a3) ans1--; string a4, a5; a4 += (((a1[0] - '0') + 1) + '0'); a5 += (((a1[0] - '0') - 1) + '0'); a4 += a1[1]; a5 += a1[1]; if (a2 == a4) ans2--; if (a3 == a5) ans2--; if (a2 == a5) ans3--; if (a3 == a4) ans3--; string a6, a7; a6 += (((a1[0] - '0') + 2) + '0'); a6 += a1[1]; a7 += (((a1[0] - '0') - 2) + '0'); a7 += a1[1]; if (a2 == a4) ans4--; if (a3 == a6) ans4--; if (a2 == a6) ans6--; if (a3 == a4) ans6--; if (a2 == a5) ans7--; if (a3 == a7) ans7--; if (a3 == a5) ans5--; if (a2 == a7) ans5--; return min(ans1, min(ans2, min(ans3, min(ans4, min(ans5, min(ans6, ans7)))))); } int main() { string a1, a2, a3; cin >> a1 >> a2 >> a3; int re = min(judge(a1, a2, a3), min(judge(a2, a1, a3), min(judge(a2, a3, a1), min(judge(a1, a3, a2), min(judge(a3, a1, a2), judge(a3, a2, a1)))))); cout << re; return 0; } ```
#include <bits/stdc++.h> using namespace std; string s[3]; int main() { cin >> s[0] >> s[1] >> s[2]; int data[3]; for (int i = 0; i < 3; i++) { if (s[i][1] == 's') { data[i] = s[i][0] - '0' + 10 * 1; } else if (s[i][1] == 'm') { data[i] = s[i][0] - '0' + 10 * 3; } else if (s[i][1] == 'p') { data[i] = s[i][0] - '0' + 10 * 5; } } sort(data, data + 3); if (data[0] == data[2]) { cout << 0 << endl; } else if (data[0] + 1 == data[1] && data[1] + 1 == data[2]) { cout << 0 << endl; } else if (data[0] == data[1] || data[1] == data[2]) { cout << 1 << endl; } else if (data[0] + 1 == data[1] || data[1] + 1 == data[2] || data[0] + 2 == data[1] || data[1] + 2 == data[2]) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s[3]; int main() { cin >> s[0] >> s[1] >> s[2]; int data[3]; for (int i = 0; i < 3; i++) { if (s[i][1] == 's') { data[i] = s[i][0] - '0' + 10 * 1; } else if (s[i][1] == 'm') { data[i] = s[i][0] - '0' + 10 * 3; } else if (s[i][1] == 'p') { data[i] = s[i][0] - '0' + 10 * 5; } } sort(data, data + 3); if (data[0] == data[2]) { cout << 0 << endl; } else if (data[0] + 1 == data[1] && data[1] + 1 == data[2]) { cout << 0 << endl; } else if (data[0] == data[1] || data[1] == data[2]) { cout << 1 << endl; } else if (data[0] + 1 == data[1] || data[1] + 1 == data[2] || data[0] + 2 == data[1] || data[1] + 2 == data[2]) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int arr[5], vis[10]; int main() { int a, b, c; char d, e, f; cin >> a >> d >> b >> e >> c >> f; vis[a]++, vis[b]++, vis[c]++; arr[2] = max(a, max(b, c)); arr[0] = min(a, min(b, c)); vis[arr[2]]++; vis[arr[0]]++; for (int i = 0; i <= 9; i++) { if (vis[i] == 1) { arr[1] = i; break; } } if (a == b && b == c && d == e && e == f) cout << 0 << endl; else if (d == e && e == f && arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) cout << 0 << endl; else if ((a == b && d == e) || (a == c && d == f) || (b == c && e == f)) cout << 1 << endl; else { if (abs(a - b) == 1 && d == e) cout << 1 << endl; else if (abs(a - c) == 1 && d == f) cout << 1 << endl; else if (abs(b - c) == 1 && e == f) cout << 1 << endl; else if (abs(a - b) == 2 && d == e) cout << 1 << endl; else if (abs(a - c) == 2 && d == f) cout << 1 << endl; else if (abs(b - c) == 2 && e == f) cout << 1 << endl; else cout << 2 << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arr[5], vis[10]; int main() { int a, b, c; char d, e, f; cin >> a >> d >> b >> e >> c >> f; vis[a]++, vis[b]++, vis[c]++; arr[2] = max(a, max(b, c)); arr[0] = min(a, min(b, c)); vis[arr[2]]++; vis[arr[0]]++; for (int i = 0; i <= 9; i++) { if (vis[i] == 1) { arr[1] = i; break; } } if (a == b && b == c && d == e && e == f) cout << 0 << endl; else if (d == e && e == f && arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) cout << 0 << endl; else if ((a == b && d == e) || (a == c && d == f) || (b == c && e == f)) cout << 1 << endl; else { if (abs(a - b) == 1 && d == e) cout << 1 << endl; else if (abs(a - c) == 1 && d == f) cout << 1 << endl; else if (abs(b - c) == 1 && e == f) cout << 1 << endl; else if (abs(a - b) == 2 && d == e) cout << 1 << endl; else if (abs(a - c) == 2 && d == f) cout << 1 << endl; else if (abs(b - c) == 2 && e == f) cout << 1 << endl; else cout << 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void ans(int64_t a) { cout << a; exit(0); } int32_t main() { ios_base::sync_with_stdio(false); vector<pair<int64_t, int64_t> > v; for (int64_t i = 0; i < 3; ++i) { int64_t a, b = 0; cin >> a; char c; cin >> c; if (c == 'm') b = 1; if (c == 'p') b = 2; v.push_back({a, b}); } sort(v.begin(), v.end()); if (v[0] == v[1] && v[1] == v[2]) ans(0); if (v[0].second == v[1].second && v[1].second == v[2].second && v[0].first + 1 == v[1].first && v[1].first + 1 == v[2].first) ans(0); if (v[0].second == v[1].second && v[0].first + 3 > v[1].first) ans(1); if (v[1].second == v[2].second && v[1].first + 3 > v[2].first) ans(1); if (v[0].second == v[2].second && v[0].first + 3 > v[2].first) ans(1); ans(2); }
### Prompt Construct a CPP code solution to the problem outlined: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void ans(int64_t a) { cout << a; exit(0); } int32_t main() { ios_base::sync_with_stdio(false); vector<pair<int64_t, int64_t> > v; for (int64_t i = 0; i < 3; ++i) { int64_t a, b = 0; cin >> a; char c; cin >> c; if (c == 'm') b = 1; if (c == 'p') b = 2; v.push_back({a, b}); } sort(v.begin(), v.end()); if (v[0] == v[1] && v[1] == v[2]) ans(0); if (v[0].second == v[1].second && v[1].second == v[2].second && v[0].first + 1 == v[1].first && v[1].first + 1 == v[2].first) ans(0); if (v[0].second == v[1].second && v[0].first + 3 > v[1].first) ans(1); if (v[1].second == v[2].second && v[1].first + 3 > v[2].first) ans(1); if (v[0].second == v[2].second && v[0].first + 3 > v[2].first) ans(1); ans(2); } ```
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; int i, j, aae = 0, co = 0; int ar[4]; char ss; cin >> a >> b >> c; if (a[1] != b[1]) co++; if (b[1] != c[1]) co++; if (c[1] != a[1]) co++; ar[0] = a[0] - '0'; ar[1] = b[0] - '0'; ar[2] = c[0] - '0'; if (co == 0) { if (ar[0] != ar[1]) aae++; if (ar[1] != ar[2]) aae++; if (ar[2] != ar[0]) aae++; if (aae == 0) cout << "0\n"; else { if (aae == 2) cout << "1\n"; else { aae = 0; int QQ = 3; sort(ar, ar + QQ); if ((ar[0] + 1) != ar[1]) aae++; if ((ar[1] + 1) != ar[2]) aae++; if (aae > 1) { if ((ar[0] + 2 == ar[1]) || (ar[1] + 2 == ar[2])) aae--; } cout << aae << '\n'; } } } else if (co == 2) { if (a[1] == b[1] || a[1] == c[1]) { ss = a[1]; ar[0] = a[0] - '0'; if (a[1] == b[1]) ar[1] = b[0] - '0'; else ar[1] = c[0] - '0'; } else { ss = b[1]; ar[0] = b[0] - '0'; ar[1] = c[0] - '0'; } i = 2; sort(ar, ar + i); if (ar[0] == ar[1]) cout << "1\n"; else if (((ar[0] + 1) == ar[1]) || ((ar[0] + 2) == ar[1])) cout << "1\n"; else cout << "2\n"; } else cout << "2\n"; return 0; }
### Prompt In cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string a, b, c; int i, j, aae = 0, co = 0; int ar[4]; char ss; cin >> a >> b >> c; if (a[1] != b[1]) co++; if (b[1] != c[1]) co++; if (c[1] != a[1]) co++; ar[0] = a[0] - '0'; ar[1] = b[0] - '0'; ar[2] = c[0] - '0'; if (co == 0) { if (ar[0] != ar[1]) aae++; if (ar[1] != ar[2]) aae++; if (ar[2] != ar[0]) aae++; if (aae == 0) cout << "0\n"; else { if (aae == 2) cout << "1\n"; else { aae = 0; int QQ = 3; sort(ar, ar + QQ); if ((ar[0] + 1) != ar[1]) aae++; if ((ar[1] + 1) != ar[2]) aae++; if (aae > 1) { if ((ar[0] + 2 == ar[1]) || (ar[1] + 2 == ar[2])) aae--; } cout << aae << '\n'; } } } else if (co == 2) { if (a[1] == b[1] || a[1] == c[1]) { ss = a[1]; ar[0] = a[0] - '0'; if (a[1] == b[1]) ar[1] = b[0] - '0'; else ar[1] = c[0] - '0'; } else { ss = b[1]; ar[0] = b[0] - '0'; ar[1] = c[0] - '0'; } i = 2; sort(ar, ar + i); if (ar[0] == ar[1]) cout << "1\n"; else if (((ar[0] + 1) == ar[1]) || ((ar[0] + 2) == ar[1])) cout << "1\n"; else cout << "2\n"; } else cout << "2\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; int ans = 2; char x, y, z; cin >> a; x = getchar(); cin >> b; y = getchar(); cin >> c; z = getchar(); vector<int> v; v.push_back(a); v.push_back(b); v.push_back(c); if (x == y && y == z) { sort(v.begin(), v.end()); a = v[0]; b = v[1]; c = v[2]; if (b - a == 1 || c - b == 1) { ans--; if (c - b == 1 && b - a == 1) ans--; } else if (a == b || b == c) { ans--; if (b == c && a == b) ans--; } else if (b - a == 2 || c - b == 2) ans--; } else if (x == y) { if (a - b == 1 || b - a == 1 || a - b == 2 || b - a == 2 || b == a) ans--; } else if (x == z) { if (a - c == 1 || c - a == 1 || a - c == 2 || c - a == 2 || c == a) ans--; } else if (z == y) { if (c - b == 1 || b - c == 1 || c - b == 2 || b - c == 2 || b == c) ans--; } cout << ans << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; int ans = 2; char x, y, z; cin >> a; x = getchar(); cin >> b; y = getchar(); cin >> c; z = getchar(); vector<int> v; v.push_back(a); v.push_back(b); v.push_back(c); if (x == y && y == z) { sort(v.begin(), v.end()); a = v[0]; b = v[1]; c = v[2]; if (b - a == 1 || c - b == 1) { ans--; if (c - b == 1 && b - a == 1) ans--; } else if (a == b || b == c) { ans--; if (b == c && a == b) ans--; } else if (b - a == 2 || c - b == 2) ans--; } else if (x == y) { if (a - b == 1 || b - a == 1 || a - b == 2 || b - a == 2 || b == a) ans--; } else if (x == z) { if (a - c == 1 || c - a == 1 || a - c == 2 || c - a == 2 || c == a) ans--; } else if (z == y) { if (c - b == 1 || b - c == 1 || c - b == 2 || b - c == 2 || b == c) ans--; } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; const int mod = 1e9 + 7; const double eps = 1e-15; const double pi = acos(-1); const int INF = 0x3f3f3f; long long read() { long long c = getchar(), Nig = 1, x = 0; while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') Nig = -1, c = getchar(); while (isdigit(c)) x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar(); return Nig * x; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int s[20], m[20], p[20]; int main() { for (int i = 1; i <= 3; i++) { string ss; cin >> ss; if (ss[1] == 's') s[(ss[0] - '0')]++; else if (ss[1] == 'p') p[ss[0] - '0']++; else m[ss[0] - '0']++; } int mx1 = 0, mx2 = 0, mx3 = 0; int l1 = 0, l2 = 0, l3 = 0; int n = 3; for (int i = 1; i <= 9; i++) { if (s[i] > mx1) mx1 = s[i]; if (s[i] && s[i + 1] && s[i + 2]) l1 = 3; else if (s[i] && s[i + 1] || s[i] && s[i + 2]) l1 = max(l1, 2); else if (s[i]) l1 = max(l1, 1); } int ans = min(n - mx1, n - l1); for (int i = 1; i <= 9; i++) { if (m[i] > mx2) mx2 = m[i]; if (m[i] && m[i + 1] && m[i + 2]) l2 = 3; else if (m[i] && m[i + 1] || m[i] && m[i + 2]) l2 = max(l2, 2); else if (m[i]) l2 = max(l2, 1); } ans = min(ans, min(n - mx2, n - l2)); for (int i = 1; i <= 9; i++) { if (p[i] > mx3) mx3 = p[i]; if (p[i] && p[i + 1] && p[i + 2]) l3 = 3; else if (p[i] && p[i + 1] || p[i] && p[i + 2]) l3 = max(l3, 2); else if (p[i]) l3 = max(l3, 1); } ans = min(ans, min(n - mx3, n - l3)); printf("%d\n", ans); }
### Prompt Generate a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; const int mod = 1e9 + 7; const double eps = 1e-15; const double pi = acos(-1); const int INF = 0x3f3f3f; long long read() { long long c = getchar(), Nig = 1, x = 0; while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') Nig = -1, c = getchar(); while (isdigit(c)) x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar(); return Nig * x; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int s[20], m[20], p[20]; int main() { for (int i = 1; i <= 3; i++) { string ss; cin >> ss; if (ss[1] == 's') s[(ss[0] - '0')]++; else if (ss[1] == 'p') p[ss[0] - '0']++; else m[ss[0] - '0']++; } int mx1 = 0, mx2 = 0, mx3 = 0; int l1 = 0, l2 = 0, l3 = 0; int n = 3; for (int i = 1; i <= 9; i++) { if (s[i] > mx1) mx1 = s[i]; if (s[i] && s[i + 1] && s[i + 2]) l1 = 3; else if (s[i] && s[i + 1] || s[i] && s[i + 2]) l1 = max(l1, 2); else if (s[i]) l1 = max(l1, 1); } int ans = min(n - mx1, n - l1); for (int i = 1; i <= 9; i++) { if (m[i] > mx2) mx2 = m[i]; if (m[i] && m[i + 1] && m[i + 2]) l2 = 3; else if (m[i] && m[i + 1] || m[i] && m[i + 2]) l2 = max(l2, 2); else if (m[i]) l2 = max(l2, 1); } ans = min(ans, min(n - mx2, n - l2)); for (int i = 1; i <= 9; i++) { if (p[i] > mx3) mx3 = p[i]; if (p[i] && p[i + 1] && p[i + 2]) l3 = 3; else if (p[i] && p[i + 1] || p[i] && p[i + 2]) l3 = max(l3, 2); else if (p[i]) l3 = max(l3, 1); } ans = min(ans, min(n - mx3, n - l3)); printf("%d\n", ans); } ```
#include <bits/stdc++.h> using namespace std; int main() { char a[5], b[5], aa[5][5]; for (int i = 1; i <= 3; i++) { scanf("%s", aa[i]); a[i] = aa[i][0]; b[i] = aa[i][1]; } if ((a[1] == a[2]) && (a[2] == a[3]) && (b[1] == b[2]) && (b[2] == b[3])) { printf("0\n"); return 0; } if (!(strcmp(aa[1], aa[2])) || !strcmp(aa[2], aa[3]) || !strcmp(aa[1], aa[3])) { printf("1\n"); return 0; } int c[5], d[5]; for (int i = 1; i <= 3; i++) { c[i] = a[i] - 48; d[i] = a[i] - 48; } sort(c + 1, c + 4); if (((c[3] - c[2]) == 1) && ((c[2] - c[1]) == 1) && (b[1] == b[2]) && (b[2] == b[3])) { printf("0\n"); return 0; } if (b[1] == b[2]) { if ((abs(d[1] - d[2]) == 1) || (abs(d[1] - d[2]) == 2)) { printf("1\n"); return 0; } } if (b[2] == b[3]) { if ((abs(d[2] - d[3]) == 1) || (abs(d[3] - d[2]) == 2)) { printf("1\n"); return 0; } } if (b[1] == b[3]) { if ((abs(d[1] - d[3]) == 1) || (abs(d[1] - d[3]) == 2)) { printf("1\n"); return 0; } } printf("2\n"); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char a[5], b[5], aa[5][5]; for (int i = 1; i <= 3; i++) { scanf("%s", aa[i]); a[i] = aa[i][0]; b[i] = aa[i][1]; } if ((a[1] == a[2]) && (a[2] == a[3]) && (b[1] == b[2]) && (b[2] == b[3])) { printf("0\n"); return 0; } if (!(strcmp(aa[1], aa[2])) || !strcmp(aa[2], aa[3]) || !strcmp(aa[1], aa[3])) { printf("1\n"); return 0; } int c[5], d[5]; for (int i = 1; i <= 3; i++) { c[i] = a[i] - 48; d[i] = a[i] - 48; } sort(c + 1, c + 4); if (((c[3] - c[2]) == 1) && ((c[2] - c[1]) == 1) && (b[1] == b[2]) && (b[2] == b[3])) { printf("0\n"); return 0; } if (b[1] == b[2]) { if ((abs(d[1] - d[2]) == 1) || (abs(d[1] - d[2]) == 2)) { printf("1\n"); return 0; } } if (b[2] == b[3]) { if ((abs(d[2] - d[3]) == 1) || (abs(d[3] - d[2]) == 2)) { printf("1\n"); return 0; } } if (b[1] == b[3]) { if ((abs(d[1] - d[3]) == 1) || (abs(d[1] - d[3]) == 2)) { printf("1\n"); return 0; } } printf("2\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int arr[3]; int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; if ((s1[1] == s2[1] && s2[1] == s3[1]) && (s1[0] == s2[0] && s2[0] == s3[0])) { cout << 0 << endl; } else if (s1[1] == s2[1] && s2[1] == s3[1]) { arr[0] = (int)s1[0]; arr[1] = (int)s2[0]; arr[2] = (int)s3[0]; sort(arr, arr + 3); if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) cout << 0 << endl; else if (arr[0] + 1 == arr[1] || arr[1] + 1 == arr[2] || arr[0] == arr[1] || arr[1] == arr[2]) cout << 1 << endl; else if (arr[0] + 1 == arr[1] || arr[0] + 2 == arr[1] || arr[1] + 1 == arr[2] || arr[1] + 2 == arr[2]) cout << 1 << endl; else cout << 2 << endl; } else if (s1[1] == s2[1]) { int x = (int)s1[0]; int y = (int)s2[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s1[1] == s3[1]) { int x = (int)s1[0]; int y = (int)s3[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s2[1] == s3[1]) { int x = (int)s2[0]; int y = (int)s3[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s2[1] != s3[1] && s1[1] != s3[1] && s2[1] != s3[1]) { cout << 2 << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arr[3]; int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; if ((s1[1] == s2[1] && s2[1] == s3[1]) && (s1[0] == s2[0] && s2[0] == s3[0])) { cout << 0 << endl; } else if (s1[1] == s2[1] && s2[1] == s3[1]) { arr[0] = (int)s1[0]; arr[1] = (int)s2[0]; arr[2] = (int)s3[0]; sort(arr, arr + 3); if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) cout << 0 << endl; else if (arr[0] + 1 == arr[1] || arr[1] + 1 == arr[2] || arr[0] == arr[1] || arr[1] == arr[2]) cout << 1 << endl; else if (arr[0] + 1 == arr[1] || arr[0] + 2 == arr[1] || arr[1] + 1 == arr[2] || arr[1] + 2 == arr[2]) cout << 1 << endl; else cout << 2 << endl; } else if (s1[1] == s2[1]) { int x = (int)s1[0]; int y = (int)s2[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s1[1] == s3[1]) { int x = (int)s1[0]; int y = (int)s3[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s2[1] == s3[1]) { int x = (int)s2[0]; int y = (int)s3[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s2[1] != s3[1] && s1[1] != s3[1] && s2[1] != s3[1]) { cout << 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); string s[3]; cin >> s[0] >> s[1] >> s[2]; sort(s, s + 3); if (s[0] == s[1] && s[1] == s[2]) { cout << "0" << endl; return 0; } if ((s[1][0] - s[0][0] == 1 && s[1][1] == s[0][1]) && (s[2][0] - s[1][0] == 1 && s[2][1] == s[1][1])) { cout << "0" << endl; return 0; } if ((s[1][0] - s[0][0] == 1 && s[1][1] == s[0][1]) || (s[2][0] - s[1][0] == 1 && s[2][1] == s[1][1]) || (s[2][0] - s[0][0] == 1 && s[2][1] == s[0][1])) { cout << "1" << endl; return 0; } if (s[1] == s[0] || s[2] == s[0] || s[1] == s[2]) { cout << "1" << endl; return 0; } if ((s[1][0] - s[0][0] == 2 && s[1][1] == s[0][1]) || (s[2][0] - s[1][0] == 2 && s[2][1] == s[1][1]) || (s[2][0] - s[0][0] == 2 && s[2][1] == s[0][1])) { cout << "1" << endl; return 0; } cout << "2"; }
### Prompt In Cpp, your task is to solve the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); string s[3]; cin >> s[0] >> s[1] >> s[2]; sort(s, s + 3); if (s[0] == s[1] && s[1] == s[2]) { cout << "0" << endl; return 0; } if ((s[1][0] - s[0][0] == 1 && s[1][1] == s[0][1]) && (s[2][0] - s[1][0] == 1 && s[2][1] == s[1][1])) { cout << "0" << endl; return 0; } if ((s[1][0] - s[0][0] == 1 && s[1][1] == s[0][1]) || (s[2][0] - s[1][0] == 1 && s[2][1] == s[1][1]) || (s[2][0] - s[0][0] == 1 && s[2][1] == s[0][1])) { cout << "1" << endl; return 0; } if (s[1] == s[0] || s[2] == s[0] || s[1] == s[2]) { cout << "1" << endl; return 0; } if ((s[1][0] - s[0][0] == 2 && s[1][1] == s[0][1]) || (s[2][0] - s[1][0] == 2 && s[2][1] == s[1][1]) || (s[2][0] - s[0][0] == 2 && s[2][1] == s[0][1])) { cout << "1" << endl; return 0; } cout << "2"; } ```
#include <bits/stdc++.h> using namespace std; void print(vector<pair<int, int> > &v) { for (long long int i = 0; i < v.size(); ++i) cout << v[i].first << " " << v[i].second << endl; } int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } int main() { string a, b, c; cin >> a >> b >> c; if (a.compare(b) == 0 && b.compare(c) == 0) cout << 0; else { if (a[1] != b[1] && b[1] != c[1] && c[1] != a[1]) cout << 2; else if (a[1] == b[1] && b[1] == c[1]) { int x = a[0] - '0', y = b[0] - '0', z = c[0] - '0'; if (x == y || y == z || z == x) cout << 1; else { int a = min(min(x, y), z); int c = max(max(x, y), z); int b = x + y + z - (a + c); if (abs(b - a) == 1 && abs(c - b) == 1) cout << 0; else if (b == (a + 1) || b == (a + 2) || c == (b + 1) || c == (b + 2)) cout << 1; else cout << 2; } } else if (a[1] == b[1]) { int x = a[0] - '0', y = b[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } else if (b[1] == c[1]) { int x = b[0] - '0', y = c[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } else if (a[1] == c[1]) { int x = a[0] - '0', y = c[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void print(vector<pair<int, int> > &v) { for (long long int i = 0; i < v.size(); ++i) cout << v[i].first << " " << v[i].second << endl; } int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } int main() { string a, b, c; cin >> a >> b >> c; if (a.compare(b) == 0 && b.compare(c) == 0) cout << 0; else { if (a[1] != b[1] && b[1] != c[1] && c[1] != a[1]) cout << 2; else if (a[1] == b[1] && b[1] == c[1]) { int x = a[0] - '0', y = b[0] - '0', z = c[0] - '0'; if (x == y || y == z || z == x) cout << 1; else { int a = min(min(x, y), z); int c = max(max(x, y), z); int b = x + y + z - (a + c); if (abs(b - a) == 1 && abs(c - b) == 1) cout << 0; else if (b == (a + 1) || b == (a + 2) || c == (b + 1) || c == (b + 2)) cout << 1; else cout << 2; } } else if (a[1] == b[1]) { int x = a[0] - '0', y = b[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } else if (b[1] == c[1]) { int x = b[0] - '0', y = c[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } else if (a[1] == c[1]) { int x = a[0] - '0', y = c[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[5]; char c[4]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 3; i++) { cin >> c; a[i] = c[0] - '0' + (c[1] - 'a') * 10; } sort(a, a + 3); int t1 = a[1] - a[0], t2 = a[2] - a[1], t3 = a[2] - a[0]; if (t1 == 0 && t2 == 0) { cout << 0; return 0; } else if (t1 == 1 && t2 == 1) { cout << 0; return 0; } else if (t1 == 2 || t2 == 2) { cout << 1; return 0; } else if (t1 == 1 || t2 == 1) { cout << 1; return 0; } else if (t1 == 0 || t2 == 0) { cout << 1; return 0; } else cout << 2; return 0; }
### Prompt Generate a Cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[5]; char c[4]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 3; i++) { cin >> c; a[i] = c[0] - '0' + (c[1] - 'a') * 10; } sort(a, a + 3); int t1 = a[1] - a[0], t2 = a[2] - a[1], t3 = a[2] - a[0]; if (t1 == 0 && t2 == 0) { cout << 0; return 0; } else if (t1 == 1 && t2 == 1) { cout << 0; return 0; } else if (t1 == 2 || t2 == 2) { cout << 1; return 0; } else if (t1 == 1 || t2 == 1) { cout << 1; return 0; } else if (t1 == 0 || t2 == 0) { cout << 1; return 0; } else cout << 2; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } int ans = 1e9 + 7; bool check0(vector<string> tmp) { if (tmp.size() < 3) return false; string a = tmp[0], b = tmp[1], c = tmp[2]; if (a == b && b == c) return true; if (c[0] - b[0] == 1 && b[0] - a[0] == 1) return true; return false; } bool check1(vector<string> tmp) { if (tmp.size() < 2) return false; if (tmp.size() == 2) { string a = tmp[0], b = tmp[1]; if (a == b) return true; if (b[0] - a[0] == 1 || b[0] - a[0] == 2) return true; return false; } string a = tmp[0], b = tmp[1], c = tmp[2]; if (a == b || b == c) return true; if (c[0] - b[0] == 1 || c[0] - b[0] == 2) return true; if (b[0] - a[0] == 1 || b[0] - a[0] == 2) return true; return false; } int main() { vector<string> mm, pp, second; for (int i = 0; i < 3; i++) { string s; cin >> s; if (s[1] == 'm') mm.push_back(s); if (s[1] == 'p') pp.push_back(s); if (s[1] == 's') second.push_back(s); } sort(begin(mm), end(mm)); sort(begin(pp), end(pp)); sort(begin(second), end(second)); if (check0(mm) || check0(pp) || check0(second)) return cout << 0 << endl, 0; if (check1(mm) || check1(pp) || check1(second)) return cout << 1 << endl, 0; cout << 2 << endl; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } int ans = 1e9 + 7; bool check0(vector<string> tmp) { if (tmp.size() < 3) return false; string a = tmp[0], b = tmp[1], c = tmp[2]; if (a == b && b == c) return true; if (c[0] - b[0] == 1 && b[0] - a[0] == 1) return true; return false; } bool check1(vector<string> tmp) { if (tmp.size() < 2) return false; if (tmp.size() == 2) { string a = tmp[0], b = tmp[1]; if (a == b) return true; if (b[0] - a[0] == 1 || b[0] - a[0] == 2) return true; return false; } string a = tmp[0], b = tmp[1], c = tmp[2]; if (a == b || b == c) return true; if (c[0] - b[0] == 1 || c[0] - b[0] == 2) return true; if (b[0] - a[0] == 1 || b[0] - a[0] == 2) return true; return false; } int main() { vector<string> mm, pp, second; for (int i = 0; i < 3; i++) { string s; cin >> s; if (s[1] == 'm') mm.push_back(s); if (s[1] == 'p') pp.push_back(s); if (s[1] == 's') second.push_back(s); } sort(begin(mm), end(mm)); sort(begin(pp), end(pp)); sort(begin(second), end(second)); if (check0(mm) || check0(pp) || check0(second)) return cout << 0 << endl, 0; if (check1(mm) || check1(pp) || check1(second)) return cout << 1 << endl, 0; cout << 2 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string s1, s2, s3; cin >> s1 >> s2 >> s3; map<char, vector<long long int> > ma; ma[s1[1]].push_back(s1[0] - '0'); ma[s2[1]].push_back(s2[0] - '0'); ma[s3[1]].push_back(s3[0] - '0'); long long int us = 0; for (map<char, vector<long long int> >::iterator it = ma.begin(); it != ma.end(); it++) { vector<long long int> v = it->second; sort((v).begin(), (v).end()); if (v.size() == 1) { us = max(us, 1LL); } else if (v.size() == 2) { if (v[1] - v[0] <= 2) { us = max(us, 2LL); } else { us = max(us, 1LL); } } else if (v.size() == 3) { if (v[0] == v[1] && v[1] == v[2]) { us = 3LL; } else if (v[1] - v[0] == 1 && v[2] - v[1] == 1) { us = 3LL; } else if (v[1] - v[0] <= 2 || v[2] - v[1] <= 2) { us = max(us, 2LL); } else { us = max(us, 1LL); } } } cout << (3LL - us) << '\n'; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string s1, s2, s3; cin >> s1 >> s2 >> s3; map<char, vector<long long int> > ma; ma[s1[1]].push_back(s1[0] - '0'); ma[s2[1]].push_back(s2[0] - '0'); ma[s3[1]].push_back(s3[0] - '0'); long long int us = 0; for (map<char, vector<long long int> >::iterator it = ma.begin(); it != ma.end(); it++) { vector<long long int> v = it->second; sort((v).begin(), (v).end()); if (v.size() == 1) { us = max(us, 1LL); } else if (v.size() == 2) { if (v[1] - v[0] <= 2) { us = max(us, 2LL); } else { us = max(us, 1LL); } } else if (v.size() == 3) { if (v[0] == v[1] && v[1] == v[2]) { us = 3LL; } else if (v[1] - v[0] == 1 && v[2] - v[1] == 1) { us = 3LL; } else if (v[1] - v[0] <= 2 || v[2] - v[1] <= 2) { us = max(us, 2LL); } else { us = max(us, 1LL); } } } cout << (3LL - us) << '\n'; return 0; } ```