output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
struct Matrix {
int size;
long long **a;
Matrix(int sz) {
size = sz;
a = new long long *[sz];
for (int i = 0; i < size; i++) a[i] = new long long[sz];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) a[i][j] = 0;
}
Matrix operator*(Matrix B) {
Matrix C = Matrix(size);
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
for (int k = 0; k < size; k++) {
C.a[i][j] += a[i][k] * B.a[k][j] % 1000000007;
if (C.a[i][j] >= 1000000007) C.a[i][j] -= 1000000007;
}
return C;
}
Matrix print() {
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) cout << a[i][j] << " \n"[j == size - 1];
}
};
int a[110], z;
long long denom, inv_denom;
long long power(long long x, long long n) {
long long res = 1, now = x;
for (; n; n >>= 1, now = now * now % 1000000007)
if (n & 1) res = res * now % 1000000007;
return res;
}
Matrix power(Matrix x, long long n) {
Matrix res = Matrix(x.size), now = x;
for (int i = 0; i < x.size; i++) res.a[i][i] = 1;
for (; n; n >>= 1, now = now * now)
if (n & 1) res = res * now;
return res;
}
int main() {
ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
z += (a[i] == 0);
}
int t = 0;
for (int i = 0; i < z; i++) t += (a[i] == 0);
denom = n * (n - 1) % 1000000007 * 500000004LL % 1000000007;
inv_denom = power(n * (n - 1) % 1000000007 * 500000004LL % 1000000007,
1000000007 - 2);
Matrix A = Matrix(z + 1);
for (int i = 0; i <= z; i++) {
if (i)
A.a[i - 1][i] = i * (n + i - 2 * z) % 1000000007 * inv_denom % 1000000007;
A.a[i][i] = (denom - (z - i) * (z - i) % 1000000007 -
i * (n + i - 2 * z) % 1000000007 + 2 * 1000000007) %
1000000007 * inv_denom % 1000000007;
if (i < z)
A.a[i + 1][i] = (z - i) * (z - i) % 1000000007 * inv_denom % 1000000007;
}
cout << power(A, k).a[z][t] << endl;
}
| ### Prompt
In cpp, your task is to solve the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Matrix {
int size;
long long **a;
Matrix(int sz) {
size = sz;
a = new long long *[sz];
for (int i = 0; i < size; i++) a[i] = new long long[sz];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) a[i][j] = 0;
}
Matrix operator*(Matrix B) {
Matrix C = Matrix(size);
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
for (int k = 0; k < size; k++) {
C.a[i][j] += a[i][k] * B.a[k][j] % 1000000007;
if (C.a[i][j] >= 1000000007) C.a[i][j] -= 1000000007;
}
return C;
}
Matrix print() {
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) cout << a[i][j] << " \n"[j == size - 1];
}
};
int a[110], z;
long long denom, inv_denom;
long long power(long long x, long long n) {
long long res = 1, now = x;
for (; n; n >>= 1, now = now * now % 1000000007)
if (n & 1) res = res * now % 1000000007;
return res;
}
Matrix power(Matrix x, long long n) {
Matrix res = Matrix(x.size), now = x;
for (int i = 0; i < x.size; i++) res.a[i][i] = 1;
for (; n; n >>= 1, now = now * now)
if (n & 1) res = res * now;
return res;
}
int main() {
ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
z += (a[i] == 0);
}
int t = 0;
for (int i = 0; i < z; i++) t += (a[i] == 0);
denom = n * (n - 1) % 1000000007 * 500000004LL % 1000000007;
inv_denom = power(n * (n - 1) % 1000000007 * 500000004LL % 1000000007,
1000000007 - 2);
Matrix A = Matrix(z + 1);
for (int i = 0; i <= z; i++) {
if (i)
A.a[i - 1][i] = i * (n + i - 2 * z) % 1000000007 * inv_denom % 1000000007;
A.a[i][i] = (denom - (z - i) * (z - i) % 1000000007 -
i * (n + i - 2 * z) % 1000000007 + 2 * 1000000007) %
1000000007 * inv_denom % 1000000007;
if (i < z)
A.a[i + 1][i] = (z - i) * (z - i) % 1000000007 * inv_denom % 1000000007;
}
cout << power(A, k).a[z][t] << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 105;
const int mod = 1000000007;
struct Matrix {
long long int val[M][M];
} I;
Matrix mul(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) {
C.val[i][j] = 0;
for (int k = 0; k < M; k++) {
C.val[i][j] += A.val[i][k] * B.val[k][j] % mod;
}
C.val[i][j] %= mod;
}
return C;
}
Matrix poww(Matrix A, int b) {
Matrix tmp = I;
while (b) {
if (b & 1) {
tmp = mul(tmp, A);
}
b >>= 1;
A = mul(A, A);
}
return tmp;
}
long long int poww(long long int a, long long int b) {
if (!a) return 0;
long long int tmp = 1;
while (b) {
if (b & 1) {
tmp = tmp * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return tmp;
}
int main() {
for (int i = 0; i < M; i++) {
I.val[i][i] = 1;
}
long long int n, k;
scanf("%lld%lld", &n, &k);
int zero = 0;
int one = 0;
int start;
vector<int> arr;
for (int i = 0; i < n; i++) {
int first;
scanf("%d", &first);
arr.push_back(first);
if (first == 0) {
zero++;
} else {
one++;
}
}
for (int i = 0; i < zero; i++) {
if (arr[i] == 0) {
start++;
}
}
Matrix mat;
memset(mat.val, 0, sizeof(mat.val));
for (int i = 0; i <= zero; i++) {
long long int inc = (zero - i) * (zero - i);
long long int dec = i * (one - zero + i);
long long int same = n * (n - 1) / 2 - inc - dec;
mat.val[i][i] = same % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i < zero)
mat.val[i + 1][i] = inc % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i > 0)
mat.val[i - 1][i] = dec % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
;
}
mat = poww(mat, k);
cout << mat.val[zero][start] << endl;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 105;
const int mod = 1000000007;
struct Matrix {
long long int val[M][M];
} I;
Matrix mul(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) {
C.val[i][j] = 0;
for (int k = 0; k < M; k++) {
C.val[i][j] += A.val[i][k] * B.val[k][j] % mod;
}
C.val[i][j] %= mod;
}
return C;
}
Matrix poww(Matrix A, int b) {
Matrix tmp = I;
while (b) {
if (b & 1) {
tmp = mul(tmp, A);
}
b >>= 1;
A = mul(A, A);
}
return tmp;
}
long long int poww(long long int a, long long int b) {
if (!a) return 0;
long long int tmp = 1;
while (b) {
if (b & 1) {
tmp = tmp * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return tmp;
}
int main() {
for (int i = 0; i < M; i++) {
I.val[i][i] = 1;
}
long long int n, k;
scanf("%lld%lld", &n, &k);
int zero = 0;
int one = 0;
int start;
vector<int> arr;
for (int i = 0; i < n; i++) {
int first;
scanf("%d", &first);
arr.push_back(first);
if (first == 0) {
zero++;
} else {
one++;
}
}
for (int i = 0; i < zero; i++) {
if (arr[i] == 0) {
start++;
}
}
Matrix mat;
memset(mat.val, 0, sizeof(mat.val));
for (int i = 0; i <= zero; i++) {
long long int inc = (zero - i) * (zero - i);
long long int dec = i * (one - zero + i);
long long int same = n * (n - 1) / 2 - inc - dec;
mat.val[i][i] = same % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i < zero)
mat.val[i + 1][i] = inc % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i > 0)
mat.val[i - 1][i] = dec % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
;
}
mat = poww(mat, k);
cout << mat.val[zero][start] << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int x = 0, f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, k;
int sum0, sum1;
struct mat {
int a[110][110];
mat() { memset(a, 0, sizeof(a)); }
inline mat operator*(const mat& rhs) const {
mat ret;
for (int i = (0); i <= (sum0); ++i)
for (int j = (0); j <= (sum0); ++j) {
for (int k = (0); k <= (sum0); ++k) {
ret.a[i][j] += 1ll * a[i][k] * rhs.a[k][j] % 1000000007;
if (ret.a[i][j] >= 1000000007) ret.a[i][j] -= 1000000007;
}
}
return ret;
}
} bas;
int c[110];
inline mat Power(mat x, int y) {
mat ret;
ret = x;
--y;
while (y) {
if (y & 1) ret = ret * x;
x = x * x;
y >>= 1;
}
return ret;
}
inline int Power(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1ll * ret * x % 1000000007;
x = 1ll * x * x % 1000000007;
y >>= 1;
}
return ret;
}
int main() {
n = read(), k = read();
for (int i = (1); i <= (n); ++i) c[i] = read();
sum0 = 0, sum1 = 0;
int a = 0, st = 0;
for (int i = (1); i <= (n); ++i) {
if (c[i]) {
++sum1;
} else {
++sum0;
++a;
}
}
for (int i = (1); i <= (a); ++i) {
if (!c[i]) ++st;
}
for (int j = (0); j <= (sum0); ++j) {
int inv = Power(n * (n - 1) / 2, 1000000007 - 2);
bas.a[j][j] = 1ll *
(a * (a - 1) / 2 + (n - a) * (n - a - 1) / 2 +
j * (sum0 - j) + (a - j) * (sum1 - a + j)) *
inv % 1000000007;
if (j) bas.a[j][j - 1] = 1ll * j * (sum1 - a + j) * inv % 1000000007;
if (j < sum0) {
bas.a[j][j + 1] =
1ll * (a - j) * (sum0 - j) % 1000000007 * inv % 1000000007;
}
}
mat p;
p.a[0][st] = 1;
p = p * Power(bas, k);
printf("%d\n", p.a[0][sum0]);
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int x = 0, f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, k;
int sum0, sum1;
struct mat {
int a[110][110];
mat() { memset(a, 0, sizeof(a)); }
inline mat operator*(const mat& rhs) const {
mat ret;
for (int i = (0); i <= (sum0); ++i)
for (int j = (0); j <= (sum0); ++j) {
for (int k = (0); k <= (sum0); ++k) {
ret.a[i][j] += 1ll * a[i][k] * rhs.a[k][j] % 1000000007;
if (ret.a[i][j] >= 1000000007) ret.a[i][j] -= 1000000007;
}
}
return ret;
}
} bas;
int c[110];
inline mat Power(mat x, int y) {
mat ret;
ret = x;
--y;
while (y) {
if (y & 1) ret = ret * x;
x = x * x;
y >>= 1;
}
return ret;
}
inline int Power(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1ll * ret * x % 1000000007;
x = 1ll * x * x % 1000000007;
y >>= 1;
}
return ret;
}
int main() {
n = read(), k = read();
for (int i = (1); i <= (n); ++i) c[i] = read();
sum0 = 0, sum1 = 0;
int a = 0, st = 0;
for (int i = (1); i <= (n); ++i) {
if (c[i]) {
++sum1;
} else {
++sum0;
++a;
}
}
for (int i = (1); i <= (a); ++i) {
if (!c[i]) ++st;
}
for (int j = (0); j <= (sum0); ++j) {
int inv = Power(n * (n - 1) / 2, 1000000007 - 2);
bas.a[j][j] = 1ll *
(a * (a - 1) / 2 + (n - a) * (n - a - 1) / 2 +
j * (sum0 - j) + (a - j) * (sum1 - a + j)) *
inv % 1000000007;
if (j) bas.a[j][j - 1] = 1ll * j * (sum1 - a + j) * inv % 1000000007;
if (j < sum0) {
bas.a[j][j + 1] =
1ll * (a - j) * (sum0 - j) % 1000000007 * inv % 1000000007;
}
}
mat p;
p.a[0][st] = 1;
p = p * Power(bas, k);
printf("%d\n", p.a[0][sum0]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
const int mod = 1e9 + 7, inv2 = (mod + 1) >> 1;
inline int fix(int x) { return x + ((x >> 31) & mod); }
inline int add(int x, int y) { return fix(x + y - mod); }
inline int dec(int x, int y) { return fix(x - y); }
inline int mul(int x, int y) { return int((long long)x * y % mod); }
inline void ADD(int &x, int y) { x = fix(x + y - mod); }
inline void DEC(int &x, int y) { x = fix(x - y); }
inline void MUL(int &x, int y) { x = int((long long)x * y % mod); }
inline int ksm(int x, int r) {
int ret = 1;
for (int i = 0; (1ll << i) <= r; i++) {
if ((r >> i) & 1) MUL(ret, x);
MUL(x, x);
}
return ret;
}
const int N = 104;
int n, m, k, st, a[N];
struct matrix {
int a[N][N];
inline matrix operator*(const matrix &x) const {
static matrix ret;
for (int i = 0; i <= k; i++)
for (int j = 0; j <= k; j++) {
ret.a[i][j] = 0;
for (int u = 0; u <= k; u++) ADD(ret.a[i][j], mul(a[i][u], x.a[u][j]));
}
return ret;
}
inline void clear() {
for (int i = 0; i <= k; i++) a[i][i] = 1;
}
} res, tmp;
int main() {
n = read();
m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
if (!a[i]) k++;
}
for (int i = 1; i <= k; i++)
if (a[i]) st++;
for (int i = 0, x = ksm(n * (n - 1) / 2, mod - 2); i <= k; i++) {
if (i) tmp.a[i][i - 1] = mul(x, i * i);
tmp.a[i][i + 1] = mul(x, (k - i) * (n - k - i));
tmp.a[i][i] = dec(1, add(i ? tmp.a[i][i - 1] : 0, tmp.a[i][i + 1]));
}
res.clear();
for (int i = 0; (1ll << i) <= m; i++) {
if ((m >> i) & 1) res = res * tmp;
tmp = tmp * tmp;
}
cout << res.a[st][0] << "\n";
return (0 - 0);
}
| ### Prompt
In Cpp, your task is to solve the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
const int mod = 1e9 + 7, inv2 = (mod + 1) >> 1;
inline int fix(int x) { return x + ((x >> 31) & mod); }
inline int add(int x, int y) { return fix(x + y - mod); }
inline int dec(int x, int y) { return fix(x - y); }
inline int mul(int x, int y) { return int((long long)x * y % mod); }
inline void ADD(int &x, int y) { x = fix(x + y - mod); }
inline void DEC(int &x, int y) { x = fix(x - y); }
inline void MUL(int &x, int y) { x = int((long long)x * y % mod); }
inline int ksm(int x, int r) {
int ret = 1;
for (int i = 0; (1ll << i) <= r; i++) {
if ((r >> i) & 1) MUL(ret, x);
MUL(x, x);
}
return ret;
}
const int N = 104;
int n, m, k, st, a[N];
struct matrix {
int a[N][N];
inline matrix operator*(const matrix &x) const {
static matrix ret;
for (int i = 0; i <= k; i++)
for (int j = 0; j <= k; j++) {
ret.a[i][j] = 0;
for (int u = 0; u <= k; u++) ADD(ret.a[i][j], mul(a[i][u], x.a[u][j]));
}
return ret;
}
inline void clear() {
for (int i = 0; i <= k; i++) a[i][i] = 1;
}
} res, tmp;
int main() {
n = read();
m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
if (!a[i]) k++;
}
for (int i = 1; i <= k; i++)
if (a[i]) st++;
for (int i = 0, x = ksm(n * (n - 1) / 2, mod - 2); i <= k; i++) {
if (i) tmp.a[i][i - 1] = mul(x, i * i);
tmp.a[i][i + 1] = mul(x, (k - i) * (n - k - i));
tmp.a[i][i] = dec(1, add(i ? tmp.a[i][i - 1] : 0, tmp.a[i][i + 1]));
}
res.clear();
for (int i = 0; (1ll << i) <= m; i++) {
if ((m >> i) & 1) res = res * tmp;
tmp = tmp * tmp;
}
cout << res.a[st][0] << "\n";
return (0 - 0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < 2 * MOD)
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
| ### Prompt
Generate a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < 2 * MOD)
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct matrix {
int row, col;
long long v[102][102];
};
matrix mul(matrix mat, matrix mat2) {
matrix r;
r.row = mat.row;
r.col = mat.col;
for (int i = 0; i < r.row; i++) {
for (int j = 0; j < r.col; j++) {
long long sum = 0;
for (int k = 0; k < mat.col; k++) {
sum += mat.v[i][k] * mat2.v[k][j];
sum %= 1000000007;
}
r.v[i][j] = sum;
}
}
return r;
}
matrix po(matrix mat, int p) {
if (p == 1) return mat;
if (p & 1) {
matrix r = po(mat, p - 1);
r = mul(mat, r);
return r;
}
matrix r = po(mat, p / 2);
r = mul(r, r);
return r;
}
long long bigmod(int v, int po) {
if (po == 1) return v;
if (po & 1) {
int g = bigmod(v, po - 1);
return (1LL * g * v) % 1000000007;
}
int g = bigmod(v, po / 2);
return (1LL * g * g) % 1000000007;
}
int ara[200];
int main() {
int i, j, k, l, m, n, zero = 0, one = 0, ase = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &ara[i]);
if (ara[i] == 1)
one++;
else
zero++;
}
for (int i = 1; i <= zero; i++) {
if (ara[i] == 0) ase++;
}
matrix mat;
mat.row = mat.col = zero + 1;
memset(mat.v, 0, sizeof mat.v);
long long hor = bigmod((n * (n - 1)) / 2, 1000000007 - 2);
for (int i = 0; i <= zero; i++) {
if ((n - zero) < (zero - i)) continue;
int rem0 = zero - i;
int vone = zero - i;
int bone = one - vone;
;
if (vone > one) continue;
if ((bone + rem0) > (n - zero)) continue;
for (int j = i - 1; j <= min(i + 1, zero); j++) {
if (j == -1) continue;
int sum;
if (j == i) {
sum = (zero * (zero - 1)) / 2 + i * rem0 + vone * bone +
((n - zero) * (n - zero - 1)) / 2;
;
mat.v[i][j] = (hor * sum) % 1000000007;
continue;
}
if (j == i + 1) {
sum = vone * rem0;
mat.v[i][j] = (hor * sum) % 1000000007;
continue;
}
if (j == i - 1) {
mat.v[i][j] = (hor * bone * i) % 1000000007;
}
}
}
mat = po(mat, k);
long long ans = mat.v[ase][zero], sum = 0;
for (int i = 0; i <= zero; i++) {
sum += mat.v[ase][i];
sum %= 1000000007;
}
cout << ans << endl;
}
| ### Prompt
Generate a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct matrix {
int row, col;
long long v[102][102];
};
matrix mul(matrix mat, matrix mat2) {
matrix r;
r.row = mat.row;
r.col = mat.col;
for (int i = 0; i < r.row; i++) {
for (int j = 0; j < r.col; j++) {
long long sum = 0;
for (int k = 0; k < mat.col; k++) {
sum += mat.v[i][k] * mat2.v[k][j];
sum %= 1000000007;
}
r.v[i][j] = sum;
}
}
return r;
}
matrix po(matrix mat, int p) {
if (p == 1) return mat;
if (p & 1) {
matrix r = po(mat, p - 1);
r = mul(mat, r);
return r;
}
matrix r = po(mat, p / 2);
r = mul(r, r);
return r;
}
long long bigmod(int v, int po) {
if (po == 1) return v;
if (po & 1) {
int g = bigmod(v, po - 1);
return (1LL * g * v) % 1000000007;
}
int g = bigmod(v, po / 2);
return (1LL * g * g) % 1000000007;
}
int ara[200];
int main() {
int i, j, k, l, m, n, zero = 0, one = 0, ase = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &ara[i]);
if (ara[i] == 1)
one++;
else
zero++;
}
for (int i = 1; i <= zero; i++) {
if (ara[i] == 0) ase++;
}
matrix mat;
mat.row = mat.col = zero + 1;
memset(mat.v, 0, sizeof mat.v);
long long hor = bigmod((n * (n - 1)) / 2, 1000000007 - 2);
for (int i = 0; i <= zero; i++) {
if ((n - zero) < (zero - i)) continue;
int rem0 = zero - i;
int vone = zero - i;
int bone = one - vone;
;
if (vone > one) continue;
if ((bone + rem0) > (n - zero)) continue;
for (int j = i - 1; j <= min(i + 1, zero); j++) {
if (j == -1) continue;
int sum;
if (j == i) {
sum = (zero * (zero - 1)) / 2 + i * rem0 + vone * bone +
((n - zero) * (n - zero - 1)) / 2;
;
mat.v[i][j] = (hor * sum) % 1000000007;
continue;
}
if (j == i + 1) {
sum = vone * rem0;
mat.v[i][j] = (hor * sum) % 1000000007;
continue;
}
if (j == i - 1) {
mat.v[i][j] = (hor * bone * i) % 1000000007;
}
}
}
mat = po(mat, k);
long long ans = mat.v[ase][zero], sum = 0;
for (int i = 0; i <= zero; i++) {
sum += mat.v[ase][i];
sum %= 1000000007;
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int a[105], n, K, sn;
struct Mat {
int a[105][105];
Mat() { memset(a, 0, sizeof(a)); }
Mat operator*(const Mat &b) const {
Mat c = Mat();
for (int i = 0; i <= sn; i++)
for (int j = 0; j <= sn; j++)
for (int k = 0; k <= sn; k++)
c.a[i][j] = (c.a[i][j] + (long long)a[i][k] * b.a[k][j]) % mod;
return c;
}
} ret, mp;
int q_pow(int x, int n) {
int ret = 1;
for (; n; n >>= 1, x = (long long)x * x % mod)
if (n & 1) ret = (long long)ret * x % mod;
return ret;
}
void q_pow(int K) {
for (; K; K >>= 1, mp = mp * mp)
if (K & 1) ret = ret * mp;
}
int main() {
scanf("%d%d", &n, &K);
int now = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sn += a[i] == 1;
for (int i = n - sn + 1; i <= n; i++) now += a[i] == 1;
ret.a[0][now] = 1;
for (int i = 0; i <= sn; i++) {
mp.a[i][i - 1] = i * (n - sn * 2 + i);
mp.a[i][i + 1] = (sn - i) * (sn - i);
mp.a[i][i] =
(n * (n - 1) >> 1) - i * (n - sn * 2 + i) - (sn - i) * (sn - i);
}
q_pow(K);
printf("%lld\n", ((long long)ret.a[0][sn] *
q_pow(q_pow(n * (n - 1) >> 1, mod - 2), K) % mod +
mod) %
mod);
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int a[105], n, K, sn;
struct Mat {
int a[105][105];
Mat() { memset(a, 0, sizeof(a)); }
Mat operator*(const Mat &b) const {
Mat c = Mat();
for (int i = 0; i <= sn; i++)
for (int j = 0; j <= sn; j++)
for (int k = 0; k <= sn; k++)
c.a[i][j] = (c.a[i][j] + (long long)a[i][k] * b.a[k][j]) % mod;
return c;
}
} ret, mp;
int q_pow(int x, int n) {
int ret = 1;
for (; n; n >>= 1, x = (long long)x * x % mod)
if (n & 1) ret = (long long)ret * x % mod;
return ret;
}
void q_pow(int K) {
for (; K; K >>= 1, mp = mp * mp)
if (K & 1) ret = ret * mp;
}
int main() {
scanf("%d%d", &n, &K);
int now = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sn += a[i] == 1;
for (int i = n - sn + 1; i <= n; i++) now += a[i] == 1;
ret.a[0][now] = 1;
for (int i = 0; i <= sn; i++) {
mp.a[i][i - 1] = i * (n - sn * 2 + i);
mp.a[i][i + 1] = (sn - i) * (sn - i);
mp.a[i][i] =
(n * (n - 1) >> 1) - i * (n - sn * 2 + i) - (sn - i) * (sn - i);
}
q_pow(K);
printf("%lld\n", ((long long)ret.a[0][sn] *
q_pow(q_pow(n * (n - 1) >> 1, mod - 2), K) % mod +
mod) %
mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 105;
long long m;
struct node {
long long row, col;
long long data[N][N];
node(long long n, long long m) {
row = n;
col = m;
for (long long i = 0; i <= n; i++)
for (long long j = 0; j <= n; j++) data[i][j] = 0;
}
node operator*(node b) {
node t(this->row, b.col);
for (long long i = 0; i <= t.row; i++) {
for (long long j = 0; j <= t.col; j++) {
for (long long k = 0; k <= this->col; k++) {
t.data[i][j] =
(t.data[i][j] + this->data[i][k] * b.data[k][j] % mod) % mod;
}
}
}
return t;
}
};
inline long long ksm(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long n, k, t, a[N];
inline long long f1(long long x) {
return (x * (n - 2 * m + x) % mod + mod) % mod;
}
inline long long f3(long long x) {
return ((m - x) * (m - x) % mod + mod) % mod;
}
inline long long f2(long long x) {
return ((n * (n - 1) / 2 - f1(x) - f3(x)) % mod + mod) % mod;
}
signed main() {
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (!a[i]) m++;
}
node ma(m, m);
node ans(m, m);
for (long long i = 1; i <= m; i++)
if (!a[i]) t++;
for (long long i = 0; i <= m; i++) {
if (i != 0) ma.data[i - 1][i] = f1(i);
ma.data[i][i] = f2(i);
if (i != m) ma.data[i + 1][i] = f3(i);
}
for (long long i = 0; i <= m; i++) ans.data[i][i] = 1;
while (k) {
if (k & 1) {
ans = ans * ma;
}
ma = ma * ma;
k /= 2;
}
long long total = 0;
for (long long i = 0; i <= m; i++) {
total = (total + ans.data[i][t]) % mod;
}
printf("%lld\n", ans.data[m][t] * ksm(total, mod - 2) % mod);
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 105;
long long m;
struct node {
long long row, col;
long long data[N][N];
node(long long n, long long m) {
row = n;
col = m;
for (long long i = 0; i <= n; i++)
for (long long j = 0; j <= n; j++) data[i][j] = 0;
}
node operator*(node b) {
node t(this->row, b.col);
for (long long i = 0; i <= t.row; i++) {
for (long long j = 0; j <= t.col; j++) {
for (long long k = 0; k <= this->col; k++) {
t.data[i][j] =
(t.data[i][j] + this->data[i][k] * b.data[k][j] % mod) % mod;
}
}
}
return t;
}
};
inline long long ksm(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long n, k, t, a[N];
inline long long f1(long long x) {
return (x * (n - 2 * m + x) % mod + mod) % mod;
}
inline long long f3(long long x) {
return ((m - x) * (m - x) % mod + mod) % mod;
}
inline long long f2(long long x) {
return ((n * (n - 1) / 2 - f1(x) - f3(x)) % mod + mod) % mod;
}
signed main() {
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (!a[i]) m++;
}
node ma(m, m);
node ans(m, m);
for (long long i = 1; i <= m; i++)
if (!a[i]) t++;
for (long long i = 0; i <= m; i++) {
if (i != 0) ma.data[i - 1][i] = f1(i);
ma.data[i][i] = f2(i);
if (i != m) ma.data[i + 1][i] = f3(i);
}
for (long long i = 0; i <= m; i++) ans.data[i][i] = 1;
while (k) {
if (k & 1) {
ans = ans * ma;
}
ma = ma * ma;
k /= 2;
}
long long total = 0;
for (long long i = 0; i <= m; i++) {
total = (total + ans.data[i][t]) % mod;
}
printf("%lld\n", ans.data[m][t] * ksm(total, mod - 2) % mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> matrix_multiply(vector<vector<long long>> a,
vector<vector<long long>> b) {
long long x = a.size(), y = a[0].size(), z = b[0].size(), i, j, k;
vector<vector<long long>> ans(x, vector<long long>(z, 0));
for (i = 0; i < x; i++) {
for (j = 0; j < z; j++)
for (k = 0; k < y; k++)
ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % 1000000007;
}
return ans;
}
long long gcd(long long a, long long b) {
while (b != 0) {
long long t = b;
b = a % b;
a = t;
}
return a;
}
long long mul_inv(long long a, long long b) {
long long b0 = b, t, q;
long long x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
long long division(long long a, long long b, long long p) {
long long ans, inv;
inv = mul_inv(b, p);
ans = ((a % p) * inv) % p;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, q, i, j, k, c, x, aa, bb, xx, g, ans;
cin >> n >> q;
vector<int> a(n);
c = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) c++;
}
x = 0;
for (i = 0; i < c; i++) {
if (a[i] == 0) x++;
}
xx = x;
vector<vector<long long>> t(n + 1, vector<long long>(n + 1, 0));
for (x = 0; x <= n; x++) {
if (x < n) {
t[x][x + 1] = (c - x) * (c - x);
aa = t[x][x + 1];
} else
aa = 0;
if (x > 0) {
t[x][x - 1] = x * (n - c - c + x);
bb = t[x][x - 1];
} else
bb = 0;
t[x][x] = n * (n - 1) / 2 - aa - bb;
}
vector<vector<vector<long long>>> cc(33);
vector<long long> total(33);
cc[0] = t;
total[0] = n * (n - 1) / 2;
for (i = 1; i < 33; i++) {
cc[i] = matrix_multiply(cc[i - 1], cc[i - 1]);
total[i] = (total[i - 1] * total[i - 1]) % 1000000007;
}
vector<vector<long long>> z(1, vector<long long>(n + 1, 0));
z[0][xx] = 1;
bb = 1;
for (i = 0; i < 33; i++) {
if ((1LL << i) & q) {
z = matrix_multiply(z, cc[i]);
bb = (bb * total[i]) % 1000000007;
}
}
aa = z[0][c];
g = gcd(aa, bb);
aa /= g;
bb /= g;
ans = division(aa, bb, 1000000007);
cout << ans << "\n";
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> matrix_multiply(vector<vector<long long>> a,
vector<vector<long long>> b) {
long long x = a.size(), y = a[0].size(), z = b[0].size(), i, j, k;
vector<vector<long long>> ans(x, vector<long long>(z, 0));
for (i = 0; i < x; i++) {
for (j = 0; j < z; j++)
for (k = 0; k < y; k++)
ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % 1000000007;
}
return ans;
}
long long gcd(long long a, long long b) {
while (b != 0) {
long long t = b;
b = a % b;
a = t;
}
return a;
}
long long mul_inv(long long a, long long b) {
long long b0 = b, t, q;
long long x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
long long division(long long a, long long b, long long p) {
long long ans, inv;
inv = mul_inv(b, p);
ans = ((a % p) * inv) % p;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, q, i, j, k, c, x, aa, bb, xx, g, ans;
cin >> n >> q;
vector<int> a(n);
c = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) c++;
}
x = 0;
for (i = 0; i < c; i++) {
if (a[i] == 0) x++;
}
xx = x;
vector<vector<long long>> t(n + 1, vector<long long>(n + 1, 0));
for (x = 0; x <= n; x++) {
if (x < n) {
t[x][x + 1] = (c - x) * (c - x);
aa = t[x][x + 1];
} else
aa = 0;
if (x > 0) {
t[x][x - 1] = x * (n - c - c + x);
bb = t[x][x - 1];
} else
bb = 0;
t[x][x] = n * (n - 1) / 2 - aa - bb;
}
vector<vector<vector<long long>>> cc(33);
vector<long long> total(33);
cc[0] = t;
total[0] = n * (n - 1) / 2;
for (i = 1; i < 33; i++) {
cc[i] = matrix_multiply(cc[i - 1], cc[i - 1]);
total[i] = (total[i - 1] * total[i - 1]) % 1000000007;
}
vector<vector<long long>> z(1, vector<long long>(n + 1, 0));
z[0][xx] = 1;
bb = 1;
for (i = 0; i < 33; i++) {
if ((1LL << i) & q) {
z = matrix_multiply(z, cc[i]);
bb = (bb * total[i]) % 1000000007;
}
}
aa = z[0][c];
g = gcd(aa, bb);
aa /= g;
bb /= g;
ans = division(aa, bb, 1000000007);
cout << ans << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
const long long MAX_N = 105;
struct Matrix {
long long mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++)
for (ans.mat[i][j] = k = 0; k < MAX_N; k++) {
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
ans.mat[i][j] %= MOD;
}
return ans;
}
Matrix matPow(Matrix base, long long p) {
p %= MOD;
Matrix ans;
long long i, j;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int fpow(int a, int p) {
if (p == 0) return 1;
long long z = fpow(a, p / 2);
z = (z * z) % MOD;
if (p % 2) z = (z * a) % MOD;
return z;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
reverse(v.begin(), v.end());
long long tot = 0;
for (int i = 0; i < n; ++i) {
if (v[i]) tot++;
}
long long cnt = 0;
for (int i = 0; i < tot; ++i) {
if (v[i]) cnt++;
}
Matrix m;
for (int i = 0; i < tot + 1; ++i) {
for (int j = 0; j < tot + 1; ++j) {
if (i + n - tot < tot) {
m.mat[i][j] = 0;
continue;
}
if (abs(i - j) > 1)
m.mat[i][j] = 0;
else if (i == j) {
m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 +
i * (tot - i) + (tot - i) * (n - 2 * tot + i);
} else if (i - j == 1) {
m.mat[i][j] = (i) * (n - 2 * tot + i);
} else if (j - i == 1) {
m.mat[i][j] = (tot - i) * (tot - i);
}
m.mat[i][j] %= MOD;
}
}
Matrix ans = matPow(m, k);
long long a1 = 0, a2 = 0;
a1 = ans.mat[cnt][tot];
for (int i = 0; i < tot + 1; ++i) {
a2 += ans.mat[cnt][i];
a2 %= MOD;
}
long long ansf = 0;
ansf = a1;
ansf *= fpow(a2, MOD - 2);
cout << (ansf) % MOD << '\n';
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
const long long MAX_N = 105;
struct Matrix {
long long mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++)
for (ans.mat[i][j] = k = 0; k < MAX_N; k++) {
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
ans.mat[i][j] %= MOD;
}
return ans;
}
Matrix matPow(Matrix base, long long p) {
p %= MOD;
Matrix ans;
long long i, j;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int fpow(int a, int p) {
if (p == 0) return 1;
long long z = fpow(a, p / 2);
z = (z * z) % MOD;
if (p % 2) z = (z * a) % MOD;
return z;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
reverse(v.begin(), v.end());
long long tot = 0;
for (int i = 0; i < n; ++i) {
if (v[i]) tot++;
}
long long cnt = 0;
for (int i = 0; i < tot; ++i) {
if (v[i]) cnt++;
}
Matrix m;
for (int i = 0; i < tot + 1; ++i) {
for (int j = 0; j < tot + 1; ++j) {
if (i + n - tot < tot) {
m.mat[i][j] = 0;
continue;
}
if (abs(i - j) > 1)
m.mat[i][j] = 0;
else if (i == j) {
m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 +
i * (tot - i) + (tot - i) * (n - 2 * tot + i);
} else if (i - j == 1) {
m.mat[i][j] = (i) * (n - 2 * tot + i);
} else if (j - i == 1) {
m.mat[i][j] = (tot - i) * (tot - i);
}
m.mat[i][j] %= MOD;
}
}
Matrix ans = matPow(m, k);
long long a1 = 0, a2 = 0;
a1 = ans.mat[cnt][tot];
for (int i = 0; i < tot + 1; ++i) {
a2 += ans.mat[cnt][i];
a2 %= MOD;
}
long long ansf = 0;
ansf = a1;
ansf *= fpow(a2, MOD - 2);
cout << (ansf) % MOD << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 105;
long long m;
struct node {
long long data[N][N];
node operator*(node b) {
node t;
for (long long i = 0; i <= m; i++) {
for (long long j = 0; j <= m; j++) {
t.data[i][j] = 0;
}
}
for (long long i = 0; i <= m; i++) {
for (long long j = 0; j <= m; j++) {
for (long long k = 0; k <= m; k++) {
t.data[i][j] = (t.data[i][j] + data[i][k] * b.data[k][j] % mod) % mod;
}
}
}
return t;
}
};
inline long long ksm(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long n, k, t, a[N];
inline long long f1(long long x) {
return (x * (n - 2 * m + x) % mod + mod) % mod;
}
inline long long f3(long long x) {
return ((m - x) * (m - x) % mod + mod) % mod;
}
inline long long f2(long long x) {
return ((n * (n - 1) / 2 - f1(x) - f3(x)) % mod + mod) % mod;
}
node ma, ans;
signed main() {
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (!a[i]) m++;
}
for (long long i = 1; i <= m; i++)
if (!a[i]) t++;
for (long long i = 0; i <= m; i++) {
if (i != 0) ma.data[i - 1][i] = f1(i);
ma.data[i][i] = f2(i);
if (i != m) ma.data[i + 1][i] = f3(i);
}
for (long long i = 0; i <= m; i++) ans.data[i][i] = 1;
while (k) {
if (k & 1) {
ans = ans * ma;
}
ma = ma * ma;
k >>= 1;
}
long long total = 0;
for (long long i = 0; i <= m; i++) {
total = (total + ans.data[i][t]) % mod;
}
printf("%lld\n", ans.data[m][t] * ksm(total, mod - 2) % mod);
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 105;
long long m;
struct node {
long long data[N][N];
node operator*(node b) {
node t;
for (long long i = 0; i <= m; i++) {
for (long long j = 0; j <= m; j++) {
t.data[i][j] = 0;
}
}
for (long long i = 0; i <= m; i++) {
for (long long j = 0; j <= m; j++) {
for (long long k = 0; k <= m; k++) {
t.data[i][j] = (t.data[i][j] + data[i][k] * b.data[k][j] % mod) % mod;
}
}
}
return t;
}
};
inline long long ksm(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long n, k, t, a[N];
inline long long f1(long long x) {
return (x * (n - 2 * m + x) % mod + mod) % mod;
}
inline long long f3(long long x) {
return ((m - x) * (m - x) % mod + mod) % mod;
}
inline long long f2(long long x) {
return ((n * (n - 1) / 2 - f1(x) - f3(x)) % mod + mod) % mod;
}
node ma, ans;
signed main() {
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (!a[i]) m++;
}
for (long long i = 1; i <= m; i++)
if (!a[i]) t++;
for (long long i = 0; i <= m; i++) {
if (i != 0) ma.data[i - 1][i] = f1(i);
ma.data[i][i] = f2(i);
if (i != m) ma.data[i + 1][i] = f3(i);
}
for (long long i = 0; i <= m; i++) ans.data[i][i] = 1;
while (k) {
if (k & 1) {
ans = ans * ma;
}
ma = ma * ma;
k >>= 1;
}
long long total = 0;
for (long long i = 0; i <= m; i++) {
total = (total + ans.data[i][t]) % mod;
}
printf("%lld\n", ans.data[m][t] * ksm(total, mod - 2) % mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 105;
const int mod = 1000000007;
struct Matrix {
long long int val[M][M];
} I;
Matrix mul(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) {
C.val[i][j] = 0;
for (int k = 0; k < M; k++) {
C.val[i][j] += A.val[i][k] * B.val[k][j] % mod;
C.val[i][j] %= mod;
}
}
return C;
}
Matrix poww(Matrix A, int b) {
Matrix tmp = I;
while (b) {
if (b & 1) {
tmp = mul(tmp, A);
}
b >>= 1;
A = mul(A, A);
}
return tmp;
}
long long int poww(long long int a, long long int b) {
if (!a) return 0;
long long int tmp = 1;
while (b) {
if (b & 1) {
tmp = tmp * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return tmp;
}
int main() {
for (int i = 0; i < M; i++) {
I.val[i][i] = 1;
}
long long int n, k;
scanf("%lld%lld", &n, &k);
int zero = 0;
int one = 0;
int start;
vector<int> arr;
for (int i = 0; i < n; i++) {
int first;
scanf("%d", &first);
arr.push_back(first);
if (first == 0) {
zero++;
} else {
one++;
}
}
for (int i = 0; i < zero; i++) {
if (arr[i] == 0) {
start++;
}
}
Matrix mat;
memset(mat.val, 0, sizeof(mat.val));
for (int i = 0; i <= zero; i++) {
long long int inc = (zero - i) * (zero - i);
long long int dec = i * (one - zero + i);
long long int same = n * (n - 1) / 2 - inc - dec;
mat.val[i][i] = same % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i < zero)
mat.val[i + 1][i] = inc % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i > 0)
mat.val[i - 1][i] = dec % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
;
}
mat = poww(mat, k);
cout << mat.val[zero][start] << endl;
}
| ### Prompt
Create a solution in Cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 105;
const int mod = 1000000007;
struct Matrix {
long long int val[M][M];
} I;
Matrix mul(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) {
C.val[i][j] = 0;
for (int k = 0; k < M; k++) {
C.val[i][j] += A.val[i][k] * B.val[k][j] % mod;
C.val[i][j] %= mod;
}
}
return C;
}
Matrix poww(Matrix A, int b) {
Matrix tmp = I;
while (b) {
if (b & 1) {
tmp = mul(tmp, A);
}
b >>= 1;
A = mul(A, A);
}
return tmp;
}
long long int poww(long long int a, long long int b) {
if (!a) return 0;
long long int tmp = 1;
while (b) {
if (b & 1) {
tmp = tmp * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return tmp;
}
int main() {
for (int i = 0; i < M; i++) {
I.val[i][i] = 1;
}
long long int n, k;
scanf("%lld%lld", &n, &k);
int zero = 0;
int one = 0;
int start;
vector<int> arr;
for (int i = 0; i < n; i++) {
int first;
scanf("%d", &first);
arr.push_back(first);
if (first == 0) {
zero++;
} else {
one++;
}
}
for (int i = 0; i < zero; i++) {
if (arr[i] == 0) {
start++;
}
}
Matrix mat;
memset(mat.val, 0, sizeof(mat.val));
for (int i = 0; i <= zero; i++) {
long long int inc = (zero - i) * (zero - i);
long long int dec = i * (one - zero + i);
long long int same = n * (n - 1) / 2 - inc - dec;
mat.val[i][i] = same % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i < zero)
mat.val[i + 1][i] = inc % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
if (i > 0)
mat.val[i - 1][i] = dec % mod * poww(n * (n - 1) / 2, mod - 2) % mod;
;
}
mat = poww(mat, k);
cout << mat.val[zero][start] << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline bool isSquare(T x) {
T y = sqrt(x + 0.5);
return (y * y) == x;
}
template <class T1, class T2>
inline T1 gcd(T1 a, T2 b) {
return b ? gcd(b, a % b) : a;
}
template <class T1, class T2>
inline T1 eqMin(T1 &x, const T2 &y) {
if (T1(y) < x) return x = y;
return x;
}
template <class T1, class T2>
inline T1 eqMax(T1 &x, const T2 &y) {
if (T1(y) > x) return x = y;
return x;
}
template <class T1, class T2>
inline T1 min(const T1 &x, const T2 &y) {
return x < (T1)y ? x : (T1)y;
}
template <class T1, class T2>
inline T1 max(T1 &x, const T2 &y) {
return x > (T1)y ? x : (T1)y;
}
template <typename T>
inline T getint() {
T x = 0, p = 1;
char ch;
do {
ch = getchar();
} while (ch <= ' ');
if (ch == '-') p = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * p;
}
struct _flag_t {
string val;
} const _1d{", "}, _2d{"\n "};
_flag_t _flag = _1d;
ostream &operator<<(ostream &os, _flag_t flag) {
_flag = flag;
return os;
}
template <class It>
ostream &_out(ostream &os, It f, It l) {
if (f == l) return os << "{}";
_flag_t cur_flag = _flag;
os << _1d << "{ " << *f;
for (; ++f != l; os << cur_flag.val << *f)
;
return os << " }";
}
template <class C>
auto operator<<(ostream &os, C const &cont)
-> decltype(begin(cont), end(cont), cont.size(), declval<ostream &>()) {
return _out(os, begin(cont), end(cont));
}
ostream &operator<<(ostream &os, string const &s) { return os << s.data(); }
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
return os << "[" << p.first << ", " << p.second << "]";
}
const double PI = acos(-1);
const double EPS = 1e-8;
const int INF = (int)2e9;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)200;
struct KArs {
vector<vector<long long>> dp;
int n;
KArs() {}
KArs(int _n) {
n = _n;
dp = vector<vector<long long>>(n + 1, vector<long long>(n + 1, 0));
}
KArs operator*(const KArs &o) const {
KArs res(n);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
res.dp[i][k] += dp[i][j] * o.dp[j][k];
res.dp[i][k] %= MOD;
}
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
res.dp[i][j] %= MOD;
}
}
return res;
}
};
int n, k;
int arr[MAXN];
int zeros, ones, Z;
KArs mat;
void input() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
zeros += arr[i] == 0;
ones += arr[i] == 1;
}
for (int i = 1; i <= zeros; i++) {
Z += arr[i] == 0;
}
}
KArs power(int step) {
KArs res(mat.n);
KArs x = mat;
for (int i = 0; i <= mat.n; i++) {
res.dp[i][i] = 1;
}
for (; step; x = x * x, step >>= 1) {
if (step & 1) res = res * x;
}
return res;
}
long long f(long long x) { return x * (x - 1) / 2; }
void init() {
mat = KArs(zeros);
for (long long prefZ = 0; prefZ <= zeros; prefZ++) {
long long prefO = zeros - prefZ;
long long sufZ = zeros - prefZ;
long long sufO = ones - prefO;
mat.dp[prefZ][prefZ] = (prefZ * sufZ + prefO * sufO + f(prefZ) + f(sufZ) +
f(prefO) + f(sufO) + prefZ * prefO + sufZ * sufO) %
MOD;
if (prefZ > 0) mat.dp[prefZ][prefZ - 1] = prefZ * sufO;
if (prefZ != zeros) {
mat.dp[prefZ][prefZ + 1] = prefO * sufZ;
}
}
}
long long power(long long x, long long step) {
long long res = 1;
for (; step; step >>= 1, x = (x * x) % MOD) {
if (step & 1) {
res = (res * x) % MOD;
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
input();
init();
KArs a = power(k);
long long x = 0, y = 0;
for (int i = 0; i <= zeros; i++) {
y += a.dp[Z][i];
}
y %= MOD;
x = a.dp[Z][zeros];
cout << (x * power(y, MOD - 2)) % MOD;
}
| ### Prompt
Create a solution in cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline bool isSquare(T x) {
T y = sqrt(x + 0.5);
return (y * y) == x;
}
template <class T1, class T2>
inline T1 gcd(T1 a, T2 b) {
return b ? gcd(b, a % b) : a;
}
template <class T1, class T2>
inline T1 eqMin(T1 &x, const T2 &y) {
if (T1(y) < x) return x = y;
return x;
}
template <class T1, class T2>
inline T1 eqMax(T1 &x, const T2 &y) {
if (T1(y) > x) return x = y;
return x;
}
template <class T1, class T2>
inline T1 min(const T1 &x, const T2 &y) {
return x < (T1)y ? x : (T1)y;
}
template <class T1, class T2>
inline T1 max(T1 &x, const T2 &y) {
return x > (T1)y ? x : (T1)y;
}
template <typename T>
inline T getint() {
T x = 0, p = 1;
char ch;
do {
ch = getchar();
} while (ch <= ' ');
if (ch == '-') p = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * p;
}
struct _flag_t {
string val;
} const _1d{", "}, _2d{"\n "};
_flag_t _flag = _1d;
ostream &operator<<(ostream &os, _flag_t flag) {
_flag = flag;
return os;
}
template <class It>
ostream &_out(ostream &os, It f, It l) {
if (f == l) return os << "{}";
_flag_t cur_flag = _flag;
os << _1d << "{ " << *f;
for (; ++f != l; os << cur_flag.val << *f)
;
return os << " }";
}
template <class C>
auto operator<<(ostream &os, C const &cont)
-> decltype(begin(cont), end(cont), cont.size(), declval<ostream &>()) {
return _out(os, begin(cont), end(cont));
}
ostream &operator<<(ostream &os, string const &s) { return os << s.data(); }
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
return os << "[" << p.first << ", " << p.second << "]";
}
const double PI = acos(-1);
const double EPS = 1e-8;
const int INF = (int)2e9;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)200;
struct KArs {
vector<vector<long long>> dp;
int n;
KArs() {}
KArs(int _n) {
n = _n;
dp = vector<vector<long long>>(n + 1, vector<long long>(n + 1, 0));
}
KArs operator*(const KArs &o) const {
KArs res(n);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
res.dp[i][k] += dp[i][j] * o.dp[j][k];
res.dp[i][k] %= MOD;
}
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
res.dp[i][j] %= MOD;
}
}
return res;
}
};
int n, k;
int arr[MAXN];
int zeros, ones, Z;
KArs mat;
void input() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
zeros += arr[i] == 0;
ones += arr[i] == 1;
}
for (int i = 1; i <= zeros; i++) {
Z += arr[i] == 0;
}
}
KArs power(int step) {
KArs res(mat.n);
KArs x = mat;
for (int i = 0; i <= mat.n; i++) {
res.dp[i][i] = 1;
}
for (; step; x = x * x, step >>= 1) {
if (step & 1) res = res * x;
}
return res;
}
long long f(long long x) { return x * (x - 1) / 2; }
void init() {
mat = KArs(zeros);
for (long long prefZ = 0; prefZ <= zeros; prefZ++) {
long long prefO = zeros - prefZ;
long long sufZ = zeros - prefZ;
long long sufO = ones - prefO;
mat.dp[prefZ][prefZ] = (prefZ * sufZ + prefO * sufO + f(prefZ) + f(sufZ) +
f(prefO) + f(sufO) + prefZ * prefO + sufZ * sufO) %
MOD;
if (prefZ > 0) mat.dp[prefZ][prefZ - 1] = prefZ * sufO;
if (prefZ != zeros) {
mat.dp[prefZ][prefZ + 1] = prefO * sufZ;
}
}
}
long long power(long long x, long long step) {
long long res = 1;
for (; step; step >>= 1, x = (x * x) % MOD) {
if (step & 1) {
res = (res * x) % MOD;
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
input();
init();
KArs a = power(k);
long long x = 0, y = 0;
for (int i = 0; i <= zeros; i++) {
y += a.dp[Z][i];
}
y %= MOD;
x = a.dp[Z][zeros];
cout << (x * power(y, MOD - 2)) % MOD;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int n, m, t;
int a[105];
long long cnt[2];
long long inv2;
long long inv;
struct matrix {
long long f[51][51];
matrix() { memset(f, 0, sizeof(f)); };
};
matrix e;
matrix f;
matrix h;
matrix add(matrix x, matrix y) {
int i, j;
matrix ret;
for (i = 0; i <= 50; i++) {
for (j = 0; j <= 50; j++) {
ret.f[i][j] = (x.f[i][j] + y.f[i][j]) % mod;
}
}
return ret;
}
matrix mul(matrix x, matrix y) {
int i, j, k;
matrix ret;
for (i = 0; i <= 50; i++) {
for (j = 0; j <= 50; j++) {
for (k = 0; k <= 50; k++) {
ret.f[i][j] = (ret.f[i][j] + x.f[i][k] * y.f[k][j]) % mod;
}
}
}
return ret;
}
matrix qpow(matrix b, long long x) {
matrix ret = e;
while (x) {
if (x & 1) {
ret = mul(ret, b);
}
b = mul(b, b);
x >>= 1;
}
return ret;
}
long long qpow(long long b, long long x) {
long long ret = 1;
while (x) {
if (x & 1) {
ret = ret * b % mod;
}
b = b * b % mod;
x >>= 1;
}
return ret;
}
long long getc(long long x) {
if (x <= 0) return 0;
return x * (x - 1) % mod * inv2 % mod;
}
int read() {
int x;
scanf("%d", &x);
return x;
}
int main() {
long long i, j;
long long x, y;
inv2 = qpow(2, mod - 2);
for (i = 0; i <= 50; i++) {
e.f[i][i] = 1;
}
n = read();
m = read();
for (i = 1; i <= n; i++) {
a[i] = read();
cnt[a[i]]++;
}
inv = qpow(getc(n), mod - 2);
x = min(cnt[0], cnt[1]);
y = 0;
for (i = 1; i <= cnt[0]; i++) {
if (a[i] != 0) y++;
}
for (i = 0; i <= x; i++) {
if (i - 1 >= 0) h.f[i][i - 1] = 1ll * i * i * inv % mod;
h.f[i][i] =
1ll *
(getc(cnt[0]) + getc(cnt[1]) + i * (cnt[0] - i) + i * (cnt[1] - i)) *
inv % mod;
if (i + 1 <= x)
h.f[i][i + 1] = 1ll * (cnt[0] - i) * (cnt[1] - i) * inv % mod;
}
f.f[0][y] = 1;
printf("%lld\n", mul(f, qpow(h, m)).f[0][0]);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int n, m, t;
int a[105];
long long cnt[2];
long long inv2;
long long inv;
struct matrix {
long long f[51][51];
matrix() { memset(f, 0, sizeof(f)); };
};
matrix e;
matrix f;
matrix h;
matrix add(matrix x, matrix y) {
int i, j;
matrix ret;
for (i = 0; i <= 50; i++) {
for (j = 0; j <= 50; j++) {
ret.f[i][j] = (x.f[i][j] + y.f[i][j]) % mod;
}
}
return ret;
}
matrix mul(matrix x, matrix y) {
int i, j, k;
matrix ret;
for (i = 0; i <= 50; i++) {
for (j = 0; j <= 50; j++) {
for (k = 0; k <= 50; k++) {
ret.f[i][j] = (ret.f[i][j] + x.f[i][k] * y.f[k][j]) % mod;
}
}
}
return ret;
}
matrix qpow(matrix b, long long x) {
matrix ret = e;
while (x) {
if (x & 1) {
ret = mul(ret, b);
}
b = mul(b, b);
x >>= 1;
}
return ret;
}
long long qpow(long long b, long long x) {
long long ret = 1;
while (x) {
if (x & 1) {
ret = ret * b % mod;
}
b = b * b % mod;
x >>= 1;
}
return ret;
}
long long getc(long long x) {
if (x <= 0) return 0;
return x * (x - 1) % mod * inv2 % mod;
}
int read() {
int x;
scanf("%d", &x);
return x;
}
int main() {
long long i, j;
long long x, y;
inv2 = qpow(2, mod - 2);
for (i = 0; i <= 50; i++) {
e.f[i][i] = 1;
}
n = read();
m = read();
for (i = 1; i <= n; i++) {
a[i] = read();
cnt[a[i]]++;
}
inv = qpow(getc(n), mod - 2);
x = min(cnt[0], cnt[1]);
y = 0;
for (i = 1; i <= cnt[0]; i++) {
if (a[i] != 0) y++;
}
for (i = 0; i <= x; i++) {
if (i - 1 >= 0) h.f[i][i - 1] = 1ll * i * i * inv % mod;
h.f[i][i] =
1ll *
(getc(cnt[0]) + getc(cnt[1]) + i * (cnt[0] - i) + i * (cnt[1] - i)) *
inv % mod;
if (i + 1 <= x)
h.f[i][i + 1] = 1ll * (cnt[0] - i) * (cnt[1] - i) * inv % mod;
}
f.f[0][y] = 1;
printf("%lld\n", mul(f, qpow(h, m)).f[0][0]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < 2 * MOD)
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
| ### Prompt
Create a solution in cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < 2 * MOD)
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5, MOD = 1e9 + 7;
long long fact[N], fact_inv[N];
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 power(long long a, long long b) {
if (!b) return 1;
long long r = power(a, b / 2);
r = mul(r, r);
if (b & 1) return mul(r, a);
return r;
}
long long mod_inv(long long x) { return power(x, MOD - 2); }
long long nCr(long long n, long long r) {
if (!r) return 1;
if (r > n) return 0;
return mul(fact[n], mul(fact_inv[n - r], fact_inv[r]));
}
void pre() {
fact[0] = fact_inv[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = mul(fact[i - 1], i);
fact_inv[i] = mod_inv(fact[i]);
}
}
using row = vector<long long>;
using mat = vector<row>;
mat operator*(const mat &a, const mat &b) {
mat ret = mat(a.size(), row(b[0].size()));
for (int i = 0; i < ret.size(); i++)
for (int j = 0; j < ret[i].size(); j++)
for (int k = 0; k < ret[i].size(); k++)
ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
mat operator^(mat a, long long b) {
mat ans(a.size(), row(a[0].size()));
for (int i = 0; i < a.size(); i++) ans[i][i] = 1;
for (; b; b >>= 1, a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
int pairs(int n) { return mul(n, mul(n - 1, mod_inv(2))); }
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
pre();
int n, k;
cin >> n >> k;
int cnt[2] = {};
vector<int> A(n);
for (auto &a : A) {
cin >> a;
cnt[a]++;
}
int all = mod_inv(pairs(n));
mat p(cnt[1] + 1, row(cnt[1] + 1, 0));
for (int good1 = 0; good1 <= cnt[1]; good1++) {
int bad1 = cnt[1] - good1;
int bad0 = bad1;
int good0 = cnt[0] - bad0;
int same = add(pairs(cnt[1]), pairs(cnt[0]));
same = add(same, add(mul(good1, bad0), mul(bad1, good0)));
int plus = mul(bad1, bad0);
int minus = mul(good1, good0);
p[good1][good1] = mul(same, all);
if (good1) p[good1][good1 - 1] = mul(minus, all);
if (good1 + 1 <= cnt[1]) p[good1][good1 + 1] = mul(plus, all);
}
auto B = A;
sort(B.begin(), B.end());
int good = 0;
for (int i = 0; i < n; i++) good += A[i] == B[i] && B[i] == 1;
p = p ^ k;
cout << p[good][cnt[1]];
}
| ### Prompt
Create a solution in Cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5, MOD = 1e9 + 7;
long long fact[N], fact_inv[N];
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 power(long long a, long long b) {
if (!b) return 1;
long long r = power(a, b / 2);
r = mul(r, r);
if (b & 1) return mul(r, a);
return r;
}
long long mod_inv(long long x) { return power(x, MOD - 2); }
long long nCr(long long n, long long r) {
if (!r) return 1;
if (r > n) return 0;
return mul(fact[n], mul(fact_inv[n - r], fact_inv[r]));
}
void pre() {
fact[0] = fact_inv[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = mul(fact[i - 1], i);
fact_inv[i] = mod_inv(fact[i]);
}
}
using row = vector<long long>;
using mat = vector<row>;
mat operator*(const mat &a, const mat &b) {
mat ret = mat(a.size(), row(b[0].size()));
for (int i = 0; i < ret.size(); i++)
for (int j = 0; j < ret[i].size(); j++)
for (int k = 0; k < ret[i].size(); k++)
ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
mat operator^(mat a, long long b) {
mat ans(a.size(), row(a[0].size()));
for (int i = 0; i < a.size(); i++) ans[i][i] = 1;
for (; b; b >>= 1, a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
int pairs(int n) { return mul(n, mul(n - 1, mod_inv(2))); }
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
pre();
int n, k;
cin >> n >> k;
int cnt[2] = {};
vector<int> A(n);
for (auto &a : A) {
cin >> a;
cnt[a]++;
}
int all = mod_inv(pairs(n));
mat p(cnt[1] + 1, row(cnt[1] + 1, 0));
for (int good1 = 0; good1 <= cnt[1]; good1++) {
int bad1 = cnt[1] - good1;
int bad0 = bad1;
int good0 = cnt[0] - bad0;
int same = add(pairs(cnt[1]), pairs(cnt[0]));
same = add(same, add(mul(good1, bad0), mul(bad1, good0)));
int plus = mul(bad1, bad0);
int minus = mul(good1, good0);
p[good1][good1] = mul(same, all);
if (good1) p[good1][good1 - 1] = mul(minus, all);
if (good1 + 1 <= cnt[1]) p[good1][good1 + 1] = mul(plus, all);
}
auto B = A;
sort(B.begin(), B.end());
int good = 0;
for (int i = 0; i < n; i++) good += A[i] == B[i] && B[i] == 1;
p = p ^ k;
cout << p[good][cnt[1]];
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
const long long MAX_N = 105;
struct Matrix {
long long mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++)
for (ans.mat[i][j] = k = 0; k < MAX_N; k++) {
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
ans.mat[i][j] %= MOD;
}
return ans;
}
Matrix matPow(Matrix base, long long p) {
p %= MOD;
Matrix ans;
long long i, j;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int fpow(int a, int p) {
if (p == 0) return 1;
long long z = fpow(a, p / 2);
z = (z * z) % MOD;
if (p % 2) z = (z * a) % MOD;
return z;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
reverse(v.begin(), v.end());
long long tot = 0;
for (int i = 0; i < n; ++i) {
if (v[i]) tot++;
}
long long cnt = 0;
for (int i = 0; i < tot; ++i) {
if (v[i]) cnt++;
}
Matrix m;
for (int i = 0; i < tot + 1; ++i) {
for (int j = 0; j < tot + 1; ++j) {
if (i + n - tot < tot) {
m.mat[i][j] = 0;
continue;
}
if (abs(i - j) > 1)
m.mat[i][j] = 0;
else if (i == j) {
m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 +
i * (tot - i) + (tot - i) * (n - 2 * tot + i);
} else if (i - j == 1) {
m.mat[i][j] = (i) * (n - 2 * tot + i);
} else if (j - i == 1) {
m.mat[i][j] = (tot - i) * (tot - i);
}
m.mat[i][j] %= MOD;
}
}
Matrix ans = matPow(m, k);
long long a1 = 0, a2 = 0;
a1 = ans.mat[cnt][tot];
for (int i = 0; i < tot + 1; ++i) {
a2 += ans.mat[cnt][i];
a2 %= MOD;
}
long long ansf = 0;
ansf = a1;
ansf *= fpow(a2, MOD - 2);
cout << (ansf) % MOD << '\n';
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
const long long MAX_N = 105;
struct Matrix {
long long mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++)
for (ans.mat[i][j] = k = 0; k < MAX_N; k++) {
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
ans.mat[i][j] %= MOD;
}
return ans;
}
Matrix matPow(Matrix base, long long p) {
p %= MOD;
Matrix ans;
long long i, j;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int fpow(int a, int p) {
if (p == 0) return 1;
long long z = fpow(a, p / 2);
z = (z * z) % MOD;
if (p % 2) z = (z * a) % MOD;
return z;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
reverse(v.begin(), v.end());
long long tot = 0;
for (int i = 0; i < n; ++i) {
if (v[i]) tot++;
}
long long cnt = 0;
for (int i = 0; i < tot; ++i) {
if (v[i]) cnt++;
}
Matrix m;
for (int i = 0; i < tot + 1; ++i) {
for (int j = 0; j < tot + 1; ++j) {
if (i + n - tot < tot) {
m.mat[i][j] = 0;
continue;
}
if (abs(i - j) > 1)
m.mat[i][j] = 0;
else if (i == j) {
m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 +
i * (tot - i) + (tot - i) * (n - 2 * tot + i);
} else if (i - j == 1) {
m.mat[i][j] = (i) * (n - 2 * tot + i);
} else if (j - i == 1) {
m.mat[i][j] = (tot - i) * (tot - i);
}
m.mat[i][j] %= MOD;
}
}
Matrix ans = matPow(m, k);
long long a1 = 0, a2 = 0;
a1 = ans.mat[cnt][tot];
for (int i = 0; i < tot + 1; ++i) {
a2 += ans.mat[cnt][i];
a2 %= MOD;
}
long long ansf = 0;
ansf = a1;
ansf *= fpow(a2, MOD - 2);
cout << (ansf) % MOD << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mpow(long long x, long long y, long long m) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x % m;
y = y >> 1;
x = x * x % m;
}
return res;
}
long long sz;
const long long M = 100;
long long mat[100][100], fin[100][100];
void mult1() {
long long temp[sz][sz];
memset(temp, 0, sizeof(temp));
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) {
for (long long k = (0); k < (sz); k++)
temp[i][j] += mat[i][k] * mat[k][j] % 1000000007;
temp[i][j] %= 1000000007;
}
}
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) mat[i][j] = temp[i][j];
}
}
void mult2() {
long long temp[sz][sz];
memset(temp, 0, sizeof(temp));
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) {
for (long long k = (0); k < (sz); k++)
temp[i][j] += fin[i][k] * mat[k][j] % 1000000007;
temp[i][j] %= 1000000007;
}
}
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) fin[i][j] = temp[i][j];
}
}
void matexp(long long p) {
while (p != 0) {
if (p & 1) mult2();
mult1();
p >>= 1;
}
}
void solve() {
long long n, k;
cin >> n >> k;
long long a[n];
for (long long i = (0); i < (n); i++) cin >> a[i];
long long ones = 0;
for (long long i = (0); i < (n); i++) {
if (a[i] == 1) ones++;
}
long long w = ones <= n / 2 ? 0 : ones - (n - ones);
sz = ones - w + 1;
long long tcb = n * (n - 1) / 2;
for (long long i = (0); i < (sz); i++) {
mat[i][i] = tcb - w * (n + w - 2 * ones) - (ones - w) * (ones - w);
if (i != 0) mat[i][i - 1] = (ones - w + 1) * (ones - w + 1);
if (i != sz - 1) mat[i][i + 1] = (w + 1) * (n + w + 1 - 2 * ones);
w++;
}
for (long long i = (0); i < (sz); i++) fin[i][i] = 1;
matexp(k);
w = ones <= n / 2 ? 0 : ones - (n - ones);
long long x = 0;
for (long long i = (n - 1); i > (n - ones - 1); i--) {
if (a[i] == 1) x++;
}
long long num = fin[sz - 1][x - w], den = mpow(tcb, k, 1000000007);
cout << num * mpow(den, 1000000007 - 2, 1000000007) % 1000000007;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
for (long long i = (1); i < (t + 1); i++) {
solve();
cout << "\n";
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mpow(long long x, long long y, long long m) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x % m;
y = y >> 1;
x = x * x % m;
}
return res;
}
long long sz;
const long long M = 100;
long long mat[100][100], fin[100][100];
void mult1() {
long long temp[sz][sz];
memset(temp, 0, sizeof(temp));
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) {
for (long long k = (0); k < (sz); k++)
temp[i][j] += mat[i][k] * mat[k][j] % 1000000007;
temp[i][j] %= 1000000007;
}
}
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) mat[i][j] = temp[i][j];
}
}
void mult2() {
long long temp[sz][sz];
memset(temp, 0, sizeof(temp));
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) {
for (long long k = (0); k < (sz); k++)
temp[i][j] += fin[i][k] * mat[k][j] % 1000000007;
temp[i][j] %= 1000000007;
}
}
for (long long i = (0); i < (sz); i++) {
for (long long j = (0); j < (sz); j++) fin[i][j] = temp[i][j];
}
}
void matexp(long long p) {
while (p != 0) {
if (p & 1) mult2();
mult1();
p >>= 1;
}
}
void solve() {
long long n, k;
cin >> n >> k;
long long a[n];
for (long long i = (0); i < (n); i++) cin >> a[i];
long long ones = 0;
for (long long i = (0); i < (n); i++) {
if (a[i] == 1) ones++;
}
long long w = ones <= n / 2 ? 0 : ones - (n - ones);
sz = ones - w + 1;
long long tcb = n * (n - 1) / 2;
for (long long i = (0); i < (sz); i++) {
mat[i][i] = tcb - w * (n + w - 2 * ones) - (ones - w) * (ones - w);
if (i != 0) mat[i][i - 1] = (ones - w + 1) * (ones - w + 1);
if (i != sz - 1) mat[i][i + 1] = (w + 1) * (n + w + 1 - 2 * ones);
w++;
}
for (long long i = (0); i < (sz); i++) fin[i][i] = 1;
matexp(k);
w = ones <= n / 2 ? 0 : ones - (n - ones);
long long x = 0;
for (long long i = (n - 1); i > (n - ones - 1); i--) {
if (a[i] == 1) x++;
}
long long num = fin[sz - 1][x - w], den = mpow(tcb, k, 1000000007);
cout << num * mpow(den, 1000000007 - 2, 1000000007) % 1000000007;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
for (long long i = (1); i < (t + 1); i++) {
solve();
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
int n, k, m, t, a[105];
struct mat {
long long d[105][105];
};
mat ans, f, c;
long long fm, fm_ni;
const int p = 1e9 + 7;
inline long long ni(long long x) {
if (x <= 1) return x;
return (p - p / x) * ni(p % x) % p;
}
inline void mul(mat &a, mat &b) {
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
c.d[i][j] = 0;
for (int k = 0; k <= m; k++) c.d[i][j] += a.d[i][k] * b.d[k][j] % p;
c.d[i][j] %= p;
}
a = c;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[i] ^= 1;
if (a[i]) m++;
}
for (int i = 1; i <= m; i++)
if (a[i]) t++;
fm = n * (n - 1) / 2;
fm_ni = ni(fm);
for (int j = 0; j <= m; j++) {
if (j) f.d[j][j - 1] = (j * (n - m - (m - j))) * fm_ni % p;
f.d[j][j] = (m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + j * (m - j) +
(m - j) * (n - m - (m - j))) *
fm_ni % p;
if (j < m) f.d[j][j + 1] = ((m - j) * (m - j)) * fm_ni % p;
}
for (int i = 0; i <= m; i++) ans.d[i][i] = 1;
while (k) {
if (k & 1) mul(ans, f);
mul(f, f);
k >>= 1;
}
printf("%I64d\n", ans.d[t][m]);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
int n, k, m, t, a[105];
struct mat {
long long d[105][105];
};
mat ans, f, c;
long long fm, fm_ni;
const int p = 1e9 + 7;
inline long long ni(long long x) {
if (x <= 1) return x;
return (p - p / x) * ni(p % x) % p;
}
inline void mul(mat &a, mat &b) {
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
c.d[i][j] = 0;
for (int k = 0; k <= m; k++) c.d[i][j] += a.d[i][k] * b.d[k][j] % p;
c.d[i][j] %= p;
}
a = c;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[i] ^= 1;
if (a[i]) m++;
}
for (int i = 1; i <= m; i++)
if (a[i]) t++;
fm = n * (n - 1) / 2;
fm_ni = ni(fm);
for (int j = 0; j <= m; j++) {
if (j) f.d[j][j - 1] = (j * (n - m - (m - j))) * fm_ni % p;
f.d[j][j] = (m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + j * (m - j) +
(m - j) * (n - m - (m - j))) *
fm_ni % p;
if (j < m) f.d[j][j + 1] = ((m - j) * (m - j)) * fm_ni % p;
}
for (int i = 0; i <= m; i++) ans.d[i][i] = 1;
while (k) {
if (k & 1) mul(ans, f);
mul(f, f);
k >>= 1;
}
printf("%I64d\n", ans.d[t][m]);
return 0;
}
``` |
#include <bits/stdc++.h>
inline void read(long long &x) {
short negative = 1;
x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') negative = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= negative;
}
inline void print(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
class Matrix {
private:
long long n, m;
public:
long long a[105][105];
inline Matrix() { memset(a, 0, sizeof(a)); }
inline Matrix(long long x, long long y) {
n = x, m = y;
memset(a, 0, sizeof(a));
}
inline Matrix(long long x, long long y, long long arr[105][105]) {
n = x, m = y;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) a[i][j] = arr[i][j];
}
inline Matrix(long long x) {
this->n = x;
this->m = x;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++)
if (i == j)
a[i][j] = 1;
else
a[i][j] = 0;
}
Matrix operator+(const Matrix &b) const {
Matrix res(n, m);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++)
res.a[i][j] = (this->a[i][j] + b.a[i][j]) % 1000000007;
return res;
}
Matrix operator-(const Matrix &b) const {
Matrix res(n, m);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++)
res.a[i][j] = (this->a[i][j] + 1000000007 - b.a[i][j]) % 1000000007;
return res;
}
Matrix operator*(const Matrix &b) const {
Matrix res(this->n, b.m);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++)
for (long long k = 0; k < m; k++)
res.a[i][j] =
(res.a[i][j] + (this->a[i][k] * b.a[k][j]) % 1000000007) %
1000000007;
return res;
}
Matrix operator=(const Matrix &b) {
this->n = b.n;
this->m = b.m;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) this->a[i][j] = b.a[i][j];
return *this;
}
Matrix operator^(long long x) {
Matrix a(*this);
Matrix ans(this->n);
while (x) {
if (x % 2) ans = ans * a;
a = a * a;
x /= 2;
}
return ans;
}
inline void show() {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) print(this->a[i][j]), putchar(' ');
putchar('\n');
}
}
};
inline long long ksm(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1) ret = ret * x % 1000000007;
x = x * x % 1000000007;
y >>= 1;
}
return ret;
}
inline long long inv(long long x) { return ksm(x, 1000000007 - 2); }
long long n, k, i, one, zero, start, aim, res, a[105];
signed main(void) {
read(n), read(k);
for (i = 0; i < n; i++) {
read(a[i]);
if (a[i])
one++;
else
zero++;
}
for (i = 0; i < zero; i++)
if (!a[i]) start++;
aim = zero;
Matrix mat(zero + 1, zero + 1);
for (i = 0; i <= zero; i++) {
if (one - zero + i >= 0)
mat.a[i][i] = zero * (zero - 1) / 2 + one * (one - 1) / 2 +
i * (zero - i) + (zero - i) * (one - zero + i);
if (i && one - zero + i >= 0) mat.a[i][i - 1] = i * (one - zero + i);
if (i + 1 <= zero) mat.a[i][i + 1] = (zero - i) * (zero - i);
}
mat = mat ^ k;
res = mat.a[start][aim] * ksm(2, k) % 1000000007 * ksm(inv(n * (n - 1)), k) %
1000000007;
print(res);
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
inline void read(long long &x) {
short negative = 1;
x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') negative = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= negative;
}
inline void print(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
class Matrix {
private:
long long n, m;
public:
long long a[105][105];
inline Matrix() { memset(a, 0, sizeof(a)); }
inline Matrix(long long x, long long y) {
n = x, m = y;
memset(a, 0, sizeof(a));
}
inline Matrix(long long x, long long y, long long arr[105][105]) {
n = x, m = y;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) a[i][j] = arr[i][j];
}
inline Matrix(long long x) {
this->n = x;
this->m = x;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++)
if (i == j)
a[i][j] = 1;
else
a[i][j] = 0;
}
Matrix operator+(const Matrix &b) const {
Matrix res(n, m);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++)
res.a[i][j] = (this->a[i][j] + b.a[i][j]) % 1000000007;
return res;
}
Matrix operator-(const Matrix &b) const {
Matrix res(n, m);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++)
res.a[i][j] = (this->a[i][j] + 1000000007 - b.a[i][j]) % 1000000007;
return res;
}
Matrix operator*(const Matrix &b) const {
Matrix res(this->n, b.m);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++)
for (long long k = 0; k < m; k++)
res.a[i][j] =
(res.a[i][j] + (this->a[i][k] * b.a[k][j]) % 1000000007) %
1000000007;
return res;
}
Matrix operator=(const Matrix &b) {
this->n = b.n;
this->m = b.m;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) this->a[i][j] = b.a[i][j];
return *this;
}
Matrix operator^(long long x) {
Matrix a(*this);
Matrix ans(this->n);
while (x) {
if (x % 2) ans = ans * a;
a = a * a;
x /= 2;
}
return ans;
}
inline void show() {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) print(this->a[i][j]), putchar(' ');
putchar('\n');
}
}
};
inline long long ksm(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1) ret = ret * x % 1000000007;
x = x * x % 1000000007;
y >>= 1;
}
return ret;
}
inline long long inv(long long x) { return ksm(x, 1000000007 - 2); }
long long n, k, i, one, zero, start, aim, res, a[105];
signed main(void) {
read(n), read(k);
for (i = 0; i < n; i++) {
read(a[i]);
if (a[i])
one++;
else
zero++;
}
for (i = 0; i < zero; i++)
if (!a[i]) start++;
aim = zero;
Matrix mat(zero + 1, zero + 1);
for (i = 0; i <= zero; i++) {
if (one - zero + i >= 0)
mat.a[i][i] = zero * (zero - 1) / 2 + one * (one - 1) / 2 +
i * (zero - i) + (zero - i) * (one - zero + i);
if (i && one - zero + i >= 0) mat.a[i][i - 1] = i * (one - zero + i);
if (i + 1 <= zero) mat.a[i][i + 1] = (zero - i) * (zero - i);
}
mat = mat ^ k;
res = mat.a[start][aim] * ksm(2, k) % 1000000007 * ksm(inv(n * (n - 1)), k) %
1000000007;
print(res);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 100;
const long long M = 1e4;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long read() {
long long s = 0;
register bool neg = 0;
register char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) neg |= (c == '-');
for (; c >= '0' && c <= '9'; s = s * 10 + (c ^ 48), c = getchar())
;
s = (neg ? -s : s);
return s;
}
long long a, b, inv[N + 5], s[N + 5];
struct mat {
long long s[105][105], n, m;
mat(long long _n = 0, long long _m = 0) {
n = _n, m = _m, memset(s, 0, sizeof(s));
}
mat operator*(const mat &o) const {
mat res(n, o.m);
for (long long i = (0); i <= (n); ++i)
for (long long j = (0); j <= (o.m); ++j)
for (long long k = (0); k <= (m); ++k)
(res.s[i][j] += s[i][k] * o.s[k][j] % mod) %= mod;
return res;
}
void print() {
for (long long i = 0; i <= n; ++i, puts(""))
for (long long j = (0); j <= (m); ++j) printf("%lld ", s[i][j]);
}
} S;
mat operator^(mat n, long long m) {
mat res = n;
--m;
for (; m; m >>= 1) {
if (m & 1) res = res * n;
n = n * n;
}
return res;
}
void init() {
inv[0] = inv[1] = 1;
for (long long i = (2); i <= (N); ++i)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
signed main() {
init();
a = read();
b = read();
long long p[2] = {0, 0}, q = 0;
for (long long i = (1); i <= (a); ++i) s[i] = read(), p[s[i]]++;
S.n = p[0];
S.m = p[0];
for (long long i = (1); i <= (p[0]); ++i) q += s[i];
long long tmp = inv[a] % mod * inv[a - 1] * 2 % mod;
for (long long i = (0); i <= (min(p[1], p[0])); ++i) {
long long sum = 0;
if (i) S.s[i][i - 1] = i * i * tmp % mod, (sum += S.s[i][i - 1]) %= mod;
if (i != p[0])
S.s[i][i + 1] = (p[0] - i) * (p[1] - i) * tmp % mod,
(sum += S.s[i][i + 1]) %= mod;
S.s[i][i] = (1 - sum + mod) % mod;
}
S = S ^ b;
printf("%lld", S.s[q][0]);
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 100;
const long long M = 1e4;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long read() {
long long s = 0;
register bool neg = 0;
register char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) neg |= (c == '-');
for (; c >= '0' && c <= '9'; s = s * 10 + (c ^ 48), c = getchar())
;
s = (neg ? -s : s);
return s;
}
long long a, b, inv[N + 5], s[N + 5];
struct mat {
long long s[105][105], n, m;
mat(long long _n = 0, long long _m = 0) {
n = _n, m = _m, memset(s, 0, sizeof(s));
}
mat operator*(const mat &o) const {
mat res(n, o.m);
for (long long i = (0); i <= (n); ++i)
for (long long j = (0); j <= (o.m); ++j)
for (long long k = (0); k <= (m); ++k)
(res.s[i][j] += s[i][k] * o.s[k][j] % mod) %= mod;
return res;
}
void print() {
for (long long i = 0; i <= n; ++i, puts(""))
for (long long j = (0); j <= (m); ++j) printf("%lld ", s[i][j]);
}
} S;
mat operator^(mat n, long long m) {
mat res = n;
--m;
for (; m; m >>= 1) {
if (m & 1) res = res * n;
n = n * n;
}
return res;
}
void init() {
inv[0] = inv[1] = 1;
for (long long i = (2); i <= (N); ++i)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
signed main() {
init();
a = read();
b = read();
long long p[2] = {0, 0}, q = 0;
for (long long i = (1); i <= (a); ++i) s[i] = read(), p[s[i]]++;
S.n = p[0];
S.m = p[0];
for (long long i = (1); i <= (p[0]); ++i) q += s[i];
long long tmp = inv[a] % mod * inv[a - 1] * 2 % mod;
for (long long i = (0); i <= (min(p[1], p[0])); ++i) {
long long sum = 0;
if (i) S.s[i][i - 1] = i * i * tmp % mod, (sum += S.s[i][i - 1]) %= mod;
if (i != p[0])
S.s[i][i + 1] = (p[0] - i) * (p[1] - i) * tmp % mod,
(sum += S.s[i][i + 1]) %= mod;
S.s[i][i] = (1 - sum + mod) % mod;
}
S = S ^ b;
printf("%lld", S.s[q][0]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 10;
const long long LNF = 2e18;
int n, k, A[110], st, C[2];
class matrix {
public:
int X[101][101] = {};
matrix operator*(matrix &P) {
matrix R;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++)
R.X[i][k] = (R.X[i][k] + 1LL * X[i][j] * P.X[j][k]) % MOD;
return R;
}
matrix operator^(int e) {
if (e == 1) return *this;
matrix T = *this ^ e / 2;
T = T * T;
return (e % 2 ? T * *this : T);
}
} T;
long long pw(long long a, int e) {
if (e == 0) return 1;
long long t = pw(a, e / 2);
t = t * t % MOD;
return (e % 2 ? t * a % MOD : t);
}
long long inv(long long x) { return pw(x % MOD, MOD - 2); }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> A[i], C[A[i]]++;
for (int i = 1; i <= C[0]; i++) st += A[i] == 0;
long long s = n * (n - 1) / 2, u = inv(s);
for (int i = 0; i <= C[0]; i++) {
long long p = i * (C[1] - (C[0] - i)), r = (C[0] - i) * (C[0] - i),
q = s - p - r;
if (i > 0) T.X[i - 1][i] = p * u % MOD;
if (i < C[0]) T.X[i + 1][i] = r * u % MOD;
T.X[i][i] = q * u % MOD;
}
matrix R = T ^ k;
cout << R.X[C[0]][st] << '\n';
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 10;
const long long LNF = 2e18;
int n, k, A[110], st, C[2];
class matrix {
public:
int X[101][101] = {};
matrix operator*(matrix &P) {
matrix R;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++)
R.X[i][k] = (R.X[i][k] + 1LL * X[i][j] * P.X[j][k]) % MOD;
return R;
}
matrix operator^(int e) {
if (e == 1) return *this;
matrix T = *this ^ e / 2;
T = T * T;
return (e % 2 ? T * *this : T);
}
} T;
long long pw(long long a, int e) {
if (e == 0) return 1;
long long t = pw(a, e / 2);
t = t * t % MOD;
return (e % 2 ? t * a % MOD : t);
}
long long inv(long long x) { return pw(x % MOD, MOD - 2); }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> A[i], C[A[i]]++;
for (int i = 1; i <= C[0]; i++) st += A[i] == 0;
long long s = n * (n - 1) / 2, u = inv(s);
for (int i = 0; i <= C[0]; i++) {
long long p = i * (C[1] - (C[0] - i)), r = (C[0] - i) * (C[0] - i),
q = s - p - r;
if (i > 0) T.X[i - 1][i] = p * u % MOD;
if (i < C[0]) T.X[i + 1][i] = r * u % MOD;
T.X[i][i] = q * u % MOD;
}
matrix R = T ^ k;
cout << R.X[C[0]][st] << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
int n, i, j, k, z, o;
bool b[105];
long long anti;
struct matrix {
int n, m;
long long mt[55][55];
} a;
matrix operator*(matrix a, matrix b) {
int i, j, k;
matrix ans;
for (i = 0; i < a.n; i++)
for (j = 0; j < b.m; j++)
for (k = ans.mt[i][j] = 0; k < a.m; k++)
ans.mt[i][j] = (ans.mt[i][j] + a.mt[i][k] * b.mt[k][j]) % 1000000007LL;
ans.n = a.n;
ans.m = b.m;
return ans;
}
long long pow(long long d, long long x) {
if (x == 0) return 1;
long long ret = pow(d, x >> 1);
ret = ret * ret % 1000000007LL;
if (x & 1) ret = ret * d % 1000000007LL;
return ret;
}
matrix pow(int x) {
if (x == 1) return a;
matrix ret = pow(x >> 1);
ret = ret * ret;
if (x & 1) ret = ret * a;
return ret;
}
inline long long min(long long a, long long b) { return a < b ? a : b; }
int main() {
for (scanf("%d%d", &n, &k), i = 0; i < n; i++) {
scanf("%d", &j);
b[i] = (j ? true : false);
if (b[i])
o++;
else
z++;
}
anti = pow(n * (n - 1), 1000000007LL - 2);
a.m = a.n = min(z, o) + 1;
for (i = 0; i <= min(z, o); i++) {
if (i != 0) a.mt[i - 1][i] = 2 * i * i * anti % 1000000007LL;
a.mt[i][i] =
(o * o + z * z - n + 2 * i * n - 4 * i * i) * anti % 1000000007LL;
if (i != min(z, o))
a.mt[i + 1][i] = 2 * (o - i) * (z - i) * anti % 1000000007LL;
}
matrix ggg = pow(k);
for (i = j = 0; i < z; i++)
if (b[i]) j++;
printf("%I64d\n", ggg.mt[0][j]);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
int n, i, j, k, z, o;
bool b[105];
long long anti;
struct matrix {
int n, m;
long long mt[55][55];
} a;
matrix operator*(matrix a, matrix b) {
int i, j, k;
matrix ans;
for (i = 0; i < a.n; i++)
for (j = 0; j < b.m; j++)
for (k = ans.mt[i][j] = 0; k < a.m; k++)
ans.mt[i][j] = (ans.mt[i][j] + a.mt[i][k] * b.mt[k][j]) % 1000000007LL;
ans.n = a.n;
ans.m = b.m;
return ans;
}
long long pow(long long d, long long x) {
if (x == 0) return 1;
long long ret = pow(d, x >> 1);
ret = ret * ret % 1000000007LL;
if (x & 1) ret = ret * d % 1000000007LL;
return ret;
}
matrix pow(int x) {
if (x == 1) return a;
matrix ret = pow(x >> 1);
ret = ret * ret;
if (x & 1) ret = ret * a;
return ret;
}
inline long long min(long long a, long long b) { return a < b ? a : b; }
int main() {
for (scanf("%d%d", &n, &k), i = 0; i < n; i++) {
scanf("%d", &j);
b[i] = (j ? true : false);
if (b[i])
o++;
else
z++;
}
anti = pow(n * (n - 1), 1000000007LL - 2);
a.m = a.n = min(z, o) + 1;
for (i = 0; i <= min(z, o); i++) {
if (i != 0) a.mt[i - 1][i] = 2 * i * i * anti % 1000000007LL;
a.mt[i][i] =
(o * o + z * z - n + 2 * i * n - 4 * i * i) * anti % 1000000007LL;
if (i != min(z, o))
a.mt[i + 1][i] = 2 * (o - i) * (z - i) * anti % 1000000007LL;
}
matrix ggg = pow(k);
for (i = j = 0; i < z; i++)
if (b[i]) j++;
printf("%I64d\n", ggg.mt[0][j]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mo = 1e9 + 7;
void add(long long &x, long long y) {
x += y;
if (x >= mo) x -= mo;
}
struct matrix {
long long c[105][105], n, m;
matrix() {
memset(c, 0, sizeof(c));
n = m = 0;
}
matrix operator*(const matrix &rhs) const {
matrix res;
res.n = n;
res.m = rhs.m;
for (int i = 0; i < n; i++)
for (int k = 0; k < m; k++)
for (int j = 0; j < rhs.m; j++)
add(res.c[i][j], c[i][k] * rhs.c[k][j] % mo);
return res;
}
void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%lld ", c[i][j]);
puts("");
}
}
} A, B;
matrix qkpow(matrix a, long long t) {
matrix res;
res.n = res.m = a.n;
for (int i = 0; i < res.n; i++) res.c[i][i] = 1;
while (t) {
if (t & 1LL) res = res * a;
a = a * a;
t >>= 1;
}
return res;
}
long long qpow(long long a, long long s) {
long long t = 1;
while (s) {
if (s & 1) t = t * a % mo;
a = a * a % mo;
s >>= 1;
}
return t;
}
long long n, a[105], k, ans, c, t;
signed main() {
scanf("%lld %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), c += (a[i] == 0);
for (int i = 1; i <= c; i++) t += (a[i] == 0);
A.n = 1;
A.m = c + 1;
A.c[0][t] = 1;
B.n = B.m = c + 1;
for (int i = 0; i <= c; i++) {
if (i != 0) B.c[i - 1][i] = 1ll * (c - i + 1) * (c - i + 1) % mo;
B.c[i][i] =
(1ll * i * (c - i) % mo + 1ll * (c - i) * (n - 2ll * c + i) % mo) % mo;
B.c[i][i] = (B.c[i][i] + 1ll * c * (c - 1) / 2ll % mo) % mo;
B.c[i][i] = (B.c[i][i] + 1ll * (n - c) * (n - c - 1) / 2ll % mo) % mo;
if (i != c) B.c[i + 1][i] = 1ll * (i + 1) * (n - 2ll * c + i + 1) % mo;
}
A = A * qkpow(B, k);
ans = A.c[0][c];
t = 0;
for (int i = 0; i <= c; i++) t = (t + A.c[0][i]) % mo;
ans = ans * qpow(t, mo - 2) % mo;
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mo = 1e9 + 7;
void add(long long &x, long long y) {
x += y;
if (x >= mo) x -= mo;
}
struct matrix {
long long c[105][105], n, m;
matrix() {
memset(c, 0, sizeof(c));
n = m = 0;
}
matrix operator*(const matrix &rhs) const {
matrix res;
res.n = n;
res.m = rhs.m;
for (int i = 0; i < n; i++)
for (int k = 0; k < m; k++)
for (int j = 0; j < rhs.m; j++)
add(res.c[i][j], c[i][k] * rhs.c[k][j] % mo);
return res;
}
void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%lld ", c[i][j]);
puts("");
}
}
} A, B;
matrix qkpow(matrix a, long long t) {
matrix res;
res.n = res.m = a.n;
for (int i = 0; i < res.n; i++) res.c[i][i] = 1;
while (t) {
if (t & 1LL) res = res * a;
a = a * a;
t >>= 1;
}
return res;
}
long long qpow(long long a, long long s) {
long long t = 1;
while (s) {
if (s & 1) t = t * a % mo;
a = a * a % mo;
s >>= 1;
}
return t;
}
long long n, a[105], k, ans, c, t;
signed main() {
scanf("%lld %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), c += (a[i] == 0);
for (int i = 1; i <= c; i++) t += (a[i] == 0);
A.n = 1;
A.m = c + 1;
A.c[0][t] = 1;
B.n = B.m = c + 1;
for (int i = 0; i <= c; i++) {
if (i != 0) B.c[i - 1][i] = 1ll * (c - i + 1) * (c - i + 1) % mo;
B.c[i][i] =
(1ll * i * (c - i) % mo + 1ll * (c - i) * (n - 2ll * c + i) % mo) % mo;
B.c[i][i] = (B.c[i][i] + 1ll * c * (c - 1) / 2ll % mo) % mo;
B.c[i][i] = (B.c[i][i] + 1ll * (n - c) * (n - c - 1) / 2ll % mo) % mo;
if (i != c) B.c[i + 1][i] = 1ll * (i + 1) * (n - 2ll * c + i + 1) % mo;
}
A = A * qkpow(B, k);
ans = A.c[0][c];
t = 0;
for (int i = 0; i <= c; i++) t = (t + A.c[0][i]) % mo;
ans = ans * qpow(t, mo - 2) % mo;
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
int a[105];
int N = 100;
long long mod = 1e9 + 7;
struct node {
long long mat[105][105];
friend node operator*(node x, node y) {
node c;
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
c.mat[i][j] = 0;
for (int k = 0; k <= N; k++) {
c.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % mod;
}
c.mat[i][j] %= mod;
}
}
return c;
}
} sta, p, anss;
node powd(node x, long long y) {
node ans;
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++)
if (i == j)
ans.mat[i][j] = 1;
else
ans.mat[i][j] = 0;
while (y > 0) {
if (y % 2 == 1) ans = ans * x;
x = x * x;
y /= 2;
}
return ans;
}
long long powd(long long x, long long y) {
long long ans = 1;
while (y > 0) {
if (y % 2 == 1) ans = ans * x % mod;
x = x * x % mod;
y /= 2;
}
return ans;
}
long long afac(long long x) { return powd(x, mod - 2); }
int main() {
while (~scanf("%lld%lld", &n, &k)) {
int sum = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += a[i];
long long x = n - sum, y = sum;
long long sum1 = 0, sum2 = 0;
for (int i = 1; i <= x; i++) {
if (a[i] == 1) sum1++;
}
sum2 = sum - sum1;
long long ac = afac(n * (n - 1) / 2);
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
if (j == 0 && i == sum1)
sta.mat[i][j] = 1;
else
sta.mat[i][j] = 0;
p.mat[i][j] = 0;
if (i > x || j > x) continue;
long long p1, p2;
p1 = (x - j) * (sum - j) % mod;
p1 = p1 * ac % mod;
p2 = j * (j) % mod;
p2 = p2 * ac % mod;
if (j == i) {
p.mat[i][j] = (1 - p1 - p2) % mod;
} else if (j == i - 1) {
p.mat[i][j] = p1;
} else if (j == i + 1) {
p.mat[i][j] = p2;
}
}
}
anss = powd(p, k) * sta;
long long tans = anss.mat[0][0] % mod;
tans = (tans + mod) % mod;
printf("%lld\n", tans);
}
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, k;
int a[105];
int N = 100;
long long mod = 1e9 + 7;
struct node {
long long mat[105][105];
friend node operator*(node x, node y) {
node c;
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
c.mat[i][j] = 0;
for (int k = 0; k <= N; k++) {
c.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % mod;
}
c.mat[i][j] %= mod;
}
}
return c;
}
} sta, p, anss;
node powd(node x, long long y) {
node ans;
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++)
if (i == j)
ans.mat[i][j] = 1;
else
ans.mat[i][j] = 0;
while (y > 0) {
if (y % 2 == 1) ans = ans * x;
x = x * x;
y /= 2;
}
return ans;
}
long long powd(long long x, long long y) {
long long ans = 1;
while (y > 0) {
if (y % 2 == 1) ans = ans * x % mod;
x = x * x % mod;
y /= 2;
}
return ans;
}
long long afac(long long x) { return powd(x, mod - 2); }
int main() {
while (~scanf("%lld%lld", &n, &k)) {
int sum = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += a[i];
long long x = n - sum, y = sum;
long long sum1 = 0, sum2 = 0;
for (int i = 1; i <= x; i++) {
if (a[i] == 1) sum1++;
}
sum2 = sum - sum1;
long long ac = afac(n * (n - 1) / 2);
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
if (j == 0 && i == sum1)
sta.mat[i][j] = 1;
else
sta.mat[i][j] = 0;
p.mat[i][j] = 0;
if (i > x || j > x) continue;
long long p1, p2;
p1 = (x - j) * (sum - j) % mod;
p1 = p1 * ac % mod;
p2 = j * (j) % mod;
p2 = p2 * ac % mod;
if (j == i) {
p.mat[i][j] = (1 - p1 - p2) % mod;
} else if (j == i - 1) {
p.mat[i][j] = p1;
} else if (j == i + 1) {
p.mat[i][j] = p2;
}
}
}
anss = powd(p, k) * sta;
long long tans = anss.mat[0][0] % mod;
tans = (tans + mod) % mod;
printf("%lld\n", tans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 1;
const int md = 1e9 + 7;
void inc(int &x, int y) { x = x + y >= md ? x + y - md : x + y; }
int len;
struct Mat {
int a[maxn][maxn];
Mat() { memset(a, 0, sizeof(a)); }
Mat operator*(const Mat &x) const {
Mat res = Mat();
for (int i = 0; i <= len; ++i)
for (int j = 0; j <= len; ++j)
for (int k = 0; k <= len; ++k)
inc(res.a[i][j], 1ll * a[i][k] * x.a[k][j] % md);
return res;
}
void print() {
for (int i = 0; i <= len; ++i, cout << endl)
for (int j = 0; j <= len; ++j) cout << a[i][j] << " ";
}
} res, mp;
int qpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % md;
a = a * a % md;
b >>= 1;
}
return res;
}
void qpow(int k) {
while (k) {
if (k & 1) res = res * mp;
mp = mp * mp;
k >>= 1;
}
}
int n, k, a[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", a + i), len += a[i] == 1;
int now = 0;
for (int i = n - len + 1; i <= n; ++i) now += a[i] == 1;
res.a[0][now] = 1;
for (int i = 0; i <= len; ++i) {
mp.a[i][i - 1] = i * (n - len * 2 + i);
mp.a[i][i + 1] = (len - i) * (len - i);
mp.a[i][i] =
(n * (n - 1) >> 1) - i * (n - len * 2 + i) - (len - i) * (len - i);
}
qpow(k);
printf("%I64d\n", ((long long)res.a[0][len] *
qpow(qpow(n * (n - 1) >> 1, md - 2), k) % md +
md) %
md);
}
| ### Prompt
Please create a solution in Cpp to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 1;
const int md = 1e9 + 7;
void inc(int &x, int y) { x = x + y >= md ? x + y - md : x + y; }
int len;
struct Mat {
int a[maxn][maxn];
Mat() { memset(a, 0, sizeof(a)); }
Mat operator*(const Mat &x) const {
Mat res = Mat();
for (int i = 0; i <= len; ++i)
for (int j = 0; j <= len; ++j)
for (int k = 0; k <= len; ++k)
inc(res.a[i][j], 1ll * a[i][k] * x.a[k][j] % md);
return res;
}
void print() {
for (int i = 0; i <= len; ++i, cout << endl)
for (int j = 0; j <= len; ++j) cout << a[i][j] << " ";
}
} res, mp;
int qpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % md;
a = a * a % md;
b >>= 1;
}
return res;
}
void qpow(int k) {
while (k) {
if (k & 1) res = res * mp;
mp = mp * mp;
k >>= 1;
}
}
int n, k, a[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", a + i), len += a[i] == 1;
int now = 0;
for (int i = n - len + 1; i <= n; ++i) now += a[i] == 1;
res.a[0][now] = 1;
for (int i = 0; i <= len; ++i) {
mp.a[i][i - 1] = i * (n - len * 2 + i);
mp.a[i][i + 1] = (len - i) * (len - i);
mp.a[i][i] =
(n * (n - 1) >> 1) - i * (n - len * 2 + i) - (len - i) * (len - i);
}
qpow(k);
printf("%I64d\n", ((long long)res.a[0][len] *
qpow(qpow(n * (n - 1) >> 1, md - 2), k) % md +
md) %
md);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int p = 1000000007;
struct matrix {
int a[105][105];
} b;
int tot, st, a[105], ans[105];
int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int pow_mod(int x, int k) {
int ans = 1;
while (k) {
if (k & 1) ans = 1LL * ans * x % p;
x = 1LL * x * x % p;
k >>= 1;
}
return ans;
}
matrix times(matrix x, matrix y) {
matrix ans;
memset(ans.a, 0, sizeof(ans.a));
for (int i = 0; i <= tot; i++) {
for (int j = 0; j <= tot; j++) {
for (int k = 0; k <= tot; k++) {
ans.a[i][k] = (ans.a[i][k] + 1LL * x.a[i][j] * y.a[j][k]) % p;
}
}
}
return ans;
}
matrix fpow(matrix x, int k) {
--k;
matrix ans = x;
while (k) {
if (k & 1) ans = times(ans, x);
x = times(x, x);
k >>= 1;
}
return ans;
}
int main() {
int n = read(), k = read();
tot = 0, st = 0;
for (int i = 1; i <= n; i++) {
a[i] = read();
if (a[i] == 0) ++tot;
}
for (int i = 1; i <= tot; i++)
if (a[i] == 0) ++st;
int t = 1LL * n * (n - 1) / 2 % p;
t = pow_mod(t, p - 2);
for (int i = 0; i <= tot; i++) {
int a0 = i, a1 = tot - i, b0 = tot - i, b1 = n - a0 - a1 - b0;
if (i < tot) b.a[i][i + 1] = 1LL * a1 * b0 % p * t % p;
if (i > 0) b.a[i][i - 1] = 1LL * a0 * b1 % p * t % p;
b.a[i][i] =
(1 + p - 1LL * a1 * b0 % p * t % p + p - 1LL * a0 * b1 % p * t % p) % p;
}
b = fpow(b, k);
int sum = 0;
for (int i = 0; i <= tot; i++) {
sum += b.a[st][i];
if (sum >= p) sum -= p;
}
printf("%d\n", 1LL * b.a[st][tot] * pow_mod(sum, p - 2) % p);
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int p = 1000000007;
struct matrix {
int a[105][105];
} b;
int tot, st, a[105], ans[105];
int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int pow_mod(int x, int k) {
int ans = 1;
while (k) {
if (k & 1) ans = 1LL * ans * x % p;
x = 1LL * x * x % p;
k >>= 1;
}
return ans;
}
matrix times(matrix x, matrix y) {
matrix ans;
memset(ans.a, 0, sizeof(ans.a));
for (int i = 0; i <= tot; i++) {
for (int j = 0; j <= tot; j++) {
for (int k = 0; k <= tot; k++) {
ans.a[i][k] = (ans.a[i][k] + 1LL * x.a[i][j] * y.a[j][k]) % p;
}
}
}
return ans;
}
matrix fpow(matrix x, int k) {
--k;
matrix ans = x;
while (k) {
if (k & 1) ans = times(ans, x);
x = times(x, x);
k >>= 1;
}
return ans;
}
int main() {
int n = read(), k = read();
tot = 0, st = 0;
for (int i = 1; i <= n; i++) {
a[i] = read();
if (a[i] == 0) ++tot;
}
for (int i = 1; i <= tot; i++)
if (a[i] == 0) ++st;
int t = 1LL * n * (n - 1) / 2 % p;
t = pow_mod(t, p - 2);
for (int i = 0; i <= tot; i++) {
int a0 = i, a1 = tot - i, b0 = tot - i, b1 = n - a0 - a1 - b0;
if (i < tot) b.a[i][i + 1] = 1LL * a1 * b0 % p * t % p;
if (i > 0) b.a[i][i - 1] = 1LL * a0 * b1 % p * t % p;
b.a[i][i] =
(1 + p - 1LL * a1 * b0 % p * t % p + p - 1LL * a0 * b1 % p * t % p) % p;
}
b = fpow(b, k);
int sum = 0;
for (int i = 0; i <= tot; i++) {
sum += b.a[st][i];
if (sum >= p) sum -= p;
}
printf("%d\n", 1LL * b.a[st][tot] * pow_mod(sum, p - 2) % p);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 110;
const long long Mod = 1e9 + 7;
long long Fact[N];
long long add(long long x, long long y) {
x %= Mod, y %= Mod;
return (x + y) % Mod;
}
long long sub(long long x, long long y) {
x %= Mod, y %= Mod;
return (x - y + Mod) % Mod;
}
long long mul(long long x, long long y) {
x %= Mod, y %= Mod;
return x * y % Mod;
}
void add_self(long long& x, long long y) { x = add(x, y); }
void sub_self(long long& x, long long y) { x = sub(x, y); }
void mul_self(long long& x, long long y) { x = mul(x, y); }
long long fp(long long x, long long y) {
if (!y) return 1;
long long Res = fp(x, y / 2);
mul_self(Res, Res);
if (y & 1) mul_self(Res, x);
return Res;
}
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }
long long inv(long long y) { return fp(y, Mod - 2); }
long long inv(long long x, long long y) { return mul(x, inv(y)); }
long long C(long long n, long long r) {
if (n < 0 || r < 0 || n < r) return 0;
return inv(Fact[n], mul(Fact[r], Fact[n - r]));
}
vector<vector<long long> > Zero(long long n, long long m) {
return vector<vector<long long> >(n, vector<long long>(m, 0));
}
vector<vector<long long> > Identity(long long n) {
vector<vector<long long> > Res = Zero(n, n);
for (long long i = 0; i < n; i++) Res[i][i] = 1;
return Res;
}
vector<vector<long long> > Muliply(const vector<vector<long long> >& A,
const vector<vector<long long> >& B) {
vector<vector<long long> > Res = Zero(A.size(), B[0].size());
for (long long i = 0; i < A.size(); i++)
for (long long k = 0; k < B.size(); k++)
if (A[i][k])
for (long long j = 0; j < B[0].size(); j++)
add_self(Res[i][j], mul(A[i][k], B[k][j]));
return Res;
}
vector<vector<long long> > Power(const vector<vector<long long> >& A,
long long k) {
if (!k) return Identity(A.size());
if (k & 1) return Muliply(A, Power(A, k - 1));
return Power(Muliply(A, A), k >> 1);
}
long long a[N];
int main() {
Fact[0] = 1;
for (long long i = 1; i < N; i++) Fact[i] = mul(i, Fact[i - 1]);
long long n, k;
scanf("%I64d%I64d", &n, &k);
for (long long i = 0; i < n; i++) scanf("%I64d", a + i);
long long c0 = 0, c1 = 0, ok0 = 0;
for (long long i = 0; i < n; i++) a[i] ? c1++ : c0++;
for (long long i = 0; i < c0; i++)
if (!a[i]) ok0++;
vector<vector<long long> > Init = Zero(1, n + 1);
vector<vector<long long> > Trans = Zero(n + 1, n + 1);
Init[0][ok0] = 1;
for (long long i = 0; i < n + 1; i++) {
long long L0 = i, R0 = c0 - i;
long long L1 = c0 - i, R1 = c1 - c0 + i;
if (L0 < 0 || R0 < 0 || L1 < 0 || R1 < 0) continue;
add_self(Trans[i][i], C(c0, 2));
add_self(Trans[i][i], C(c1, 2));
add_self(Trans[i][i], mul(L0, R0));
add_self(Trans[i][i], mul(L1, R1));
if (i + 1 < n + 1) add_self(Trans[i][i + 1], mul(L1, R0));
if (i - 1 >= 0) add_self(Trans[i][i - 1], mul(L0, R1));
}
vector<vector<long long> > Staff = Muliply(Init, Power(Trans, k));
long long P = Staff[0][c0];
long long Q = fp(C(n, 2), k);
long long G = GCD(P, Q);
P /= G, Q /= G;
long long Ans = inv(P, Q);
cout << Ans;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 110;
const long long Mod = 1e9 + 7;
long long Fact[N];
long long add(long long x, long long y) {
x %= Mod, y %= Mod;
return (x + y) % Mod;
}
long long sub(long long x, long long y) {
x %= Mod, y %= Mod;
return (x - y + Mod) % Mod;
}
long long mul(long long x, long long y) {
x %= Mod, y %= Mod;
return x * y % Mod;
}
void add_self(long long& x, long long y) { x = add(x, y); }
void sub_self(long long& x, long long y) { x = sub(x, y); }
void mul_self(long long& x, long long y) { x = mul(x, y); }
long long fp(long long x, long long y) {
if (!y) return 1;
long long Res = fp(x, y / 2);
mul_self(Res, Res);
if (y & 1) mul_self(Res, x);
return Res;
}
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }
long long inv(long long y) { return fp(y, Mod - 2); }
long long inv(long long x, long long y) { return mul(x, inv(y)); }
long long C(long long n, long long r) {
if (n < 0 || r < 0 || n < r) return 0;
return inv(Fact[n], mul(Fact[r], Fact[n - r]));
}
vector<vector<long long> > Zero(long long n, long long m) {
return vector<vector<long long> >(n, vector<long long>(m, 0));
}
vector<vector<long long> > Identity(long long n) {
vector<vector<long long> > Res = Zero(n, n);
for (long long i = 0; i < n; i++) Res[i][i] = 1;
return Res;
}
vector<vector<long long> > Muliply(const vector<vector<long long> >& A,
const vector<vector<long long> >& B) {
vector<vector<long long> > Res = Zero(A.size(), B[0].size());
for (long long i = 0; i < A.size(); i++)
for (long long k = 0; k < B.size(); k++)
if (A[i][k])
for (long long j = 0; j < B[0].size(); j++)
add_self(Res[i][j], mul(A[i][k], B[k][j]));
return Res;
}
vector<vector<long long> > Power(const vector<vector<long long> >& A,
long long k) {
if (!k) return Identity(A.size());
if (k & 1) return Muliply(A, Power(A, k - 1));
return Power(Muliply(A, A), k >> 1);
}
long long a[N];
int main() {
Fact[0] = 1;
for (long long i = 1; i < N; i++) Fact[i] = mul(i, Fact[i - 1]);
long long n, k;
scanf("%I64d%I64d", &n, &k);
for (long long i = 0; i < n; i++) scanf("%I64d", a + i);
long long c0 = 0, c1 = 0, ok0 = 0;
for (long long i = 0; i < n; i++) a[i] ? c1++ : c0++;
for (long long i = 0; i < c0; i++)
if (!a[i]) ok0++;
vector<vector<long long> > Init = Zero(1, n + 1);
vector<vector<long long> > Trans = Zero(n + 1, n + 1);
Init[0][ok0] = 1;
for (long long i = 0; i < n + 1; i++) {
long long L0 = i, R0 = c0 - i;
long long L1 = c0 - i, R1 = c1 - c0 + i;
if (L0 < 0 || R0 < 0 || L1 < 0 || R1 < 0) continue;
add_self(Trans[i][i], C(c0, 2));
add_self(Trans[i][i], C(c1, 2));
add_self(Trans[i][i], mul(L0, R0));
add_self(Trans[i][i], mul(L1, R1));
if (i + 1 < n + 1) add_self(Trans[i][i + 1], mul(L1, R0));
if (i - 1 >= 0) add_self(Trans[i][i - 1], mul(L0, R1));
}
vector<vector<long long> > Staff = Muliply(Init, Power(Trans, k));
long long P = Staff[0][c0];
long long Q = fp(C(n, 2), k);
long long G = GCD(P, Q);
P /= G, Q /= G;
long long Ans = inv(P, Q);
cout << Ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 107;
const long long MOD = 1000 * 1000 * 1000 + 7;
inline long long mod(long long n) {
if (n < MOD)
return n;
else
return n % MOD;
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
struct md {
long long n;
md(long long x) { n = mod(x); }
md() {}
md operator+(md x) { return md(n + x.n); }
md operator*(md x) { return md(n * x.n); }
md operator/(md x) { return md(dv(n, x.n)); }
};
long long cnt[2];
bool a[N];
md m[N][N], ans[N][N], t[N][N];
void add(md a[N][N], md b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j].n = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
t[i][j] = t[i][j] + a[i][k] * b[k][j];
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) {
ans[i][i].n = 1;
}
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
md all(long long n, long long k) { return md(fp(n * (n - 1) / 2, k)); }
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
for (long long i = 0; i <= cnt[0] && i <= cnt[1]; ++i) {
long long l = cnt[0];
long long r = n - l;
long long l1 = i;
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
if (i) m[i][i - 1] = md(l1 * r0);
m[i][i + 1] = md(l0 * r1);
m[i][i] = md(l * (l - 1) / 2 + r * (r - 1) / 2 + l0 * r0 + l1 * r1);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < cnt[0]; ++i) {
sum += a[i];
}
cout << (ans[sum][0] / all(n, k)).n << '\n';
}
| ### Prompt
Generate a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 107;
const long long MOD = 1000 * 1000 * 1000 + 7;
inline long long mod(long long n) {
if (n < MOD)
return n;
else
return n % MOD;
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
struct md {
long long n;
md(long long x) { n = mod(x); }
md() {}
md operator+(md x) { return md(n + x.n); }
md operator*(md x) { return md(n * x.n); }
md operator/(md x) { return md(dv(n, x.n)); }
};
long long cnt[2];
bool a[N];
md m[N][N], ans[N][N], t[N][N];
void add(md a[N][N], md b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j].n = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
t[i][j] = t[i][j] + a[i][k] * b[k][j];
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) {
ans[i][i].n = 1;
}
for (long long i = 0; (1ll << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
md all(long long n, long long k) { return md(fp(n * (n - 1) / 2, k)); }
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
for (long long i = 0; i <= cnt[0] && i <= cnt[1]; ++i) {
long long l = cnt[0];
long long r = n - l;
long long l1 = i;
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
if (i) m[i][i - 1] = md(l1 * r0);
m[i][i + 1] = md(l0 * r1);
m[i][i] = md(l * (l - 1) / 2 + r * (r - 1) / 2 + l0 * r0 + l1 * r1);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < cnt[0]; ++i) {
sum += a[i];
}
cout << (ans[sum][0] / all(n, k)).n << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <std::uint32_t mod>
class modint {
private:
std::uint32_t n;
public:
modint() : n(0){};
modint(std::uint64_t n_) : n(n_ % mod){};
bool operator==(const modint& m) const { return n == m.n; }
bool operator!=(const modint& m) const { return n != m.n; }
std::uint32_t get() const { return n; }
modint& operator+=(const modint& m) {
n += m.n;
n = (n < mod ? n : n - mod);
return *this;
}
modint& operator-=(const modint& m) {
n += mod - m.n;
n = (n < mod ? n : n - mod);
return *this;
}
modint& operator*=(const modint& m) {
n = std::uint64_t(n) * m.n % mod;
return *this;
}
modint operator+(const modint& m) const { return modint(*this) += m; }
modint operator-(const modint& m) const { return modint(*this) -= m; }
modint operator*(const modint& m) const { return modint(*this) *= m; }
modint binpow(std::uint64_t b) const {
modint ans = 1, m = modint(*this);
while (b) {
if (b & 1) ans *= m;
m *= m;
b >>= 1;
}
return ans;
}
modint inv() { return (*this).binpow(mod - 2); }
};
const int maxn = 110, mod = 1e9 + 7;
int arr[maxn];
modint<mod> mat[maxn][maxn], dp[maxn], tmp[maxn][maxn], ans[maxn][maxn];
int main() {
int n, k, a, b, s;
a = b = s = 0;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
if (arr[i])
++b;
else
++a;
}
for (int i = 0; i < a; ++i)
if (arr[i] == 0) ++s;
for (int i = 0; i <= n; ++i) ans[i][i] = modint<mod>(1);
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
if (j >= a - b && j <= a && i >= a - b && i <= a) {
int a0 = j, a1 = a - a0, b1 = b - a1, b0 = b - b1;
if (j - 1 == i)
mat[i][j] = modint<mod>(a0 * b1);
else if (j + 1 == i)
mat[i][j] = modint<mod>(a1 * b0);
else if (j == i)
mat[i][j] = modint<mod>(n * (n - 1) / 2 - a0 * b1 - a1 * b0);
}
}
}
int ok = k;
while (k) {
if (k & 1) {
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
tmp[i][j] = modint<mod>(0);
for (int k = 0; k <= n; ++k) {
tmp[i][j] += mat[i][k] * ans[k][j];
}
}
}
memcpy(ans, tmp, sizeof ans);
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
tmp[i][j] = modint<mod>(0);
for (int k = 0; k <= n; ++k) {
tmp[i][j] += mat[i][k] * mat[k][j];
}
}
}
memcpy(mat, tmp, sizeof(mat));
k >>= 1;
}
long long num = ans[a][s].get(),
deninv = modint<mod>(n * (n - 1) / 2).binpow(ok).inv().get();
num = num * deninv % mod;
if (num < 0) num += mod;
cout << num;
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <std::uint32_t mod>
class modint {
private:
std::uint32_t n;
public:
modint() : n(0){};
modint(std::uint64_t n_) : n(n_ % mod){};
bool operator==(const modint& m) const { return n == m.n; }
bool operator!=(const modint& m) const { return n != m.n; }
std::uint32_t get() const { return n; }
modint& operator+=(const modint& m) {
n += m.n;
n = (n < mod ? n : n - mod);
return *this;
}
modint& operator-=(const modint& m) {
n += mod - m.n;
n = (n < mod ? n : n - mod);
return *this;
}
modint& operator*=(const modint& m) {
n = std::uint64_t(n) * m.n % mod;
return *this;
}
modint operator+(const modint& m) const { return modint(*this) += m; }
modint operator-(const modint& m) const { return modint(*this) -= m; }
modint operator*(const modint& m) const { return modint(*this) *= m; }
modint binpow(std::uint64_t b) const {
modint ans = 1, m = modint(*this);
while (b) {
if (b & 1) ans *= m;
m *= m;
b >>= 1;
}
return ans;
}
modint inv() { return (*this).binpow(mod - 2); }
};
const int maxn = 110, mod = 1e9 + 7;
int arr[maxn];
modint<mod> mat[maxn][maxn], dp[maxn], tmp[maxn][maxn], ans[maxn][maxn];
int main() {
int n, k, a, b, s;
a = b = s = 0;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
if (arr[i])
++b;
else
++a;
}
for (int i = 0; i < a; ++i)
if (arr[i] == 0) ++s;
for (int i = 0; i <= n; ++i) ans[i][i] = modint<mod>(1);
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
if (j >= a - b && j <= a && i >= a - b && i <= a) {
int a0 = j, a1 = a - a0, b1 = b - a1, b0 = b - b1;
if (j - 1 == i)
mat[i][j] = modint<mod>(a0 * b1);
else if (j + 1 == i)
mat[i][j] = modint<mod>(a1 * b0);
else if (j == i)
mat[i][j] = modint<mod>(n * (n - 1) / 2 - a0 * b1 - a1 * b0);
}
}
}
int ok = k;
while (k) {
if (k & 1) {
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
tmp[i][j] = modint<mod>(0);
for (int k = 0; k <= n; ++k) {
tmp[i][j] += mat[i][k] * ans[k][j];
}
}
}
memcpy(ans, tmp, sizeof ans);
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
tmp[i][j] = modint<mod>(0);
for (int k = 0; k <= n; ++k) {
tmp[i][j] += mat[i][k] * mat[k][j];
}
}
}
memcpy(mat, tmp, sizeof(mat));
k >>= 1;
}
long long num = ans[a][s].get(),
deninv = modint<mod>(n * (n - 1) / 2).binpow(ok).inv().get();
num = num * deninv % mod;
if (num < 0) num += mod;
cout << num;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC diagnostic ignored "-Wmissing-declarations"
inline int safe_mul(const int x, const int y)
__attribute__((warn_unused_result));
int const mod = 1e9 + 7;
inline int safe_mul(const int x, const int y) {
return x * static_cast<int64_t>(y) % mod;
}
inline void safe_add(int& x, const int y) {
x += y;
if (x >= mod) x -= mod;
}
inline int safe_pow(int x, int y) {
int res = 1;
for (; y > 0; y >>= 1, x = safe_mul(x, x))
if (y & 1) res = safe_mul(res, x);
return res;
}
inline int safe_inv(const int x) { return safe_pow(x, mod - 2); }
vector<vector<int>> mul(const vector<vector<int>>& a,
const vector<vector<int>>& b) {
auto n = a.size();
vector<vector<int>> res(n, vector<int>(n, 0));
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)
for (size_t k = 0; k < n; ++k)
safe_add(res[i][j], safe_mul(a[i][k], b[k][j]));
return res;
}
vector<vector<int>> pow(vector<vector<int>> a, int y) {
auto n = a.size();
vector<vector<int>> res(n, vector<int>(n, 0));
for (size_t i = 0; i < n; ++i) res[i][i] = 1;
for (; y > 0; y >>= 1, a = mul(a, a))
if (y & 1) res = mul(res, a);
return res;
}
int const maxn = 105;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int& x : a) cin >> x;
int c = count(begin(a), end(a), 0);
vector<vector<int>> mat(c + 1, vector<int>(c + 1, 0));
int cnt = n * (n - 1) / 2;
int d = safe_inv(cnt);
for (int f = 0; f <= c; ++f) {
if (c - f > n - c) continue;
int l = safe_mul(f * ((n - c) - (c - f)), d);
int r = safe_mul((c - f) * (c - f), d);
int t = 1;
safe_add(t, mod - l);
safe_add(t, mod - r);
if (l) mat[f - 1][f] = l;
if (r) mat[f + 1][f] = r;
mat[f][f] = t;
}
mat = pow(mat, k);
int c0 = count(begin(a), begin(a) + c, 0);
cout << mat[c][c0] << endl;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma GCC diagnostic ignored "-Wmissing-declarations"
inline int safe_mul(const int x, const int y)
__attribute__((warn_unused_result));
int const mod = 1e9 + 7;
inline int safe_mul(const int x, const int y) {
return x * static_cast<int64_t>(y) % mod;
}
inline void safe_add(int& x, const int y) {
x += y;
if (x >= mod) x -= mod;
}
inline int safe_pow(int x, int y) {
int res = 1;
for (; y > 0; y >>= 1, x = safe_mul(x, x))
if (y & 1) res = safe_mul(res, x);
return res;
}
inline int safe_inv(const int x) { return safe_pow(x, mod - 2); }
vector<vector<int>> mul(const vector<vector<int>>& a,
const vector<vector<int>>& b) {
auto n = a.size();
vector<vector<int>> res(n, vector<int>(n, 0));
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)
for (size_t k = 0; k < n; ++k)
safe_add(res[i][j], safe_mul(a[i][k], b[k][j]));
return res;
}
vector<vector<int>> pow(vector<vector<int>> a, int y) {
auto n = a.size();
vector<vector<int>> res(n, vector<int>(n, 0));
for (size_t i = 0; i < n; ++i) res[i][i] = 1;
for (; y > 0; y >>= 1, a = mul(a, a))
if (y & 1) res = mul(res, a);
return res;
}
int const maxn = 105;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int& x : a) cin >> x;
int c = count(begin(a), end(a), 0);
vector<vector<int>> mat(c + 1, vector<int>(c + 1, 0));
int cnt = n * (n - 1) / 2;
int d = safe_inv(cnt);
for (int f = 0; f <= c; ++f) {
if (c - f > n - c) continue;
int l = safe_mul(f * ((n - c) - (c - f)), d);
int r = safe_mul((c - f) * (c - f), d);
int t = 1;
safe_add(t, mod - l);
safe_add(t, mod - r);
if (l) mat[f - 1][f] = l;
if (r) mat[f + 1][f] = r;
mat[f][f] = t;
}
mat = pow(mat, k);
int c0 = count(begin(a), begin(a) + c, 0);
cout << mat[c][c0] << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll modpow(ll x, ll p, ll mod) {
ll res = 1LL;
for (; p; p >>= 1, (x *= x) %= mod)
if (p & 1) (res *= x) %= mod;
return res;
}
template <ll mod>
struct Mint {
ll x;
Mint(ll x = 0) : x((x %= mod) < 0 ? x + mod : x) {}
Mint& operator+=(const Mint& rhs) {
if ((x += rhs.x) >= mod) x -= mod;
return *this;
}
Mint& operator-=(const Mint& rhs) { return *this += mod - rhs.x; }
Mint& operator*=(const Mint& rhs) {
(x *= rhs.x) %= mod;
return *this;
}
Mint& operator/=(const Mint& rhs) {
return *this *= modpow(rhs.x, mod - 2, mod);
}
Mint power(ll p) const { return Mint(modpow(x, p, mod)); }
bool operator==(const Mint& rhs) const { return x == rhs.x; }
bool operator<(const Mint& rhs) const { return x < rhs.x; }
friend Mint operator+(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) += rhs;
}
friend Mint operator-(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) -= rhs;
}
friend Mint operator*(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) *= rhs;
}
friend Mint operator/(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) /= rhs;
}
friend ostream& operator<<(ostream& out, const Mint& a) { return out << a.x; }
friend istream& operator>>(istream& in, Mint& a) {
ll x;
in >> x;
a = Mint(x);
return in;
}
};
template <typename T>
T templatepow(T x, ll p) {
assert(p >= 0);
T res(1);
while (p > 0) {
if (p & 1) res = res * x;
x = x * x, p >>= 1;
}
return res;
}
template <typename T, int N, int M>
struct Matrix {
T A[N][M];
int row[N];
Matrix() {
fill(&A[0][0], &A[0][0] + N * M, T(0));
iota(begin(row), end(row), 0);
}
Matrix(T value) : Matrix() {
for (int i = 0; i < min(N, M); ++i) A[i][i] = value;
}
Matrix(initializer_list<initializer_list<T>> lst) : Matrix() {
int i = 0, j = 0;
for (const auto& v : lst) {
for (const auto& x : v) A[i][j++] = x;
i++, j = 0;
}
}
T* operator[](int i) { return A[row[i]]; }
const T* operator[](int i) const { return A[row[i]]; }
void swap_rows(int i, int j) { swap(row[i], row[j]); }
template <typename Op>
Matrix& compose(const Matrix& rhs, Op&& op) {
auto& lhs = *this;
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j) lhs[i][j] = op(lhs[i][j], rhs[i][j]);
return *this;
}
Matrix& operator+=(const Matrix& rhs) { return compose(rhs, std::plus<T>()); }
Matrix& operator-=(const Matrix& rhs) {
return compose(rhs, std::minus<T>());
}
Matrix operator+(const Matrix& rhs) const { return Matrix(*this) += rhs; }
Matrix operator-(const Matrix& rhs) const { return Matrix(*this) -= rhs; }
template <int K>
Matrix<T, N, K> operator*(const Matrix<T, M, K>& rhs) const {
const auto& lhs = *this;
Matrix<T, N, K> res;
for (int i = 0; i < N; ++i)
for (int j = 0; j < K; ++j)
for (int k = 0; k < M; ++k) res[i][j] += lhs[i][k] * rhs[k][j];
return res;
}
friend Matrix operator*(const T& alpha, Matrix A) {
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j) A[i][j] *= alpha;
return A;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
constexpr ll mod = 1e9 + 7;
using mint = Mint<mod>;
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
for (auto& x : a) cin >> x;
constexpr int nmax = 100;
Matrix<mint, nmax, nmax> A;
mint p = 1 / mint(1LL * n * (n - 1) / 2);
int ones = accumulate(begin(a), end(a), 0), zeros = n - ones,
N = min(ones, zeros);
for (int x = 0; x <= N; ++x) {
A[x][x] = 1 - (1LL * x * x + 1LL * (ones - x) * (zeros - x)) * p;
if (x + 1 <= N) A[x][x + 1] = (1LL * (ones - x) * (zeros - x)) * p;
if (x > 0) A[x][x - 1] = (1LL * x * x) * p;
}
A = templatepow(A, k);
int x = 0;
for (int i = 0; i < zeros; ++i) x += a[i] == 1;
cerr << "x"
<< " == " << (x) << '\n';
;
mint ans = A[x][0];
cout << ans << '\n';
exit(0);
}
| ### Prompt
Please create a solution in CPP to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll modpow(ll x, ll p, ll mod) {
ll res = 1LL;
for (; p; p >>= 1, (x *= x) %= mod)
if (p & 1) (res *= x) %= mod;
return res;
}
template <ll mod>
struct Mint {
ll x;
Mint(ll x = 0) : x((x %= mod) < 0 ? x + mod : x) {}
Mint& operator+=(const Mint& rhs) {
if ((x += rhs.x) >= mod) x -= mod;
return *this;
}
Mint& operator-=(const Mint& rhs) { return *this += mod - rhs.x; }
Mint& operator*=(const Mint& rhs) {
(x *= rhs.x) %= mod;
return *this;
}
Mint& operator/=(const Mint& rhs) {
return *this *= modpow(rhs.x, mod - 2, mod);
}
Mint power(ll p) const { return Mint(modpow(x, p, mod)); }
bool operator==(const Mint& rhs) const { return x == rhs.x; }
bool operator<(const Mint& rhs) const { return x < rhs.x; }
friend Mint operator+(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) += rhs;
}
friend Mint operator-(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) -= rhs;
}
friend Mint operator*(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) *= rhs;
}
friend Mint operator/(const Mint& lhs, const Mint& rhs) {
return Mint(lhs) /= rhs;
}
friend ostream& operator<<(ostream& out, const Mint& a) { return out << a.x; }
friend istream& operator>>(istream& in, Mint& a) {
ll x;
in >> x;
a = Mint(x);
return in;
}
};
template <typename T>
T templatepow(T x, ll p) {
assert(p >= 0);
T res(1);
while (p > 0) {
if (p & 1) res = res * x;
x = x * x, p >>= 1;
}
return res;
}
template <typename T, int N, int M>
struct Matrix {
T A[N][M];
int row[N];
Matrix() {
fill(&A[0][0], &A[0][0] + N * M, T(0));
iota(begin(row), end(row), 0);
}
Matrix(T value) : Matrix() {
for (int i = 0; i < min(N, M); ++i) A[i][i] = value;
}
Matrix(initializer_list<initializer_list<T>> lst) : Matrix() {
int i = 0, j = 0;
for (const auto& v : lst) {
for (const auto& x : v) A[i][j++] = x;
i++, j = 0;
}
}
T* operator[](int i) { return A[row[i]]; }
const T* operator[](int i) const { return A[row[i]]; }
void swap_rows(int i, int j) { swap(row[i], row[j]); }
template <typename Op>
Matrix& compose(const Matrix& rhs, Op&& op) {
auto& lhs = *this;
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j) lhs[i][j] = op(lhs[i][j], rhs[i][j]);
return *this;
}
Matrix& operator+=(const Matrix& rhs) { return compose(rhs, std::plus<T>()); }
Matrix& operator-=(const Matrix& rhs) {
return compose(rhs, std::minus<T>());
}
Matrix operator+(const Matrix& rhs) const { return Matrix(*this) += rhs; }
Matrix operator-(const Matrix& rhs) const { return Matrix(*this) -= rhs; }
template <int K>
Matrix<T, N, K> operator*(const Matrix<T, M, K>& rhs) const {
const auto& lhs = *this;
Matrix<T, N, K> res;
for (int i = 0; i < N; ++i)
for (int j = 0; j < K; ++j)
for (int k = 0; k < M; ++k) res[i][j] += lhs[i][k] * rhs[k][j];
return res;
}
friend Matrix operator*(const T& alpha, Matrix A) {
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j) A[i][j] *= alpha;
return A;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
constexpr ll mod = 1e9 + 7;
using mint = Mint<mod>;
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
for (auto& x : a) cin >> x;
constexpr int nmax = 100;
Matrix<mint, nmax, nmax> A;
mint p = 1 / mint(1LL * n * (n - 1) / 2);
int ones = accumulate(begin(a), end(a), 0), zeros = n - ones,
N = min(ones, zeros);
for (int x = 0; x <= N; ++x) {
A[x][x] = 1 - (1LL * x * x + 1LL * (ones - x) * (zeros - x)) * p;
if (x + 1 <= N) A[x][x + 1] = (1LL * (ones - x) * (zeros - x)) * p;
if (x > 0) A[x][x - 1] = (1LL * x * x) * p;
}
A = templatepow(A, k);
int x = 0;
for (int i = 0; i < zeros; ++i) x += a[i] == 1;
cerr << "x"
<< " == " << (x) << '\n';
;
mint ans = A[x][0];
cout << ans << '\n';
exit(0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline long long int modadd(long long int n, long long int m,
long long int p = 1000000007) {
return ((n + m) % p + p) % p;
}
inline long long int modsub(long long int n, long long int m,
long long int p = 1000000007) {
return ((n - m + p) % p + p) % p;
}
inline long long int modpro(long long int n, long long int m,
long long int p = 1000000007) {
return (((n % p) * (m % p)) % p + p) % p;
}
unsigned long long int powe(long long int first, long long int second) {
unsigned long long int res = 1;
while (second > 0) {
if (second & 1) res = res * first;
second = second >> 1;
first = first * first;
}
return res;
}
long long int modpow(long long int first, long long int second,
long long int p = 1000000007) {
long long int res = 1;
while (second > 0) {
if (second & 1) res = modpro(res, first, p);
second = second >> 1;
first = modpro(first, first, p);
}
return res;
}
inline long long int modInverse(long long int n, long long int p = 1000000007) {
if (n == 1) return 1;
return modpow(n, p - 2, p);
}
inline long long int moddiv(long long int n, long long int m,
long long int p = 1000000007) {
return modpro(n, modInverse(m, p), p);
}
inline long long int modadd3(long long int first, long long int second,
long long int z, long long int p = 1000000007) {
return modadd(modadd(first, second, p), z, p);
}
inline long long int modadd4(long long int first, long long int second,
long long int z, long long int w,
long long int p = 1000000007) {
return modadd(modadd(first, second, p), modadd(z, w, p), p);
}
inline long long int modnCr(long long int fac[], int n, int r,
long long int p = 1000000007) {
if (r == 0) return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T>
inline T max3(T first, T second, T z) {
return max(max(first, second), z);
}
template <typename T>
inline T max4(T first, T second, T z, T w) {
return max(max3(first, second, w), z);
}
template <typename T>
inline T min3(T first, T second, T z) {
return min(min(first, second), z);
}
template <typename T>
inline T min4(T first, T second, T z, T w) {
return min(min3(first, second, w), z);
}
template <typename T>
void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
class matrix {
public:
int row, col;
std::vector<std::vector<long long int>> num;
matrix(int row, int col, int defaultValue = 0) {
this->num = std::vector<std::vector<long long int>>(
row, std::vector<long long int>(col, defaultValue));
this->row = row, this->col = col;
}
matrix(std::vector<std::vector<long long int>> num) {
this->num = num;
this->row = this->num.size();
this->col = this->num[0].size();
}
matrix operator*(matrix &another) {
if (this->col != another.row) {
printf("Wrong size: %d*%d X %d*%d\n", this->row, this->col, another.row,
another.col);
throw "Wrong size";
}
matrix newone(this->row, another.col);
for (int r = 0; r < newone.row; r++)
for (int c = 0; c < newone.col; c++) {
for (int k = 0; k < this->col; k++) {
newone.num[r][c] =
(this->num[r][k] * another.num[k][c] + newone.num[r][c]) %
1000000007;
}
}
return newone;
}
matrix operator^(long long int first) {
if (first == 0) {
printf("Not implemented yet.\n");
throw "Not implemented";
} else if (first == 1)
return *this;
else {
matrix halfpower = (*this) ^ (first / 2);
if (first % 2 == 0)
return halfpower * halfpower;
else
return halfpower * halfpower * (*this);
}
}
};
bool a[2005];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int erer = 1;
for (int erer2 = (1); erer2 < (erer + 1); erer2++) {
long long int n, k;
cin >> n >> k;
int one = 0;
for (int i = (0); i < (n); i++) {
cin >> a[i];
one += a[i];
}
matrix tpm(one + 1, one + 1);
long long int now = n * (n - 1) / 2;
long long int inv = modInverse(now % 1000000007);
for (int i = (0); i < (one + 1); i++) {
int rone = i;
int rzero = one - i;
int lone = one - rone;
int lzero = n - one - rzero;
long long int sum = 0;
if (i < one) {
long long int temp = lone * rzero;
tpm.num[i][i + 1] = (temp * inv) % 1000000007;
sum += temp;
}
if (i) {
long long int temp = rone * lzero;
tpm.num[i][i - 1] = (temp * inv) % 1000000007;
sum += temp;
}
tpm.num[i][i] = ((now - sum) * inv) % 1000000007;
}
tpm = tpm ^ k;
int c = 0;
for (int i = (n)-1; i >= (n - one); i--) {
if (a[i]) c++;
}
cout << tpm.num[c][one];
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long int modadd(long long int n, long long int m,
long long int p = 1000000007) {
return ((n + m) % p + p) % p;
}
inline long long int modsub(long long int n, long long int m,
long long int p = 1000000007) {
return ((n - m + p) % p + p) % p;
}
inline long long int modpro(long long int n, long long int m,
long long int p = 1000000007) {
return (((n % p) * (m % p)) % p + p) % p;
}
unsigned long long int powe(long long int first, long long int second) {
unsigned long long int res = 1;
while (second > 0) {
if (second & 1) res = res * first;
second = second >> 1;
first = first * first;
}
return res;
}
long long int modpow(long long int first, long long int second,
long long int p = 1000000007) {
long long int res = 1;
while (second > 0) {
if (second & 1) res = modpro(res, first, p);
second = second >> 1;
first = modpro(first, first, p);
}
return res;
}
inline long long int modInverse(long long int n, long long int p = 1000000007) {
if (n == 1) return 1;
return modpow(n, p - 2, p);
}
inline long long int moddiv(long long int n, long long int m,
long long int p = 1000000007) {
return modpro(n, modInverse(m, p), p);
}
inline long long int modadd3(long long int first, long long int second,
long long int z, long long int p = 1000000007) {
return modadd(modadd(first, second, p), z, p);
}
inline long long int modadd4(long long int first, long long int second,
long long int z, long long int w,
long long int p = 1000000007) {
return modadd(modadd(first, second, p), modadd(z, w, p), p);
}
inline long long int modnCr(long long int fac[], int n, int r,
long long int p = 1000000007) {
if (r == 0) return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T>
inline T max3(T first, T second, T z) {
return max(max(first, second), z);
}
template <typename T>
inline T max4(T first, T second, T z, T w) {
return max(max3(first, second, w), z);
}
template <typename T>
inline T min3(T first, T second, T z) {
return min(min(first, second), z);
}
template <typename T>
inline T min4(T first, T second, T z, T w) {
return min(min3(first, second, w), z);
}
template <typename T>
void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
class matrix {
public:
int row, col;
std::vector<std::vector<long long int>> num;
matrix(int row, int col, int defaultValue = 0) {
this->num = std::vector<std::vector<long long int>>(
row, std::vector<long long int>(col, defaultValue));
this->row = row, this->col = col;
}
matrix(std::vector<std::vector<long long int>> num) {
this->num = num;
this->row = this->num.size();
this->col = this->num[0].size();
}
matrix operator*(matrix &another) {
if (this->col != another.row) {
printf("Wrong size: %d*%d X %d*%d\n", this->row, this->col, another.row,
another.col);
throw "Wrong size";
}
matrix newone(this->row, another.col);
for (int r = 0; r < newone.row; r++)
for (int c = 0; c < newone.col; c++) {
for (int k = 0; k < this->col; k++) {
newone.num[r][c] =
(this->num[r][k] * another.num[k][c] + newone.num[r][c]) %
1000000007;
}
}
return newone;
}
matrix operator^(long long int first) {
if (first == 0) {
printf("Not implemented yet.\n");
throw "Not implemented";
} else if (first == 1)
return *this;
else {
matrix halfpower = (*this) ^ (first / 2);
if (first % 2 == 0)
return halfpower * halfpower;
else
return halfpower * halfpower * (*this);
}
}
};
bool a[2005];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int erer = 1;
for (int erer2 = (1); erer2 < (erer + 1); erer2++) {
long long int n, k;
cin >> n >> k;
int one = 0;
for (int i = (0); i < (n); i++) {
cin >> a[i];
one += a[i];
}
matrix tpm(one + 1, one + 1);
long long int now = n * (n - 1) / 2;
long long int inv = modInverse(now % 1000000007);
for (int i = (0); i < (one + 1); i++) {
int rone = i;
int rzero = one - i;
int lone = one - rone;
int lzero = n - one - rzero;
long long int sum = 0;
if (i < one) {
long long int temp = lone * rzero;
tpm.num[i][i + 1] = (temp * inv) % 1000000007;
sum += temp;
}
if (i) {
long long int temp = rone * lzero;
tpm.num[i][i - 1] = (temp * inv) % 1000000007;
sum += temp;
}
tpm.num[i][i] = ((now - sum) * inv) % 1000000007;
}
tpm = tpm ^ k;
int c = 0;
for (int i = (n)-1; i >= (n - one); i--) {
if (a[i]) c++;
}
cout << tpm.num[c][one];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 105;
int n, K, m;
int a[N];
long long f[N][N];
long long Powx(long long a, int b) {
long long ans = 1;
while (b != 0) {
if (b % 2) ans = (ans * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return ans;
}
long long c[N][N];
void Chen(long long a[][N], long long b[][N]) {
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) c[i][j] = 0;
for (int k = 0; k <= m; k++)
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) a[i][j] = c[i][j];
}
long long ans[N][N];
void Pow(long long a[][N], int b) {
for (int i = 0; i <= m; i++) ans[i][i] = 1;
while (b != 0) {
if (b % 2) Chen(ans, a);
Chen(a, a);
b /= 2;
}
}
int main() {
cin >> n >> K;
m = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) m++;
}
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
if (m - i > n - m || m - j > n - m) continue;
if (abs(i - j) > 1) continue;
if (j == i - 1) f[i][j] = i * (n - m - (m - i));
if (j == i)
f[i][j] = m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + i * (m - i) +
(n - m - (m - i)) * (m - i);
if (j == i + 1) f[i][j] = (m - i) * (m - i);
}
Pow(f, K);
int x = 0;
for (int i = 1; i <= m; i++)
if (a[i] == 0) x++;
long long anss[N];
for (int i = 0; i <= m; i++) anss[i] = ans[x][i];
long long sum = 0;
for (int i = 0; i <= m; i++) sum = (sum + anss[i]) % mod;
cout << anss[m] * Powx(sum, mod - 2) % mod;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 105;
int n, K, m;
int a[N];
long long f[N][N];
long long Powx(long long a, int b) {
long long ans = 1;
while (b != 0) {
if (b % 2) ans = (ans * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return ans;
}
long long c[N][N];
void Chen(long long a[][N], long long b[][N]) {
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) c[i][j] = 0;
for (int k = 0; k <= m; k++)
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) a[i][j] = c[i][j];
}
long long ans[N][N];
void Pow(long long a[][N], int b) {
for (int i = 0; i <= m; i++) ans[i][i] = 1;
while (b != 0) {
if (b % 2) Chen(ans, a);
Chen(a, a);
b /= 2;
}
}
int main() {
cin >> n >> K;
m = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) m++;
}
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
if (m - i > n - m || m - j > n - m) continue;
if (abs(i - j) > 1) continue;
if (j == i - 1) f[i][j] = i * (n - m - (m - i));
if (j == i)
f[i][j] = m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + i * (m - i) +
(n - m - (m - i)) * (m - i);
if (j == i + 1) f[i][j] = (m - i) * (m - i);
}
Pow(f, K);
int x = 0;
for (int i = 1; i <= m; i++)
if (a[i] == 0) x++;
long long anss[N];
for (int i = 0; i <= m; i++) anss[i] = ans[x][i];
long long sum = 0;
for (int i = 0; i <= m; i++) sum = (sum + anss[i]) % mod;
cout << anss[m] * Powx(sum, mod - 2) % mod;
}
``` |
#include <bits/stdc++.h>
const long long md = 1e9 + 7;
const int Inf = 1e9;
const long long Inf64 = 1e18;
const long long MaxN = 200001;
const long long MaxM = 1000001;
const long double eps = 1e-15;
const long long dx[4] = {0, 1, 0, -1};
const long long dy[4] = {1, 0, -1, 0};
using namespace std;
long long gcd(long long a, long long b) {
while (a) {
b %= a;
swap(a, b);
}
return b;
}
long long gcdex(long long a, long long b, long long& x, long long& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
} else {
long long x1, y1;
long long d = gcdex(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
}
struct Mat {
long long a[105][105];
long long N;
Mat() { memset(a, 0, sizeof(a)); }
Mat(long long n) {
N = n;
memset(a, 0, sizeof(a));
}
Mat operator*(Mat b) {
Mat c = Mat(N);
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
for (int k = 0; k <= N; k++) {
c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % md;
}
}
}
return c;
}
};
Mat e_pow(Mat a, long long s) {
Mat c(a.N);
for (int i = 0; i <= a.N; i++) {
c.a[i][i] = 1;
}
while (s) {
if (s & 1) c = c * a;
a = a * a;
s >>= 1;
}
return c;
}
long long e_pow(long long a, long long s) {
long long c = 1;
while (s) {
if (s & 1) c = (c * a) % md;
a = (a * a) % md;
s >>= 1;
}
return c;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long N, K;
cin >> N >> K;
vector<long long> A(N);
long long ed = 0;
for (int i = 0; i < N; i++) {
cin >> A[i];
ed += A[i];
}
long long no = N - ed;
Mat Mt(N);
long long cnt = 0;
for (int i = 0; i < no; i++) {
cnt += A[i];
}
for (int i = 0; i <= N; i++) {
Mt.a[i][i] = no * (no - 1) / 2 + ed * (ed - 1) / 2;
if (no > i) Mt.a[i][i] += (no - i) * i;
if (ed > i) Mt.a[i][i] += (ed - i) * i;
if (i < N) {
if (ed > i && no > i) Mt.a[i][i + 1] = (ed - i) * (no - i);
}
if (i > 0) {
Mt.a[i][i - 1] = i * i;
}
}
Mt = e_pow(Mt, K);
cerr << e_pow(e_pow(N * (N - 1) / 2, K), md - 2);
long long Ans = Mt.a[cnt][0] * e_pow(e_pow(N * (N - 1) / 2, K), md - 2) % md;
cout << Ans;
cerr << '\n'
<< "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
<< '\n';
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
const long long md = 1e9 + 7;
const int Inf = 1e9;
const long long Inf64 = 1e18;
const long long MaxN = 200001;
const long long MaxM = 1000001;
const long double eps = 1e-15;
const long long dx[4] = {0, 1, 0, -1};
const long long dy[4] = {1, 0, -1, 0};
using namespace std;
long long gcd(long long a, long long b) {
while (a) {
b %= a;
swap(a, b);
}
return b;
}
long long gcdex(long long a, long long b, long long& x, long long& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
} else {
long long x1, y1;
long long d = gcdex(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
}
struct Mat {
long long a[105][105];
long long N;
Mat() { memset(a, 0, sizeof(a)); }
Mat(long long n) {
N = n;
memset(a, 0, sizeof(a));
}
Mat operator*(Mat b) {
Mat c = Mat(N);
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
for (int k = 0; k <= N; k++) {
c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % md;
}
}
}
return c;
}
};
Mat e_pow(Mat a, long long s) {
Mat c(a.N);
for (int i = 0; i <= a.N; i++) {
c.a[i][i] = 1;
}
while (s) {
if (s & 1) c = c * a;
a = a * a;
s >>= 1;
}
return c;
}
long long e_pow(long long a, long long s) {
long long c = 1;
while (s) {
if (s & 1) c = (c * a) % md;
a = (a * a) % md;
s >>= 1;
}
return c;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long N, K;
cin >> N >> K;
vector<long long> A(N);
long long ed = 0;
for (int i = 0; i < N; i++) {
cin >> A[i];
ed += A[i];
}
long long no = N - ed;
Mat Mt(N);
long long cnt = 0;
for (int i = 0; i < no; i++) {
cnt += A[i];
}
for (int i = 0; i <= N; i++) {
Mt.a[i][i] = no * (no - 1) / 2 + ed * (ed - 1) / 2;
if (no > i) Mt.a[i][i] += (no - i) * i;
if (ed > i) Mt.a[i][i] += (ed - i) * i;
if (i < N) {
if (ed > i && no > i) Mt.a[i][i + 1] = (ed - i) * (no - i);
}
if (i > 0) {
Mt.a[i][i - 1] = i * i;
}
}
Mt = e_pow(Mt, K);
cerr << e_pow(e_pow(N * (N - 1) / 2, K), md - 2);
long long Ans = Mt.a[cnt][0] * e_pow(e_pow(N * (N - 1) / 2, K), md - 2) % md;
cout << Ans;
cerr << '\n'
<< "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
<< '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int Mod = 1e9 + 7;
struct Matrix {
int n, m, a[146][146];
Matrix(int _n = 0, int _m = 0, int x = 0) {
n = _n, m = _m;
memset(a, 0, sizeof a);
for (int i = 0; i < min(n, m); ++i) a[i][i] = x;
}
Matrix operator*(const Matrix& b) {
Matrix res = Matrix(n, b.m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (a[i][j])
for (int k = 0; k < b.m; ++k)
(res.a[i][k] += 1ll * a[i][j] * b.a[j][k] % Mod) %= Mod;
return res;
}
Matrix operator^(int p) {
Matrix res = Matrix(n, n, 1), x = *this;
while (p) {
if (p & 1) res = res * x;
x = x * x;
p >>= 1;
}
return res;
}
};
inline int fsp(int x, int p = Mod - 2) {
int res = 1;
while (p) {
if (p & 1) res = 1ll * res * x % Mod;
x = 1ll * x * x % Mod;
p >>= 1;
}
return res;
}
int n, m, cnt, tmp, a[146];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
if (!a[i]) ++cnt;
}
for (int i = 1; i <= cnt; ++i) tmp += a[i] ^ 1;
Matrix base = Matrix(cnt + 1, cnt + 1), res;
for (int i = 0; i <= cnt; ++i) {
if (i < cnt) base.a[i + 1][i] += (cnt - i) * (cnt - i);
base.a[i][i] += cnt * (cnt - 1) / 2 + (n - cnt) * (n - cnt - 1) / 2 +
i * (cnt - i) + (cnt - i) * (n + i - 2 * cnt);
if (i) base.a[i - 1][i] += i * (n + i - 2 * cnt);
}
res = base ^ m;
long long ans = 0;
for (int i = 0; i <= cnt; ++i) (ans += res.a[i][tmp]) %= Mod;
cout << 1ll * res.a[cnt][tmp] * fsp(ans) % Mod << endl;
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int Mod = 1e9 + 7;
struct Matrix {
int n, m, a[146][146];
Matrix(int _n = 0, int _m = 0, int x = 0) {
n = _n, m = _m;
memset(a, 0, sizeof a);
for (int i = 0; i < min(n, m); ++i) a[i][i] = x;
}
Matrix operator*(const Matrix& b) {
Matrix res = Matrix(n, b.m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (a[i][j])
for (int k = 0; k < b.m; ++k)
(res.a[i][k] += 1ll * a[i][j] * b.a[j][k] % Mod) %= Mod;
return res;
}
Matrix operator^(int p) {
Matrix res = Matrix(n, n, 1), x = *this;
while (p) {
if (p & 1) res = res * x;
x = x * x;
p >>= 1;
}
return res;
}
};
inline int fsp(int x, int p = Mod - 2) {
int res = 1;
while (p) {
if (p & 1) res = 1ll * res * x % Mod;
x = 1ll * x * x % Mod;
p >>= 1;
}
return res;
}
int n, m, cnt, tmp, a[146];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
if (!a[i]) ++cnt;
}
for (int i = 1; i <= cnt; ++i) tmp += a[i] ^ 1;
Matrix base = Matrix(cnt + 1, cnt + 1), res;
for (int i = 0; i <= cnt; ++i) {
if (i < cnt) base.a[i + 1][i] += (cnt - i) * (cnt - i);
base.a[i][i] += cnt * (cnt - 1) / 2 + (n - cnt) * (n - cnt - 1) / 2 +
i * (cnt - i) + (cnt - i) * (n + i - 2 * cnt);
if (i) base.a[i - 1][i] += i * (n + i - 2 * cnt);
}
res = base ^ m;
long long ans = 0;
for (int i = 0; i <= cnt; ++i) (ans += res.a[i][tmp]) %= Mod;
cout << 1ll * res.a[cnt][tmp] * fsp(ans) % Mod << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
const long long MAX_N = 105;
struct Matrix {
long long mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++)
for (ans.mat[i][j] = k = 0; k < MAX_N; k++) {
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
ans.mat[i][j] %= MOD;
}
return ans;
}
Matrix matPow(Matrix base, long long p) {
p %= MOD;
Matrix ans;
long long i, j;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int fpow(int a, int p) {
if (p == 0) return 1;
long long z = fpow(a, p / 2);
z = (z * z) % MOD;
if (p % 2) z = (z * a) % MOD;
return z;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
reverse(v.begin(), v.end());
long long tot = 0;
for (int i = 0; i < n; ++i) {
if (v[i]) tot++;
}
long long cnt = 0;
for (int i = 0; i < tot; ++i) {
if (v[i]) cnt++;
}
Matrix m;
for (int i = 0; i < tot + 1; ++i) {
for (int j = 0; j < tot + 1; ++j) {
if (i + n - tot < tot) {
m.mat[i][j] = 0;
continue;
}
if (abs(i - j) > 1)
m.mat[i][j] = 0;
else if (i == j) {
m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 +
i * (tot - i) + (tot - i) * (n - 2 * tot + i);
} else if (i - j == 1) {
m.mat[i][j] = (i) * (n - 2 * tot + i);
} else if (j - i == 1) {
m.mat[i][j] = (tot - i) * (tot - i);
}
m.mat[i][j] %= MOD;
}
}
Matrix ans = matPow(m, k);
long long a1 = 0, a2 = 0;
a1 = ans.mat[cnt][tot];
for (int i = 0; i < tot + 1; ++i) {
a2 += ans.mat[cnt][i];
a2 %= MOD;
}
long long ansf = 0;
ansf = a1;
ansf *= fpow(a2, MOD - 2);
cout << (ansf) % MOD << '\n';
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
const long long MAX_N = 105;
struct Matrix {
long long mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++)
for (ans.mat[i][j] = k = 0; k < MAX_N; k++) {
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
ans.mat[i][j] %= MOD;
}
return ans;
}
Matrix matPow(Matrix base, long long p) {
p %= MOD;
Matrix ans;
long long i, j;
for (i = 0; i < MAX_N; i++)
for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int fpow(int a, int p) {
if (p == 0) return 1;
long long z = fpow(a, p / 2);
z = (z * z) % MOD;
if (p % 2) z = (z * a) % MOD;
return z;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
reverse(v.begin(), v.end());
long long tot = 0;
for (int i = 0; i < n; ++i) {
if (v[i]) tot++;
}
long long cnt = 0;
for (int i = 0; i < tot; ++i) {
if (v[i]) cnt++;
}
Matrix m;
for (int i = 0; i < tot + 1; ++i) {
for (int j = 0; j < tot + 1; ++j) {
if (i + n - tot < tot) {
m.mat[i][j] = 0;
continue;
}
if (abs(i - j) > 1)
m.mat[i][j] = 0;
else if (i == j) {
m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 +
i * (tot - i) + (tot - i) * (n - 2 * tot + i);
} else if (i - j == 1) {
m.mat[i][j] = (i) * (n - 2 * tot + i);
} else if (j - i == 1) {
m.mat[i][j] = (tot - i) * (tot - i);
}
m.mat[i][j] %= MOD;
}
}
Matrix ans = matPow(m, k);
long long a1 = 0, a2 = 0;
a1 = ans.mat[cnt][tot];
for (int i = 0; i < tot + 1; ++i) {
a2 += ans.mat[cnt][i];
a2 %= MOD;
}
long long ansf = 0;
ansf = a1;
ansf *= fpow(a2, MOD - 2);
cout << (ansf) % MOD << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int mod = 1e9 + 7;
int n, m, sum_0, right_sum_0;
int x[N];
long long a[N], b[N][N], c[N][N];
long long calc(long long x, long long y) {
long long z = 1;
while (y) {
if (y & 1) (z *= x) %= mod;
(x *= x) %= mod, y /= 2;
}
return z;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &x[i]);
sum_0 += (x[i] == 0);
}
for (int i = 1; i <= sum_0; i++) right_sum_0 += (!x[i]);
a[right_sum_0] = 1;
for (int i = 0; i <= sum_0; i++) {
int sum_1 = n - sum_0;
int right_sum_0 = i;
int n_right_sum_0 = sum_0 - right_sum_0;
int n_right_sum_1 = sum_0 - right_sum_0;
int right_sum_1 = n - sum_0 - n_right_sum_1;
b[i][i + 1] += n_right_sum_0 * n_right_sum_1;
b[i][i - 1] += right_sum_0 * right_sum_1;
b[i][i] += sum_0 * max(sum_0 - 1, 0) / 2 + sum_1 * max(sum_1 - 1, 0) / 2 +
right_sum_0 * n_right_sum_1 + right_sum_1 * n_right_sum_0;
}
int mm = m;
while (m) {
if (m & 1) {
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) (c[0][j] += a[i] * b[i][j]) %= mod;
for (int i = 0; i <= n; i++) a[i] = c[0][i], c[0][i] = 0;
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) (c[i][k] += b[i][j] * b[j][k]) %= mod;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) b[i][j] = c[i][j], c[i][j] = 0;
m /= 2;
}
printf("%lld\n", a[sum_0] * calc(calc(n * (n - 1) / 2, mm), mod - 2) % mod);
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int mod = 1e9 + 7;
int n, m, sum_0, right_sum_0;
int x[N];
long long a[N], b[N][N], c[N][N];
long long calc(long long x, long long y) {
long long z = 1;
while (y) {
if (y & 1) (z *= x) %= mod;
(x *= x) %= mod, y /= 2;
}
return z;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &x[i]);
sum_0 += (x[i] == 0);
}
for (int i = 1; i <= sum_0; i++) right_sum_0 += (!x[i]);
a[right_sum_0] = 1;
for (int i = 0; i <= sum_0; i++) {
int sum_1 = n - sum_0;
int right_sum_0 = i;
int n_right_sum_0 = sum_0 - right_sum_0;
int n_right_sum_1 = sum_0 - right_sum_0;
int right_sum_1 = n - sum_0 - n_right_sum_1;
b[i][i + 1] += n_right_sum_0 * n_right_sum_1;
b[i][i - 1] += right_sum_0 * right_sum_1;
b[i][i] += sum_0 * max(sum_0 - 1, 0) / 2 + sum_1 * max(sum_1 - 1, 0) / 2 +
right_sum_0 * n_right_sum_1 + right_sum_1 * n_right_sum_0;
}
int mm = m;
while (m) {
if (m & 1) {
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) (c[0][j] += a[i] * b[i][j]) %= mod;
for (int i = 0; i <= n; i++) a[i] = c[0][i], c[0][i] = 0;
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) (c[i][k] += b[i][j] * b[j][k]) %= mod;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) b[i][j] = c[i][j], c[i][j] = 0;
m /= 2;
}
printf("%lld\n", a[sum_0] * calc(calc(n * (n - 1) / 2, mm), mod - 2) % mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int w = 0, x = 0;
char c = getchar();
while (!isdigit(c)) w |= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return w ? -x : x;
}
namespace star {
const int maxn = 105, mod = 1e9 + 7;
int n, m, k, a[maxn];
inline int fpow(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) ans = 1ll * ans * a % mod;
return ans;
}
struct mat {
int a[maxn][maxn];
mat() { memset(a, 0, sizeof a); }
inline void set() {
for (int i = 0; i <= n; i++) a[i][i] = 1;
}
inline const int *operator[](const int &x) const { return a[x]; }
inline int *operator[](const int &x) { return a[x]; }
inline mat operator*(const mat &b) {
mat ans;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++)
ans[i][j] = (ans[i][j] + 1ll * a[i][k] * b[k][j] % mod) % mod;
return ans;
}
} now, pow;
inline mat fpow(mat a, int b) {
mat ans;
ans.set();
for (; b; b >>= 1, a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
inline void work() {
m = read();
k = read();
for (int i = 1; i <= m; i++) n += (a[i] = read()) == 0;
int t = 0;
for (int i = 1; i <= n; i++) t += a[i] == 0;
now[0][t] = 1;
for (int i = 0; i <= n; i++) {
if (i) pow[i - 1][i] = 1ll * (n - i + 1) * (n - i + 1) % mod;
pow[i][i] =
(1ll * i * (n - i) % mod + 1ll * (n - i) * (m - n - n + i) % mod) % mod;
pow[i][i] = (pow[i][i] + 1ll * n * (n - 1) / 2 % mod) % mod;
pow[i][i] = (pow[i][i] + 1ll * (m - n) * (m - n - 1) / 2 % mod) % mod;
if (i != n) pow[i + 1][i] = 1ll * (i + 1) * (m - n - n + i + 1) % mod;
}
now = now * fpow(pow, k);
int up = now[0][n], down = 0;
for (int i = 0; i <= n; i++) down = (down + now[0][i]) % mod;
printf("%lld\n", 1ll * up * fpow(down, mod - 2) % mod);
}
} // namespace star
signed main() {
star::work();
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int w = 0, x = 0;
char c = getchar();
while (!isdigit(c)) w |= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return w ? -x : x;
}
namespace star {
const int maxn = 105, mod = 1e9 + 7;
int n, m, k, a[maxn];
inline int fpow(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) ans = 1ll * ans * a % mod;
return ans;
}
struct mat {
int a[maxn][maxn];
mat() { memset(a, 0, sizeof a); }
inline void set() {
for (int i = 0; i <= n; i++) a[i][i] = 1;
}
inline const int *operator[](const int &x) const { return a[x]; }
inline int *operator[](const int &x) { return a[x]; }
inline mat operator*(const mat &b) {
mat ans;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++)
ans[i][j] = (ans[i][j] + 1ll * a[i][k] * b[k][j] % mod) % mod;
return ans;
}
} now, pow;
inline mat fpow(mat a, int b) {
mat ans;
ans.set();
for (; b; b >>= 1, a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
inline void work() {
m = read();
k = read();
for (int i = 1; i <= m; i++) n += (a[i] = read()) == 0;
int t = 0;
for (int i = 1; i <= n; i++) t += a[i] == 0;
now[0][t] = 1;
for (int i = 0; i <= n; i++) {
if (i) pow[i - 1][i] = 1ll * (n - i + 1) * (n - i + 1) % mod;
pow[i][i] =
(1ll * i * (n - i) % mod + 1ll * (n - i) * (m - n - n + i) % mod) % mod;
pow[i][i] = (pow[i][i] + 1ll * n * (n - 1) / 2 % mod) % mod;
pow[i][i] = (pow[i][i] + 1ll * (m - n) * (m - n - 1) / 2 % mod) % mod;
if (i != n) pow[i + 1][i] = 1ll * (i + 1) * (m - n - n + i + 1) % mod;
}
now = now * fpow(pow, k);
int up = now[0][n], down = 0;
for (int i = 0; i <= n; i++) down = (down + now[0][i]) % mod;
printf("%lld\n", 1ll * up * fpow(down, mod - 2) % mod);
}
} // namespace star
signed main() {
star::work();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct Matrix {
int sz;
vector<vector<long long> > arr;
Matrix() {}
Matrix(int is) {
sz = is;
arr = vector<vector<long long> >(sz + 1, vector<long long>(sz + 1, 0));
}
void unitize() {
for (int i = 0; i <= sz; i++) {
for (int j = 0; j <= sz; j++) {
arr[i][j] = (i == j);
}
}
}
Matrix operator*(const Matrix &a) {
Matrix c(sz);
for (int i = 0; i <= sz; i++) {
for (int j = 0; j <= sz; j++) {
for (int k = 0; k <= sz; k++) {
(c.arr[i][j] += arr[i][k] * a.arr[k][j]) %= mod;
}
}
}
return c;
}
Matrix operator+(const Matrix &a) {
Matrix c(sz);
for (int i = 0; i <= sz; i++) {
for (int j = 0; j <= sz; j++) {
c.arr[i][j] = arr[i][j] + a.arr[i][j];
if (c.arr[i][j] >= mod) c.arr[i][j] -= mod;
}
}
return c;
}
};
int a[105];
long long modPow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) (ans *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ans;
}
Matrix powerup(Matrix a, long long b) {
Matrix ans(a.sz);
ans.unitize();
while (b) {
if (b & 1) (ans = ans * a);
(a = a * a);
b >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(32);
int n, k;
cin >> n >> k;
int m = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
m -= a[i];
}
if (m == n || m == 0) {
cout << 1 << endl;
return 0;
}
Matrix A(m);
for (int i = 0; i <= m; i++) {
long long tmp = m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + i * (m - i) +
(m - i) * (n + i - 2 * m);
tmp *= modPow(n * (n - 1) / 2, mod - 2);
tmp %= mod;
A.arr[i][i] = tmp;
if (i != m) {
A.arr[i][i + 1] =
(m - i) * (m - i) * modPow(n * (n - 1) / 2, mod - 2) % mod;
}
if (i != 0) {
A.arr[i][i - 1] =
i * (n + i - 2 * m) * modPow(n * (n - 1) / 2, mod - 2) % mod;
}
}
Matrix B = powerup(A, k);
int p = m;
for (int i = 1; i <= m; i++) {
p -= a[i];
}
cout << B.arr[p][m] << endl;
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct Matrix {
int sz;
vector<vector<long long> > arr;
Matrix() {}
Matrix(int is) {
sz = is;
arr = vector<vector<long long> >(sz + 1, vector<long long>(sz + 1, 0));
}
void unitize() {
for (int i = 0; i <= sz; i++) {
for (int j = 0; j <= sz; j++) {
arr[i][j] = (i == j);
}
}
}
Matrix operator*(const Matrix &a) {
Matrix c(sz);
for (int i = 0; i <= sz; i++) {
for (int j = 0; j <= sz; j++) {
for (int k = 0; k <= sz; k++) {
(c.arr[i][j] += arr[i][k] * a.arr[k][j]) %= mod;
}
}
}
return c;
}
Matrix operator+(const Matrix &a) {
Matrix c(sz);
for (int i = 0; i <= sz; i++) {
for (int j = 0; j <= sz; j++) {
c.arr[i][j] = arr[i][j] + a.arr[i][j];
if (c.arr[i][j] >= mod) c.arr[i][j] -= mod;
}
}
return c;
}
};
int a[105];
long long modPow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) (ans *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ans;
}
Matrix powerup(Matrix a, long long b) {
Matrix ans(a.sz);
ans.unitize();
while (b) {
if (b & 1) (ans = ans * a);
(a = a * a);
b >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(32);
int n, k;
cin >> n >> k;
int m = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
m -= a[i];
}
if (m == n || m == 0) {
cout << 1 << endl;
return 0;
}
Matrix A(m);
for (int i = 0; i <= m; i++) {
long long tmp = m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + i * (m - i) +
(m - i) * (n + i - 2 * m);
tmp *= modPow(n * (n - 1) / 2, mod - 2);
tmp %= mod;
A.arr[i][i] = tmp;
if (i != m) {
A.arr[i][i + 1] =
(m - i) * (m - i) * modPow(n * (n - 1) / 2, mod - 2) % mod;
}
if (i != 0) {
A.arr[i][i - 1] =
i * (n + i - 2 * m) * modPow(n * (n - 1) / 2, mod - 2) % mod;
}
}
Matrix B = powerup(A, k);
int p = m;
for (int i = 1; i <= m; i++) {
p -= a[i];
}
cout << B.arr[p][m] << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline long long int pow(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = res * a % 1000000007;
b >>= 1;
a = a * a % 1000000007;
}
return res;
}
inline long long int inv(long long int x) { return pow(x, 1000000007 - 2); }
int n, k;
int a[100], cnt0 = 0, p = 0;
struct matrix {
long long int mat[101][101];
} m;
matrix operator*(matrix m1, matrix m2) {
matrix res;
memset(res.mat, 0, sizeof(res.mat));
for (int i = 0; i <= cnt0; i++)
for (int j = 0; j <= cnt0; j++)
for (int t = 0; t <= cnt0; t++)
res.mat[i][j] =
(res.mat[i][j] + m1.mat[i][t] * m2.mat[t][j]) % 1000000007;
return res;
}
matrix matrix_pow(matrix x, int t) {
matrix ans;
for (int i = 0; i <= cnt0; i++) ans.mat[i][i] = 1;
while (t > 0) {
if (t & 1) ans = ans * x;
x = x * x;
t >>= 1;
}
return ans;
}
int main(void) {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) cnt0++;
}
for (int i = 0; i < cnt0; i++)
if (a[i] == 0) p++;
long long int f = inv(n * (n - 1)) * 2 % 1000000007;
for (int i = max(cnt0 * 2 - n, 0); i <= cnt0; i++) {
m.mat[i][i] = 1 + 1000000007 * 2;
if (i != 0) {
m.mat[i][i - 1] = i * (n - cnt0 * 2 + i) * f % 1000000007;
m.mat[i][i] -= m.mat[i][i - 1];
}
if (i != cnt0) {
m.mat[i][i + 1] = (cnt0 - i) * (cnt0 - i) * f % 1000000007;
m.mat[i][i] -= m.mat[i][i + 1];
}
m.mat[i][i] %= 1000000007;
}
m = matrix_pow(m, k);
printf("%lld\n", m.mat[p][cnt0]);
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long int pow(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = res * a % 1000000007;
b >>= 1;
a = a * a % 1000000007;
}
return res;
}
inline long long int inv(long long int x) { return pow(x, 1000000007 - 2); }
int n, k;
int a[100], cnt0 = 0, p = 0;
struct matrix {
long long int mat[101][101];
} m;
matrix operator*(matrix m1, matrix m2) {
matrix res;
memset(res.mat, 0, sizeof(res.mat));
for (int i = 0; i <= cnt0; i++)
for (int j = 0; j <= cnt0; j++)
for (int t = 0; t <= cnt0; t++)
res.mat[i][j] =
(res.mat[i][j] + m1.mat[i][t] * m2.mat[t][j]) % 1000000007;
return res;
}
matrix matrix_pow(matrix x, int t) {
matrix ans;
for (int i = 0; i <= cnt0; i++) ans.mat[i][i] = 1;
while (t > 0) {
if (t & 1) ans = ans * x;
x = x * x;
t >>= 1;
}
return ans;
}
int main(void) {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) cnt0++;
}
for (int i = 0; i < cnt0; i++)
if (a[i] == 0) p++;
long long int f = inv(n * (n - 1)) * 2 % 1000000007;
for (int i = max(cnt0 * 2 - n, 0); i <= cnt0; i++) {
m.mat[i][i] = 1 + 1000000007 * 2;
if (i != 0) {
m.mat[i][i - 1] = i * (n - cnt0 * 2 + i) * f % 1000000007;
m.mat[i][i] -= m.mat[i][i - 1];
}
if (i != cnt0) {
m.mat[i][i + 1] = (cnt0 - i) * (cnt0 - i) * f % 1000000007;
m.mat[i][i] -= m.mat[i][i + 1];
}
m.mat[i][i] %= 1000000007;
}
m = matrix_pow(m, k);
printf("%lld\n", m.mat[p][cnt0]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
vector<vector<long long> > mulmat(vector<vector<long long> > &mat1,
vector<vector<long long> > &mat2) {
vector<vector<long long> > ans(mat1.size(),
vector<long long>(mat2[0].size(), 0));
for (int i = 0; i < mat1.size(); i++) {
for (int j = 0; j < mat2[0].size(); j++) {
for (int k = 0; k < mat2.size(); k++) {
ans[i][j] = (ans[i][j] + mat1[i][k] * mat2[k][j]) % mod;
}
}
}
return ans;
}
vector<vector<long long> > matrixpow(vector<vector<long long> > &mat,
long long pow) {
int n = mat.size();
vector<vector<long long> > bas(mat.size(), vector<long long>(mat.size(), 0));
vector<vector<long long> > mul(mat);
for (int i = 0; i < mat.size(); i++) {
bas[i][i] = 1;
}
while (pow) {
if (pow & 1) bas = mulmat(bas, mul);
pow >>= 1;
mul = mulmat(mul, mul);
}
return bas;
}
long long fastpow(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;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k, zc = 0;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) zc++;
}
vector<vector<long long> > vals(zc + 1, vector<long long>(zc + 1, 0));
for (int i = 0; i <= zc; i++) {
long long zer = i;
long long on = zc - zer;
long long zero = zc - i;
long long ono = n - zc - zero;
if (on < 0 || zero < 0 || ono < 0) continue;
if (i) vals[zer][zer - 1] = ono * zer;
vals[zer][zer] = n * (n - 1) / 2 - ono * zer - zero * on;
if (i != zc) vals[zer][zer + 1] = zero * on;
}
vector<vector<long long> > fmat = matrixpow(vals, k);
vector<vector<long long> > start(1, vector<long long>(zc + 1, 0));
int zco = 0;
for (int i = 0; i < zc; i++)
if (a[i] == 0) zco++;
start[0][zco] = 1;
vector<vector<long long> > finb = mulmat(start, fmat);
long long num = finb[0][zc];
long long den = 0;
for (int i = 0; i <= zc; i++) den = (den + finb[0][i]) % mod;
den = fastpow(den, mod - 2);
cout << (num * den) % mod << '\n';
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
vector<vector<long long> > mulmat(vector<vector<long long> > &mat1,
vector<vector<long long> > &mat2) {
vector<vector<long long> > ans(mat1.size(),
vector<long long>(mat2[0].size(), 0));
for (int i = 0; i < mat1.size(); i++) {
for (int j = 0; j < mat2[0].size(); j++) {
for (int k = 0; k < mat2.size(); k++) {
ans[i][j] = (ans[i][j] + mat1[i][k] * mat2[k][j]) % mod;
}
}
}
return ans;
}
vector<vector<long long> > matrixpow(vector<vector<long long> > &mat,
long long pow) {
int n = mat.size();
vector<vector<long long> > bas(mat.size(), vector<long long>(mat.size(), 0));
vector<vector<long long> > mul(mat);
for (int i = 0; i < mat.size(); i++) {
bas[i][i] = 1;
}
while (pow) {
if (pow & 1) bas = mulmat(bas, mul);
pow >>= 1;
mul = mulmat(mul, mul);
}
return bas;
}
long long fastpow(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;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k, zc = 0;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) zc++;
}
vector<vector<long long> > vals(zc + 1, vector<long long>(zc + 1, 0));
for (int i = 0; i <= zc; i++) {
long long zer = i;
long long on = zc - zer;
long long zero = zc - i;
long long ono = n - zc - zero;
if (on < 0 || zero < 0 || ono < 0) continue;
if (i) vals[zer][zer - 1] = ono * zer;
vals[zer][zer] = n * (n - 1) / 2 - ono * zer - zero * on;
if (i != zc) vals[zer][zer + 1] = zero * on;
}
vector<vector<long long> > fmat = matrixpow(vals, k);
vector<vector<long long> > start(1, vector<long long>(zc + 1, 0));
int zco = 0;
for (int i = 0; i < zc; i++)
if (a[i] == 0) zco++;
start[0][zco] = 1;
vector<vector<long long> > finb = mulmat(start, fmat);
long long num = finb[0][zc];
long long den = 0;
for (int i = 0; i <= zc; i++) den = (den + finb[0][i]) % mod;
den = fastpow(den, mod - 2);
cout << (num * den) % mod << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 107, MOD = 1e9 + 7;
int a[MAXN], o = 0, z, x, n, k, dp[MAXN][MAXN] = {};
int fir[MAXN], ans[MAXN];
void add(int &a, const int &b) {
a += b;
if (a >= MOD) a -= MOD;
}
struct matrix {
int f[MAXN][MAXN] = {};
matrix operator*(matrix &b) {
matrix c;
for (int i = 0; i <= z; ++i)
for (int j = 0; j <= z; ++j) {
c.f[i][j] = 0;
for (int k = 0; k <= z; ++k)
add(c.f[i][j], 1LL * this->f[i][k] * b.f[k][j] % MOD);
}
return c;
}
} base;
matrix pw(matrix cs, int sm) {
if (sm == 1) return cs;
matrix hpw = pw(cs, sm >> 1);
hpw = hpw * hpw;
if (sm & 1) hpw = hpw * cs;
return hpw;
}
int pw(int cs, int sm) {
if (sm == 0) return 1;
int hpw = pw(cs, sm >> 1);
hpw = 1LL * hpw * hpw % MOD;
if (sm & 1) hpw = 1LL * hpw * cs % MOD;
return hpw;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], o += a[i];
z = n - o, x = z;
for (int i = 1; i <= z; ++i) x -= a[i];
if (z == 0) return cout << 1, 0;
fir[x] = 1;
for (int i = 0; i <= z; ++i) {
if (i > 0) {
base.f[i - 1][i] = (z - i + 1) * (z - i + 1);
}
if (i < z) {
base.f[i + 1][i] = (o - z + i + 1) * (i + 1);
}
base.f[i][i] += z * (z - 1) / 2;
base.f[i][i] += (n - z) * (n - z - 1) / 2;
base.f[i][i] += i * (z - i);
base.f[i][i] += (z - i) * (o - z + i);
}
base = pw(base, k);
for (int j = 0; j <= z; ++j) {
ans[j] = 0;
for (int k = 0; k <= z; ++k) add(ans[j], 1LL * fir[k] * base.f[k][j] % MOD);
}
int mau = pw(n * (n - 1) / 2, MOD - 2);
cout << 1LL * ans[z] * pw(mau, k) % MOD;
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 107, MOD = 1e9 + 7;
int a[MAXN], o = 0, z, x, n, k, dp[MAXN][MAXN] = {};
int fir[MAXN], ans[MAXN];
void add(int &a, const int &b) {
a += b;
if (a >= MOD) a -= MOD;
}
struct matrix {
int f[MAXN][MAXN] = {};
matrix operator*(matrix &b) {
matrix c;
for (int i = 0; i <= z; ++i)
for (int j = 0; j <= z; ++j) {
c.f[i][j] = 0;
for (int k = 0; k <= z; ++k)
add(c.f[i][j], 1LL * this->f[i][k] * b.f[k][j] % MOD);
}
return c;
}
} base;
matrix pw(matrix cs, int sm) {
if (sm == 1) return cs;
matrix hpw = pw(cs, sm >> 1);
hpw = hpw * hpw;
if (sm & 1) hpw = hpw * cs;
return hpw;
}
int pw(int cs, int sm) {
if (sm == 0) return 1;
int hpw = pw(cs, sm >> 1);
hpw = 1LL * hpw * hpw % MOD;
if (sm & 1) hpw = 1LL * hpw * cs % MOD;
return hpw;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], o += a[i];
z = n - o, x = z;
for (int i = 1; i <= z; ++i) x -= a[i];
if (z == 0) return cout << 1, 0;
fir[x] = 1;
for (int i = 0; i <= z; ++i) {
if (i > 0) {
base.f[i - 1][i] = (z - i + 1) * (z - i + 1);
}
if (i < z) {
base.f[i + 1][i] = (o - z + i + 1) * (i + 1);
}
base.f[i][i] += z * (z - 1) / 2;
base.f[i][i] += (n - z) * (n - z - 1) / 2;
base.f[i][i] += i * (z - i);
base.f[i][i] += (z - i) * (o - z + i);
}
base = pw(base, k);
for (int j = 0; j <= z; ++j) {
ans[j] = 0;
for (int k = 0; k <= z; ++k) add(ans[j], 1LL * fir[k] * base.f[k][j] % MOD);
}
int mau = pw(n * (n - 1) / 2, MOD - 2);
cout << 1LL * ans[z] * pw(mau, k) % MOD;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int a[101][101];
long long int res[101][101], tmp2[101][101];
const int mod = 1e9 + 7;
long long int mypow(long long int x, long long int b) {
long long int res = 1;
while (b) {
if (b & 1) res = res * x % mod;
x = x * x % mod;
b >>= 1;
}
return res;
}
void ex(int n, long long int b) {
for (int i = 0; i < n; i++) res[i][i] = 1;
while (b) {
if (b & 1) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) tmp2[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
tmp2[i][j] = (tmp2[i][j] + res[i][k] * a[k][j]) % mod;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) res[i][j] = tmp2[i][j];
}
b >>= 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) tmp2[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
tmp2[i][j] = (tmp2[i][j] + a[i][k] * a[k][j]) % mod;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) a[i][j] = tmp2[i][j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) a[i][j] = res[i][j];
}
int v[101];
int c[101];
long long int cnt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, k;
cin >> N >> k;
long long int tmp = N * (N - 1) / 2;
for (int i = 0; i < N; i++) cin >> v[i];
for (int i = 0; i < N; i++)
if (v[i] == 0) cnt++;
int tttmp = 0;
for (int i = cnt; i < N; i++)
if (v[i] == 1) tttmp++;
for (int j = 0; j <= N; j++) {
if (j > N - cnt || N - j - cnt > cnt) continue;
long long int now = tmp;
for (int i = 0; i <= N; i++) {
if (i == j - 1) {
now = ((now - (2 * cnt + j - N) * j) % mod + mod) % mod;
a[i][j] = ((2 * cnt + j - N) * j) % mod * mypow(tmp, mod - 2) % mod;
} else if (i == j + 1) {
now = ((now - (N - cnt - j) * (N - cnt - j)) % mod + mod) % mod;
a[i][j] =
((N - cnt - j) * (N - cnt - j) % mod * mypow(tmp, mod - 2)) % mod;
}
}
a[j][j] = now * mypow(tmp, mod - 2) % mod;
}
ex(N + 1, k);
cout << a[N - cnt][tttmp] << endl;
}
| ### Prompt
Generate a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int a[101][101];
long long int res[101][101], tmp2[101][101];
const int mod = 1e9 + 7;
long long int mypow(long long int x, long long int b) {
long long int res = 1;
while (b) {
if (b & 1) res = res * x % mod;
x = x * x % mod;
b >>= 1;
}
return res;
}
void ex(int n, long long int b) {
for (int i = 0; i < n; i++) res[i][i] = 1;
while (b) {
if (b & 1) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) tmp2[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
tmp2[i][j] = (tmp2[i][j] + res[i][k] * a[k][j]) % mod;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) res[i][j] = tmp2[i][j];
}
b >>= 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) tmp2[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
tmp2[i][j] = (tmp2[i][j] + a[i][k] * a[k][j]) % mod;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) a[i][j] = tmp2[i][j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) a[i][j] = res[i][j];
}
int v[101];
int c[101];
long long int cnt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, k;
cin >> N >> k;
long long int tmp = N * (N - 1) / 2;
for (int i = 0; i < N; i++) cin >> v[i];
for (int i = 0; i < N; i++)
if (v[i] == 0) cnt++;
int tttmp = 0;
for (int i = cnt; i < N; i++)
if (v[i] == 1) tttmp++;
for (int j = 0; j <= N; j++) {
if (j > N - cnt || N - j - cnt > cnt) continue;
long long int now = tmp;
for (int i = 0; i <= N; i++) {
if (i == j - 1) {
now = ((now - (2 * cnt + j - N) * j) % mod + mod) % mod;
a[i][j] = ((2 * cnt + j - N) * j) % mod * mypow(tmp, mod - 2) % mod;
} else if (i == j + 1) {
now = ((now - (N - cnt - j) * (N - cnt - j)) % mod + mod) % mod;
a[i][j] =
((N - cnt - j) * (N - cnt - j) % mod * mypow(tmp, mod - 2)) % mod;
}
}
a[j][j] = now * mypow(tmp, mod - 2) % mod;
}
ex(N + 1, k);
cout << a[N - cnt][tttmp] << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
void add(int64_t& a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t& a, int64_t b) { a = (a * b) % MOD; }
int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
int64_t d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
int64_t inv_mod(int64_t a) {
int64_t x, y;
extgcd(a, MOD, x, y);
return (MOD + x % MOD) % MOD;
}
const int SZ = 100;
void matmul(int64_t A[SZ][SZ], int64_t B[SZ][SZ]) {
int64_t ret[SZ][SZ] = {0};
for (int i = 0; i < SZ; i++)
for (int j = 0; j < SZ; j++)
for (int k = 0; k < SZ; k++) add(ret[i][j], A[i][k] * B[k][j]);
for (int i = 0; i < SZ; i++)
for (int j = 0; j < SZ; j++) A[i][j] = ret[i][j];
}
int main() {
int N, K, A[100];
cin >> N >> K;
for (int i = 0; i < N; i++) cin >> A[i];
int one = accumulate(A, A + N, 0);
int zero = N - one;
int d = 0;
for (int i = 0; i < zero; i++) d += A[i];
int64_t M[100][100] = {0}, S[100][100] = {0};
for (int i = 0; i <= min(one, zero); i++) {
S[i][i] = 1;
M[i][i] = one * (one - 1) / 2 + zero * (zero - 1) / 2 + i * (N - 2 * i);
if (i < min(one, zero)) M[i + 1][i] = (zero - i) * (one - i);
if (i > 0) M[i - 1][i] = i * i;
}
int64_t c = 1, base = N * (N - 1) / 2;
while (K > 0) {
if (K & 1) {
mul(c, base);
matmul(S, M);
}
matmul(M, M);
mul(base, base);
K /= 2;
}
int64_t ans = S[0][d];
mul(ans, inv_mod(c));
cout << ans << endl;
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
void add(int64_t& a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t& a, int64_t b) { a = (a * b) % MOD; }
int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
int64_t d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
int64_t inv_mod(int64_t a) {
int64_t x, y;
extgcd(a, MOD, x, y);
return (MOD + x % MOD) % MOD;
}
const int SZ = 100;
void matmul(int64_t A[SZ][SZ], int64_t B[SZ][SZ]) {
int64_t ret[SZ][SZ] = {0};
for (int i = 0; i < SZ; i++)
for (int j = 0; j < SZ; j++)
for (int k = 0; k < SZ; k++) add(ret[i][j], A[i][k] * B[k][j]);
for (int i = 0; i < SZ; i++)
for (int j = 0; j < SZ; j++) A[i][j] = ret[i][j];
}
int main() {
int N, K, A[100];
cin >> N >> K;
for (int i = 0; i < N; i++) cin >> A[i];
int one = accumulate(A, A + N, 0);
int zero = N - one;
int d = 0;
for (int i = 0; i < zero; i++) d += A[i];
int64_t M[100][100] = {0}, S[100][100] = {0};
for (int i = 0; i <= min(one, zero); i++) {
S[i][i] = 1;
M[i][i] = one * (one - 1) / 2 + zero * (zero - 1) / 2 + i * (N - 2 * i);
if (i < min(one, zero)) M[i + 1][i] = (zero - i) * (one - i);
if (i > 0) M[i - 1][i] = i * i;
}
int64_t c = 1, base = N * (N - 1) / 2;
while (K > 0) {
if (K & 1) {
mul(c, base);
matmul(S, M);
}
matmul(M, M);
mul(base, base);
K /= 2;
}
int64_t ans = S[0][d];
mul(ans, inv_mod(c));
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const long long P = 1e9 + 7;
inline long long add(long long x, long long y) {
x += y;
return x >= P ? x - P : x;
}
inline long long sub(long long x, long long y) {
x -= y;
return x < 0 ? x + P : x;
}
map<long long, long long> INV;
inline long long inv(long long x) {
if (!INV[x]) {
int y = P - 2;
long long res = 1;
while (y) {
if (y & 1) res = res * x % P;
x = x * x % P;
y >>= 1;
}
INV[x] = res;
}
return INV[x];
}
struct Matrix {
long long w[N][N];
int lim;
void clear(int x) {
lim = x;
memset(w, 0, sizeof(w));
}
void init(int x) {
clear(x);
for (int i = 0; i <= lim; ++i) w[i][i] = 1;
}
friend Matrix operator*(Matrix x, Matrix y) {
Matrix z;
z.clear(x.lim);
for (int i = 0; i <= z.lim; ++i)
for (int j = 0; j <= z.lim; ++j)
for (int k = 0; k <= z.lim; ++k)
z.w[i][j] = add(z.w[i][j], x.w[i][k] * y.w[k][j] % P);
return z;
}
void print() {
for (int i = 0; i <= lim; ++i) {
for (int j = 0; j <= lim; ++j) printf("%d ", w[i][j]);
printf("\n");
}
}
} beg, chg;
int n, m, a[N], cnt;
inline Matrix mpow(Matrix x, int y) {
Matrix res;
res.init(cnt);
while (y) {
if (y & 1) res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), cnt += (a[i] ^ 1);
beg.clear(cnt);
chg.clear(cnt);
int tt = 0;
for (int i = 1; i <= cnt; ++i) tt += (a[i] ^ 1);
beg.w[tt][0] = 1;
for (int i = 0; i <= cnt; ++i)
if (n + i >= (cnt << 1)) {
long long c00 = i, c01 = cnt - i, c10 = cnt - i,
c11 = 1ll * n - c00 - c01 - c10;
if (i < cnt)
chg.w[i + 1][i] = 1ll * (1ll * c01 * c10 % P) *
inv(1ll * n * (n - 1) % P * inv(2) % P) % P;
if (i)
chg.w[i - 1][i] = 1ll * (1ll * c00 * c11 % P) *
inv(1ll * n * (n - 1) % P * inv(2) % P) % P;
chg.w[i][i] = sub(1, add(chg.w[i + 1][i], chg.w[i - 1][i]));
}
Matrix res = mpow(chg, m);
res = res * beg;
printf("%lld\n", res.w[cnt][0]);
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const long long P = 1e9 + 7;
inline long long add(long long x, long long y) {
x += y;
return x >= P ? x - P : x;
}
inline long long sub(long long x, long long y) {
x -= y;
return x < 0 ? x + P : x;
}
map<long long, long long> INV;
inline long long inv(long long x) {
if (!INV[x]) {
int y = P - 2;
long long res = 1;
while (y) {
if (y & 1) res = res * x % P;
x = x * x % P;
y >>= 1;
}
INV[x] = res;
}
return INV[x];
}
struct Matrix {
long long w[N][N];
int lim;
void clear(int x) {
lim = x;
memset(w, 0, sizeof(w));
}
void init(int x) {
clear(x);
for (int i = 0; i <= lim; ++i) w[i][i] = 1;
}
friend Matrix operator*(Matrix x, Matrix y) {
Matrix z;
z.clear(x.lim);
for (int i = 0; i <= z.lim; ++i)
for (int j = 0; j <= z.lim; ++j)
for (int k = 0; k <= z.lim; ++k)
z.w[i][j] = add(z.w[i][j], x.w[i][k] * y.w[k][j] % P);
return z;
}
void print() {
for (int i = 0; i <= lim; ++i) {
for (int j = 0; j <= lim; ++j) printf("%d ", w[i][j]);
printf("\n");
}
}
} beg, chg;
int n, m, a[N], cnt;
inline Matrix mpow(Matrix x, int y) {
Matrix res;
res.init(cnt);
while (y) {
if (y & 1) res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), cnt += (a[i] ^ 1);
beg.clear(cnt);
chg.clear(cnt);
int tt = 0;
for (int i = 1; i <= cnt; ++i) tt += (a[i] ^ 1);
beg.w[tt][0] = 1;
for (int i = 0; i <= cnt; ++i)
if (n + i >= (cnt << 1)) {
long long c00 = i, c01 = cnt - i, c10 = cnt - i,
c11 = 1ll * n - c00 - c01 - c10;
if (i < cnt)
chg.w[i + 1][i] = 1ll * (1ll * c01 * c10 % P) *
inv(1ll * n * (n - 1) % P * inv(2) % P) % P;
if (i)
chg.w[i - 1][i] = 1ll * (1ll * c00 * c11 % P) *
inv(1ll * n * (n - 1) % P * inv(2) % P) % P;
chg.w[i][i] = sub(1, add(chg.w[i + 1][i], chg.w[i - 1][i]));
}
Matrix res = mpow(chg, m);
res = res * beg;
printf("%lld\n", res.w[cnt][0]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[105], n, k, s, t, Lucina;
inline int add(int a, int b) {
a += b;
return a >= mod ? a - mod : a;
}
inline int sub(int a, int b) {
a -= b;
return a < 0 ? a + mod : a;
}
inline int mul(int a, int b) { return (int)((long long)a * b % mod); }
inline int power(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
struct matrix {
int m[102][102];
matrix() {
for (int i = 0; i <= Lucina + 1; i++)
for (int j = 0; j <= Lucina + 1; j++) m[i][j] = 0;
}
matrix I() {
matrix tmp;
for (int i = 0; i <= Lucina + 1; i++) tmp.m[i][i] = 1;
return tmp;
}
matrix operator*(const matrix x) const {
matrix tmp;
for (int i = 0; i <= Lucina; i++)
for (int j = 0; j <= Lucina; j++) {
for (int r = 0; r <= Lucina; r++) {
tmp.m[i][j] = add(tmp.m[i][j], mul(m[i][r], x.m[r][j]));
}
}
return tmp;
}
matrix power(matrix x, int b) {
matrix tmp = I();
while (b > 0) {
if (b & 1) {
tmp = (tmp * x);
}
x = (x * x);
b >>= 1;
}
return tmp;
}
void print() {
for (int i = 0; i <= Lucina; i++)
for (int j = 0; j <= Lucina; j++)
printf("%d%c", m[i][j], j == Lucina ? '\n' : ' ');
}
};
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s += (a[i] == 0);
}
if (s == 0 || s == n) return printf("1"), 0;
Lucina = s;
for (int i = 1; i <= Lucina; i++) {
t += (a[i] == 0);
}
matrix x;
x.m[t][0] = 1;
int d = mul(n, n - 1) / 2;
int crazy = n * (n - 1) / 2;
d = power(d, mod - 2);
matrix l;
int peace = ((Lucina * (Lucina - 1)) + ((n - Lucina) * (n - Lucina - 1))) / 2;
for (int i = 0; i <= Lucina; i++) {
l.m[i][i] = add(l.m[i][i], peace);
l.m[i][i] = add(l.m[i][i], mul(n - Lucina * 2 + i * 2, Lucina - i));
if (i - 1 >= 0)
l.m[i][i - 1] = add(l.m[i][i - 1], mul(Lucina - i + 1, Lucina - i + 1));
if (i + 1 <= Lucina)
l.m[i][i + 1] =
add(l.m[i][i + 1], mul(i + 1, n - Lucina - Lucina + i + 1));
l.m[i][i - 1] = mul(l.m[i][i - 1], d);
l.m[i][i + 1] = mul(l.m[i][i + 1], d);
l.m[i][i] = mul(l.m[i][i], d);
}
matrix r = l.power(l, k);
matrix ans = r * x;
printf("%d\n", ans.m[Lucina][0]);
}
| ### Prompt
Develop a solution in CPP to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[105], n, k, s, t, Lucina;
inline int add(int a, int b) {
a += b;
return a >= mod ? a - mod : a;
}
inline int sub(int a, int b) {
a -= b;
return a < 0 ? a + mod : a;
}
inline int mul(int a, int b) { return (int)((long long)a * b % mod); }
inline int power(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
struct matrix {
int m[102][102];
matrix() {
for (int i = 0; i <= Lucina + 1; i++)
for (int j = 0; j <= Lucina + 1; j++) m[i][j] = 0;
}
matrix I() {
matrix tmp;
for (int i = 0; i <= Lucina + 1; i++) tmp.m[i][i] = 1;
return tmp;
}
matrix operator*(const matrix x) const {
matrix tmp;
for (int i = 0; i <= Lucina; i++)
for (int j = 0; j <= Lucina; j++) {
for (int r = 0; r <= Lucina; r++) {
tmp.m[i][j] = add(tmp.m[i][j], mul(m[i][r], x.m[r][j]));
}
}
return tmp;
}
matrix power(matrix x, int b) {
matrix tmp = I();
while (b > 0) {
if (b & 1) {
tmp = (tmp * x);
}
x = (x * x);
b >>= 1;
}
return tmp;
}
void print() {
for (int i = 0; i <= Lucina; i++)
for (int j = 0; j <= Lucina; j++)
printf("%d%c", m[i][j], j == Lucina ? '\n' : ' ');
}
};
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s += (a[i] == 0);
}
if (s == 0 || s == n) return printf("1"), 0;
Lucina = s;
for (int i = 1; i <= Lucina; i++) {
t += (a[i] == 0);
}
matrix x;
x.m[t][0] = 1;
int d = mul(n, n - 1) / 2;
int crazy = n * (n - 1) / 2;
d = power(d, mod - 2);
matrix l;
int peace = ((Lucina * (Lucina - 1)) + ((n - Lucina) * (n - Lucina - 1))) / 2;
for (int i = 0; i <= Lucina; i++) {
l.m[i][i] = add(l.m[i][i], peace);
l.m[i][i] = add(l.m[i][i], mul(n - Lucina * 2 + i * 2, Lucina - i));
if (i - 1 >= 0)
l.m[i][i - 1] = add(l.m[i][i - 1], mul(Lucina - i + 1, Lucina - i + 1));
if (i + 1 <= Lucina)
l.m[i][i + 1] =
add(l.m[i][i + 1], mul(i + 1, n - Lucina - Lucina + i + 1));
l.m[i][i - 1] = mul(l.m[i][i - 1], d);
l.m[i][i + 1] = mul(l.m[i][i + 1], d);
l.m[i][i] = mul(l.m[i][i], d);
}
matrix r = l.power(l, k);
matrix ans = r * x;
printf("%d\n", ans.m[Lucina][0]);
}
``` |
#include <bits/stdc++.h>
void err() { std::cout << std::endl; }
template <typename T, typename... Args>
void err(T a, Args... args) {
std::cout << a << ' ';
err(args...);
}
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1 << 30;
const int maxn = 100 + 5;
long long qpow(long long x, long long n) {
long long r = 1;
while (n > 0) {
if (n & 1) r = r * x % mod;
n >>= 1;
x = x * x % mod;
}
return r;
}
long long inv(long long x) { return qpow(x, mod - 2); }
void add(long long& x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
struct Mat {
static const int M = 100 + 5;
long long a[M][M];
Mat() { memset(a, 0, sizeof(a)); }
void clear() { memset(a, 0, sizeof(a)); }
void eye() {
for (int i = 0; i < M; i++) a[i][i] = 1;
}
long long* operator[](long long x) { return a[x]; }
const long long* operator[](long long x) const { return a[x]; }
Mat operator*(const Mat& b) {
const Mat& a = *this;
Mat r;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
for (int k = 0; k < M; k++)
r[i][j] = (r[i][j] + a[i][k] * b[k][j]) % mod;
return r;
}
Mat pow(long long n) const {
Mat a = *this, r;
r.eye();
while (n > 0) {
if (n & 1) r = r * a;
n >>= 1;
a = a * a;
}
return r;
}
Mat operator+(const Mat& b) {
const Mat& a = *this;
Mat r;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) r[i][j] = (a[i][j] + b[i][j]) % mod;
return r;
}
void print() const {
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
printf("%lld%c", (*this)[i][j], j == M - 1 ? '\n' : ' ');
}
} F, T;
int n, k, m, a[maxn];
long long dp[2][maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
if (a[i] == 0) m++;
}
int c = 0;
for (int i = 1; i <= m; i++)
if (a[i] == 0) c++;
F[c][0] = 1;
long long iv = inv(n * (n - 1) / 2), c1 = m * (m - 1) / 2,
c2 = (n - m) * (n - m - 1) / 2;
for (int i = 0; i <= m; i++) {
T[i][i] =
1ll * (c1 + c2 + i * (m - i) + (m - i) * (n - 2 * m + i)) * iv % mod;
if (i > 0) {
T[i][i - 1] = 1ll * (m - i + 1) * (m - i + 1) * iv % mod;
}
if (i < m) {
T[i][i + 1] = 1ll * (i + 1) * (n - 2 * m + i + 1) * iv % mod;
}
}
cout << (T.pow(k) * F)[m][0] << endl;
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
void err() { std::cout << std::endl; }
template <typename T, typename... Args>
void err(T a, Args... args) {
std::cout << a << ' ';
err(args...);
}
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1 << 30;
const int maxn = 100 + 5;
long long qpow(long long x, long long n) {
long long r = 1;
while (n > 0) {
if (n & 1) r = r * x % mod;
n >>= 1;
x = x * x % mod;
}
return r;
}
long long inv(long long x) { return qpow(x, mod - 2); }
void add(long long& x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
struct Mat {
static const int M = 100 + 5;
long long a[M][M];
Mat() { memset(a, 0, sizeof(a)); }
void clear() { memset(a, 0, sizeof(a)); }
void eye() {
for (int i = 0; i < M; i++) a[i][i] = 1;
}
long long* operator[](long long x) { return a[x]; }
const long long* operator[](long long x) const { return a[x]; }
Mat operator*(const Mat& b) {
const Mat& a = *this;
Mat r;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
for (int k = 0; k < M; k++)
r[i][j] = (r[i][j] + a[i][k] * b[k][j]) % mod;
return r;
}
Mat pow(long long n) const {
Mat a = *this, r;
r.eye();
while (n > 0) {
if (n & 1) r = r * a;
n >>= 1;
a = a * a;
}
return r;
}
Mat operator+(const Mat& b) {
const Mat& a = *this;
Mat r;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) r[i][j] = (a[i][j] + b[i][j]) % mod;
return r;
}
void print() const {
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
printf("%lld%c", (*this)[i][j], j == M - 1 ? '\n' : ' ');
}
} F, T;
int n, k, m, a[maxn];
long long dp[2][maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
if (a[i] == 0) m++;
}
int c = 0;
for (int i = 1; i <= m; i++)
if (a[i] == 0) c++;
F[c][0] = 1;
long long iv = inv(n * (n - 1) / 2), c1 = m * (m - 1) / 2,
c2 = (n - m) * (n - m - 1) / 2;
for (int i = 0; i <= m; i++) {
T[i][i] =
1ll * (c1 + c2 + i * (m - i) + (m - i) * (n - 2 * m + i)) * iv % mod;
if (i > 0) {
T[i][i - 1] = 1ll * (m - i + 1) * (m - i + 1) * iv % mod;
}
if (i < m) {
T[i][i + 1] = 1ll * (i + 1) * (n - 2 * m + i + 1) * iv % mod;
}
}
cout << (T.pow(k) * F)[m][0] << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 105, mod = 1e9 + 7;
struct ab {
long long a[N][N];
ab() { memset(a, 0, sizeof a); }
} ans, xx;
long long a[N], n;
long long p2(long long x) { return x * (x - 1) / 2; }
long long POW(long long x, long long y) {
long long ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
ab mul(ab x, ab y) {
ab z;
for (long long i = 0; i <= n; i++) {
for (long long j = 0; j <= n; j++) {
for (long long k = 0; k <= n; k++)
z.a[i][j] = (z.a[i][j] + x.a[i][k] * y.a[k][j]) % mod;
}
}
return z;
}
signed main() {
long long k, sum = 0, now = 0;
scanf("%lld%lld", &n, &k);
long long inv = POW(p2(n), mod - 2);
for (long long i = 1; i <= n; i++) {
scanf("%lld", a + i);
sum += a[i];
}
sum = n - sum;
for (long long i = 1; i <= sum; i++) now += a[i];
now = sum - now;
for (long long i = 0; i <= n; i++) ans.a[i][i] = 1;
for (long long i = max(0ll, 2 * sum - n); i <= sum; i++) {
if (i > 0) xx.a[i][i - 1] = (sum - i + 1) * (sum - i + 1) * inv % mod;
xx.a[i][i] = (p2(sum) + p2(n - sum) + i * (sum - i) +
(sum - i) * (n + i - 2 * sum)) *
inv % mod;
xx.a[i][i + 1] = (i + 1) * (n + i + 1 - 2 * sum) * inv % mod;
}
while (k) {
if (k & 1) ans = mul(ans, xx);
xx = mul(xx, xx);
k >>= 1;
}
printf("%lld\n", ans.a[sum][now]);
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 105, mod = 1e9 + 7;
struct ab {
long long a[N][N];
ab() { memset(a, 0, sizeof a); }
} ans, xx;
long long a[N], n;
long long p2(long long x) { return x * (x - 1) / 2; }
long long POW(long long x, long long y) {
long long ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
ab mul(ab x, ab y) {
ab z;
for (long long i = 0; i <= n; i++) {
for (long long j = 0; j <= n; j++) {
for (long long k = 0; k <= n; k++)
z.a[i][j] = (z.a[i][j] + x.a[i][k] * y.a[k][j]) % mod;
}
}
return z;
}
signed main() {
long long k, sum = 0, now = 0;
scanf("%lld%lld", &n, &k);
long long inv = POW(p2(n), mod - 2);
for (long long i = 1; i <= n; i++) {
scanf("%lld", a + i);
sum += a[i];
}
sum = n - sum;
for (long long i = 1; i <= sum; i++) now += a[i];
now = sum - now;
for (long long i = 0; i <= n; i++) ans.a[i][i] = 1;
for (long long i = max(0ll, 2 * sum - n); i <= sum; i++) {
if (i > 0) xx.a[i][i - 1] = (sum - i + 1) * (sum - i + 1) * inv % mod;
xx.a[i][i] = (p2(sum) + p2(n - sum) + i * (sum - i) +
(sum - i) * (n + i - 2 * sum)) *
inv % mod;
xx.a[i][i + 1] = (i + 1) * (n + i + 1 - 2 * sum) * inv % mod;
}
while (k) {
if (k & 1) ans = mul(ans, xx);
xx = mul(xx, xx);
k >>= 1;
}
printf("%lld\n", ans.a[sum][now]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
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); }
struct matrix {
long long a[105][105];
int row, col;
matrix() : row(105), col(105) { memset(a, 0, sizeof(a)); }
matrix(int x, int y) : row(x), col(y) { memset(a, 0, sizeof(a)); }
long long* operator[](int x) { return a[x]; }
matrix operator*(matrix x) {
matrix tmp(col, x.row);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (a[i][j])
for (int k = 0; k < x.col; k++)
if (x[j][k]) {
tmp[i][k] += a[i][j] * x[j][k];
tmp[i][k] %= mod;
}
return tmp;
}
void operator*=(matrix x) { *this = *this * x; }
matrix operator^(int x) {
matrix ret(row, col);
for (int i = 0; i < col; i++) ret[i][i] = 1;
matrix tmp = *this;
for (; x > 0; x >>= 1, tmp *= tmp)
if (x & 1) ret *= tmp;
return ret;
}
};
int c2(int x) { return x * (x - 1) % mod * inv(2) % mod; }
int a[105];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int m = 0;
for (int i = 0; i < n; i++)
if (a[i] == 0) m++;
int t = 0;
n = n - m;
for (int i = 0; i < m; i++)
if (a[i] == 1) t++;
int p = min(m, n);
matrix mat(p + 1, p + 1);
for (int j = 0; j <= p; j++) {
mat.a[j][j] = (c2(m - j) + c2(n - j) + 2 * (m - j) * j + 2 * (n - j) * j +
2 * c2(j)) %
mod * inv(c2(m + n)) % mod;
if (j + 1 <= p)
mat.a[j][j + 1] = ((j + 1) * (j + 1)) % mod * inv(c2(m + n)) % mod;
if (j - 1 >= 0)
mat.a[j][j - 1] =
((m - j + 1) * (n - j + 1)) % mod * inv(c2(m + n)) % mod;
}
mat = mat ^ k;
printf("%lld\n", mat.a[0][t]);
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
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); }
struct matrix {
long long a[105][105];
int row, col;
matrix() : row(105), col(105) { memset(a, 0, sizeof(a)); }
matrix(int x, int y) : row(x), col(y) { memset(a, 0, sizeof(a)); }
long long* operator[](int x) { return a[x]; }
matrix operator*(matrix x) {
matrix tmp(col, x.row);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (a[i][j])
for (int k = 0; k < x.col; k++)
if (x[j][k]) {
tmp[i][k] += a[i][j] * x[j][k];
tmp[i][k] %= mod;
}
return tmp;
}
void operator*=(matrix x) { *this = *this * x; }
matrix operator^(int x) {
matrix ret(row, col);
for (int i = 0; i < col; i++) ret[i][i] = 1;
matrix tmp = *this;
for (; x > 0; x >>= 1, tmp *= tmp)
if (x & 1) ret *= tmp;
return ret;
}
};
int c2(int x) { return x * (x - 1) % mod * inv(2) % mod; }
int a[105];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int m = 0;
for (int i = 0; i < n; i++)
if (a[i] == 0) m++;
int t = 0;
n = n - m;
for (int i = 0; i < m; i++)
if (a[i] == 1) t++;
int p = min(m, n);
matrix mat(p + 1, p + 1);
for (int j = 0; j <= p; j++) {
mat.a[j][j] = (c2(m - j) + c2(n - j) + 2 * (m - j) * j + 2 * (n - j) * j +
2 * c2(j)) %
mod * inv(c2(m + n)) % mod;
if (j + 1 <= p)
mat.a[j][j + 1] = ((j + 1) * (j + 1)) % mod * inv(c2(m + n)) % mod;
if (j - 1 >= 0)
mat.a[j][j - 1] =
((m - j + 1) * (n - j + 1)) % mod * inv(c2(m + n)) % mod;
}
mat = mat ^ k;
printf("%lld\n", mat.a[0][t]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[110];
int src[30][110][110];
int temp[110][110];
int dst[110][110];
int add(int a, int b) { return (a + b) % 1000000007; }
int sub(int a, int b) { return (a - b + 1000000007) % 1000000007; }
int prod(int a, int b) { return 1LL * a * b % 1000000007; }
int power(int a, int n) {
if (!n) return 1;
int mid = power(a, n / 2);
mid = prod(mid, mid);
if (n % 2) mid = prod(mid, a);
return mid;
}
int inv(int a) { return power(a, 1000000007 - 2); }
void matmul(int a[110][110], int b[110][110], int c[110][110], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
temp[i][j] = 0;
for (int k = 0; k < n; k++)
temp[i][j] = add(temp[i][j], prod(a[i][k], b[k][j]));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) c[i][j] = temp[i][j];
}
}
void matPower(int k, int n) {
for (int i = 1; i < 30; i++) matmul(src[i - 1], src[i - 1], src[i], n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) dst[i][j] = (i == j);
}
for (int i = 0; i < 30; i++) {
if (k & (1 << i)) matmul(dst, src[i], dst, n);
}
}
int main(void) {
int N, K, cnt, C;
cin >> N >> K;
C = 0;
cnt = 0;
for (int i = 0; i < N; i++) {
cin >> arr[i];
if (!arr[i]) cnt++;
}
for (int i = 0; i < cnt; i++) C += !arr[i];
for (int i = max(0, 2 * cnt - N); i <= cnt; i++) {
src[0][i][i] = 1;
if (i > max(0, 2 * cnt - N)) {
src[0][i][i - 1] = prod(inv(N * (N - 1) / 2), i * (N + i - cnt - cnt));
src[0][i][i] = sub(src[0][i][i], src[0][i][i - 1]);
}
if (i < cnt) {
src[0][i][i + 1] = prod(inv(N * (N - 1) / 2), (cnt - i) * (cnt - i));
src[0][i][i] = sub(src[0][i][i], src[0][i][i + 1]);
}
}
for (int i = max(0, 2 * cnt - N); i <= cnt; i++) {
for (int j = max(0, 2 * cnt - N); j <= cnt; j++)
src[0][i - max(0, 2 * cnt - N)][j - max(0, 2 * cnt - N)] = src[0][i][j];
}
C -= max(0, 2 * cnt - N);
cnt -= max(0, 2 * cnt - N);
matPower(K, cnt + 1);
cout << dst[C][cnt] << endl;
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[110];
int src[30][110][110];
int temp[110][110];
int dst[110][110];
int add(int a, int b) { return (a + b) % 1000000007; }
int sub(int a, int b) { return (a - b + 1000000007) % 1000000007; }
int prod(int a, int b) { return 1LL * a * b % 1000000007; }
int power(int a, int n) {
if (!n) return 1;
int mid = power(a, n / 2);
mid = prod(mid, mid);
if (n % 2) mid = prod(mid, a);
return mid;
}
int inv(int a) { return power(a, 1000000007 - 2); }
void matmul(int a[110][110], int b[110][110], int c[110][110], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
temp[i][j] = 0;
for (int k = 0; k < n; k++)
temp[i][j] = add(temp[i][j], prod(a[i][k], b[k][j]));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) c[i][j] = temp[i][j];
}
}
void matPower(int k, int n) {
for (int i = 1; i < 30; i++) matmul(src[i - 1], src[i - 1], src[i], n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) dst[i][j] = (i == j);
}
for (int i = 0; i < 30; i++) {
if (k & (1 << i)) matmul(dst, src[i], dst, n);
}
}
int main(void) {
int N, K, cnt, C;
cin >> N >> K;
C = 0;
cnt = 0;
for (int i = 0; i < N; i++) {
cin >> arr[i];
if (!arr[i]) cnt++;
}
for (int i = 0; i < cnt; i++) C += !arr[i];
for (int i = max(0, 2 * cnt - N); i <= cnt; i++) {
src[0][i][i] = 1;
if (i > max(0, 2 * cnt - N)) {
src[0][i][i - 1] = prod(inv(N * (N - 1) / 2), i * (N + i - cnt - cnt));
src[0][i][i] = sub(src[0][i][i], src[0][i][i - 1]);
}
if (i < cnt) {
src[0][i][i + 1] = prod(inv(N * (N - 1) / 2), (cnt - i) * (cnt - i));
src[0][i][i] = sub(src[0][i][i], src[0][i][i + 1]);
}
}
for (int i = max(0, 2 * cnt - N); i <= cnt; i++) {
for (int j = max(0, 2 * cnt - N); j <= cnt; j++)
src[0][i - max(0, 2 * cnt - N)][j - max(0, 2 * cnt - N)] = src[0][i][j];
}
C -= max(0, 2 * cnt - N);
cnt -= max(0, 2 * cnt - N);
matPower(K, cnt + 1);
cout << dst[C][cnt] << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int matrix[102][102];
long long int mod = 1000000007;
long long int res[102][102];
long long int org[102][102];
long long int d;
void mult1() {
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
res[i][j] = 0;
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
for (long long int k = 0; k <= d; k++) {
res[i][j] += matrix[i][k] * matrix[k][j];
if (res[i][j] >= mod) {
res[i][j] %= mod;
}
}
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
matrix[i][j] = res[i][j];
}
}
return;
}
void mult2() {
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
res[i][j] = 0;
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
for (long long int k = 0; k <= d; k++) {
res[i][j] += matrix[i][k] * org[k][j];
if (res[i][j] >= mod) {
res[i][j] %= mod;
}
}
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
matrix[i][j] = res[i][j];
}
}
return;
}
void mexp(long long int n) {
if (n == 1) {
return;
}
if (n % 2 == 1) {
mexp((n - 1) / 2);
mult1();
mult2();
return;
} else {
mexp(n / 2);
mult1();
return;
}
}
long long int arr[105];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, k;
cin >> n >> k;
long long int z = 0;
long long int o = 0;
for (long long int i = 1; i <= n; i++) {
cin >> arr[i];
if (arr[i]) {
o++;
} else {
z++;
}
}
long long int c = 0;
for (long long int i = 1; i <= z; i++) {
if (arr[i] == 0) {
c++;
}
}
d = z;
long long int tot = (n * (n - 1)) / 2;
for (long long int i = 0; i <= d; i++) {
if (o - z + i >= 0) {
org[i][i] = (((z * (z - 1)) / 2 + (o * (o - 1)) / 2 + i * (z - i) +
((z - i) * (o - z + i))) *
(power(tot, mod - 2, mod))) %
mod;
if (i > 0) {
org[i][i - 1] =
((((z - i + 1) * (z - i + 1))) * (power(tot, mod - 2, mod))) % mod;
}
if (i < d && (o - (z - i - 1) >= 0)) {
org[i][i + 1] =
((((i + 1) * (o - (z - i - 1)))) * (power(tot, mod - 2, mod))) %
mod;
}
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
matrix[i][j] = org[i][j];
}
}
mexp(k);
cout << matrix[d][c];
}
| ### Prompt
Generate a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int matrix[102][102];
long long int mod = 1000000007;
long long int res[102][102];
long long int org[102][102];
long long int d;
void mult1() {
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
res[i][j] = 0;
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
for (long long int k = 0; k <= d; k++) {
res[i][j] += matrix[i][k] * matrix[k][j];
if (res[i][j] >= mod) {
res[i][j] %= mod;
}
}
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
matrix[i][j] = res[i][j];
}
}
return;
}
void mult2() {
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
res[i][j] = 0;
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
for (long long int k = 0; k <= d; k++) {
res[i][j] += matrix[i][k] * org[k][j];
if (res[i][j] >= mod) {
res[i][j] %= mod;
}
}
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
matrix[i][j] = res[i][j];
}
}
return;
}
void mexp(long long int n) {
if (n == 1) {
return;
}
if (n % 2 == 1) {
mexp((n - 1) / 2);
mult1();
mult2();
return;
} else {
mexp(n / 2);
mult1();
return;
}
}
long long int arr[105];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, k;
cin >> n >> k;
long long int z = 0;
long long int o = 0;
for (long long int i = 1; i <= n; i++) {
cin >> arr[i];
if (arr[i]) {
o++;
} else {
z++;
}
}
long long int c = 0;
for (long long int i = 1; i <= z; i++) {
if (arr[i] == 0) {
c++;
}
}
d = z;
long long int tot = (n * (n - 1)) / 2;
for (long long int i = 0; i <= d; i++) {
if (o - z + i >= 0) {
org[i][i] = (((z * (z - 1)) / 2 + (o * (o - 1)) / 2 + i * (z - i) +
((z - i) * (o - z + i))) *
(power(tot, mod - 2, mod))) %
mod;
if (i > 0) {
org[i][i - 1] =
((((z - i + 1) * (z - i + 1))) * (power(tot, mod - 2, mod))) % mod;
}
if (i < d && (o - (z - i - 1) >= 0)) {
org[i][i + 1] =
((((i + 1) * (o - (z - i - 1)))) * (power(tot, mod - 2, mod))) %
mod;
}
}
}
for (long long int i = 0; i <= d; i++) {
for (long long int j = 0; j <= d; j++) {
matrix[i][j] = org[i][j];
}
}
mexp(k);
cout << matrix[d][c];
}
``` |
#include <bits/stdc++.h>
using namespace std;
void add(int &x, int y) {
x += y;
if (x >= 1000000007) x -= 1000000007;
}
inline int read() {
int x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') t = true, ch = getchar();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n, N, K, z, C[105][105], a[105];
int fpow(int a, int b) {
int s = 1;
while (b) {
if (b & 1) s = 1ll * s * a % 1000000007;
a = 1ll * a * a % 1000000007;
b >>= 1;
}
return s;
}
struct Matrix {
int s[105][105];
void clear() { memset(s, 0, sizeof(s)); }
void init() {
clear();
for (int i = 0; i <= N; ++i) s[i][i] = 1;
}
int *operator[](int x) { return s[x]; }
} A, B;
Matrix operator*(Matrix a, Matrix b) {
Matrix c;
c.clear();
for (int i = 0; i <= N; ++i)
for (int j = 0; j <= N; ++j)
for (int k = 0; k <= N; ++k)
c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % 1000000007;
return c;
}
Matrix fpow(Matrix a, int b) {
Matrix s;
s.init();
while (b) {
if (b & 1) s = s * a;
a = a * a;
b >>= 1;
}
return s;
}
int main() {
n = read();
K = read();
for (int i = 1; i <= n; ++i) a[i] = read(), z += a[i] ^ 1;
for (int i = 0; i <= n; ++i) C[i][0] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % 1000000007;
int s = 0;
for (int i = 1; i <= n; ++i)
if (i <= z && a[i]) ++s;
N = min(n - z, z);
B[0][s] = 1;
for (int i = 0; i <= N; ++i) {
add(A[i][i], (C[z][2] + C[n - z][2]) % 1000000007);
add(A[i][i], 1ll * i * (n - z - i) % 1000000007);
add(A[i][i], 1ll * (z - i) * i % 1000000007);
if (i) add(A[i][i - 1], 1ll * i * i % 1000000007);
if (i < N) add(A[i][i + 1], 1ll * (z - i) * (n - z - i) % 1000000007);
}
B = B * fpow(A, K);
int ans = 1ll * B[0][0] * fpow(fpow(n * (n - 1) / 2, K), 1000000007 - 2) %
1000000007;
printf("%d\n", ans);
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void add(int &x, int y) {
x += y;
if (x >= 1000000007) x -= 1000000007;
}
inline int read() {
int x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') t = true, ch = getchar();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n, N, K, z, C[105][105], a[105];
int fpow(int a, int b) {
int s = 1;
while (b) {
if (b & 1) s = 1ll * s * a % 1000000007;
a = 1ll * a * a % 1000000007;
b >>= 1;
}
return s;
}
struct Matrix {
int s[105][105];
void clear() { memset(s, 0, sizeof(s)); }
void init() {
clear();
for (int i = 0; i <= N; ++i) s[i][i] = 1;
}
int *operator[](int x) { return s[x]; }
} A, B;
Matrix operator*(Matrix a, Matrix b) {
Matrix c;
c.clear();
for (int i = 0; i <= N; ++i)
for (int j = 0; j <= N; ++j)
for (int k = 0; k <= N; ++k)
c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % 1000000007;
return c;
}
Matrix fpow(Matrix a, int b) {
Matrix s;
s.init();
while (b) {
if (b & 1) s = s * a;
a = a * a;
b >>= 1;
}
return s;
}
int main() {
n = read();
K = read();
for (int i = 1; i <= n; ++i) a[i] = read(), z += a[i] ^ 1;
for (int i = 0; i <= n; ++i) C[i][0] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % 1000000007;
int s = 0;
for (int i = 1; i <= n; ++i)
if (i <= z && a[i]) ++s;
N = min(n - z, z);
B[0][s] = 1;
for (int i = 0; i <= N; ++i) {
add(A[i][i], (C[z][2] + C[n - z][2]) % 1000000007);
add(A[i][i], 1ll * i * (n - z - i) % 1000000007);
add(A[i][i], 1ll * (z - i) * i % 1000000007);
if (i) add(A[i][i - 1], 1ll * i * i % 1000000007);
if (i < N) add(A[i][i + 1], 1ll * (z - i) * (n - z - i) % 1000000007);
}
B = B * fpow(A, K);
int ans = 1ll * B[0][0] * fpow(fpow(n * (n - 1) / 2, K), 1000000007 - 2) %
1000000007;
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
inline int read() {
int res = 0;
bool bo = 0;
char c;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-')
bo = 1;
else
res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
const int N = 105, ZZQ = 1e9 + 7;
int n, k, a[N], cnt, c0, c1, st, I;
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % ZZQ;
a = 1ll * a * a % ZZQ;
b >>= 1;
}
return res;
}
struct matrix {
int n, m, a[N][N];
matrix() {}
matrix(int _n, int _m) : n(_n), m(_m) { memset(a, 0, sizeof(a)); }
friend inline matrix operator*(matrix a, matrix b) {
matrix res = matrix(a.n, b.m);
for (int i = 1; i <= res.n; i++)
for (int j = 1; j <= res.m; j++)
for (int k = 1; k <= a.m; k++)
res.a[i][j] = (1ll * a.a[i][k] * b.a[k][j] + res.a[i][j]) % ZZQ;
return res;
}
friend inline matrix operator^(matrix a, int b) {
matrix res = matrix(a.n, a.m);
for (int i = 1; i <= res.n; i++) res.a[i][i] = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
} A, St;
int main() {
n = read();
k = read();
for (int i = 1; i <= n; i++) a[i] = read(), cnt += !a[i];
I = qpow(n * (n - 1) >> 1, ZZQ - 2);
c0 = cnt;
c1 = n - cnt;
if (n - cnt < cnt) cnt = n - cnt;
A = matrix(cnt + 1, cnt + 1);
St = matrix(cnt + 1, 1);
for (int i = 1; i <= c0; i++)
if (a[i]) st++;
St.a[st + 1][1] = 1;
for (int i = 0; i <= cnt; i++) {
int cur = n * (n - 1) >> 1;
if (i) A.a[i][i + 1] = 1ll * i * i * I % ZZQ, cur -= i * i;
if (i < cnt)
A.a[i + 2][i + 1] = 1ll * (c0 - i) * (c1 - i) * I % ZZQ,
cur -= (c0 - i) * (c1 - i);
A.a[i + 1][i + 1] = 1ll * cur * I % ZZQ;
}
std::cout << ((A ^ k) * St).a[1][1] << std::endl;
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
inline int read() {
int res = 0;
bool bo = 0;
char c;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-')
bo = 1;
else
res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
const int N = 105, ZZQ = 1e9 + 7;
int n, k, a[N], cnt, c0, c1, st, I;
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % ZZQ;
a = 1ll * a * a % ZZQ;
b >>= 1;
}
return res;
}
struct matrix {
int n, m, a[N][N];
matrix() {}
matrix(int _n, int _m) : n(_n), m(_m) { memset(a, 0, sizeof(a)); }
friend inline matrix operator*(matrix a, matrix b) {
matrix res = matrix(a.n, b.m);
for (int i = 1; i <= res.n; i++)
for (int j = 1; j <= res.m; j++)
for (int k = 1; k <= a.m; k++)
res.a[i][j] = (1ll * a.a[i][k] * b.a[k][j] + res.a[i][j]) % ZZQ;
return res;
}
friend inline matrix operator^(matrix a, int b) {
matrix res = matrix(a.n, a.m);
for (int i = 1; i <= res.n; i++) res.a[i][i] = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
} A, St;
int main() {
n = read();
k = read();
for (int i = 1; i <= n; i++) a[i] = read(), cnt += !a[i];
I = qpow(n * (n - 1) >> 1, ZZQ - 2);
c0 = cnt;
c1 = n - cnt;
if (n - cnt < cnt) cnt = n - cnt;
A = matrix(cnt + 1, cnt + 1);
St = matrix(cnt + 1, 1);
for (int i = 1; i <= c0; i++)
if (a[i]) st++;
St.a[st + 1][1] = 1;
for (int i = 0; i <= cnt; i++) {
int cur = n * (n - 1) >> 1;
if (i) A.a[i][i + 1] = 1ll * i * i * I % ZZQ, cur -= i * i;
if (i < cnt)
A.a[i + 2][i + 1] = 1ll * (c0 - i) * (c1 - i) * I % ZZQ,
cur -= (c0 - i) * (c1 - i);
A.a[i + 1][i + 1] = 1ll * cur * I % ZZQ;
}
std::cout << ((A ^ k) * St).a[1][1] << std::endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long dp[10002][103];
long long n;
long long power(long long x, long long k) {
long long result = 1;
while (k) {
if (k & 1) result = (result * x) % 1000000007;
x = (x * x) % 1000000007;
k = k >> 1;
}
return result;
}
class matrix {
public:
long long X[105][105] = {};
matrix operator*(matrix& P) {
matrix R;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
R.X[i][k] = (R.X[i][k] + 1ll * X[i][j] * P.X[j][k]) % 1000000007;
}
}
}
return R;
}
matrix operator^(long long e) {
if (e == 1) {
return *this;
}
matrix T = *this ^ (e / 2);
T = T * T;
return e % 2 ? T * *this : T;
}
} T;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long i, j, k, c = 0;
cin >> n >> k;
long long a[n + 5], p;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) c++;
}
long long s = 0;
for (i = 0; i < c; i++)
if (a[i] == 0) s++;
for (i = 0; i < c + 1; i++)
for (j = 0; j < c + 1; j++) {
T.X[i][j] = 0;
}
for (j = 0; j < c + 1; j++) {
T.X[j][j] = ((n - c) * (n - c - 1) / 2 + c * (c - 1) / 2 +
(c - j) * (n - 2 * c + j) + j * (c - j)) %
1000000007;
if (j != 0) T.X[j][j - 1] = ((c - j + 1) * (c - j + 1)) % 1000000007;
if (j != c) T.X[j][j + 1] = ((j + 1) * (n - 2 * c + j + 1)) % 1000000007;
}
matrix A = T ^ k;
p = A.X[c][s];
j = n * (n - 1) / 2;
long long q = power(j, k);
q = power(q, 1000000007 - 2);
long long ans = p * q;
ans = ans % 1000000007;
cout << ans;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long dp[10002][103];
long long n;
long long power(long long x, long long k) {
long long result = 1;
while (k) {
if (k & 1) result = (result * x) % 1000000007;
x = (x * x) % 1000000007;
k = k >> 1;
}
return result;
}
class matrix {
public:
long long X[105][105] = {};
matrix operator*(matrix& P) {
matrix R;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
R.X[i][k] = (R.X[i][k] + 1ll * X[i][j] * P.X[j][k]) % 1000000007;
}
}
}
return R;
}
matrix operator^(long long e) {
if (e == 1) {
return *this;
}
matrix T = *this ^ (e / 2);
T = T * T;
return e % 2 ? T * *this : T;
}
} T;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long i, j, k, c = 0;
cin >> n >> k;
long long a[n + 5], p;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) c++;
}
long long s = 0;
for (i = 0; i < c; i++)
if (a[i] == 0) s++;
for (i = 0; i < c + 1; i++)
for (j = 0; j < c + 1; j++) {
T.X[i][j] = 0;
}
for (j = 0; j < c + 1; j++) {
T.X[j][j] = ((n - c) * (n - c - 1) / 2 + c * (c - 1) / 2 +
(c - j) * (n - 2 * c + j) + j * (c - j)) %
1000000007;
if (j != 0) T.X[j][j - 1] = ((c - j + 1) * (c - j + 1)) % 1000000007;
if (j != c) T.X[j][j + 1] = ((j + 1) * (n - 2 * c + j + 1)) % 1000000007;
}
matrix A = T ^ k;
p = A.X[c][s];
j = n * (n - 1) / 2;
long long q = power(j, k);
q = power(q, 1000000007 - 2);
long long ans = p * q;
ans = ans % 1000000007;
cout << ans;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <class T>
using v2d = vector<vector<T>>;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1e2 + 10;
const long long mod = 1e9 + 7;
struct Matrix {
int n, m;
vector<vector<long long>> s;
Matrix() : n(0), m(0) {}
Matrix(vector<vector<long long>> &a) {
s = a;
n = s.size();
if (n > 0) {
m = s[0].size();
}
}
void assign(vector<vector<long long>> &a) {
s = a;
n = s.size();
if (n > 0) {
m = s[0].size();
}
}
Matrix(int n, int m) : n(n), m(m) {
s = vector<vector<long long>>(n, vector<long long>(m));
}
vector<long long> &operator[](int i) { return s[i]; }
};
void mul(Matrix &a, Matrix b) {
assert(a.m == b.n);
int n = a.n, m = a.m, p = b.m;
vector<vector<long long>> &s = a.s, &t = b.s, r(n, vector<long long>(p));
for (int i = 0; i < (int)(n); ++i) {
for (int k = 0; k < (int)(p); ++k) {
for (int j = 0; j < (int)(m); ++j) {
r[i][j] += s[i][k] * t[k][j] % mod;
if (r[i][j] >= mod) {
r[i][j] -= mod;
}
}
}
}
a.assign(r);
}
Matrix power(Matrix a, long long b) {
assert(a.n == a.m);
int n = a.n;
Matrix r(n, n);
for (int i = 0; i < (int)(n); ++i) {
r[i][i] = 1;
}
while (b) {
if (b & 1) {
mul(r, a);
}
b >>= 1;
mul(a, a);
}
return r;
}
long long power(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) {
r = r * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return r;
}
long long nC2(int n) { return n * (n - 1) / 2; }
long long inv(long long a) { return power(a, mod - 2); }
int n, p, a[maxN];
long long k;
void solve() {
cin >> n >> k;
for (int i = 1; i <= (int)(n); ++i) {
cin >> a[i];
p += a[i] == 0;
}
int tmp = 0;
for (int i = 1; i <= (int)(p); ++i) {
tmp += a[i] == 0;
}
Matrix b(p + 1, p + 1);
long long iv = inv(nC2(n));
for (int i = 0; i < (int)(p + 1); ++i) {
if (i >= 1) {
b[i - 1][i] = (p - i + 1) * (p - i + 1) * iv % mod;
}
if (i < p) {
b[i + 1][i] = (n - p * 2 + i + 1) % mod * (i + 1) % mod * iv % mod;
}
b[i][i] = (nC2(p) + nC2(n - p) + i * (p - i) + (p - i) * (n - p * 2 + i)) %
mod * iv % mod;
}
Matrix ans(1, p + 1);
ans[0][tmp] = 1;
mul(ans, power(b, k));
cout << ans[0][p];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while (T--) {
solve();
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <class T>
using v2d = vector<vector<T>>;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1e2 + 10;
const long long mod = 1e9 + 7;
struct Matrix {
int n, m;
vector<vector<long long>> s;
Matrix() : n(0), m(0) {}
Matrix(vector<vector<long long>> &a) {
s = a;
n = s.size();
if (n > 0) {
m = s[0].size();
}
}
void assign(vector<vector<long long>> &a) {
s = a;
n = s.size();
if (n > 0) {
m = s[0].size();
}
}
Matrix(int n, int m) : n(n), m(m) {
s = vector<vector<long long>>(n, vector<long long>(m));
}
vector<long long> &operator[](int i) { return s[i]; }
};
void mul(Matrix &a, Matrix b) {
assert(a.m == b.n);
int n = a.n, m = a.m, p = b.m;
vector<vector<long long>> &s = a.s, &t = b.s, r(n, vector<long long>(p));
for (int i = 0; i < (int)(n); ++i) {
for (int k = 0; k < (int)(p); ++k) {
for (int j = 0; j < (int)(m); ++j) {
r[i][j] += s[i][k] * t[k][j] % mod;
if (r[i][j] >= mod) {
r[i][j] -= mod;
}
}
}
}
a.assign(r);
}
Matrix power(Matrix a, long long b) {
assert(a.n == a.m);
int n = a.n;
Matrix r(n, n);
for (int i = 0; i < (int)(n); ++i) {
r[i][i] = 1;
}
while (b) {
if (b & 1) {
mul(r, a);
}
b >>= 1;
mul(a, a);
}
return r;
}
long long power(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) {
r = r * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return r;
}
long long nC2(int n) { return n * (n - 1) / 2; }
long long inv(long long a) { return power(a, mod - 2); }
int n, p, a[maxN];
long long k;
void solve() {
cin >> n >> k;
for (int i = 1; i <= (int)(n); ++i) {
cin >> a[i];
p += a[i] == 0;
}
int tmp = 0;
for (int i = 1; i <= (int)(p); ++i) {
tmp += a[i] == 0;
}
Matrix b(p + 1, p + 1);
long long iv = inv(nC2(n));
for (int i = 0; i < (int)(p + 1); ++i) {
if (i >= 1) {
b[i - 1][i] = (p - i + 1) * (p - i + 1) * iv % mod;
}
if (i < p) {
b[i + 1][i] = (n - p * 2 + i + 1) % mod * (i + 1) % mod * iv % mod;
}
b[i][i] = (nC2(p) + nC2(n - p) + i * (p - i) + (p - i) * (n - p * 2 + i)) %
mod * iv % mod;
}
Matrix ans(1, p + 1);
ans[0][tmp] = 1;
mul(ans, power(b, k));
cout << ans[0][p];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while (T--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
inline int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
inline void inc(int &x, int y) { (x += y) >= mod && (x -= mod); }
inline int mul(int x, int y) { return 1LL * x * y - 1LL * x * y / mod * mod; }
inline int modpower(int x, long long y) {
int res = 1;
while (y) {
if (y & 1) res = mul(res, x);
y >>= 1, x = mul(x, x);
}
return res;
}
int n, c, k, tot;
int a[105];
struct matrix {
int mat[105][105];
void clear() { memset(mat, 0, sizeof(mat)); }
matrix(int x = 0) {
clear();
for (int i = 0; i <= c; ++i) mat[i][i] = x;
}
int *operator[](const int &x) { return mat[x]; }
friend matrix operator*(matrix &a, matrix &b) {
matrix res;
for (int i = 0; i <= c; ++i)
for (int j = 0; j <= c; ++j)
for (int k = 0; k <= c; ++k) inc(res[i][j], mul(a[i][k], b[k][j]));
return res;
}
friend matrix operator^(matrix a, long long b) {
matrix res(1);
while (b) {
if (b & 1) res *= a;
b >>= 1, a *= a;
}
return res;
}
matrix &operator*=(matrix &x) { return *this = *this * x; }
matrix &operator^=(long long x) { return *this = *this ^ x; }
} dp, trans;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", a + i), c += (a[i] == 0);
dp[0][count(a + 1, a + c + 1, 0)] = 1;
for (int j = 0; j <= c; ++j) {
if (j > 0) trans[j - 1][j] = (c - (j - 1)) * (c - (j - 1));
if (j < c) trans[j + 1][j] = (j + 1) * (n - c - (c - (j + 1)));
trans[j][j] = (j * (c - j)) + (c - j) * (n - c - (c - j));
trans[j][j] += c * (c - 1) / 2 + (n - c) * (n - c - 1) / 2;
}
trans ^= k;
dp *= trans;
for (int j = 0; j <= c; ++j) inc(tot, dp[0][j]);
printf("%d\n", mul(dp[0][c], modpower(tot, mod - 2)));
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
inline int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
inline void inc(int &x, int y) { (x += y) >= mod && (x -= mod); }
inline int mul(int x, int y) { return 1LL * x * y - 1LL * x * y / mod * mod; }
inline int modpower(int x, long long y) {
int res = 1;
while (y) {
if (y & 1) res = mul(res, x);
y >>= 1, x = mul(x, x);
}
return res;
}
int n, c, k, tot;
int a[105];
struct matrix {
int mat[105][105];
void clear() { memset(mat, 0, sizeof(mat)); }
matrix(int x = 0) {
clear();
for (int i = 0; i <= c; ++i) mat[i][i] = x;
}
int *operator[](const int &x) { return mat[x]; }
friend matrix operator*(matrix &a, matrix &b) {
matrix res;
for (int i = 0; i <= c; ++i)
for (int j = 0; j <= c; ++j)
for (int k = 0; k <= c; ++k) inc(res[i][j], mul(a[i][k], b[k][j]));
return res;
}
friend matrix operator^(matrix a, long long b) {
matrix res(1);
while (b) {
if (b & 1) res *= a;
b >>= 1, a *= a;
}
return res;
}
matrix &operator*=(matrix &x) { return *this = *this * x; }
matrix &operator^=(long long x) { return *this = *this ^ x; }
} dp, trans;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", a + i), c += (a[i] == 0);
dp[0][count(a + 1, a + c + 1, 0)] = 1;
for (int j = 0; j <= c; ++j) {
if (j > 0) trans[j - 1][j] = (c - (j - 1)) * (c - (j - 1));
if (j < c) trans[j + 1][j] = (j + 1) * (n - c - (c - (j + 1)));
trans[j][j] = (j * (c - j)) + (c - j) * (n - c - (c - j));
trans[j][j] += c * (c - 1) / 2 + (n - c) * (n - c - 1) / 2;
}
trans ^= k;
dp *= trans;
for (int j = 0; j <= c; ++j) inc(tot, dp[0][j]);
printf("%d\n", mul(dp[0][c], modpower(tot, mod - 2)));
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
if (n + MOD >= 0)
return n + MOD;
else
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < (MOD << 1))
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
if (!a[i][k] || !b[k][j]) continue;
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
| ### Prompt
Create a solution in Cpp for the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
if (n + MOD >= 0)
return n + MOD;
else
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < (MOD << 1))
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
if (!a[i][k] || !b[k][j]) continue;
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
int a[200], n, k, c, t;
struct Matrix {
long long m[105][105];
Matrix() { memset(m, 0, sizeof m); };
friend Matrix operator*(const Matrix &a, const Matrix &b) {
Matrix temp;
for (int i = 0; i <= 100; ++i)
for (int j = 0; j <= 100; j++)
for (int k = 0; k <= 100; k++)
temp.m[i][j] =
(temp.m[i][j] + a.m[i][k] * b.m[k][j] % mod + mod) % mod;
return temp;
}
};
Matrix fast(Matrix x, int n) {
Matrix ans;
for (int i = 0; i <= 100; i++) ans.m[i][i] = 1;
while (n) {
if (n & 1) ans = ans * x;
x = x * x;
n >>= 1;
}
return ans;
}
long long fast1(long long x, int n) {
long long ans = 1;
while (n) {
if (n & 1) ans = ans * x % mod;
x = x * x % mod;
n >>= 1;
}
return ans;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), c += (a[i] == 0);
for (int i = 1; i <= c; i++) t += (a[i] == 0);
Matrix a;
a.m[0][t] = 1;
Matrix ans;
for (int j = 0; j <= c; j++) {
ans.m[j][j] = 1ll *
(c * (c - 1) / 2 % mod + (n - c) * (n - c - 1) / 2 % mod +
j * (c - j) % mod + (c - j) * (n - 2 * c + j) % mod + mod) %
mod;
if (j) ans.m[j - 1][j] = 1ll * (c - j + 1) * (c - j + 1) % mod;
if (j != c) ans.m[j + 1][j] = 1ll * (j + 1) * (n - 2 * c + j + 1) % mod;
}
ans = fast(ans, k);
a = a * ans;
long long ret = 0;
for (int i = 0; i <= c; i++) ret = (ret + ans.m[0][i]) % mod;
printf("%lld\n", (a.m[0][c] * fast1(ret, mod - 2) + mod) % mod);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
int a[200], n, k, c, t;
struct Matrix {
long long m[105][105];
Matrix() { memset(m, 0, sizeof m); };
friend Matrix operator*(const Matrix &a, const Matrix &b) {
Matrix temp;
for (int i = 0; i <= 100; ++i)
for (int j = 0; j <= 100; j++)
for (int k = 0; k <= 100; k++)
temp.m[i][j] =
(temp.m[i][j] + a.m[i][k] * b.m[k][j] % mod + mod) % mod;
return temp;
}
};
Matrix fast(Matrix x, int n) {
Matrix ans;
for (int i = 0; i <= 100; i++) ans.m[i][i] = 1;
while (n) {
if (n & 1) ans = ans * x;
x = x * x;
n >>= 1;
}
return ans;
}
long long fast1(long long x, int n) {
long long ans = 1;
while (n) {
if (n & 1) ans = ans * x % mod;
x = x * x % mod;
n >>= 1;
}
return ans;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), c += (a[i] == 0);
for (int i = 1; i <= c; i++) t += (a[i] == 0);
Matrix a;
a.m[0][t] = 1;
Matrix ans;
for (int j = 0; j <= c; j++) {
ans.m[j][j] = 1ll *
(c * (c - 1) / 2 % mod + (n - c) * (n - c - 1) / 2 % mod +
j * (c - j) % mod + (c - j) * (n - 2 * c + j) % mod + mod) %
mod;
if (j) ans.m[j - 1][j] = 1ll * (c - j + 1) * (c - j + 1) % mod;
if (j != c) ans.m[j + 1][j] = 1ll * (j + 1) * (n - 2 * c + j + 1) % mod;
}
ans = fast(ans, k);
a = a * ans;
long long ret = 0;
for (int i = 0; i <= c; i++) ret = (ret + ans.m[0][i]) % mod;
printf("%lld\n", (a.m[0][c] * fast1(ret, mod - 2) + mod) % mod);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-reorder-functions")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
if (n + MOD >= 0)
return n + MOD;
else
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < (MOD << 1))
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
if (!a[i][k] || !b[k][j]) continue;
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-reorder-functions")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long mod(long long n) {
if (n < 0) {
if (n + MOD >= 0)
return n + MOD;
else
return (n % MOD + MOD) % MOD;
} else {
if (n < MOD)
return n;
else if (n < (MOD << 1))
return n - MOD;
else
return n % MOD;
}
}
long long fp(long long a, long long p) {
long long ans = 1, cur = a;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) ans = mod(ans * cur);
cur = mod(cur * cur);
}
return ans;
}
long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
const long long N = 101;
long long m[N][N], ans[N][N], t[N][N];
void add(long long a[N][N], long long b[N][N]) {
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
t[i][j] = 0;
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
for (long long k = 0; k < N; ++k) {
if (!a[i][k] || !b[k][j]) continue;
t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]);
}
}
}
for (long long i = 0; i < N; ++i) {
for (long long j = 0; j < N; ++j) {
a[i][j] = t[i][j];
}
}
}
void pw(long long p) {
for (long long i = 0; i < N; ++i) ans[i][i] = 1;
for (long long i = 0; (1 << i) <= p; ++i) {
if ((p >> i) & 1) add(ans, m);
add(m, m);
}
}
bool a[N];
long long cnt[2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long l = cnt[0];
long long r = n - l;
long long op = n * (n - 1) / 2;
for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) {
long long l0 = l - l1;
long long r0 = cnt[0] - l0;
long long r1 = cnt[1] - l1;
m[l1][l1] = op;
if (l1) {
m[l1][l1 - 1] = mod(l1 * r0);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]);
}
m[l1][l1 + 1] = mod(l0 * r1);
m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]);
}
pw(k);
long long sum = 0;
for (long long i = 0; i < l; ++i) sum += a[i];
cout << dv(ans[sum][0], fp(op, k)) << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[55], b[55];
int dp[55][55][55];
const int Mod = 998244353;
int nii[5555];
int ksmii(int a, int b) {
if (!b) {
return 1;
}
int x = ksmii(a, b >> 1);
x = 1LL * x * x % Mod;
if (b & 1) {
x = 1LL * x * a % Mod;
}
return x;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i < 5555; i++) {
nii[i] = ksmii(i, Mod - 2);
}
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = 1; i <= n; i++) {
scanf("%d", b + i);
}
for (int i = 1; i <= n; i++) {
memset((int*)dp, 0, sizeof(dp));
int proba = 0, probb = 0;
for (int j = 1; j <= n; j++) {
if (!a[j]) {
probb += b[j];
} else {
proba += b[j];
}
}
dp[0][0][0]++;
for (int j = 0; j < m; j++) {
for (int l = 0; l <= j; l++) {
int m = j - l;
for (int n = 0; n < 55; n++) {
if (a[i]) {
dp[j + 1][l + 1][n + 1] += 1LL * dp[j][l][n] * (n + b[i]) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l + 1][n + 1] >= Mod) {
dp[j + 1][l + 1][n + 1] -= Mod;
}
} else {
dp[j + 1][l][n + 1] += 1LL * dp[j][l][n] * (b[i] - n) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l][n + 1] >= Mod) {
dp[j + 1][l][n + 1] -= Mod;
}
}
if (a[i]) {
dp[j + 1][l + 1][n] += 1LL * dp[j][l][n] * (proba + l - n - b[i]) %
Mod * nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l + 1][n] >= Mod) {
dp[j + 1][l + 1][n] -= Mod;
}
dp[j + 1][l][n] += 1LL * dp[j][l][n] * (probb - m) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l][n] >= Mod) {
dp[j + 1][l][n] -= Mod;
}
} else {
dp[j + 1][l + 1][n] += 1LL * dp[j][l][n] * (proba + l) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l + 1][n] >= Mod) {
dp[j + 1][l + 1][n] -= Mod;
}
dp[j + 1][l][n] += 1LL * dp[j][l][n] * (probb - m - b[i] + n) %
Mod * nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l][n] >= Mod) {
dp[j + 1][l][n] -= Mod;
}
}
}
}
}
int all = 0;
for (int j = 0; j < 52; j++) {
for (int k = 0; k < 52; k++) {
all += 1LL * dp[m][j][k] * (a[i] ? b[i] + k : b[i] - k) % Mod;
if (all >= Mod) {
all -= Mod;
}
}
}
printf("%d\n", all);
}
}
| ### 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;
int n, m;
int a[55], b[55];
int dp[55][55][55];
const int Mod = 998244353;
int nii[5555];
int ksmii(int a, int b) {
if (!b) {
return 1;
}
int x = ksmii(a, b >> 1);
x = 1LL * x * x % Mod;
if (b & 1) {
x = 1LL * x * a % Mod;
}
return x;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i < 5555; i++) {
nii[i] = ksmii(i, Mod - 2);
}
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = 1; i <= n; i++) {
scanf("%d", b + i);
}
for (int i = 1; i <= n; i++) {
memset((int*)dp, 0, sizeof(dp));
int proba = 0, probb = 0;
for (int j = 1; j <= n; j++) {
if (!a[j]) {
probb += b[j];
} else {
proba += b[j];
}
}
dp[0][0][0]++;
for (int j = 0; j < m; j++) {
for (int l = 0; l <= j; l++) {
int m = j - l;
for (int n = 0; n < 55; n++) {
if (a[i]) {
dp[j + 1][l + 1][n + 1] += 1LL * dp[j][l][n] * (n + b[i]) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l + 1][n + 1] >= Mod) {
dp[j + 1][l + 1][n + 1] -= Mod;
}
} else {
dp[j + 1][l][n + 1] += 1LL * dp[j][l][n] * (b[i] - n) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l][n + 1] >= Mod) {
dp[j + 1][l][n + 1] -= Mod;
}
}
if (a[i]) {
dp[j + 1][l + 1][n] += 1LL * dp[j][l][n] * (proba + l - n - b[i]) %
Mod * nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l + 1][n] >= Mod) {
dp[j + 1][l + 1][n] -= Mod;
}
dp[j + 1][l][n] += 1LL * dp[j][l][n] * (probb - m) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l][n] >= Mod) {
dp[j + 1][l][n] -= Mod;
}
} else {
dp[j + 1][l + 1][n] += 1LL * dp[j][l][n] * (proba + l) % Mod *
nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l + 1][n] >= Mod) {
dp[j + 1][l + 1][n] -= Mod;
}
dp[j + 1][l][n] += 1LL * dp[j][l][n] * (probb - m - b[i] + n) %
Mod * nii[proba + probb + l - m] % Mod;
if (dp[j + 1][l][n] >= Mod) {
dp[j + 1][l][n] -= Mod;
}
}
}
}
}
int all = 0;
for (int j = 0; j < 52; j++) {
for (int k = 0; k < 52; k++) {
all += 1LL * dp[m][j][k] * (a[i] ? b[i] + k : b[i] - k) % Mod;
if (all >= Mod) {
all -= Mod;
}
}
}
printf("%d\n", all);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxm = 3001;
const int maxn = 2e6;
const long long mod = 998244353;
long long add(long long a, long long b) { return (a + b) % mod; }
long long sub(long long a, long long b) { return ((a - b) % mod + mod) % mod; }
long long mul(long long a, long long b) { return (a * b) % mod; }
long long ldp[maxm][maxm], ddp[maxm][maxm], tl, tdl;
int n, m, like[maxn], weight[maxn];
long long power(long long a, long long b, long long m) {
if (b == 0) return 1;
long long tmp = power(a, b / 2, m);
tmp = (tmp * tmp) % m;
if (b & 1) tmp = (tmp * a) % m;
return tmp;
}
long long disp[10000], st;
void preInv() {
st = tl + tdl + 5000;
for (int i = 0; i < 10000; i++) {
if (i > st) break;
disp[i] = power(st - i, mod - 2, mod);
}
}
long long inv(long long a) { return disp[st - a]; }
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 >> like[i];
for (int i = 1; i <= n; i++) {
cin >> weight[i];
if (like[i])
tl += weight[i];
else
tdl += weight[i];
}
preInv();
for (int i = 0; i <= m; i++) {
if ((m - i) > tdl) continue;
ldp[i][m - i] = 1;
ddp[i][m - i] = 1;
}
for (int i = m - 1; i >= 0; i--) {
for (int j = min(m - i - 1LL, tdl); j >= 0; j--) {
ldp[i][j] =
add(mul(2, mul(ldp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))),
add(mul(tl + i - 1,
mul(ldp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))),
mul(max(0LL, tdl - j),
mul(ldp[i][j + 1], inv(tl + i + max(0LL, tdl - j))))));
ddp[i][j] =
add(mul(max(0LL, tdl - j - 1),
mul(ddp[i][j + 1], inv(tl + i + max(0LL, tdl - j)))),
mul(tl + i, mul(ddp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))));
}
}
for (int i = 1; i <= n; i++) {
if (like[i]) {
cout << mul(weight[i], ldp[0][0]) << "\n";
} else {
cout << mul(weight[i], ddp[0][0]) << "\n";
}
}
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 maxm = 3001;
const int maxn = 2e6;
const long long mod = 998244353;
long long add(long long a, long long b) { return (a + b) % mod; }
long long sub(long long a, long long b) { return ((a - b) % mod + mod) % mod; }
long long mul(long long a, long long b) { return (a * b) % mod; }
long long ldp[maxm][maxm], ddp[maxm][maxm], tl, tdl;
int n, m, like[maxn], weight[maxn];
long long power(long long a, long long b, long long m) {
if (b == 0) return 1;
long long tmp = power(a, b / 2, m);
tmp = (tmp * tmp) % m;
if (b & 1) tmp = (tmp * a) % m;
return tmp;
}
long long disp[10000], st;
void preInv() {
st = tl + tdl + 5000;
for (int i = 0; i < 10000; i++) {
if (i > st) break;
disp[i] = power(st - i, mod - 2, mod);
}
}
long long inv(long long a) { return disp[st - a]; }
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 >> like[i];
for (int i = 1; i <= n; i++) {
cin >> weight[i];
if (like[i])
tl += weight[i];
else
tdl += weight[i];
}
preInv();
for (int i = 0; i <= m; i++) {
if ((m - i) > tdl) continue;
ldp[i][m - i] = 1;
ddp[i][m - i] = 1;
}
for (int i = m - 1; i >= 0; i--) {
for (int j = min(m - i - 1LL, tdl); j >= 0; j--) {
ldp[i][j] =
add(mul(2, mul(ldp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))),
add(mul(tl + i - 1,
mul(ldp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))),
mul(max(0LL, tdl - j),
mul(ldp[i][j + 1], inv(tl + i + max(0LL, tdl - j))))));
ddp[i][j] =
add(mul(max(0LL, tdl - j - 1),
mul(ddp[i][j + 1], inv(tl + i + max(0LL, tdl - j)))),
mul(tl + i, mul(ddp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))));
}
}
for (int i = 1; i <= n; i++) {
if (like[i]) {
cout << mul(weight[i], ldp[0][0]) << "\n";
} else {
cout << mul(weight[i], ddp[0][0]) << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long sum_mod(long long a, long long b) {
a += b;
if (a >= 998244353ll) return a - 998244353ll;
return a;
}
long long mul_mod(long long a, long long b) { return a * b % 998244353ll; }
long long sub_mod(long long a, long long b) {
if (b > a) return a + 998244353ll - b;
return a - b;
}
long long pow_mod(long long a, long long b) {
if (!b) return 1;
if (b & 1) return mul_mod(a, pow_mod(a, b - 1));
long long d = pow_mod(a, b / 2);
return mul_mod(d, d);
}
long long div_mod(long long a, long long b) {
long long res = mul_mod(a, pow_mod(b, 998244353ll - 2));
return res;
}
const long long MAXN = 101;
long long dp[MAXN][MAXN][MAXN];
long long SA, SB;
long long calc_res1(long long w, long long m) {
memset(dp, 0, sizeof(dp));
dp[w][0][0] = 1;
for (long long sm = 0; sm < m; sm++) {
for (long long i = 0; i <= sm; ++i) {
for (long long wi = 0; wi + 1 < MAXN; ++wi) {
long long j = sm - i;
if (dp[wi][i][j] == 0) continue;
long long obr = div_mod(1, sum_mod(sum_mod(SA, i), sub_mod(SB, j)));
dp[wi + 1][i + 1][j] = sum_mod(dp[wi + 1][i + 1][j],
mul_mod(mul_mod(wi, obr), dp[wi][i][j]));
dp[wi][i + 1][j] = sum_mod(
dp[wi][i + 1][j],
mul_mod(mul_mod(sub_mod(sum_mod(SA, i), wi), obr), dp[wi][i][j]));
dp[wi][i][j + 1] =
sum_mod(dp[wi][i][j + 1],
mul_mod(mul_mod(sub_mod(SB, j), obr), dp[wi][i][j]));
}
}
}
long long res = 0;
for (long long i = 0; i <= m; ++i) {
for (long long wi = 0; wi < MAXN; ++wi) {
res = sum_mod(res, mul_mod(dp[wi][i][m - i], wi));
}
}
return res;
}
long long calc_res2(long long w, long long m) {
memset(dp, 0, sizeof(dp));
dp[w][0][0] = 1;
for (long long sm = 0; sm < m; sm++) {
for (long long i = 0; i <= sm; ++i) {
for (long long wi = 1; wi < MAXN; wi++) {
long long j = sm - i;
if (dp[wi][i][j] == 0) continue;
long long obr = div_mod(1, sum_mod(sum_mod(SA, i), sub_mod(SB, j)));
dp[wi - 1][i][j + 1] = sum_mod(dp[wi - 1][i][j + 1],
mul_mod(mul_mod(wi, obr), dp[wi][i][j]));
dp[wi][i][j + 1] = sum_mod(
dp[wi][i][j + 1],
mul_mod(mul_mod(sub_mod(sub_mod(SB, j), wi), obr), dp[wi][i][j]));
dp[wi][i + 1][j] =
sum_mod(dp[wi][i + 1][j],
mul_mod(mul_mod(sum_mod(SA, i), obr), dp[wi][i][j]));
}
}
}
long long res = 0;
for (long long i = 0; i <= m; ++i) {
for (long long wi = 0; wi < MAXN; ++wi) {
res = sum_mod(res, mul_mod(dp[wi][i][m - i], wi));
}
}
return res;
}
void Solve() {
long long n, m;
cin >> n >> m;
vector<long long> a(n);
for (signed i = 0; i < (n); i++) cin >> a[i];
;
vector<long long> w(n);
for (signed i = 0; i < (n); i++) cin >> w[i];
;
SA = 0, SB = 0;
for (long long i = 0; i < n; ++i) {
if (a[i])
SA += w[i];
else
SB += w[i];
}
for (signed i = 0; i < (n); i++) {
long long res = (a[i] ? calc_res1(w[i], m) : calc_res2(w[i], m));
cout << res << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
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 sum_mod(long long a, long long b) {
a += b;
if (a >= 998244353ll) return a - 998244353ll;
return a;
}
long long mul_mod(long long a, long long b) { return a * b % 998244353ll; }
long long sub_mod(long long a, long long b) {
if (b > a) return a + 998244353ll - b;
return a - b;
}
long long pow_mod(long long a, long long b) {
if (!b) return 1;
if (b & 1) return mul_mod(a, pow_mod(a, b - 1));
long long d = pow_mod(a, b / 2);
return mul_mod(d, d);
}
long long div_mod(long long a, long long b) {
long long res = mul_mod(a, pow_mod(b, 998244353ll - 2));
return res;
}
const long long MAXN = 101;
long long dp[MAXN][MAXN][MAXN];
long long SA, SB;
long long calc_res1(long long w, long long m) {
memset(dp, 0, sizeof(dp));
dp[w][0][0] = 1;
for (long long sm = 0; sm < m; sm++) {
for (long long i = 0; i <= sm; ++i) {
for (long long wi = 0; wi + 1 < MAXN; ++wi) {
long long j = sm - i;
if (dp[wi][i][j] == 0) continue;
long long obr = div_mod(1, sum_mod(sum_mod(SA, i), sub_mod(SB, j)));
dp[wi + 1][i + 1][j] = sum_mod(dp[wi + 1][i + 1][j],
mul_mod(mul_mod(wi, obr), dp[wi][i][j]));
dp[wi][i + 1][j] = sum_mod(
dp[wi][i + 1][j],
mul_mod(mul_mod(sub_mod(sum_mod(SA, i), wi), obr), dp[wi][i][j]));
dp[wi][i][j + 1] =
sum_mod(dp[wi][i][j + 1],
mul_mod(mul_mod(sub_mod(SB, j), obr), dp[wi][i][j]));
}
}
}
long long res = 0;
for (long long i = 0; i <= m; ++i) {
for (long long wi = 0; wi < MAXN; ++wi) {
res = sum_mod(res, mul_mod(dp[wi][i][m - i], wi));
}
}
return res;
}
long long calc_res2(long long w, long long m) {
memset(dp, 0, sizeof(dp));
dp[w][0][0] = 1;
for (long long sm = 0; sm < m; sm++) {
for (long long i = 0; i <= sm; ++i) {
for (long long wi = 1; wi < MAXN; wi++) {
long long j = sm - i;
if (dp[wi][i][j] == 0) continue;
long long obr = div_mod(1, sum_mod(sum_mod(SA, i), sub_mod(SB, j)));
dp[wi - 1][i][j + 1] = sum_mod(dp[wi - 1][i][j + 1],
mul_mod(mul_mod(wi, obr), dp[wi][i][j]));
dp[wi][i][j + 1] = sum_mod(
dp[wi][i][j + 1],
mul_mod(mul_mod(sub_mod(sub_mod(SB, j), wi), obr), dp[wi][i][j]));
dp[wi][i + 1][j] =
sum_mod(dp[wi][i + 1][j],
mul_mod(mul_mod(sum_mod(SA, i), obr), dp[wi][i][j]));
}
}
}
long long res = 0;
for (long long i = 0; i <= m; ++i) {
for (long long wi = 0; wi < MAXN; ++wi) {
res = sum_mod(res, mul_mod(dp[wi][i][m - i], wi));
}
}
return res;
}
void Solve() {
long long n, m;
cin >> n >> m;
vector<long long> a(n);
for (signed i = 0; i < (n); i++) cin >> a[i];
;
vector<long long> w(n);
for (signed i = 0; i < (n); i++) cin >> w[i];
;
SA = 0, SB = 0;
for (long long i = 0; i < n; ++i) {
if (a[i])
SA += w[i];
else
SB += w[i];
}
for (signed i = 0; i < (n); i++) {
long long res = (a[i] ? calc_res1(w[i], m) : calc_res2(w[i], m));
cout << res << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
pair<int, int> v[55];
int dp[2][105][55][55], inv[3005];
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);
for (int cur = 0; cur < n; cur++) {
int c = v[cur].first;
memset(dp, 0, sizeof dp);
dp[c][v[cur].second][0][0] = 1;
if (c) {
for (int w = 1; w < 100; 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[c][w][i + 1][j + 1],
mul(mul(dp[c][w][i][j], (liked + j - w)), aux));
add(dp[c][w][i + 1][j],
mul(mul(dp[c][w][i][j], (disliked - k)), aux));
add(dp[c][w + 1][i + 1][j + 1], mul(mul(dp[c][w][i][j], w), aux));
}
} else {
for (int w = 100; 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[c][w][i + 1][j],
mul(mul(dp[c][w][i][j], (disliked - k - w)), aux));
add(dp[c][w][i + 1][j + 1],
mul(mul(dp[c][w][i][j], (liked + j)), aux));
add(dp[c][w - 1][i + 1][j], mul(mul(dp[c][w][i][j], w), aux));
}
}
int ans = 0;
for (int w = 0; w <= 100; w++) {
for (int j = 0; j <= m; j++) add(ans, mul(w, dp[c][w][m][j]));
}
printf("%d\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 int MOD = 998244353;
pair<int, int> v[55];
int dp[2][105][55][55], inv[3005];
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);
for (int cur = 0; cur < n; cur++) {
int c = v[cur].first;
memset(dp, 0, sizeof dp);
dp[c][v[cur].second][0][0] = 1;
if (c) {
for (int w = 1; w < 100; 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[c][w][i + 1][j + 1],
mul(mul(dp[c][w][i][j], (liked + j - w)), aux));
add(dp[c][w][i + 1][j],
mul(mul(dp[c][w][i][j], (disliked - k)), aux));
add(dp[c][w + 1][i + 1][j + 1], mul(mul(dp[c][w][i][j], w), aux));
}
} else {
for (int w = 100; 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[c][w][i + 1][j],
mul(mul(dp[c][w][i][j], (disliked - k - w)), aux));
add(dp[c][w][i + 1][j + 1],
mul(mul(dp[c][w][i][j], (liked + j)), aux));
add(dp[c][w - 1][i + 1][j], mul(mul(dp[c][w][i][j], w), aux));
}
}
int ans = 0;
for (int w = 0; w <= 100; w++) {
for (int j = 0; j <= m; j++) add(ans, mul(w, dp[c][w][m][j]));
}
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
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 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;
int n, m, tot, dep;
long long ny(long long x) {
return x == 1 ? 1
: ny(998244353 % x) * (998244353 - 998244353 / x) % 998244353;
}
long long dp[60][60][60], a[55], w[55], f[10];
int main() {
cin >> n >> m;
for (int i = (1); i <= (n); i++) cin >> a[i];
for (int i = (1); i <= (n); i++) {
cin >> w[i];
f[a[i]] += w[i];
}
for (int i = (1); i <= (n); i++) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int j = (1); j <= (m); j++) {
for (int x = (0); x <= (j - 1); x++)
for (int y = (0); y <= (j - 1); y++) {
long long ww = f[0] + f[1] + y - (j - 1 - y),
wi = w[i] + (a[i] ? x : -x);
if (ww <= 0) continue;
if (wi > 0)
(dp[j][x + 1][y + a[i]] += dp[j - 1][x][y] * wi % 998244353 *
ny(ww) % 998244353) %= 998244353;
(dp[j][x][y + 1] += dp[j - 1][x][y] * (f[1] + y - (a[i] ? wi : 0)) %
998244353 * ny(ww) % 998244353) %= 998244353;
if (f[0] - (j - 1 - y) - (a[i] ? 0 : wi) > 0)
(dp[j][x][y] += dp[j - 1][x][y] *
(f[0] - (j - 1 - y) - (a[i] ? 0 : wi)) % 998244353 *
ny(ww) % 998244353) %= 998244353;
}
}
long long ans = 0;
for (int x = (0); x <= (m); x++)
for (int y = (0); y <= (m); y++)
if (f[0] >= m - y && (a[i] || (w[i] + (a[i] ? x : -x)) > 0))
(ans += dp[m][x][y] * (w[i] + (a[i] ? x : -x)) % 998244353) %=
998244353;
cout << ans << endl;
}
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;
int n, m, tot, dep;
long long ny(long long x) {
return x == 1 ? 1
: ny(998244353 % x) * (998244353 - 998244353 / x) % 998244353;
}
long long dp[60][60][60], a[55], w[55], f[10];
int main() {
cin >> n >> m;
for (int i = (1); i <= (n); i++) cin >> a[i];
for (int i = (1); i <= (n); i++) {
cin >> w[i];
f[a[i]] += w[i];
}
for (int i = (1); i <= (n); i++) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int j = (1); j <= (m); j++) {
for (int x = (0); x <= (j - 1); x++)
for (int y = (0); y <= (j - 1); y++) {
long long ww = f[0] + f[1] + y - (j - 1 - y),
wi = w[i] + (a[i] ? x : -x);
if (ww <= 0) continue;
if (wi > 0)
(dp[j][x + 1][y + a[i]] += dp[j - 1][x][y] * wi % 998244353 *
ny(ww) % 998244353) %= 998244353;
(dp[j][x][y + 1] += dp[j - 1][x][y] * (f[1] + y - (a[i] ? wi : 0)) %
998244353 * ny(ww) % 998244353) %= 998244353;
if (f[0] - (j - 1 - y) - (a[i] ? 0 : wi) > 0)
(dp[j][x][y] += dp[j - 1][x][y] *
(f[0] - (j - 1 - y) - (a[i] ? 0 : wi)) % 998244353 *
ny(ww) % 998244353) %= 998244353;
}
}
long long ans = 0;
for (int x = (0); x <= (m); x++)
for (int y = (0); y <= (m); y++)
if (f[0] >= m - y && (a[i] || (w[i] + (a[i] ? x : -x)) > 0))
(ans += dp[m][x][y] * (w[i] + (a[i] ? x : -x)) % 998244353) %=
998244353;
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
const int mod = 998244353;
int like[maxn];
long long dp[maxn * 2][maxn][maxn][2];
int wi[maxn];
int n, m;
long long quick(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long inv(long long a) { return quick(a, mod - 2); }
bool cur = 0;
int suma = 0, sumb = 0;
long long dfs(int w, int k, int j) {
if (k + j == m) return w;
if (w <= 0) return 0;
if (dp[w][k][j][cur]) return dp[w][k][j][cur];
long long &ret = dp[w][k][j][cur];
long long ud = inv(suma + k + sumb - j);
long long curk = k + suma, curj = -j + sumb;
if (cur) {
ret = 1LL * w * ud % mod * dfs(w + 1, k + 1, j) % mod;
ret = (ret + 1LL * (curk - w + mod) * ud % mod * dfs(w, k + 1, j) % mod) %
mod;
ret = (ret + curj * ud % mod * dfs(w, k, j + 1) % mod) % mod;
} else {
ret = 1LL * w * ud % mod * dfs(w - 1, k, j + 1) % mod;
ret = (ret + 1LL * (curj - w + mod) * ud % mod * dfs(w, k, j + 1) % mod) %
mod;
ret = (ret + 1LL * curk * ud % mod * dfs(w, k + 1, j) % mod) % mod;
};
return ret;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &like[i]);
for (int i = 0; i < n; i++) {
scanf("%d", &wi[i]);
if (like[i])
suma += wi[i];
else
sumb += wi[i];
};
for (int i = 0; i < n; i++) {
cur = like[i];
printf("%lld\n", dfs(wi[i], 0, 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 int maxn = 55;
const int mod = 998244353;
int like[maxn];
long long dp[maxn * 2][maxn][maxn][2];
int wi[maxn];
int n, m;
long long quick(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long inv(long long a) { return quick(a, mod - 2); }
bool cur = 0;
int suma = 0, sumb = 0;
long long dfs(int w, int k, int j) {
if (k + j == m) return w;
if (w <= 0) return 0;
if (dp[w][k][j][cur]) return dp[w][k][j][cur];
long long &ret = dp[w][k][j][cur];
long long ud = inv(suma + k + sumb - j);
long long curk = k + suma, curj = -j + sumb;
if (cur) {
ret = 1LL * w * ud % mod * dfs(w + 1, k + 1, j) % mod;
ret = (ret + 1LL * (curk - w + mod) * ud % mod * dfs(w, k + 1, j) % mod) %
mod;
ret = (ret + curj * ud % mod * dfs(w, k, j + 1) % mod) % mod;
} else {
ret = 1LL * w * ud % mod * dfs(w - 1, k, j + 1) % mod;
ret = (ret + 1LL * (curj - w + mod) * ud % mod * dfs(w, k, j + 1) % mod) %
mod;
ret = (ret + 1LL * curk * ud % mod * dfs(w, k + 1, j) % mod) % mod;
};
return ret;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &like[i]);
for (int i = 0; i < n; i++) {
scanf("%d", &wi[i]);
if (like[i])
suma += wi[i];
else
sumb += wi[i];
};
for (int i = 0; i < n; i++) {
cur = like[i];
printf("%lld\n", dfs(wi[i], 0, 0));
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
const long long Mod = 998244353;
const int N = 200007;
const int M = 3005;
long long A[N], W[N], n, m, a, b, X, Y, dp[M][M];
long long power(long long x, long long y) {
if (y == 0)
return 1ll;
else {
long long a = (power(x, y / 2));
long long b = (a * a) % Mod;
if (y & 1)
return (b * x) % Mod;
else
return b;
}
}
long long add(long long a, long long b) { return (a + b) % Mod; }
long long mult(long long a, long long b) { return (a * b) % Mod; }
int main() {
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];
if (A[i])
X += W[i];
else
Y += W[i];
}
dp[0][0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
long long a = X + j, b = Y - (i - j);
long long inv = power(a + b, Mod - 2);
dp[i + 1][j + 1] = add(dp[i + 1][j + 1], mult(dp[i][j], mult(a, inv)));
dp[i + 1][j] = add(dp[i + 1][j], mult(dp[i][j], mult(b, inv)));
}
}
long long ea = 0, eb = 0;
for (int i = 0; i <= m; i++) {
ea = (ea + mult(dp[m][i], X + i)) % Mod;
eb = (eb + mult(dp[m][i], Y - (m - i))) % Mod;
}
a = power(X, Mod - 2);
b = power(Y, Mod - 2);
for (int i = 1; i <= n; i++) {
if (A[i])
cout << mult(W[i], mult(ea, a)) << endl;
else
cout << mult(W[i], mult(eb, b)) << endl;
}
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;
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
const long long Mod = 998244353;
const int N = 200007;
const int M = 3005;
long long A[N], W[N], n, m, a, b, X, Y, dp[M][M];
long long power(long long x, long long y) {
if (y == 0)
return 1ll;
else {
long long a = (power(x, y / 2));
long long b = (a * a) % Mod;
if (y & 1)
return (b * x) % Mod;
else
return b;
}
}
long long add(long long a, long long b) { return (a + b) % Mod; }
long long mult(long long a, long long b) { return (a * b) % Mod; }
int main() {
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];
if (A[i])
X += W[i];
else
Y += W[i];
}
dp[0][0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
long long a = X + j, b = Y - (i - j);
long long inv = power(a + b, Mod - 2);
dp[i + 1][j + 1] = add(dp[i + 1][j + 1], mult(dp[i][j], mult(a, inv)));
dp[i + 1][j] = add(dp[i + 1][j], mult(dp[i][j], mult(b, inv)));
}
}
long long ea = 0, eb = 0;
for (int i = 0; i <= m; i++) {
ea = (ea + mult(dp[m][i], X + i)) % Mod;
eb = (eb + mult(dp[m][i], Y - (m - i))) % Mod;
}
a = power(X, Mod - 2);
b = power(Y, Mod - 2);
for (int i = 1; i <= n; i++) {
if (A[i])
cout << mult(W[i], mult(ea, a)) << endl;
else
cout << mult(W[i], mult(eb, b)) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dp[102][55][55][55][2];
long long w[55];
long long a[55];
long long sa, sb;
long long n, m;
const long long MOD = 998244353;
long long funct(long long x) {
long long answer = 1, current = x;
long long p = MOD - 2;
while (p != 0) {
if (p % 2 != 0) {
answer = (answer * current) % MOD;
}
p /= 2;
current = (current * current) % MOD;
}
return answer;
}
long long solve(long long w, long long x, long long y, long long pos,
long long u) {
if (pos == m) return w;
if (w == 0) return 0;
if (dp[w][x][y][pos][u] != -1) return dp[w][x][y][pos][u];
long long ans = 0;
long long sumBad = -y + sb;
long long sumGood = x + sa;
if (u == 1) sumGood -= w;
if (u == 0) sumBad -= w;
if (sumBad != 0)
ans += (((sumBad * funct(sumBad + sumGood + w)) % MOD) *
solve(w, x, y + 1, pos + 1, u)) %
MOD;
if (sumGood != 0)
ans += (((sumGood * funct(sumBad + sumGood + w)) % MOD) *
solve(w, x + 1, y, pos + 1, u)) %
MOD;
if (w != 0) {
if (u == 0) {
ans += (((w * funct(sumGood + sumBad + w)) % MOD) *
solve(w - 1, x, y + 1, pos + 1, u)) %
MOD;
}
if (u == 1) {
ans += (((w * funct(sumGood + sumBad + w)) % MOD) *
solve(w + 1, x + 1, y, pos + 1, u)) %
MOD;
}
}
ans %= MOD;
dp[w][x][y][pos][u] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
long long i, j;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) {
cin >> w[i];
sa += a[i] * w[i];
sb += (1 - a[i]) * w[i];
}
memset(dp, -1, sizeof(dp));
for (i = 1; i <= n; i++) {
cout << solve(w[i], 0, 0, 0, a[i]) << endl;
}
}
| ### 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;
int dp[102][55][55][55][2];
long long w[55];
long long a[55];
long long sa, sb;
long long n, m;
const long long MOD = 998244353;
long long funct(long long x) {
long long answer = 1, current = x;
long long p = MOD - 2;
while (p != 0) {
if (p % 2 != 0) {
answer = (answer * current) % MOD;
}
p /= 2;
current = (current * current) % MOD;
}
return answer;
}
long long solve(long long w, long long x, long long y, long long pos,
long long u) {
if (pos == m) return w;
if (w == 0) return 0;
if (dp[w][x][y][pos][u] != -1) return dp[w][x][y][pos][u];
long long ans = 0;
long long sumBad = -y + sb;
long long sumGood = x + sa;
if (u == 1) sumGood -= w;
if (u == 0) sumBad -= w;
if (sumBad != 0)
ans += (((sumBad * funct(sumBad + sumGood + w)) % MOD) *
solve(w, x, y + 1, pos + 1, u)) %
MOD;
if (sumGood != 0)
ans += (((sumGood * funct(sumBad + sumGood + w)) % MOD) *
solve(w, x + 1, y, pos + 1, u)) %
MOD;
if (w != 0) {
if (u == 0) {
ans += (((w * funct(sumGood + sumBad + w)) % MOD) *
solve(w - 1, x, y + 1, pos + 1, u)) %
MOD;
}
if (u == 1) {
ans += (((w * funct(sumGood + sumBad + w)) % MOD) *
solve(w + 1, x + 1, y, pos + 1, u)) %
MOD;
}
}
ans %= MOD;
dp[w][x][y][pos][u] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
long long i, j;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) {
cin >> w[i];
sa += a[i] * w[i];
sb += (1 - a[i]) * w[i];
}
memset(dp, -1, sizeof(dp));
for (i = 1; i <= n; i++) {
cout << solve(w[i], 0, 0, 0, a[i]) << endl;
}
}
``` |
#include <bits/stdc++.h>
const int64_t mod = 998244353;
int gcd(int64_t a, int64_t b) {
while (a != 0 && b != 0) {
if (a >= b)
a = a % b;
else
b = b % a;
}
return a + b;
}
struct Fraction {
int64_t numerator;
int64_t devider;
Fraction(int64_t n = 0, int64_t d = 1) : numerator(n), devider(d){};
const Fraction& operator+(const Fraction& rv) {
if (rv.devider == devider) {
numerator += rv.numerator;
int64_t g = gcd(numerator, devider);
numerator /= g;
devider /= g;
numerator %= mod;
return *this;
} else {
numerator *= rv.devider;
numerator %= mod;
numerator += rv.numerator * devider;
numerator %= mod;
devider *= rv.devider;
int64_t g = gcd(numerator, devider);
numerator /= g;
devider /= g;
devider %= mod;
return *this;
}
}
const Fraction& operator*(const Fraction& rv) {
numerator *= rv.numerator;
devider *= rv.devider;
int64_t g = gcd(numerator, devider);
numerator /= g;
devider /= g;
numerator %= mod;
devider %= mod;
return *this;
}
};
struct Item {
Item(Fraction f, int64_t s) : possibility(f), sum(s){};
Fraction possibility;
int64_t sum;
};
void extended_euclid(int64_t a, int64_t b, int64_t& x, int64_t& y, int64_t& d) {
int64_t q, r, x1, x2, y1, y2;
if (b == 0) {
d = a, x = 1, y = 0;
return;
}
x2 = 1, x1 = 0, y2 = 0, y1 = 1;
while (b > 0) {
q = a / b, r = a - q * b;
x = x2 - q * x1, y = y2 - q * y1;
a = b, b = r;
x2 = x1, x1 = x, y2 = y1, y1 = y;
}
d = a, x = x2, y = y2;
}
int main() {
int64_t n, m;
std::cin >> n >> m;
std::vector<std::pair<int64_t, bool>> data;
int64_t posWeight = 0, negWeight = 0;
for (int64_t i = 0; i < n; ++i) {
int64_t a;
std::cin >> a;
if (a == 0)
data.push_back(std::make_pair(-1, false));
else
data.push_back(std::make_pair(-1, true));
}
for (auto& i : data) {
int64_t w;
std::cin >> w;
i.first = w;
if (i.second)
posWeight += w;
else
negWeight += w;
}
std::map<int64_t, Item> posMap{
std::make_pair(posWeight, Item(Fraction(1, 1), posWeight + negWeight))},
negMap{std::make_pair(negWeight,
Item(Fraction(1, 1), posWeight + negWeight))};
for (int64_t i = 0; i < m; ++i) {
std::map<int64_t, Item> newPos, newNeg;
for (auto j : posMap) {
if (newPos.find(j.first + 1) == newPos.end()) {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newPos.insert(std::make_pair(j.first + 1, Item(f, j.second.sum + 1)));
} else {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newPos.at(j.first + 1).possibility =
newPos.at(j.first + 1).possibility + f;
}
if (newPos.find(j.first) == newPos.end() && j.second.sum > j.first) {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newPos.insert(std::make_pair(j.first, Item(f, j.second.sum - 1)));
} else if (j.second.sum > j.first) {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newPos.at(j.first).possibility = newPos.at(j.first).possibility + f;
}
}
for (auto j : negMap) {
if (newNeg.find(j.first - 1) == newNeg.end() && j.first - 1 > 0) {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.insert(std::make_pair(j.first - 1, Item(f, j.second.sum - 1)));
} else if (j.first - 1 > 0) {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.at(j.first - 1).possibility =
newNeg.at(j.first - 1).possibility + f;
}
if (newNeg.find(j.first) == newNeg.end()) {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.insert(std::make_pair(j.first, Item(f, j.second.sum + 1)));
} else {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.at(j.first).possibility = newNeg.at(j.first).possibility + f;
}
}
posMap = newPos;
negMap = newNeg;
}
Fraction pos, neg;
for (auto i : posMap) pos = pos + Fraction(i.first, 1) * i.second.possibility;
for (auto i : negMap) neg = neg + Fraction(i.first, 1) * i.second.possibility;
for (auto i : data) {
Fraction res;
if (i.second)
res = Fraction(i.first, posWeight) * pos;
else
res = Fraction(i.first, negWeight) * neg;
int64_t d, x, y;
extended_euclid(res.devider, mod, x, y, d);
std::cout << (((x % mod + mod) % mod) * res.numerator) % mod << std::endl;
}
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>
const int64_t mod = 998244353;
int gcd(int64_t a, int64_t b) {
while (a != 0 && b != 0) {
if (a >= b)
a = a % b;
else
b = b % a;
}
return a + b;
}
struct Fraction {
int64_t numerator;
int64_t devider;
Fraction(int64_t n = 0, int64_t d = 1) : numerator(n), devider(d){};
const Fraction& operator+(const Fraction& rv) {
if (rv.devider == devider) {
numerator += rv.numerator;
int64_t g = gcd(numerator, devider);
numerator /= g;
devider /= g;
numerator %= mod;
return *this;
} else {
numerator *= rv.devider;
numerator %= mod;
numerator += rv.numerator * devider;
numerator %= mod;
devider *= rv.devider;
int64_t g = gcd(numerator, devider);
numerator /= g;
devider /= g;
devider %= mod;
return *this;
}
}
const Fraction& operator*(const Fraction& rv) {
numerator *= rv.numerator;
devider *= rv.devider;
int64_t g = gcd(numerator, devider);
numerator /= g;
devider /= g;
numerator %= mod;
devider %= mod;
return *this;
}
};
struct Item {
Item(Fraction f, int64_t s) : possibility(f), sum(s){};
Fraction possibility;
int64_t sum;
};
void extended_euclid(int64_t a, int64_t b, int64_t& x, int64_t& y, int64_t& d) {
int64_t q, r, x1, x2, y1, y2;
if (b == 0) {
d = a, x = 1, y = 0;
return;
}
x2 = 1, x1 = 0, y2 = 0, y1 = 1;
while (b > 0) {
q = a / b, r = a - q * b;
x = x2 - q * x1, y = y2 - q * y1;
a = b, b = r;
x2 = x1, x1 = x, y2 = y1, y1 = y;
}
d = a, x = x2, y = y2;
}
int main() {
int64_t n, m;
std::cin >> n >> m;
std::vector<std::pair<int64_t, bool>> data;
int64_t posWeight = 0, negWeight = 0;
for (int64_t i = 0; i < n; ++i) {
int64_t a;
std::cin >> a;
if (a == 0)
data.push_back(std::make_pair(-1, false));
else
data.push_back(std::make_pair(-1, true));
}
for (auto& i : data) {
int64_t w;
std::cin >> w;
i.first = w;
if (i.second)
posWeight += w;
else
negWeight += w;
}
std::map<int64_t, Item> posMap{
std::make_pair(posWeight, Item(Fraction(1, 1), posWeight + negWeight))},
negMap{std::make_pair(negWeight,
Item(Fraction(1, 1), posWeight + negWeight))};
for (int64_t i = 0; i < m; ++i) {
std::map<int64_t, Item> newPos, newNeg;
for (auto j : posMap) {
if (newPos.find(j.first + 1) == newPos.end()) {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newPos.insert(std::make_pair(j.first + 1, Item(f, j.second.sum + 1)));
} else {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newPos.at(j.first + 1).possibility =
newPos.at(j.first + 1).possibility + f;
}
if (newPos.find(j.first) == newPos.end() && j.second.sum > j.first) {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newPos.insert(std::make_pair(j.first, Item(f, j.second.sum - 1)));
} else if (j.second.sum > j.first) {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newPos.at(j.first).possibility = newPos.at(j.first).possibility + f;
}
}
for (auto j : negMap) {
if (newNeg.find(j.first - 1) == newNeg.end() && j.first - 1 > 0) {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.insert(std::make_pair(j.first - 1, Item(f, j.second.sum - 1)));
} else if (j.first - 1 > 0) {
Fraction f(j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.at(j.first - 1).possibility =
newNeg.at(j.first - 1).possibility + f;
}
if (newNeg.find(j.first) == newNeg.end()) {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.insert(std::make_pair(j.first, Item(f, j.second.sum + 1)));
} else {
Fraction f(j.second.sum - j.first, j.second.sum);
f = f * j.second.possibility;
newNeg.at(j.first).possibility = newNeg.at(j.first).possibility + f;
}
}
posMap = newPos;
negMap = newNeg;
}
Fraction pos, neg;
for (auto i : posMap) pos = pos + Fraction(i.first, 1) * i.second.possibility;
for (auto i : negMap) neg = neg + Fraction(i.first, 1) * i.second.possibility;
for (auto i : data) {
Fraction res;
if (i.second)
res = Fraction(i.first, posWeight) * pos;
else
res = Fraction(i.first, negWeight) * neg;
int64_t d, x, y;
extended_euclid(res.devider, mod, x, y, d);
std::cout << (((x % mod + mod) % mod) * res.numerator) % mod << std::endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int MOD = 998244353;
const int kN = 50 + 5;
int n, m;
int a[kN], w[kN];
int dp[kN][kN][kN];
const int kM = kN * kN * 20;
int Inv[kM];
int inv(int x) {
return x == 1 ? 1 : (MOD - MOD / x) * 1LL * inv(MOD % x) % MOD;
}
inline void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
int main() {
scanf("%d%d", &n, &m);
Inv[1] = 1;
for (int i = 2; i < kM; ++i) Inv[i] = inv(i);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
for (int i = 0; i < n; ++i) scanf("%d", w + i);
int negative = 0;
int sum = 0;
for (int i = 0; i < n; ++i)
if (a[i] == 0) negative += w[i];
for (int i = 0; i < n; ++i) sum += w[i];
for (int i = 0; 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 r = 0; r <= j; ++r)
if (dp[j][k][r]) {
int c = a[i] == 1 ? w[i] + k : w[i] - k;
if (c) {
add(dp[j + 1][k + 1][r + (a[i] == 0)],
dp[j][k][r] * 1LL * c % MOD * Inv[sum - r + j - r] % MOD);
}
int ne_ci = negative - (a[i] == 0 ? w[i] : 0);
int sum_ci = sum - w[i];
int r_ci = r - (a[i] == 0 ? k : 0);
int f_ci = j - r - (a[i] == 1 ? k : 0);
if (ne_ci < sum_ci) {
add(dp[j + 1][k][r], dp[j][k][r] * 1LL * (sum_ci - ne_ci + f_ci) %
MOD * Inv[sum - r + j - r] % MOD);
}
if (ne_ci > r_ci) {
add(dp[j + 1][k][r + 1], dp[j][k][r] * 1LL * (ne_ci - r_ci) %
MOD * Inv[sum - r + j - r] % MOD);
}
}
}
}
int result = w[i];
for (int k = 0; k <= m; ++k)
for (int r = 0; r <= m; ++r)
if (dp[m][k][r])
add(result, dp[m][k][r] * 1LL * (a[i] == 1 ? k : (MOD - k)) % MOD);
printf("%d\n", result);
}
}
| ### 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>
const int MOD = 998244353;
const int kN = 50 + 5;
int n, m;
int a[kN], w[kN];
int dp[kN][kN][kN];
const int kM = kN * kN * 20;
int Inv[kM];
int inv(int x) {
return x == 1 ? 1 : (MOD - MOD / x) * 1LL * inv(MOD % x) % MOD;
}
inline void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
int main() {
scanf("%d%d", &n, &m);
Inv[1] = 1;
for (int i = 2; i < kM; ++i) Inv[i] = inv(i);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
for (int i = 0; i < n; ++i) scanf("%d", w + i);
int negative = 0;
int sum = 0;
for (int i = 0; i < n; ++i)
if (a[i] == 0) negative += w[i];
for (int i = 0; i < n; ++i) sum += w[i];
for (int i = 0; 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 r = 0; r <= j; ++r)
if (dp[j][k][r]) {
int c = a[i] == 1 ? w[i] + k : w[i] - k;
if (c) {
add(dp[j + 1][k + 1][r + (a[i] == 0)],
dp[j][k][r] * 1LL * c % MOD * Inv[sum - r + j - r] % MOD);
}
int ne_ci = negative - (a[i] == 0 ? w[i] : 0);
int sum_ci = sum - w[i];
int r_ci = r - (a[i] == 0 ? k : 0);
int f_ci = j - r - (a[i] == 1 ? k : 0);
if (ne_ci < sum_ci) {
add(dp[j + 1][k][r], dp[j][k][r] * 1LL * (sum_ci - ne_ci + f_ci) %
MOD * Inv[sum - r + j - r] % MOD);
}
if (ne_ci > r_ci) {
add(dp[j + 1][k][r + 1], dp[j][k][r] * 1LL * (ne_ci - r_ci) %
MOD * Inv[sum - r + j - r] % MOD);
}
}
}
}
int result = w[i];
for (int k = 0; k <= m; ++k)
for (int r = 0; r <= m; ++r)
if (dp[m][k][r])
add(result, dp[m][k][r] * 1LL * (a[i] == 1 ? k : (MOD - k)) % MOD);
printf("%d\n", result);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
inline void OUT(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
cout << first << " ";
OUT(rest...);
return;
}
template <typename T>
void vec_print(vector<T> VEC) {
for (long long i = (long long)0; i < (long long)VEC.size(); ++i) {
cout << VEC[i] << " ";
}
cout << "\n";
};
template <typename T>
void mat_print(vector<vector<T>> MAT) {
for (long long i = (long long)0; i < (long long)MAT.size(); ++i) {
for (long long j = (long long)0; j < (long long)MAT[i].size(); ++j) {
cout << MAT[i][j] << " ";
}
cout << "\n";
}
};
template <typename CLASS1, typename CLASS2>
class HOGE {
public:
CLASS1 key;
CLASS2 value;
HOGE(void) { return; };
HOGE(CLASS1 key, CLASS2 value) {
this->key = key;
this->value = value;
};
~HOGE(void) { return; };
void print(void) {
cout << "key : " << key << ", value : " << value << "\n";
return;
};
bool operator==(const HOGE &obj) { return (this->value == obj.value); };
bool operator<(const HOGE &obj) { return (this->value < obj.value); };
bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};
template <typename CLASS1, typename CLASS2>
bool operator==(const HOGE<CLASS1, CLASS2> &hoge1,
const HOGE<CLASS1, CLASS2> &hoge2) {
return hoge1.value == hoge2.value;
};
template <typename CLASS1, typename CLASS2>
bool operator<(const HOGE<CLASS1, CLASS2> &hoge1,
const HOGE<CLASS1, CLASS2> &hoge2) {
return hoge1.value < hoge2.value;
};
template <typename CLASS1, typename CLASS2>
bool operator>(const HOGE<CLASS1, CLASS2> &hoge1,
const HOGE<CLASS1, CLASS2> &hoge2) {
return hoge1.value > hoge2.value;
};
constexpr int INF = (1 << 30);
constexpr long long INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr long long MOD = (long long)(998244353);
vector<long long> inv(10000);
void initialize_inv(void) {
inv[1] = 1;
for (int i = 2; i < 10000; ++i) {
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
}
return;
}
vector<vector<long long>> dp_all(2557, vector<long long>(2557, 0));
set<pair<long long, long long>> st_all;
void calc_all(long long wp, long long wm, long long m) {
vector<map<pair<long long, long long>, long long>> vmp(m + 1);
vmp[0][make_pair(wp, wm)] += 1;
for (long long M = (long long)0; M < (long long)m; ++M) {
for (auto itr : vmp[M]) {
long long wp_tmp = itr.first.first;
long long wm_tmp = itr.first.second;
long long dp_tmp = itr.second;
long long inv_sum = inv[wp_tmp + wm_tmp];
vmp[M + 1][make_pair(wp_tmp + 1, wm_tmp)] +=
(dp_tmp * ((wp_tmp * inv_sum) % MOD)) % MOD;
vmp[M + 1][make_pair(wp_tmp + 1, wm_tmp)] %= MOD;
if (wm_tmp - 1 >= 0) {
vmp[M + 1][make_pair(wp_tmp, wm_tmp - 1)] +=
(dp_tmp * ((wm_tmp * inv_sum) % MOD)) % MOD;
vmp[M + 1][make_pair(wp_tmp, wm_tmp - 1)] %= MOD;
}
}
}
for (auto itr : vmp[m]) {
long long wp_tmp = itr.first.first;
long long wm_tmp = itr.first.second;
dp_all[wp_tmp][wm_tmp] = itr.second;
pair<long long, long long> st_tmp = make_pair(wp_tmp, wm_tmp);
st_all.emplace(st_tmp);
}
return;
}
long long calc_p(long long w, long long wp, long long m) {
vector<vector<long long>> dp(107, vector<long long>(3000, 0));
dp[w][wp] = 1;
set<pair<long long, long long>> st;
pair<long long, long long> st_tmp = make_pair(w, wp);
st.emplace(st_tmp);
for (long long M = (long long)0; M < (long long)m; ++M) {
set<pair<long long, long long>> st_new;
for (auto itr : st) {
long long w_tmp = itr.first;
long long wp_tmp = itr.second;
long long inv_sum = inv[w_tmp + wp_tmp];
dp[w_tmp + 1][wp_tmp] +=
(dp[w_tmp][wp_tmp] * ((w_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp + 1][wp_tmp] %= MOD;
pair<long long, long long> st_tmp = make_pair(w_tmp + 1, wp_tmp);
st_new.emplace(st_tmp);
dp[w_tmp][wp_tmp + 1] +=
(dp[w_tmp][wp_tmp] * ((wp_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp][wp_tmp + 1] %= MOD;
st_tmp = make_pair(w_tmp, wp_tmp + 1);
st_new.emplace(st_tmp);
}
st = st_new;
}
long long ans = 0;
for (auto itr : st_all) {
long long wp_tmp = itr.first;
long long wm_tmp = itr.second;
long long k = wp_tmp - w - wp;
long long count = 0;
for (int i = 0; i <= k && wp_tmp - w - i >= 0; ++i) {
ans += ((w + i) *
((dp_all[wp_tmp][wm_tmp] * dp[w + i][wp_tmp - w - i]) % MOD)) %
MOD;
ans %= MOD;
count++;
}
}
return ans;
}
long long calc_m(long long w, long long wm, long long m) {
vector<vector<long long>> dp(107, vector<long long>(3000, 0));
dp[w][wm] = 1;
set<pair<long long, long long>> st;
pair<long long, long long> st_tmp = make_pair(w, wm);
st.emplace(st_tmp);
for (long long M = (long long)0; M < (long long)m; ++M) {
set<pair<long long, long long>> st_new;
for (auto itr : st) {
long long w_tmp = itr.first;
long long wm_tmp = itr.second;
long long inv_sum = inv[w_tmp + wm_tmp];
if (w_tmp - 1 >= 0) {
dp[w_tmp - 1][wm_tmp] +=
(dp[w_tmp][wm_tmp] * ((w_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp - 1][wm_tmp] %= MOD;
pair<long long, long long> st_tmp = make_pair(w_tmp - 1, wm_tmp);
st_new.emplace(st_tmp);
}
if (wm_tmp - 1 >= 0) {
dp[w_tmp][wm_tmp - 1] +=
(dp[w_tmp][wm_tmp] * ((wm_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp][wm_tmp - 1] %= MOD;
st_tmp = make_pair(w_tmp, wm_tmp - 1);
st_new.emplace(st_tmp);
}
}
st = st_new;
}
long long ans = 0;
for (auto itr : st_all) {
long long wp_tmp = itr.first;
long long wm_tmp = itr.second;
long long k = w + wm - wm_tmp;
for (int i = 0; i <= k && w - i > 0; ++i) {
if (wm_tmp - w + i >= 0) {
ans += ((w - i) *
((dp_all[wp_tmp][wm_tmp] * dp[w - i][wm_tmp - w + i]) % MOD)) %
MOD;
ans %= MOD;
}
}
}
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
initialize_inv();
long long n, m;
IN(n, m);
vector<long long> a(n);
for (long long i = (long long)0; i < (long long)n; ++i) IN(a[i]);
vector<long long> w(n);
long long wp_sum = 0, wm_sum = 0;
for (long long i = (long long)0; i < (long long)n; ++i) {
IN(w[i]);
if (a[i] == 0)
wm_sum += w[i];
else
wp_sum += w[i];
}
calc_all(wp_sum, wm_sum, m);
for (long long i = (long long)0; i < (long long)n; ++i) {
if (a[i] == 0) {
OUT(calc_m(w[i], wm_sum - w[i], m));
} else {
OUT(calc_p(w[i], wp_sum - w[i], m));
}
}
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;
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
inline void OUT(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
cout << first << " ";
OUT(rest...);
return;
}
template <typename T>
void vec_print(vector<T> VEC) {
for (long long i = (long long)0; i < (long long)VEC.size(); ++i) {
cout << VEC[i] << " ";
}
cout << "\n";
};
template <typename T>
void mat_print(vector<vector<T>> MAT) {
for (long long i = (long long)0; i < (long long)MAT.size(); ++i) {
for (long long j = (long long)0; j < (long long)MAT[i].size(); ++j) {
cout << MAT[i][j] << " ";
}
cout << "\n";
}
};
template <typename CLASS1, typename CLASS2>
class HOGE {
public:
CLASS1 key;
CLASS2 value;
HOGE(void) { return; };
HOGE(CLASS1 key, CLASS2 value) {
this->key = key;
this->value = value;
};
~HOGE(void) { return; };
void print(void) {
cout << "key : " << key << ", value : " << value << "\n";
return;
};
bool operator==(const HOGE &obj) { return (this->value == obj.value); };
bool operator<(const HOGE &obj) { return (this->value < obj.value); };
bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};
template <typename CLASS1, typename CLASS2>
bool operator==(const HOGE<CLASS1, CLASS2> &hoge1,
const HOGE<CLASS1, CLASS2> &hoge2) {
return hoge1.value == hoge2.value;
};
template <typename CLASS1, typename CLASS2>
bool operator<(const HOGE<CLASS1, CLASS2> &hoge1,
const HOGE<CLASS1, CLASS2> &hoge2) {
return hoge1.value < hoge2.value;
};
template <typename CLASS1, typename CLASS2>
bool operator>(const HOGE<CLASS1, CLASS2> &hoge1,
const HOGE<CLASS1, CLASS2> &hoge2) {
return hoge1.value > hoge2.value;
};
constexpr int INF = (1 << 30);
constexpr long long INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr long long MOD = (long long)(998244353);
vector<long long> inv(10000);
void initialize_inv(void) {
inv[1] = 1;
for (int i = 2; i < 10000; ++i) {
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
}
return;
}
vector<vector<long long>> dp_all(2557, vector<long long>(2557, 0));
set<pair<long long, long long>> st_all;
void calc_all(long long wp, long long wm, long long m) {
vector<map<pair<long long, long long>, long long>> vmp(m + 1);
vmp[0][make_pair(wp, wm)] += 1;
for (long long M = (long long)0; M < (long long)m; ++M) {
for (auto itr : vmp[M]) {
long long wp_tmp = itr.first.first;
long long wm_tmp = itr.first.second;
long long dp_tmp = itr.second;
long long inv_sum = inv[wp_tmp + wm_tmp];
vmp[M + 1][make_pair(wp_tmp + 1, wm_tmp)] +=
(dp_tmp * ((wp_tmp * inv_sum) % MOD)) % MOD;
vmp[M + 1][make_pair(wp_tmp + 1, wm_tmp)] %= MOD;
if (wm_tmp - 1 >= 0) {
vmp[M + 1][make_pair(wp_tmp, wm_tmp - 1)] +=
(dp_tmp * ((wm_tmp * inv_sum) % MOD)) % MOD;
vmp[M + 1][make_pair(wp_tmp, wm_tmp - 1)] %= MOD;
}
}
}
for (auto itr : vmp[m]) {
long long wp_tmp = itr.first.first;
long long wm_tmp = itr.first.second;
dp_all[wp_tmp][wm_tmp] = itr.second;
pair<long long, long long> st_tmp = make_pair(wp_tmp, wm_tmp);
st_all.emplace(st_tmp);
}
return;
}
long long calc_p(long long w, long long wp, long long m) {
vector<vector<long long>> dp(107, vector<long long>(3000, 0));
dp[w][wp] = 1;
set<pair<long long, long long>> st;
pair<long long, long long> st_tmp = make_pair(w, wp);
st.emplace(st_tmp);
for (long long M = (long long)0; M < (long long)m; ++M) {
set<pair<long long, long long>> st_new;
for (auto itr : st) {
long long w_tmp = itr.first;
long long wp_tmp = itr.second;
long long inv_sum = inv[w_tmp + wp_tmp];
dp[w_tmp + 1][wp_tmp] +=
(dp[w_tmp][wp_tmp] * ((w_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp + 1][wp_tmp] %= MOD;
pair<long long, long long> st_tmp = make_pair(w_tmp + 1, wp_tmp);
st_new.emplace(st_tmp);
dp[w_tmp][wp_tmp + 1] +=
(dp[w_tmp][wp_tmp] * ((wp_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp][wp_tmp + 1] %= MOD;
st_tmp = make_pair(w_tmp, wp_tmp + 1);
st_new.emplace(st_tmp);
}
st = st_new;
}
long long ans = 0;
for (auto itr : st_all) {
long long wp_tmp = itr.first;
long long wm_tmp = itr.second;
long long k = wp_tmp - w - wp;
long long count = 0;
for (int i = 0; i <= k && wp_tmp - w - i >= 0; ++i) {
ans += ((w + i) *
((dp_all[wp_tmp][wm_tmp] * dp[w + i][wp_tmp - w - i]) % MOD)) %
MOD;
ans %= MOD;
count++;
}
}
return ans;
}
long long calc_m(long long w, long long wm, long long m) {
vector<vector<long long>> dp(107, vector<long long>(3000, 0));
dp[w][wm] = 1;
set<pair<long long, long long>> st;
pair<long long, long long> st_tmp = make_pair(w, wm);
st.emplace(st_tmp);
for (long long M = (long long)0; M < (long long)m; ++M) {
set<pair<long long, long long>> st_new;
for (auto itr : st) {
long long w_tmp = itr.first;
long long wm_tmp = itr.second;
long long inv_sum = inv[w_tmp + wm_tmp];
if (w_tmp - 1 >= 0) {
dp[w_tmp - 1][wm_tmp] +=
(dp[w_tmp][wm_tmp] * ((w_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp - 1][wm_tmp] %= MOD;
pair<long long, long long> st_tmp = make_pair(w_tmp - 1, wm_tmp);
st_new.emplace(st_tmp);
}
if (wm_tmp - 1 >= 0) {
dp[w_tmp][wm_tmp - 1] +=
(dp[w_tmp][wm_tmp] * ((wm_tmp * inv_sum) % MOD)) % MOD;
dp[w_tmp][wm_tmp - 1] %= MOD;
st_tmp = make_pair(w_tmp, wm_tmp - 1);
st_new.emplace(st_tmp);
}
}
st = st_new;
}
long long ans = 0;
for (auto itr : st_all) {
long long wp_tmp = itr.first;
long long wm_tmp = itr.second;
long long k = w + wm - wm_tmp;
for (int i = 0; i <= k && w - i > 0; ++i) {
if (wm_tmp - w + i >= 0) {
ans += ((w - i) *
((dp_all[wp_tmp][wm_tmp] * dp[w - i][wm_tmp - w + i]) % MOD)) %
MOD;
ans %= MOD;
}
}
}
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
initialize_inv();
long long n, m;
IN(n, m);
vector<long long> a(n);
for (long long i = (long long)0; i < (long long)n; ++i) IN(a[i]);
vector<long long> w(n);
long long wp_sum = 0, wm_sum = 0;
for (long long i = (long long)0; i < (long long)n; ++i) {
IN(w[i]);
if (a[i] == 0)
wm_sum += w[i];
else
wp_sum += w[i];
}
calc_all(wp_sum, wm_sum, m);
for (long long i = (long long)0; i < (long long)n; ++i) {
if (a[i] == 0) {
OUT(calc_m(w[i], wm_sum - w[i], m));
} else {
OUT(calc_p(w[i], wp_sum - w[i], m));
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long lh[100005];
long long w[100005];
long long n, M;
long long sumw = 0, suml = 0, sumh = 0;
long long dp[55][55][55];
long long md = 998244353;
long long modInverse(long long a, long long m) {
if (a == 0) return 0;
long long m0 = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x = (m0 + x % m0);
return x;
}
long long d(int i, int j, int k, int l) {
long long o = (M + (md - (i + k) % md) % md) % md;
if (dp[i][j][k] != -1) return dp[i][j][k];
if (i == 0) return dp[i][j][k] = 0;
if (lh[l]) {
long long p =
(((w[l] + j) % md) *
modInverse(((sumw + (k + (md - o) % md) % md) % md) % md, md)) %
md;
long long p2 =
(((suml + (k + (md - (w[l] + j) % md) % md) % md) % md) *
modInverse(
(sumw + (k + (md - (o + ((w[l] + j) % md) % md) % md) % md) % md) %
md,
md)) %
md;
long long x = (1 + (md - p) % md) % md;
long long xx = (x * (1 + (md - p2) % md) % md) % md;
return dp[i][j][k] = ((((d(i - 1, j + 1, k + 1, l) + 1) % md) * p) % md +
((((d(i - 1, j, k + 1, l) * x) % md) * p2) % md +
((d(i - 1, j, k, l) * xx) % md) % md) %
md) %
md;
} else {
long long p = (((w[l] + (md - j) % md) % md) *
modInverse((sumw + (k + (md - o) % md) % md) % md, md)) %
md;
long long p2 =
(((sumh + md - (o + (w[l] + md - j) % md) % md) % md) *
modInverse(
(sumw + (k + md - (o + (w[l] + md - j) % md) % md) % md) % md,
md)) %
md;
long long x = (1 + (md - p) % md) % md;
long long xx = (x * (1 + (md - p2) % md) % md) % md;
return dp[i][j][k] =
((((d(i - 1, j + 1, k, l) + (md - 1) % md) % md) * p) % md +
((((d(i - 1, j, k, l) * x) % md) * p2) % md +
((d(i - 1, j, k + 1, l) * xx) % md) % md) %
md) %
md;
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> M;
for (int i = 0; i < n; i++) {
cin >> lh[i];
}
for (int i = 0; i < n; i++) {
cin >> w[i];
sumw = (sumw + w[i]) % md;
if (lh[i])
suml = (suml + w[i]) % md;
else
sumh = (sumh + w[i]) % md;
}
for (int l = 0; l < n; l++) {
for (int i = 0; i < 55; i++) {
for (int j = 0; j < 55; j++) {
for (int k = 0; k < 55; k++) {
dp[i][j][k] = -1;
}
}
}
cout << (w[l] + d(M, 0, 0, l)) % md << endl;
}
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 lh[100005];
long long w[100005];
long long n, M;
long long sumw = 0, suml = 0, sumh = 0;
long long dp[55][55][55];
long long md = 998244353;
long long modInverse(long long a, long long m) {
if (a == 0) return 0;
long long m0 = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x = (m0 + x % m0);
return x;
}
long long d(int i, int j, int k, int l) {
long long o = (M + (md - (i + k) % md) % md) % md;
if (dp[i][j][k] != -1) return dp[i][j][k];
if (i == 0) return dp[i][j][k] = 0;
if (lh[l]) {
long long p =
(((w[l] + j) % md) *
modInverse(((sumw + (k + (md - o) % md) % md) % md) % md, md)) %
md;
long long p2 =
(((suml + (k + (md - (w[l] + j) % md) % md) % md) % md) *
modInverse(
(sumw + (k + (md - (o + ((w[l] + j) % md) % md) % md) % md) % md) %
md,
md)) %
md;
long long x = (1 + (md - p) % md) % md;
long long xx = (x * (1 + (md - p2) % md) % md) % md;
return dp[i][j][k] = ((((d(i - 1, j + 1, k + 1, l) + 1) % md) * p) % md +
((((d(i - 1, j, k + 1, l) * x) % md) * p2) % md +
((d(i - 1, j, k, l) * xx) % md) % md) %
md) %
md;
} else {
long long p = (((w[l] + (md - j) % md) % md) *
modInverse((sumw + (k + (md - o) % md) % md) % md, md)) %
md;
long long p2 =
(((sumh + md - (o + (w[l] + md - j) % md) % md) % md) *
modInverse(
(sumw + (k + md - (o + (w[l] + md - j) % md) % md) % md) % md,
md)) %
md;
long long x = (1 + (md - p) % md) % md;
long long xx = (x * (1 + (md - p2) % md) % md) % md;
return dp[i][j][k] =
((((d(i - 1, j + 1, k, l) + (md - 1) % md) % md) * p) % md +
((((d(i - 1, j, k, l) * x) % md) * p2) % md +
((d(i - 1, j, k + 1, l) * xx) % md) % md) %
md) %
md;
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> M;
for (int i = 0; i < n; i++) {
cin >> lh[i];
}
for (int i = 0; i < n; i++) {
cin >> w[i];
sumw = (sumw + w[i]) % md;
if (lh[i])
suml = (suml + w[i]) % md;
else
sumh = (sumh + w[i]) % md;
}
for (int l = 0; l < n; l++) {
for (int i = 0; i < 55; i++) {
for (int j = 0; j < 55; j++) {
for (int k = 0; k < 55; k++) {
dp[i][j][k] = -1;
}
}
}
cout << (w[l] + d(M, 0, 0, l)) % md << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void file(bool opt) {
if (opt && fopen("in.txt", "r")) {
freopen("in.txt", "r", stdin);
}
}
const long long mod = 998244353;
long long dp[2][3000 + 5][3000 + 5];
long long sum[2];
long long a[200000 + 5];
long long inv[6000 + 5];
bool check[200000 + 5];
long long poww(long long a, long long b) {
long long res = 1;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &check[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), sum[check[i]] += a[i];
for (int i = max(0ll, m - sum[0]); i <= 2 * m; i++)
inv[i] = poww(sum[0] + sum[1] + i - m, mod - 2);
for (int i = m; i >= 0; i--) {
dp[0][i][m - i] = dp[1][i][m - i] = 1;
for (int j = min((long long)m - i - 1, sum[0]); j >= 0; j--) {
dp[1][i][j] = (dp[1][i + 1][j] * (long long)(sum[1] + i + 1) % mod +
dp[1][i][j + 1] * (long long)(sum[0] - j) % mod) *
inv[i - j + m] % mod;
dp[0][i][j] = (dp[0][i + 1][j] * (long long)(sum[1] + i) % mod +
dp[0][i][j + 1] * (long long)(sum[0] - j - 1) % mod) *
inv[i - j + m] % mod;
}
}
for (int i = 1; i <= n; i++)
printf("%lld\n", a[i] * dp[check[i]][0][0] % mod);
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;
void file(bool opt) {
if (opt && fopen("in.txt", "r")) {
freopen("in.txt", "r", stdin);
}
}
const long long mod = 998244353;
long long dp[2][3000 + 5][3000 + 5];
long long sum[2];
long long a[200000 + 5];
long long inv[6000 + 5];
bool check[200000 + 5];
long long poww(long long a, long long b) {
long long res = 1;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &check[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), sum[check[i]] += a[i];
for (int i = max(0ll, m - sum[0]); i <= 2 * m; i++)
inv[i] = poww(sum[0] + sum[1] + i - m, mod - 2);
for (int i = m; i >= 0; i--) {
dp[0][i][m - i] = dp[1][i][m - i] = 1;
for (int j = min((long long)m - i - 1, sum[0]); j >= 0; j--) {
dp[1][i][j] = (dp[1][i + 1][j] * (long long)(sum[1] + i + 1) % mod +
dp[1][i][j + 1] * (long long)(sum[0] - j) % mod) *
inv[i - j + m] % mod;
dp[0][i][j] = (dp[0][i + 1][j] * (long long)(sum[1] + i) % mod +
dp[0][i][j + 1] * (long long)(sum[0] - j - 1) % mod) *
inv[i - j + m] % mod;
}
}
for (int i = 1; i <= n; i++)
printf("%lld\n", a[i] * dp[check[i]][0][0] % mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int put(int a, int p) {
int ans = 1;
while (p) {
if (p & 1) ans = 1LL * ans * a % 998244353;
a = 1LL * a * a % 998244353;
p /= 2;
}
return ans;
}
int invmod(int x) { return put(x, 998244353 - 2); }
void add(int& a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
const int MMAX = 3010;
const int NMAX = 200010;
int w[NMAX], like[NMAX];
int n, m, s, splus, sminus;
int dp[MMAX][MMAX];
int serieplus[NMAX];
int serieminus[NMAX];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", like + i);
for (int i = 1; i <= n; i++) {
scanf("%d", w + i);
s += w[i];
if (like[i])
splus += w[i];
else
sminus += w[i];
}
dp[0][0] = 1;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= i; j++) {
int nrplus = splus + j;
int nrminus = sminus - (i - j);
if (nrminus < 0) assert(dp[i][j] == 0);
int nrtot = nrplus + nrminus;
int inv = invmod(nrtot);
add(dp[i + 1][j], 1LL * dp[i][j] * nrminus % 998244353 * inv % 998244353);
add(dp[i + 1][j + 1],
1LL * dp[i][j] * nrplus % 998244353 * inv % 998244353);
}
}
int mult_plus = 0, mult_minus = 0;
serieplus[0] = 1;
for (int i = 1; i <= m; i++)
serieplus[i] =
1LL * serieplus[i - 1] * (1 + invmod(splus + i - 1)) % 998244353;
serieminus[0] = 1;
for (int i = 1; i <= min(m, sminus); i++)
serieminus[i] = 1LL * serieminus[i - 1] *
(1 - invmod(sminus - i + 1) + 998244353) % 998244353;
for (int i = 0; i <= m; i++) {
add(mult_plus, 1LL * dp[m][i] * serieplus[i] % 998244353);
add(mult_minus, 1LL * dp[m][m - i] * serieminus[i] % 998244353);
}
for (int i = 1; i <= n; i++) {
int ans = 1LL * w[i] * (like[i] ? mult_plus : mult_minus) % 998244353;
if (ans < 0) ans += 998244353;
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;
int put(int a, int p) {
int ans = 1;
while (p) {
if (p & 1) ans = 1LL * ans * a % 998244353;
a = 1LL * a * a % 998244353;
p /= 2;
}
return ans;
}
int invmod(int x) { return put(x, 998244353 - 2); }
void add(int& a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
const int MMAX = 3010;
const int NMAX = 200010;
int w[NMAX], like[NMAX];
int n, m, s, splus, sminus;
int dp[MMAX][MMAX];
int serieplus[NMAX];
int serieminus[NMAX];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", like + i);
for (int i = 1; i <= n; i++) {
scanf("%d", w + i);
s += w[i];
if (like[i])
splus += w[i];
else
sminus += w[i];
}
dp[0][0] = 1;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= i; j++) {
int nrplus = splus + j;
int nrminus = sminus - (i - j);
if (nrminus < 0) assert(dp[i][j] == 0);
int nrtot = nrplus + nrminus;
int inv = invmod(nrtot);
add(dp[i + 1][j], 1LL * dp[i][j] * nrminus % 998244353 * inv % 998244353);
add(dp[i + 1][j + 1],
1LL * dp[i][j] * nrplus % 998244353 * inv % 998244353);
}
}
int mult_plus = 0, mult_minus = 0;
serieplus[0] = 1;
for (int i = 1; i <= m; i++)
serieplus[i] =
1LL * serieplus[i - 1] * (1 + invmod(splus + i - 1)) % 998244353;
serieminus[0] = 1;
for (int i = 1; i <= min(m, sminus); i++)
serieminus[i] = 1LL * serieminus[i - 1] *
(1 - invmod(sminus - i + 1) + 998244353) % 998244353;
for (int i = 0; i <= m; i++) {
add(mult_plus, 1LL * dp[m][i] * serieplus[i] % 998244353);
add(mult_minus, 1LL * dp[m][m - i] * serieminus[i] % 998244353);
}
for (int i = 1; i <= n; i++) {
int ans = 1LL * w[i] * (like[i] ? mult_plus : mult_minus) % 998244353;
if (ans < 0) ans += 998244353;
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 998244353, M = 3e3 + 5;
int n, m;
long long a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3];
int ksm(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = 1LL * ans * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &w[i]);
sum[a[i]] += w[i];
sum[2] += w[i];
}
for (int i = 0; i <= 2 * m; i++) inv[i] = ksm(sum[2] + 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, (int)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 (int i = 1; i <= n; i++)
cout << w[i] * (a[i] ? f[0][0] : g[0][0]) % 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 N = 1e5 + 5, mod = 998244353, M = 3e3 + 5;
int n, m;
long long a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3];
int ksm(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = 1LL * ans * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &w[i]);
sum[a[i]] += w[i];
sum[2] += w[i];
}
for (int i = 0; i <= 2 * m; i++) inv[i] = ksm(sum[2] + 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, (int)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 (int i = 1; i <= n; i++)
cout << w[i] * (a[i] ? f[0][0] : g[0][0]) % mod << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long read() {
int f = 1;
long long res = 0;
char ch;
do {
ch = getchar();
if (ch == '-') f = -f;
} while (ch < '0' || ch > '9');
do {
res = res * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
return f == 1 ? res : -res;
}
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int N = 200010;
const int M = 3010;
const int mod = 998244353;
const long long INF = 1e18;
int n, m;
int A[N], B[N];
char str[N];
int head[N], to[N * 2], nxt[N * 2], tot;
void addEdge(int u, int v) {
tot++;
nxt[tot] = head[u];
to[tot] = v;
head[u] = tot;
}
template <class T>
T mmax(T a, T b) {
return a < b ? b : a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
int countOne(long long set) {
int res = 0;
while (set) {
res++;
set &= set - 1;
}
return res;
}
bool contain(long long set, int i) { return (set & (1LL << i)) > 0; }
long long myPow(long long a, int p) {
if (p == 0) return 1;
long long res = myPow(a, p / 2);
res *= res;
res %= mod;
if (p % 2 == 1) {
res *= a;
res %= mod;
}
return res;
}
void addMode(long long &a, long long b) { a = (a + b) % mod; }
long long mul(long long a, long long b) { return a * b % mod; }
template <class T>
void mySwap(T &a, T &b) {
T tmp = a;
a = b;
b = tmp;
}
long long sum[2];
long long inv[M * 2];
long long f[M][M];
long long g[M][M];
int main() {
fast_io();
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> A[i];
for (int i = 0; i < n; i++) {
cin >> B[i];
sum[A[i]] += B[i];
}
for (int i = mmax(0LL, m - sum[0] - sum[1]); i <= 2 * m; i++) {
inv[i] = myPow(sum[0] + sum[1] - m + i, mod - 2);
}
for (int i = m; i >= 0; i--) {
g[i][m - i] = f[i][m - i] = 1;
for (int j = mmin((long long)m - i - 1, sum[0]); j >= 0; j--) {
f[i][j] = (f[i + 1][j] * (sum[1] + i + 1) + f[i][j + 1] * (sum[0] - j)) %
mod * inv[m + i - j] % mod;
g[i][j] = (g[i + 1][j] * (sum[1] + i) + g[i][j + 1] * (sum[0] - j - 1)) %
mod * inv[m + i - j] % mod;
}
}
for (int i = 0; i < n; i++) {
cout << (A[i] ? f[0][0] : g[0][0]) * B[i] % 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;
long long read() {
int f = 1;
long long res = 0;
char ch;
do {
ch = getchar();
if (ch == '-') f = -f;
} while (ch < '0' || ch > '9');
do {
res = res * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
return f == 1 ? res : -res;
}
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int N = 200010;
const int M = 3010;
const int mod = 998244353;
const long long INF = 1e18;
int n, m;
int A[N], B[N];
char str[N];
int head[N], to[N * 2], nxt[N * 2], tot;
void addEdge(int u, int v) {
tot++;
nxt[tot] = head[u];
to[tot] = v;
head[u] = tot;
}
template <class T>
T mmax(T a, T b) {
return a < b ? b : a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
int countOne(long long set) {
int res = 0;
while (set) {
res++;
set &= set - 1;
}
return res;
}
bool contain(long long set, int i) { return (set & (1LL << i)) > 0; }
long long myPow(long long a, int p) {
if (p == 0) return 1;
long long res = myPow(a, p / 2);
res *= res;
res %= mod;
if (p % 2 == 1) {
res *= a;
res %= mod;
}
return res;
}
void addMode(long long &a, long long b) { a = (a + b) % mod; }
long long mul(long long a, long long b) { return a * b % mod; }
template <class T>
void mySwap(T &a, T &b) {
T tmp = a;
a = b;
b = tmp;
}
long long sum[2];
long long inv[M * 2];
long long f[M][M];
long long g[M][M];
int main() {
fast_io();
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> A[i];
for (int i = 0; i < n; i++) {
cin >> B[i];
sum[A[i]] += B[i];
}
for (int i = mmax(0LL, m - sum[0] - sum[1]); i <= 2 * m; i++) {
inv[i] = myPow(sum[0] + sum[1] - m + i, mod - 2);
}
for (int i = m; i >= 0; i--) {
g[i][m - i] = f[i][m - i] = 1;
for (int j = mmin((long long)m - i - 1, sum[0]); j >= 0; j--) {
f[i][j] = (f[i + 1][j] * (sum[1] + i + 1) + f[i][j + 1] * (sum[0] - j)) %
mod * inv[m + i - j] % mod;
g[i][j] = (g[i + 1][j] * (sum[1] + i) + g[i][j + 1] * (sum[0] - j - 1)) %
mod * inv[m + i - j] % mod;
}
}
for (int i = 0; i < n; i++) {
cout << (A[i] ? f[0][0] : g[0][0]) * B[i] % mod << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mxn = 262144, md = 998244353;
int rd() {
int x = 0, flg = 1;
char c = getchar();
for (; (c < 48 || c > 57) && c != '-'; c = getchar())
;
if (c == '-') flg = -1, c = getchar();
for (; c > 47 && c < 58; x = x * 10 + c - 48, c = getchar())
;
return x * flg;
}
int power(int x, int p, int num = 1) {
for (; p; p >>= 1, x = 1ll * x * x % md)
if (p & 1) num = 1ll * num * x % md;
return num;
}
int n, m, sa, sb, tg[64], f[2][64][64][128], inv[16384];
int main() {
n = rd(), m = rd();
for (int i = 1; i <= n; ++i) tg[i] = rd();
for (int i = 1, x; i <= n; ++i)
x = rd(), f[0][i][0][x] = 1, sa += tg[i] * x, sb += (!tg[i]) * x;
bool cur = 0;
for (int t = 0; t < m; ++t) {
cur ^= 1;
memset(f[cur], 0, sizeof(f[cur]));
for (int i = 1; i <= n; ++i)
if (tg[i])
for (int j = 0; j <= t; ++j) {
int inv = power(sa + sb - t + 2 * j, md - 2);
for (int k = 0; k <= 100; ++k)
if (f[cur ^ 1][i][j][k]) {
f[cur][i][j + 1][k + 1] =
(f[cur][i][j + 1][k + 1] +
1ll * f[cur ^ 1][i][j][k] * k % md * inv) %
md;
f[cur][i][j + 1][k] =
(f[cur][i][j + 1][k] +
1ll * f[cur ^ 1][i][j][k] * (sa + j - k) % md * inv) %
md;
f[cur][i][j][k] =
(f[cur][i][j][k] +
1ll * f[cur ^ 1][i][j][k] * (sb - t + j) % md * inv) %
md;
}
}
else
for (int j = 0; j <= t; ++j) {
int inv = power(sa + sb - t + 2 * j, md - 2);
for (int k = 0; k <= 100; ++k)
if (f[cur ^ 1][i][j][k]) {
f[cur][i][j][k - 1] = (f[cur][i][j][k - 1] +
1ll * f[cur ^ 1][i][j][k] * k % md * inv) %
md;
f[cur][i][j][k] =
(f[cur][i][j][k] +
1ll * f[cur ^ 1][i][j][k] * (sb - t + j - k) % md * inv) %
md;
f[cur][i][j + 1][k] =
(f[cur][i][j + 1][k] +
1ll * f[cur ^ 1][i][j][k] * (sa + j) % md * inv) %
md;
}
}
}
for (int i = 1; i <= n; ++i) {
int ans = 0;
for (int j = 0; j <= m; ++j)
for (int k = 0; k <= 100; ++k)
ans = (ans + 1ll * f[cur][i][j][k] * k) % md;
printf("%d\n", ans);
}
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 mxn = 262144, md = 998244353;
int rd() {
int x = 0, flg = 1;
char c = getchar();
for (; (c < 48 || c > 57) && c != '-'; c = getchar())
;
if (c == '-') flg = -1, c = getchar();
for (; c > 47 && c < 58; x = x * 10 + c - 48, c = getchar())
;
return x * flg;
}
int power(int x, int p, int num = 1) {
for (; p; p >>= 1, x = 1ll * x * x % md)
if (p & 1) num = 1ll * num * x % md;
return num;
}
int n, m, sa, sb, tg[64], f[2][64][64][128], inv[16384];
int main() {
n = rd(), m = rd();
for (int i = 1; i <= n; ++i) tg[i] = rd();
for (int i = 1, x; i <= n; ++i)
x = rd(), f[0][i][0][x] = 1, sa += tg[i] * x, sb += (!tg[i]) * x;
bool cur = 0;
for (int t = 0; t < m; ++t) {
cur ^= 1;
memset(f[cur], 0, sizeof(f[cur]));
for (int i = 1; i <= n; ++i)
if (tg[i])
for (int j = 0; j <= t; ++j) {
int inv = power(sa + sb - t + 2 * j, md - 2);
for (int k = 0; k <= 100; ++k)
if (f[cur ^ 1][i][j][k]) {
f[cur][i][j + 1][k + 1] =
(f[cur][i][j + 1][k + 1] +
1ll * f[cur ^ 1][i][j][k] * k % md * inv) %
md;
f[cur][i][j + 1][k] =
(f[cur][i][j + 1][k] +
1ll * f[cur ^ 1][i][j][k] * (sa + j - k) % md * inv) %
md;
f[cur][i][j][k] =
(f[cur][i][j][k] +
1ll * f[cur ^ 1][i][j][k] * (sb - t + j) % md * inv) %
md;
}
}
else
for (int j = 0; j <= t; ++j) {
int inv = power(sa + sb - t + 2 * j, md - 2);
for (int k = 0; k <= 100; ++k)
if (f[cur ^ 1][i][j][k]) {
f[cur][i][j][k - 1] = (f[cur][i][j][k - 1] +
1ll * f[cur ^ 1][i][j][k] * k % md * inv) %
md;
f[cur][i][j][k] =
(f[cur][i][j][k] +
1ll * f[cur ^ 1][i][j][k] * (sb - t + j - k) % md * inv) %
md;
f[cur][i][j + 1][k] =
(f[cur][i][j + 1][k] +
1ll * f[cur ^ 1][i][j][k] * (sa + j) % md * inv) %
md;
}
}
}
for (int i = 1; i <= n; ++i) {
int ans = 0;
for (int j = 0; j <= m; ++j)
for (int k = 0; k <= 100; ++k)
ans = (ans + 1ll * f[cur][i][j][k] * k) % md;
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = INT_MAX;
const long long MOD = 998244353;
long long extgcd(long long a, long long b, long long &x, long long &y) {
long long g = a;
x = 1;
y = 0;
if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
return g;
}
long long invMod(long long a, long long m) {
long long x, y;
if (extgcd(a, m, x, y) == 1)
return (x + m) % m;
else
return 0;
}
long long N, M;
bool a[60];
long long w[60];
long long inc, decr;
map<tuple<long long, long long, long long, long long>, long long> mp;
long long dfs(long long i, long long j, long long k, long long in,
long long de) {
if (mp.count(make_tuple(j, k, in, de))) return mp[make_tuple(j, k, in, de)];
if (j == M) {
return k;
}
long long ret = 0;
if (k > 0)
ret = (ret + k * dfs(i, j + 1, k + 2 * a[i] - 1, in, de) % MOD) % MOD;
ret = (ret + in * dfs(i, j + 1, k, in + 1, de) % MOD) % MOD;
if (de > 0) ret = (ret + de * dfs(i, j + 1, k, in, de - 1) % MOD) % MOD;
ret = ret * invMod(k + in + de, MOD) % MOD;
return mp[make_tuple(j, k, in, de)] = ret;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
for (long long i = 0; i < N; i++) cin >> a[i];
for (long long i = 0; i < N; i++) cin >> w[i];
for (long long i = 0; i < N; i++) {
if (a[i])
inc += w[i];
else
decr += w[i];
}
for (long long i = 0; i < N; i++) {
if (a[i])
inc -= w[i];
else
decr -= w[i];
long long ans = dfs(i, 0, w[i], inc, decr);
cout << ans << endl;
if (a[i])
inc += w[i];
else
decr += w[i];
}
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 long long INF = INT_MAX;
const long long MOD = 998244353;
long long extgcd(long long a, long long b, long long &x, long long &y) {
long long g = a;
x = 1;
y = 0;
if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
return g;
}
long long invMod(long long a, long long m) {
long long x, y;
if (extgcd(a, m, x, y) == 1)
return (x + m) % m;
else
return 0;
}
long long N, M;
bool a[60];
long long w[60];
long long inc, decr;
map<tuple<long long, long long, long long, long long>, long long> mp;
long long dfs(long long i, long long j, long long k, long long in,
long long de) {
if (mp.count(make_tuple(j, k, in, de))) return mp[make_tuple(j, k, in, de)];
if (j == M) {
return k;
}
long long ret = 0;
if (k > 0)
ret = (ret + k * dfs(i, j + 1, k + 2 * a[i] - 1, in, de) % MOD) % MOD;
ret = (ret + in * dfs(i, j + 1, k, in + 1, de) % MOD) % MOD;
if (de > 0) ret = (ret + de * dfs(i, j + 1, k, in, de - 1) % MOD) % MOD;
ret = ret * invMod(k + in + de, MOD) % MOD;
return mp[make_tuple(j, k, in, de)] = ret;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
for (long long i = 0; i < N; i++) cin >> a[i];
for (long long i = 0; i < N; i++) cin >> w[i];
for (long long i = 0; i < N; i++) {
if (a[i])
inc += w[i];
else
decr += w[i];
}
for (long long i = 0; i < N; i++) {
if (a[i])
inc -= w[i];
else
decr -= w[i];
long long ans = dfs(i, 0, w[i], inc, decr);
cout << ans << endl;
if (a[i])
inc += w[i];
else
decr += w[i];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
int n, m, a[55], w[55], f[55][105][55], g[55][105][55], x, sa, sb;
int power(int b, int e) {
if (!e) return 1;
int tmp = power(b, e >> 1);
tmp = 1ll * tmp * tmp % Mod;
if (e & 1) tmp = 1ll * tmp * b % Mod;
return tmp;
}
int main() {
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++)
if (!a[i])
sa += w[i];
else
sb += w[i];
for (int j = 0; j <= 100; j++)
for (int k = 0; k <= m; k++) f[0][j][k] = j;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= 100; j++)
for (int k = 0; k <= m; k++)
if (sb + k >= j && sa >= m - i - k) {
f[i][j][k] = (1ll * j * f[i - 1][j + 1][k + 1] +
1ll * (sb + k - j) * f[i - 1][j][k + 1] +
1ll * (sa - m + i + k) * f[i - 1][j][k]) %
Mod;
f[i][j][k] =
1ll * f[i][j][k] * power(sa + sb + k - m + i + k, Mod - 2) % Mod;
}
for (int j = 0; j <= 100; j++)
for (int k = 0; k <= m; k++) g[0][j][k] = j;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= 100; j++)
for (int k = 0; k <= m; k++) {
g[i][j][k] = (1ll * j * g[i - 1][j - 1][k] +
1ll * (sb + k) * g[i - 1][j][k + 1] +
1ll * (sa - j - m + i + k) * g[i - 1][j][k]) %
Mod;
g[i][j][k] =
1ll * g[i][j][k] * power(sa + sb + k - m + i + k, Mod - 2) % Mod;
}
for (int i = 1; i <= n; i++)
if (a[i])
cout << f[m][w[i]][0] << '\n';
else
cout << g[m][w[i]][0] << '\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;
const int Mod = 998244353;
int n, m, a[55], w[55], f[55][105][55], g[55][105][55], x, sa, sb;
int power(int b, int e) {
if (!e) return 1;
int tmp = power(b, e >> 1);
tmp = 1ll * tmp * tmp % Mod;
if (e & 1) tmp = 1ll * tmp * b % Mod;
return tmp;
}
int main() {
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++)
if (!a[i])
sa += w[i];
else
sb += w[i];
for (int j = 0; j <= 100; j++)
for (int k = 0; k <= m; k++) f[0][j][k] = j;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= 100; j++)
for (int k = 0; k <= m; k++)
if (sb + k >= j && sa >= m - i - k) {
f[i][j][k] = (1ll * j * f[i - 1][j + 1][k + 1] +
1ll * (sb + k - j) * f[i - 1][j][k + 1] +
1ll * (sa - m + i + k) * f[i - 1][j][k]) %
Mod;
f[i][j][k] =
1ll * f[i][j][k] * power(sa + sb + k - m + i + k, Mod - 2) % Mod;
}
for (int j = 0; j <= 100; j++)
for (int k = 0; k <= m; k++) g[0][j][k] = j;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= 100; j++)
for (int k = 0; k <= m; k++) {
g[i][j][k] = (1ll * j * g[i - 1][j - 1][k] +
1ll * (sb + k) * g[i - 1][j][k + 1] +
1ll * (sa - j - m + i + k) * g[i - 1][j][k]) %
Mod;
g[i][j][k] =
1ll * g[i][j][k] * power(sa + sb + k - m + i + k, Mod - 2) % Mod;
}
for (int i = 1; i <= n; i++)
if (a[i])
cout << f[m][w[i]][0] << '\n';
else
cout << g[m][w[i]][0] << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long b[55], a[55];
long long dp[55][55][55];
long long inv[200000];
int main() {
inv[1] = 1;
for (int i = 2; i <= 199999; i++)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] == 0) a[i] = -1;
}
long long sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
if (a[i] == -1)
sum1 += b[i];
else
sum2 += b[i];
}
for (int q = 1; q <= n; q++) {
long long ans = 0;
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
for (int k = 0; k <= m; k++) dp[i][j][k] = 0;
dp[0][0][0] = 1;
for (int i = 0; i <= m; i++) {
for (int j = 0; j + i <= m; j++) {
for (int k = 0; k + j + i <= m; k++) {
long long pi = dp[i][j][k];
if (pi) {
long long s = sum1 + sum2 + a[q] * i - j + k;
long long t1 = b[q] + a[q] * i;
long long t2, t3;
if (a[q] == -1) {
t2 = sum1 - b[q] - j;
t3 = sum2 + k;
} else {
t2 = sum1 - j;
t3 = sum2 - b[q] + k;
}
if (t1 < 0 || t2 < 0 || t3 < 0) {
dp[i][j][k] = 0;
continue;
}
if (t1)
dp[i + 1][j][k] =
(dp[i + 1][j][k] + pi * t1 % mod * inv[s] % mod) % mod;
if (t2)
dp[i][j + 1][k] =
(dp[i][j + 1][k] + pi * t2 % mod * inv[s] % mod) % mod;
if (t3)
dp[i][j][k + 1] =
(dp[i][j][k + 1] + pi * t3 % mod * inv[s] % mod) % mod;
}
}
}
}
for (int i = 0; i <= m; i++) {
for (int j = 0; j + i <= m; j++) {
for (int k = 0; k + j + i <= m; k++) {
if (i + j + k == m && dp[i][j][k]) {
long long tmp = (b[q] + a[q] * i) * dp[i][j][k] % mod;
ans = (ans + tmp + mod) % mod;
}
}
}
}
printf("%lld\n", ans);
}
}
| ### 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;
long long b[55], a[55];
long long dp[55][55][55];
long long inv[200000];
int main() {
inv[1] = 1;
for (int i = 2; i <= 199999; i++)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] == 0) a[i] = -1;
}
long long sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
if (a[i] == -1)
sum1 += b[i];
else
sum2 += b[i];
}
for (int q = 1; q <= n; q++) {
long long ans = 0;
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
for (int k = 0; k <= m; k++) dp[i][j][k] = 0;
dp[0][0][0] = 1;
for (int i = 0; i <= m; i++) {
for (int j = 0; j + i <= m; j++) {
for (int k = 0; k + j + i <= m; k++) {
long long pi = dp[i][j][k];
if (pi) {
long long s = sum1 + sum2 + a[q] * i - j + k;
long long t1 = b[q] + a[q] * i;
long long t2, t3;
if (a[q] == -1) {
t2 = sum1 - b[q] - j;
t3 = sum2 + k;
} else {
t2 = sum1 - j;
t3 = sum2 - b[q] + k;
}
if (t1 < 0 || t2 < 0 || t3 < 0) {
dp[i][j][k] = 0;
continue;
}
if (t1)
dp[i + 1][j][k] =
(dp[i + 1][j][k] + pi * t1 % mod * inv[s] % mod) % mod;
if (t2)
dp[i][j + 1][k] =
(dp[i][j + 1][k] + pi * t2 % mod * inv[s] % mod) % mod;
if (t3)
dp[i][j][k + 1] =
(dp[i][j][k + 1] + pi * t3 % mod * inv[s] % mod) % mod;
}
}
}
}
for (int i = 0; i <= m; i++) {
for (int j = 0; j + i <= m; j++) {
for (int k = 0; k + j + i <= m; k++) {
if (i + j + k == m && dp[i][j][k]) {
long long tmp = (b[q] + a[q] * i) * dp[i][j][k] % mod;
ans = (ans + tmp + mod) % mod;
}
}
}
}
printf("%lld\n", ans);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 50;
int isgood[N], w[N];
int n, m;
long long power(long long a, long long b) {
long long x = 1, y = a;
while (b > 0) {
if (b & 1) {
x = (x * y);
if (x > MOD) x %= MOD;
}
y = y * y;
if (y > MOD) y %= MOD;
b >>= 1;
}
return x;
}
int inv(int x) { return power(x, MOD - 2); }
int mul(int x, int y) { return 1LL * x * y % MOD; }
int add(int x, int y) {
int ret = x + y;
if (ret >= MOD) ret -= MOD;
return ret;
}
map<pair<pair<int, int>, pair<int, int> >, int> map1;
int solve2(int idx, int me, int g, int b, bool flag) {
if (idx == m) return me;
if (map1.find(make_pair(make_pair(idx, me), make_pair(g, b))) != map1.end())
return map1[{{idx, me}, {g, b}}];
int ret = mul(mul(g, inv(g + b + me)), solve2(idx + 1, me, g + 1, b, flag));
if (b)
ret = add(
ret, mul(mul(b, inv(g + b + me)), solve2(idx + 1, me, g, b - 1, flag)));
if (flag || me)
ret = add(ret, mul(mul(me, inv(g + b + me)),
solve2(idx + 1, me + (flag ? 1 : -1), g, b, flag)));
return map1[{{idx, me}, {g, b}}] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int g = 0, b = 0;
for (int i = 0; i < int(n); i++) cin >> isgood[i];
for (int i = 0; i < int(n); i++) cin >> w[i];
for (int i = 0; i < int(n); i++) {
if (isgood[i])
g += w[i];
else
b += w[i];
}
for (int i = 0; i < int(n); i++) {
int cur = solve2(0, w[i], g - (isgood[i] ? w[i] : 0),
b - (isgood[i] ? 0 : w[i]), isgood[i]);
cout << cur << '\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;
const int MOD = 998244353;
const int N = 50;
int isgood[N], w[N];
int n, m;
long long power(long long a, long long b) {
long long x = 1, y = a;
while (b > 0) {
if (b & 1) {
x = (x * y);
if (x > MOD) x %= MOD;
}
y = y * y;
if (y > MOD) y %= MOD;
b >>= 1;
}
return x;
}
int inv(int x) { return power(x, MOD - 2); }
int mul(int x, int y) { return 1LL * x * y % MOD; }
int add(int x, int y) {
int ret = x + y;
if (ret >= MOD) ret -= MOD;
return ret;
}
map<pair<pair<int, int>, pair<int, int> >, int> map1;
int solve2(int idx, int me, int g, int b, bool flag) {
if (idx == m) return me;
if (map1.find(make_pair(make_pair(idx, me), make_pair(g, b))) != map1.end())
return map1[{{idx, me}, {g, b}}];
int ret = mul(mul(g, inv(g + b + me)), solve2(idx + 1, me, g + 1, b, flag));
if (b)
ret = add(
ret, mul(mul(b, inv(g + b + me)), solve2(idx + 1, me, g, b - 1, flag)));
if (flag || me)
ret = add(ret, mul(mul(me, inv(g + b + me)),
solve2(idx + 1, me + (flag ? 1 : -1), g, b, flag)));
return map1[{{idx, me}, {g, b}}] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int g = 0, b = 0;
for (int i = 0; i < int(n); i++) cin >> isgood[i];
for (int i = 0; i < int(n); i++) cin >> w[i];
for (int i = 0; i < int(n); i++) {
if (isgood[i])
g += w[i];
else
b += w[i];
}
for (int i = 0; i < int(n); i++) {
int cur = solve2(0, w[i], g - (isgood[i] ? w[i] : 0),
b - (isgood[i] ? 0 : w[i]), isgood[i]);
cout << cur << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long w0, w1;
long long a[200005], b[200005];
long long dp[3005][3005];
long long fpow(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long ans[200005];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; ++i) cin >> a[i];
for (long long i = 1; i <= n; ++i) cin >> b[i];
for (long long i = 1; i <= n; ++i)
if (a[i])
w1 += b[i];
else
w0 += b[i];
dp[0][0] = 1;
for (long long i = 0; i <= m; ++i) {
for (long long j = 0; j <= m; ++j) {
dp[i][j] += (w1 + i - 1) * fpow(w0 + w1 + i - 1 - j, mod - 2) % mod *
dp[i - 1][j] % mod;
dp[i][j] += (w0 - j + 1) * fpow(w0 + w1 + i - j + 1, mod - 2) % mod *
dp[i][j - 1] % mod;
while (dp[i][j] > mod) dp[i][j] -= mod;
}
}
long long a0 = 0, a1 = 0;
for (long long i = 0; i <= m; ++i) {
a0 += (m - i) * dp[i][m - i] % mod;
a1 += i * dp[i][m - i] % mod;
if (a0 > mod) a0 -= mod;
if (a1 > mod) a1 -= mod;
}
for (long long i = 1; i <= n; ++i) {
if (a[i])
ans[i] = b[i] + a1 * b[i] % mod * fpow(w1, mod - 2) % mod;
else
ans[i] = b[i] - a0 * b[i] % mod * fpow(w0, mod - 2) % mod;
ans[i] %= mod;
if (ans[i] < 0) ans[i] += mod;
cout << ans[i] << endl;
}
}
| ### 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;
long long w0, w1;
long long a[200005], b[200005];
long long dp[3005][3005];
long long fpow(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
long long ans[200005];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; ++i) cin >> a[i];
for (long long i = 1; i <= n; ++i) cin >> b[i];
for (long long i = 1; i <= n; ++i)
if (a[i])
w1 += b[i];
else
w0 += b[i];
dp[0][0] = 1;
for (long long i = 0; i <= m; ++i) {
for (long long j = 0; j <= m; ++j) {
dp[i][j] += (w1 + i - 1) * fpow(w0 + w1 + i - 1 - j, mod - 2) % mod *
dp[i - 1][j] % mod;
dp[i][j] += (w0 - j + 1) * fpow(w0 + w1 + i - j + 1, mod - 2) % mod *
dp[i][j - 1] % mod;
while (dp[i][j] > mod) dp[i][j] -= mod;
}
}
long long a0 = 0, a1 = 0;
for (long long i = 0; i <= m; ++i) {
a0 += (m - i) * dp[i][m - i] % mod;
a1 += i * dp[i][m - i] % mod;
if (a0 > mod) a0 -= mod;
if (a1 > mod) a1 -= mod;
}
for (long long i = 1; i <= n; ++i) {
if (a[i])
ans[i] = b[i] + a1 * b[i] % mod * fpow(w1, mod - 2) % mod;
else
ans[i] = b[i] - a0 * b[i] % mod * fpow(w0, mod - 2) % mod;
ans[i] %= mod;
if (ans[i] < 0) ans[i] += mod;
cout << ans[i] << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N, M;
int a[55], w[55];
long long qpow(long long x, int n) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % int(998244353);
x = x * x % int(998244353);
n /= 2;
}
return res;
}
long long inv(long long x) { return qpow(x, int(998244353) - 2); }
long long s[2];
long long memo[55][55][55];
long long dfs(int id, int i, int j, int k) {
if (memo[i][j][k] != -1) return memo[i][j][k];
long long w1;
if (a[id] == 0) w1 = w[id] - j;
if (a[id] == 1) w1 = w[id] + j;
if (i == M + 1) return memo[i][j][k] = w1;
long long p = w1 * inv(s[0] + s[1] + (i - 1) - 2 * k) % int(998244353);
long long ans = 0;
if (p > 0)
ans =
p * dfs(id, i + 1, j + 1, k + ((a[id] == 0) ? 1 : 0)) % int(998244353);
if (p != 1) {
long long p0 = (s[0] - k - ((a[id] == 0) ? w1 : 0)) *
inv(s[0] + s[1] + (i - 1) - 2 * k) % int(998244353);
long long p1 = (1 - p - p0 + 2 * int(998244353)) % int(998244353);
ans =
(ans + p0 * dfs(id, i + 1, j, k + 1) % int(998244353)) % int(998244353);
ans = (ans + p1 * dfs(id, i + 1, j, k) % int(998244353)) % int(998244353);
}
return memo[i][j][k] = ans;
}
int main() {
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = 1; i <= N; i++) {
cin >> w[i];
s[a[i]] += w[i];
}
for (int i = 1; i <= N; i++) {
memset(memo, -1, sizeof(memo));
cout << dfs(i, 1, 0, 0) << endl;
}
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;
int N, M;
int a[55], w[55];
long long qpow(long long x, int n) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % int(998244353);
x = x * x % int(998244353);
n /= 2;
}
return res;
}
long long inv(long long x) { return qpow(x, int(998244353) - 2); }
long long s[2];
long long memo[55][55][55];
long long dfs(int id, int i, int j, int k) {
if (memo[i][j][k] != -1) return memo[i][j][k];
long long w1;
if (a[id] == 0) w1 = w[id] - j;
if (a[id] == 1) w1 = w[id] + j;
if (i == M + 1) return memo[i][j][k] = w1;
long long p = w1 * inv(s[0] + s[1] + (i - 1) - 2 * k) % int(998244353);
long long ans = 0;
if (p > 0)
ans =
p * dfs(id, i + 1, j + 1, k + ((a[id] == 0) ? 1 : 0)) % int(998244353);
if (p != 1) {
long long p0 = (s[0] - k - ((a[id] == 0) ? w1 : 0)) *
inv(s[0] + s[1] + (i - 1) - 2 * k) % int(998244353);
long long p1 = (1 - p - p0 + 2 * int(998244353)) % int(998244353);
ans =
(ans + p0 * dfs(id, i + 1, j, k + 1) % int(998244353)) % int(998244353);
ans = (ans + p1 * dfs(id, i + 1, j, k) % int(998244353)) % int(998244353);
}
return memo[i][j][k] = ans;
}
int main() {
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = 1; i <= N; i++) {
cin >> w[i];
s[a[i]] += w[i];
}
for (int i = 1; i <= N; i++) {
memset(memo, -1, sizeof(memo));
cout << dfs(i, 1, 0, 0) << endl;
}
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);
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 *
fast(sum + tmp * (2 * j - i - 1), 998244353 - 2) %
998244353) %
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 *
fast(sum + tmp * (2 * j - i - 1), 998244353 - 2) %
998244353) %
998244353;
}
if (i == m)
ans =
(ans + (w[q] + tmp * k) % 998244353 * dp[i][j][k] % 998244353) %
998244353;
}
}
}
cout << ans << '\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;
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);
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 *
fast(sum + tmp * (2 * j - i - 1), 998244353 - 2) %
998244353) %
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 *
fast(sum + tmp * (2 * j - i - 1), 998244353 - 2) %
998244353) %
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;
const int MOD = 998244353;
template <typename T>
struct modular {
constexpr modular() : val(0) {}
constexpr modular(const modular<T>& _m) : val(_m.val) {}
template <typename U>
constexpr modular(const U& _r = U()) {
val = -MOD <= _r && _r < MOD ? _r : _r % MOD;
if (val < 0) {
val += MOD;
}
}
const T operator()() { return val; }
template <typename U>
explicit operator U() const {
return static_cast<U>(val);
}
modular<T>& operator+=(const modular<T>& _m) {
if ((val += _m.val) >= MOD) {
val -= MOD;
}
return *this;
}
modular<T>& operator-=(const modular<T>& _m) {
if ((val -= _m.val) < 0) {
val += MOD;
}
return *this;
}
modular<T>& operator*=(const modular<T>& _m) {
val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val))
.val;
return *this;
}
modular<T>& operator/=(const modular<T>& _m) {
T a = _m.val, b = MOD, u = 0, v = 1;
while (a != 0) {
T q = b / a;
b -= q * a;
swap(a, b);
u -= q * v;
swap(u, v);
}
return *this *= u;
}
modular<T>& operator=(const modular<T>& _m) {
val = _m.val;
return *this;
}
template <typename U>
modular<T>& operator+=(const U& _r) {
return *this += modular<T>(_r);
}
template <typename U>
modular<T>& operator-=(const U& _r) {
return *this -= modular<T>(_r);
}
template <typename U>
modular<T>& operator*=(const U& _r) {
return *this *= modular<T>(_r);
}
template <typename U>
modular<T>& operator/=(const U& _r) {
return *this /= modular<T>(_r);
}
template <typename U>
modular<T>& operator=(const U& _r) {
val = modular<T>(_r).val;
return *this;
}
modular<T> operator-() { return modular<T>(-val); }
template <typename U>
friend bool operator==(const modular<U>&, const modular<U>&);
friend std::istream& operator>>(std::istream& os, modular<T>& _m) {
os >> _m.val;
_m *= 1;
return os;
}
friend std::ostream& operator<<(std::ostream& os, const modular<T>& _m) {
return os << _m.val;
}
template <typename U>
modular<T> exp(U e) {
modular<T> res = 1;
modular<T> b = val;
if (e < 0) {
b = 1 / b;
e *= -1;
}
for (; e; e >>= 1) {
if (e & 1) {
res *= b;
}
b *= b;
}
return res;
}
private:
T val;
};
template <typename T>
inline modular<T> operator+(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) += _rhs;
}
template <typename T>
inline modular<T> operator-(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) -= _rhs;
}
template <typename T>
inline modular<T> operator*(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) *= _rhs;
}
template <typename T>
inline modular<T> operator/(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) /= _rhs;
}
template <typename T>
inline bool operator==(const modular<T>& _lhs, const modular<T>& _rhs) {
return _lhs.val == _rhs.val;
}
template <typename T, typename U>
inline bool operator==(const modular<T>& _lhs, const U& _rhs) {
return _lhs == modular<T>(_rhs);
}
template <typename T, typename U>
inline bool operator==(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) == _rhs;
}
template <typename T>
inline bool operator!=(const modular<T>& _lhs, const modular<T>& _rhs) {
return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const modular<T>& _lhs, const U& _rhs) {
return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const U& _lhs, const modular<T>& _rhs) {
return !(_lhs == _rhs);
}
const int N = 3003;
modular<int> dp[N][N];
void solve() {
memset(dp, 0, sizeof dp);
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<int> w(n);
for (auto& x : a) {
cin >> x;
}
int H = 0, T = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
w[i] = x;
if (a[i])
H += x;
else
T += x;
}
int tot = H + T;
dp[0][0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
if (i - j <= T) {
dp[i + 1][j] += dp[i][j] * (T - (i - j)) / (tot + j - (i - j));
dp[i + 1][j + 1] += dp[i][j] * (H + j) / (tot + j - (i - j));
}
}
}
modular<int> ad = 0;
modular<int> mi = 0;
for (int j = 0; j <= m; j++) {
ad += dp[m][j] * j;
mi += dp[m][j] * (m - j);
}
vector<modular<int> > res(n);
for (int i = 0; i < n; i++) {
if (a[i]) {
res[i] = w[i] + ad * w[i] / H;
} else {
res[i] = w[i] - mi * w[i] / T;
}
}
for (auto& x : res) {
cout << x << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
cout << endl;
}
| ### 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 MOD = 998244353;
template <typename T>
struct modular {
constexpr modular() : val(0) {}
constexpr modular(const modular<T>& _m) : val(_m.val) {}
template <typename U>
constexpr modular(const U& _r = U()) {
val = -MOD <= _r && _r < MOD ? _r : _r % MOD;
if (val < 0) {
val += MOD;
}
}
const T operator()() { return val; }
template <typename U>
explicit operator U() const {
return static_cast<U>(val);
}
modular<T>& operator+=(const modular<T>& _m) {
if ((val += _m.val) >= MOD) {
val -= MOD;
}
return *this;
}
modular<T>& operator-=(const modular<T>& _m) {
if ((val -= _m.val) < 0) {
val += MOD;
}
return *this;
}
modular<T>& operator*=(const modular<T>& _m) {
val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val))
.val;
return *this;
}
modular<T>& operator/=(const modular<T>& _m) {
T a = _m.val, b = MOD, u = 0, v = 1;
while (a != 0) {
T q = b / a;
b -= q * a;
swap(a, b);
u -= q * v;
swap(u, v);
}
return *this *= u;
}
modular<T>& operator=(const modular<T>& _m) {
val = _m.val;
return *this;
}
template <typename U>
modular<T>& operator+=(const U& _r) {
return *this += modular<T>(_r);
}
template <typename U>
modular<T>& operator-=(const U& _r) {
return *this -= modular<T>(_r);
}
template <typename U>
modular<T>& operator*=(const U& _r) {
return *this *= modular<T>(_r);
}
template <typename U>
modular<T>& operator/=(const U& _r) {
return *this /= modular<T>(_r);
}
template <typename U>
modular<T>& operator=(const U& _r) {
val = modular<T>(_r).val;
return *this;
}
modular<T> operator-() { return modular<T>(-val); }
template <typename U>
friend bool operator==(const modular<U>&, const modular<U>&);
friend std::istream& operator>>(std::istream& os, modular<T>& _m) {
os >> _m.val;
_m *= 1;
return os;
}
friend std::ostream& operator<<(std::ostream& os, const modular<T>& _m) {
return os << _m.val;
}
template <typename U>
modular<T> exp(U e) {
modular<T> res = 1;
modular<T> b = val;
if (e < 0) {
b = 1 / b;
e *= -1;
}
for (; e; e >>= 1) {
if (e & 1) {
res *= b;
}
b *= b;
}
return res;
}
private:
T val;
};
template <typename T>
inline modular<T> operator+(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) += _rhs;
}
template <typename T>
inline modular<T> operator-(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) -= _rhs;
}
template <typename T>
inline modular<T> operator*(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) *= _rhs;
}
template <typename T>
inline modular<T> operator/(const modular<T>& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const modular<T>& _lhs, const U& _rhs) {
return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) /= _rhs;
}
template <typename T>
inline bool operator==(const modular<T>& _lhs, const modular<T>& _rhs) {
return _lhs.val == _rhs.val;
}
template <typename T, typename U>
inline bool operator==(const modular<T>& _lhs, const U& _rhs) {
return _lhs == modular<T>(_rhs);
}
template <typename T, typename U>
inline bool operator==(const U& _lhs, const modular<T>& _rhs) {
return modular<T>(_lhs) == _rhs;
}
template <typename T>
inline bool operator!=(const modular<T>& _lhs, const modular<T>& _rhs) {
return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const modular<T>& _lhs, const U& _rhs) {
return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const U& _lhs, const modular<T>& _rhs) {
return !(_lhs == _rhs);
}
const int N = 3003;
modular<int> dp[N][N];
void solve() {
memset(dp, 0, sizeof dp);
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<int> w(n);
for (auto& x : a) {
cin >> x;
}
int H = 0, T = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
w[i] = x;
if (a[i])
H += x;
else
T += x;
}
int tot = H + T;
dp[0][0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
if (i - j <= T) {
dp[i + 1][j] += dp[i][j] * (T - (i - j)) / (tot + j - (i - j));
dp[i + 1][j + 1] += dp[i][j] * (H + j) / (tot + j - (i - j));
}
}
}
modular<int> ad = 0;
modular<int> mi = 0;
for (int j = 0; j <= m; j++) {
ad += dp[m][j] * j;
mi += dp[m][j] * (m - j);
}
vector<modular<int> > res(n);
for (int i = 0; i < n; i++) {
if (a[i]) {
res[i] = w[i] + ad * w[i] / H;
} else {
res[i] = w[i] - mi * w[i] / T;
}
}
for (auto& x : res) {
cout << x << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
cout << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int add(int _a, int _b) {
_a = (_a + 998244353) % 998244353;
_b = (_b + 998244353) % 998244353;
return (_a + _b) % 998244353;
}
int mul(int _a, int _b) {
_a = (_a + 998244353) % 998244353;
_b = (_b + 998244353) % 998244353;
return ((long long int)((long long int)_a * (long long int)_b)) % 998244353;
}
int bigMod(int v, int p) {
if (p == 0) {
return 1;
}
int ret = bigMod(v, p / 2) % 998244353;
if (p % 2 == 0) {
return mul(ret, ret);
} else {
return mul(ret, mul(ret, v));
}
}
int n, m, ara[2 * 100010], bra[2 * 100010], mxa, mxb, suma, sumb, sum,
inv[50 * 2 * 100010], f[2][52][52][102], g[2][52][52][102];
int frqa[52], frqb[52], ans[52];
void input() {
int i, j;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%d", &ara[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &bra[i]);
}
suma = sumb = sum = 0, mxa = mxb = -1;
for (i = 0; i < n; i++) {
if (ara[i] == 1) {
suma += bra[i];
mxa = max(mxa, bra[i]);
frqa[bra[i]]++;
} else {
sumb += bra[i], mxb = max(mxb, bra[i]);
frqb[bra[i]]++;
}
sum += bra[i];
}
}
void pre() {
int i, j;
for (i = 1; i <= sum + m; i++) {
inv[i] = bigMod(i, 998244353 - 2);
}
}
void solve() {
pre();
int i, j, ret = 0, v, k, l;
for (i = 0; i <= m; i++) {
if (i == 0) {
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxa + j; l >= 0; l--) {
f[i % 2][j][k][l] = l;
}
}
}
continue;
}
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxa + j; l >= 0; l--) {
ret = 0;
v = mul(mul(l, inv[suma + j + sumb - k]),
f[(i - 1) % 2][j + 1][k][l + 1]);
ret = add(ret, v);
v = mul(mul(suma + j - l, inv[suma + j + sumb - k]),
f[(i - 1) % 2][j + 1][k][l]);
ret = add(ret, v);
v = mul(mul(sumb - k, inv[suma + j + sumb - k]),
f[(i - 1) % 2][j][k + 1][l]);
ret = add(ret, v);
f[i % 2][j][k][l] = ret;
}
}
}
}
for (i = 0; i <= m; i++) {
if (i == 0) {
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxb; l >= 0; l--) {
g[i % 2][j][k][l] = l;
}
}
}
continue;
}
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxb; l >= 0; l--) {
ret = 0;
if (l - 1 >= 0) {
v = mul(mul(l, inv[suma + j + sumb - k]),
g[(i - 1) % 2][j][k + 1][l - 1]);
} else {
v = 0;
}
ret = add(ret, v);
v = mul(mul(sumb - k - l, inv[suma + j + sumb - k]),
g[(i - 1) % 2][j][k + 1][l]);
ret = add(ret, v);
v = mul(mul(suma + j, inv[suma + j + sumb - k]),
g[(i - 1) % 2][j + 1][k][l]);
ret = add(ret, v);
g[i % 2][j][k][l] = ret;
}
}
}
}
for (i = 0; i < n; i++) {
if (ara[i] == 1) {
printf("%d", f[m % 2][0][0][bra[i]]);
puts("");
} else {
printf("%d", g[m % 2][0][0][bra[i]]);
puts("");
}
}
}
int main() {
input();
solve();
}
| ### 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;
int add(int _a, int _b) {
_a = (_a + 998244353) % 998244353;
_b = (_b + 998244353) % 998244353;
return (_a + _b) % 998244353;
}
int mul(int _a, int _b) {
_a = (_a + 998244353) % 998244353;
_b = (_b + 998244353) % 998244353;
return ((long long int)((long long int)_a * (long long int)_b)) % 998244353;
}
int bigMod(int v, int p) {
if (p == 0) {
return 1;
}
int ret = bigMod(v, p / 2) % 998244353;
if (p % 2 == 0) {
return mul(ret, ret);
} else {
return mul(ret, mul(ret, v));
}
}
int n, m, ara[2 * 100010], bra[2 * 100010], mxa, mxb, suma, sumb, sum,
inv[50 * 2 * 100010], f[2][52][52][102], g[2][52][52][102];
int frqa[52], frqb[52], ans[52];
void input() {
int i, j;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%d", &ara[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &bra[i]);
}
suma = sumb = sum = 0, mxa = mxb = -1;
for (i = 0; i < n; i++) {
if (ara[i] == 1) {
suma += bra[i];
mxa = max(mxa, bra[i]);
frqa[bra[i]]++;
} else {
sumb += bra[i], mxb = max(mxb, bra[i]);
frqb[bra[i]]++;
}
sum += bra[i];
}
}
void pre() {
int i, j;
for (i = 1; i <= sum + m; i++) {
inv[i] = bigMod(i, 998244353 - 2);
}
}
void solve() {
pre();
int i, j, ret = 0, v, k, l;
for (i = 0; i <= m; i++) {
if (i == 0) {
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxa + j; l >= 0; l--) {
f[i % 2][j][k][l] = l;
}
}
}
continue;
}
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxa + j; l >= 0; l--) {
ret = 0;
v = mul(mul(l, inv[suma + j + sumb - k]),
f[(i - 1) % 2][j + 1][k][l + 1]);
ret = add(ret, v);
v = mul(mul(suma + j - l, inv[suma + j + sumb - k]),
f[(i - 1) % 2][j + 1][k][l]);
ret = add(ret, v);
v = mul(mul(sumb - k, inv[suma + j + sumb - k]),
f[(i - 1) % 2][j][k + 1][l]);
ret = add(ret, v);
f[i % 2][j][k][l] = ret;
}
}
}
}
for (i = 0; i <= m; i++) {
if (i == 0) {
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxb; l >= 0; l--) {
g[i % 2][j][k][l] = l;
}
}
}
continue;
}
for (j = m - i; j >= 0; j--) {
for (k = min(m - j - i, sumb); k >= 0; k--) {
for (l = mxb; l >= 0; l--) {
ret = 0;
if (l - 1 >= 0) {
v = mul(mul(l, inv[suma + j + sumb - k]),
g[(i - 1) % 2][j][k + 1][l - 1]);
} else {
v = 0;
}
ret = add(ret, v);
v = mul(mul(sumb - k - l, inv[suma + j + sumb - k]),
g[(i - 1) % 2][j][k + 1][l]);
ret = add(ret, v);
v = mul(mul(suma + j, inv[suma + j + sumb - k]),
g[(i - 1) % 2][j + 1][k][l]);
ret = add(ret, v);
g[i % 2][j][k][l] = ret;
}
}
}
}
for (i = 0; i < n; i++) {
if (ara[i] == 1) {
printf("%d", f[m % 2][0][0][bra[i]]);
puts("");
} else {
printf("%d", g[m % 2][0][0][bra[i]]);
puts("");
}
}
}
int main() {
input();
solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 998244353;
int n, m;
int a[200009];
long long w[200009];
long long sump = 0;
long long sumn = 0;
long long dp1[3009][3009];
long long dp2[3009][3009];
long long getinv(long long x) {
x %= MOD;
long long kit = MOD - 2;
long long res = 1;
while (kit > 0) {
if (kit % 2) res = (res * x) % MOD;
kit /= 2;
x = (x * x) % MOD;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.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];
if (a[i] == 0)
sumn += w[i];
else
sump += w[i];
}
for (int h = m; h >= 0; h--) {
for (int i = 0; i <= h; i++) {
int j = h - i;
if (h == m) {
dp1[i][j] = 1;
dp2[i][j] = 1;
continue;
}
dp1[i][j] = (dp1[i + 1][j] * (sump + i + 1)) % MOD;
dp1[i][j] = (dp1[i][j] * getinv(sump + sumn + i - j + MOD)) % MOD;
long long ppls = (dp1[i][j + 1] * (sumn - j + MOD)) % MOD;
ppls = (ppls * getinv(sump + sumn + i - j + MOD)) % MOD;
dp1[i][j] = (dp1[i][j] + ppls) % MOD;
dp2[i][j] = (dp2[i + 1][j] * (sump + i)) % MOD;
dp2[i][j] = (dp2[i][j] * getinv(sump + sumn + i - j + MOD)) % MOD;
long long npls = (dp2[i][j + 1] * (sumn - j - 1 + MOD)) % MOD;
npls = (npls * getinv(sump + sumn + i - j + MOD)) % MOD;
dp2[i][j] = (dp2[i][j] + npls) % MOD;
}
}
for (int i = 1; i <= n; i++) {
if (a[i] == 0)
cout << (w[i] * dp2[0][0]) % MOD << '\n';
else
cout << (w[i] * dp1[0][0]) % MOD << '\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 MOD = 998244353;
int n, m;
int a[200009];
long long w[200009];
long long sump = 0;
long long sumn = 0;
long long dp1[3009][3009];
long long dp2[3009][3009];
long long getinv(long long x) {
x %= MOD;
long long kit = MOD - 2;
long long res = 1;
while (kit > 0) {
if (kit % 2) res = (res * x) % MOD;
kit /= 2;
x = (x * x) % MOD;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.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];
if (a[i] == 0)
sumn += w[i];
else
sump += w[i];
}
for (int h = m; h >= 0; h--) {
for (int i = 0; i <= h; i++) {
int j = h - i;
if (h == m) {
dp1[i][j] = 1;
dp2[i][j] = 1;
continue;
}
dp1[i][j] = (dp1[i + 1][j] * (sump + i + 1)) % MOD;
dp1[i][j] = (dp1[i][j] * getinv(sump + sumn + i - j + MOD)) % MOD;
long long ppls = (dp1[i][j + 1] * (sumn - j + MOD)) % MOD;
ppls = (ppls * getinv(sump + sumn + i - j + MOD)) % MOD;
dp1[i][j] = (dp1[i][j] + ppls) % MOD;
dp2[i][j] = (dp2[i + 1][j] * (sump + i)) % MOD;
dp2[i][j] = (dp2[i][j] * getinv(sump + sumn + i - j + MOD)) % MOD;
long long npls = (dp2[i][j + 1] * (sumn - j - 1 + MOD)) % MOD;
npls = (npls * getinv(sump + sumn + i - j + MOD)) % MOD;
dp2[i][j] = (dp2[i][j] + npls) % MOD;
}
}
for (int i = 1; i <= n; i++) {
if (a[i] == 0)
cout << (w[i] * dp2[0][0]) % MOD << '\n';
else
cout << (w[i] * dp1[0][0]) % MOD << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 59;
const long long mod = 998244353;
int add[maxn];
long long w[maxn];
long long dp[52][52][52];
long long val[52][52][52];
bool vis[52][52][52];
long long Pow(long long a, long long b) {
long long res = 1;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
inline long long inv(long long a) { return Pow(a, mod - 2); }
int flag;
long long dfs(int step, long long p, int i, int j, int k, long long x,
long long y, long long z) {
if (step == 0) 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(step - 1, p * x % mod * inv(x + y + z) % mod, i + 1, j, k,
max(0ll, x + flag), y, z);
val[i][j][k] += dfs(step - 1, p * y % mod * inv(x + y + z) % mod, i, j + 1, k,
x, y + 1, z);
val[i][j][k] += dfs(step - 1, p * z % mod * inv(x + y + z) % mod, i, j, k + 1,
x, y, max(0ll, z - 1));
val[i][j][k] %= mod;
vis[i][j][k] = 1;
return val[i][j][k];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
long long ori, s0 = 0, s1 = 0;
for (int i = 1; i <= n; i++) scanf("%d", add + i);
for (int i = 1; i <= n; i++) {
scanf("%lld", w + i);
if (add[i])
s1 += w[i];
else
s0 += w[i];
}
ori = s0 + s1;
int x, y, z, step;
for (int i = 1; i <= n; i++) {
memset(dp, 0, sizeof dp);
memset(val, 0, sizeof val);
memset(vis, 0, sizeof vis);
if (add[i])
flag = 1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1 - w[i], s0);
else
flag = -1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1, s0 - w[i]);
printf("%lld\n", val[0][0][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 = 59;
const long long mod = 998244353;
int add[maxn];
long long w[maxn];
long long dp[52][52][52];
long long val[52][52][52];
bool vis[52][52][52];
long long Pow(long long a, long long b) {
long long res = 1;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
inline long long inv(long long a) { return Pow(a, mod - 2); }
int flag;
long long dfs(int step, long long p, int i, int j, int k, long long x,
long long y, long long z) {
if (step == 0) 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(step - 1, p * x % mod * inv(x + y + z) % mod, i + 1, j, k,
max(0ll, x + flag), y, z);
val[i][j][k] += dfs(step - 1, p * y % mod * inv(x + y + z) % mod, i, j + 1, k,
x, y + 1, z);
val[i][j][k] += dfs(step - 1, p * z % mod * inv(x + y + z) % mod, i, j, k + 1,
x, y, max(0ll, z - 1));
val[i][j][k] %= mod;
vis[i][j][k] = 1;
return val[i][j][k];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
long long ori, s0 = 0, s1 = 0;
for (int i = 1; i <= n; i++) scanf("%d", add + i);
for (int i = 1; i <= n; i++) {
scanf("%lld", w + i);
if (add[i])
s1 += w[i];
else
s0 += w[i];
}
ori = s0 + s1;
int x, y, z, step;
for (int i = 1; i <= n; i++) {
memset(dp, 0, sizeof dp);
memset(val, 0, sizeof val);
memset(vis, 0, sizeof vis);
if (add[i])
flag = 1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1 - w[i], s0);
else
flag = -1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1, s0 - w[i]);
printf("%lld\n", val[0][0][0]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, flag = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') flag = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * flag;
}
const int MOD = 998244353;
const int MAXN = 305;
long long ksm(long long x, int k) {
long long res = 1;
for (; k; k >>= 1, x = x * x % MOD)
if (k & 1) res = res * x % MOD;
return res;
}
int N, M;
bool A[MAXN];
int W[MAXN];
int w1, w2, sum;
long long F[3005][3005];
long long G[3005][3005];
void prepare() {
F[0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= i; j++) {
if (j && w2 >= i - j)
F[i][j] = F[i - 1][j - 1] * (w1 + j - 1) % MOD *
ksm(sum + (j - 1) - (i - 1 - (j - 1)), MOD - 2) % MOD;
if (w2 >= i - j - 1)
F[i][j] += F[i - 1][j] * (w2 - (i - j - 1)) % MOD *
ksm(sum + j - (i - 1 - j), MOD - 2) % MOD;
F[i][j] %= MOD;
}
}
}
void solve(int id) {
for (int i = 0; i <= 100; i++)
for (int j = 0; j <= 100; j++) G[i][j] = 0;
G[0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= i; j++) {
if (A[id]) {
G[i][j] = G[i - 1][j] * (w1 + i - 1 - (W[id] + j)) % MOD *
ksm(w1 + i - 1, MOD - 2) % MOD;
if (j)
G[i][j] += G[i - 1][j - 1] * (W[id] + j - 1) % MOD *
ksm(w1 + i - 1, MOD - 2) % MOD;
G[i][j] %= MOD;
} else {
if (W[id] >= j)
G[i][j] = G[i - 1][j] * (w2 - (i - 1) - (W[id] - j)) % MOD *
ksm(w2 - (i - 1), MOD - 2) % MOD;
if (W[id] >= j)
G[i][j] += G[i - 1][j - 1] * (W[id] - (j - 1)) % MOD *
ksm(w2 - (i - 1), MOD - 2) % MOD;
G[i][j] %= MOD;
}
}
}
long long Ans = 0;
for (int i = 0; i <= M; i++) {
for (int j = 0; j <= i; j++) {
if (A[id])
Ans += F[M][i] * G[i][j] % MOD * (W[id] + j) % MOD;
else
Ans += F[M][M - i] * G[i][j] % MOD * (W[id] - j) % MOD;
Ans %= MOD;
}
}
printf("%lld\n", Ans);
}
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(), sum += W[i];
if (A[i])
w1 += W[i];
else
w2 += W[i];
}
prepare();
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, flag = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') flag = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * flag;
}
const int MOD = 998244353;
const int MAXN = 305;
long long ksm(long long x, int k) {
long long res = 1;
for (; k; k >>= 1, x = x * x % MOD)
if (k & 1) res = res * x % MOD;
return res;
}
int N, M;
bool A[MAXN];
int W[MAXN];
int w1, w2, sum;
long long F[3005][3005];
long long G[3005][3005];
void prepare() {
F[0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= i; j++) {
if (j && w2 >= i - j)
F[i][j] = F[i - 1][j - 1] * (w1 + j - 1) % MOD *
ksm(sum + (j - 1) - (i - 1 - (j - 1)), MOD - 2) % MOD;
if (w2 >= i - j - 1)
F[i][j] += F[i - 1][j] * (w2 - (i - j - 1)) % MOD *
ksm(sum + j - (i - 1 - j), MOD - 2) % MOD;
F[i][j] %= MOD;
}
}
}
void solve(int id) {
for (int i = 0; i <= 100; i++)
for (int j = 0; j <= 100; j++) G[i][j] = 0;
G[0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= i; j++) {
if (A[id]) {
G[i][j] = G[i - 1][j] * (w1 + i - 1 - (W[id] + j)) % MOD *
ksm(w1 + i - 1, MOD - 2) % MOD;
if (j)
G[i][j] += G[i - 1][j - 1] * (W[id] + j - 1) % MOD *
ksm(w1 + i - 1, MOD - 2) % MOD;
G[i][j] %= MOD;
} else {
if (W[id] >= j)
G[i][j] = G[i - 1][j] * (w2 - (i - 1) - (W[id] - j)) % MOD *
ksm(w2 - (i - 1), MOD - 2) % MOD;
if (W[id] >= j)
G[i][j] += G[i - 1][j - 1] * (W[id] - (j - 1)) % MOD *
ksm(w2 - (i - 1), MOD - 2) % MOD;
G[i][j] %= MOD;
}
}
}
long long Ans = 0;
for (int i = 0; i <= M; i++) {
for (int j = 0; j <= i; j++) {
if (A[id])
Ans += F[M][i] * G[i][j] % MOD * (W[id] + j) % MOD;
else
Ans += F[M][M - i] * G[i][j] % MOD * (W[id] - j) % MOD;
Ans %= MOD;
}
}
printf("%lld\n", Ans);
}
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(), sum += W[i];
if (A[i])
w1 += W[i];
else
w2 += W[i];
}
prepare();
for (int i = 1; i <= N; i++) solve(i);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long dp[1010000];
long long mpow(long long x, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans = ans * x % 998244353;
x = x * x % 998244353;
n >>= 1;
}
return ans;
}
long long inv(long long x) { return mpow(x, 998244353 - 2); }
int n, m;
long long a[1010000];
int b[1010000];
long long sum[2];
long long ans[2];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[b[i]] += a[i];
}
dp[0] = 1;
for (int i = 0; i < m; i++)
for (int j = i; j >= 0; j--) {
if (i - j <= sum[0]) {
long long sum0 = sum[0] - (i - j);
long long sum1 = sum[1] + j;
dp[j + 1] += dp[j] * sum1 % 998244353 * inv(sum0 + sum1) % 998244353;
dp[j] = dp[j] * sum0 % 998244353 * inv(sum0 + sum1) % 998244353;
}
}
for (int i = 0; i <= m; i++) {
ans[0] += (sum[0] - (m - i)) * dp[i] % 998244353;
ans[1] += (sum[1] + i) * dp[i] % 998244353;
}
for (int i = 1; i <= n; i++)
cout << (ans[b[i]] * a[i] % 998244353 * inv(sum[b[i]]) % 998244353 +
998244353) %
998244353
<< endl;
return ~~(0 ^ 0 ^ 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;
long long dp[1010000];
long long mpow(long long x, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans = ans * x % 998244353;
x = x * x % 998244353;
n >>= 1;
}
return ans;
}
long long inv(long long x) { return mpow(x, 998244353 - 2); }
int n, m;
long long a[1010000];
int b[1010000];
long long sum[2];
long long ans[2];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[b[i]] += a[i];
}
dp[0] = 1;
for (int i = 0; i < m; i++)
for (int j = i; j >= 0; j--) {
if (i - j <= sum[0]) {
long long sum0 = sum[0] - (i - j);
long long sum1 = sum[1] + j;
dp[j + 1] += dp[j] * sum1 % 998244353 * inv(sum0 + sum1) % 998244353;
dp[j] = dp[j] * sum0 % 998244353 * inv(sum0 + sum1) % 998244353;
}
}
for (int i = 0; i <= m; i++) {
ans[0] += (sum[0] - (m - i)) * dp[i] % 998244353;
ans[1] += (sum[1] + i) * dp[i] % 998244353;
}
for (int i = 1; i <= n; i++)
cout << (ans[b[i]] * a[i] % 998244353 * inv(sum[b[i]]) % 998244353 +
998244353) %
998244353
<< endl;
return ~~(0 ^ 0 ^ 0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50 + 5;
const long long mod = 998244353;
int n, m;
long long qpow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans *= a, ans %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ans;
}
long long inv(long long a) { return qpow(a, mod - 2); }
bool vis[maxn][maxn][maxn];
long long val[maxn][maxn][maxn];
long long dfs(int pos, int i, int j, int k, int x, int y, int z, int p) {
if (!pos) return x;
if (vis[i][j][k]) return val[i][j][k];
long long &d = val[i][j][k];
d += x * inv(x + y + z) % mod *
dfs(pos - 1, i + 1, j, k, max(0, x + p), y, z, p);
d %= mod;
d += y * inv(x + y + z) % mod * dfs(pos - 1, i, j + 1, k, x, y + 1, z, p);
d %= mod;
d += z * inv(x + y + z) % mod *
dfs(pos - 1, i, j, k + 1, x, y, max(0, z - 1), p);
d %= mod;
vis[i][j][k] = 1;
return d;
}
int a[maxn], b[maxn];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
if (a[i])
sum1 += b[i];
else
sum2 += b[i];
}
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
memset(val, 0, sizeof(val));
long long ans = 0;
if (a[i])
ans = dfs(m, 0, 0, 0, b[i], sum1 - b[i], sum2, 1);
else
ans = dfs(m, 0, 0, 0, b[i], sum1, sum2 - b[i], -1);
printf("%lld\n", ans);
}
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 maxn = 50 + 5;
const long long mod = 998244353;
int n, m;
long long qpow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans *= a, ans %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ans;
}
long long inv(long long a) { return qpow(a, mod - 2); }
bool vis[maxn][maxn][maxn];
long long val[maxn][maxn][maxn];
long long dfs(int pos, int i, int j, int k, int x, int y, int z, int p) {
if (!pos) return x;
if (vis[i][j][k]) return val[i][j][k];
long long &d = val[i][j][k];
d += x * inv(x + y + z) % mod *
dfs(pos - 1, i + 1, j, k, max(0, x + p), y, z, p);
d %= mod;
d += y * inv(x + y + z) % mod * dfs(pos - 1, i, j + 1, k, x, y + 1, z, p);
d %= mod;
d += z * inv(x + y + z) % mod *
dfs(pos - 1, i, j, k + 1, x, y, max(0, z - 1), p);
d %= mod;
vis[i][j][k] = 1;
return d;
}
int a[maxn], b[maxn];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
if (a[i])
sum1 += b[i];
else
sum2 += b[i];
}
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
memset(val, 0, sizeof(val));
long long ans = 0;
if (a[i])
ans = dfs(m, 0, 0, 0, b[i], sum1 - b[i], sum2, 1);
else
ans = dfs(m, 0, 0, 0, b[i], sum1, sum2 - b[i], -1);
printf("%lld\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 2e5 + 100;
long long dp[52][52][52];
bool vis[52][52][52];
long long val[52][52][52];
int a[maxn], w[maxn];
long long qmod(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;
}
int flag;
int dfs(int n, int i, int j, int k, long long p, long long v, long long sp,
long long sn) {
if (!n) {
return 1LL * p * v % mod;
}
if (vis[i][j][k]) {
return val[i][j][k] * qmod(dp[i][j][k], mod - 2) % mod * p % mod;
}
dp[i][j][k] = p;
val[i][j][k] +=
dfs(n - 1, i + 1, j, k,
1LL * p * sp % mod * qmod(v + sp + sn, mod - 2) % mod, v, sp + 1, sn);
val[i][j][k] += dfs(n - 1, i, j + 1, k,
1LL * p * sn % mod * qmod(v + sp + sn, mod - 2) % mod, v,
sp, max(sn - 1, 0LL));
val[i][j][k] += dfs(n - 1, i, j, k + 1,
1LL * p * v % mod * qmod(v + sp + sn, mod - 2) % mod,
max(v + flag, 0LL), sp, sn);
val[i][j][k] %= mod;
vis[i][j][k] = 1;
return val[i][j][k];
}
int main() {
ios::sync_with_stdio(false);
long long sn = 0, sp = 0;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> w[i];
if (a[i]) {
sp += w[i];
} else
sn += w[i];
}
for (int i = 0; i < n; ++i) {
memset(dp, 0, sizeof dp);
memset(vis, 0, sizeof vis);
memset(val, 0, sizeof val);
if (a[i]) {
flag = 1;
sp -= w[i];
} else {
flag = -1;
sn -= w[i];
}
cout << dfs(m, 0, 0, 0, 1, w[i], sp, sn) << endl;
if (a[i]) {
sp += w[i];
} else
sn += w[i];
}
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 maxn = 2e5 + 100;
long long dp[52][52][52];
bool vis[52][52][52];
long long val[52][52][52];
int a[maxn], w[maxn];
long long qmod(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;
}
int flag;
int dfs(int n, int i, int j, int k, long long p, long long v, long long sp,
long long sn) {
if (!n) {
return 1LL * p * v % mod;
}
if (vis[i][j][k]) {
return val[i][j][k] * qmod(dp[i][j][k], mod - 2) % mod * p % mod;
}
dp[i][j][k] = p;
val[i][j][k] +=
dfs(n - 1, i + 1, j, k,
1LL * p * sp % mod * qmod(v + sp + sn, mod - 2) % mod, v, sp + 1, sn);
val[i][j][k] += dfs(n - 1, i, j + 1, k,
1LL * p * sn % mod * qmod(v + sp + sn, mod - 2) % mod, v,
sp, max(sn - 1, 0LL));
val[i][j][k] += dfs(n - 1, i, j, k + 1,
1LL * p * v % mod * qmod(v + sp + sn, mod - 2) % mod,
max(v + flag, 0LL), sp, sn);
val[i][j][k] %= mod;
vis[i][j][k] = 1;
return val[i][j][k];
}
int main() {
ios::sync_with_stdio(false);
long long sn = 0, sp = 0;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> w[i];
if (a[i]) {
sp += w[i];
} else
sn += w[i];
}
for (int i = 0; i < n; ++i) {
memset(dp, 0, sizeof dp);
memset(vis, 0, sizeof vis);
memset(val, 0, sizeof val);
if (a[i]) {
flag = 1;
sp -= w[i];
} else {
flag = -1;
sn -= w[i];
}
cout << dfs(m, 0, 0, 0, 1, w[i], sp, sn) << endl;
if (a[i]) {
sp += w[i];
} else
sn += w[i];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const long long MOD = 998244353;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = 1e-8;
int _;
using namespace std;
long long powermod(long long n, long long k, long long MOD) {
long long ans = 1;
while (k) {
if (k & 1) {
ans = ((ans % MOD) * (n % MOD)) % MOD;
}
n = ((n % MOD) * (n % MOD)) % MOD;
k >>= 1;
}
return ans;
}
int aa[105];
long long gg[105];
long long dp_sa[3005][3005];
long long dp_sb[3005][3005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int N, M;
while (cin >> N >> M) {
long long sa = 0;
long long sb = 0;
for (int i = 1; i <= N; ++i) cin >> aa[i];
for (int i = 1; i <= N; ++i) {
cin >> gg[i];
if (aa[i])
sa += gg[i];
else
sb += gg[i];
}
memset(dp_sa, 0, sizeof(dp_sa));
memset(dp_sb, 0, sizeof(dp_sb));
for (int i = M; i >= 0; --i) {
dp_sa[i][M - i] = dp_sb[i][M - i] = 1;
for (int j = min(1LL * M - i - 1, sb); j >= 0; --j) {
dp_sa[i][j] = ((sa + i + 1) % MOD * dp_sa[i + 1][j] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD +
(sb - j) % MOD * dp_sa[i][j + 1] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD) %
MOD;
dp_sb[i][j] = ((sa + i) % MOD * dp_sb[i + 1][j] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD +
(sb - j - 1 + MOD) % MOD * dp_sb[i][j + 1] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD) %
MOD;
}
}
for (int i = 1; i <= N; ++i) {
if (aa[i]) {
cout << gg[i] * dp_sa[0][0] % MOD << endl;
} else {
cout << gg[i] * dp_sb[0][0] % 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 = 200005;
const long long MOD = 998244353;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = 1e-8;
int _;
using namespace std;
long long powermod(long long n, long long k, long long MOD) {
long long ans = 1;
while (k) {
if (k & 1) {
ans = ((ans % MOD) * (n % MOD)) % MOD;
}
n = ((n % MOD) * (n % MOD)) % MOD;
k >>= 1;
}
return ans;
}
int aa[105];
long long gg[105];
long long dp_sa[3005][3005];
long long dp_sb[3005][3005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int N, M;
while (cin >> N >> M) {
long long sa = 0;
long long sb = 0;
for (int i = 1; i <= N; ++i) cin >> aa[i];
for (int i = 1; i <= N; ++i) {
cin >> gg[i];
if (aa[i])
sa += gg[i];
else
sb += gg[i];
}
memset(dp_sa, 0, sizeof(dp_sa));
memset(dp_sb, 0, sizeof(dp_sb));
for (int i = M; i >= 0; --i) {
dp_sa[i][M - i] = dp_sb[i][M - i] = 1;
for (int j = min(1LL * M - i - 1, sb); j >= 0; --j) {
dp_sa[i][j] = ((sa + i + 1) % MOD * dp_sa[i + 1][j] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD +
(sb - j) % MOD * dp_sa[i][j + 1] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD) %
MOD;
dp_sb[i][j] = ((sa + i) % MOD * dp_sb[i + 1][j] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD +
(sb - j - 1 + MOD) % MOD * dp_sb[i][j + 1] % MOD *
powermod(sa + sb + i - j, MOD - 2, MOD) % MOD) %
MOD;
}
}
for (int i = 1; i <= N; ++i) {
if (aa[i]) {
cout << gg[i] * dp_sa[0][0] % MOD << endl;
} else {
cout << gg[i] * dp_sb[0][0] % MOD << endl;
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long Mod = 998244353;
long long qpw(long long a, long long b) {
long long ans = 1;
a = (a % Mod + Mod) % Mod;
while (b) {
if (b & 1) ans = ans * a % Mod;
a = a * a % Mod;
b >>= 1;
}
return ans;
}
long long w[55], a[55], W0, W1;
long long dp[55][55][55];
void add(long long &x, long long y) {
x += y;
if (x >= Mod) x -= Mod;
if (x < 0) x += Mod;
}
long long inv(long long x) { return qpw(x, Mod - 2); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> w[i];
if (a[i])
W1 += w[i];
else
W0 += w[i];
}
for (int t = 0; t < n; ++t) {
memset(dp, 0, sizeof dp);
dp[0][0][0] = 1;
int tmpW0 = W0, tmpW1 = W1;
if (a[t])
tmpW1 -= w[t];
else
tmpW0 -= w[t];
for (int i = 0; i < m; ++i) {
for (int j = 0; j <= i && j <= tmpW0; j++) {
for (int k = 0; k + j <= i; k++) {
if (a[t]) {
if (j + 1 <= tmpW0)
add(dp[i + 1][j + 1][k],
dp[i][j][k] * (tmpW0 - j) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod);
add(dp[i + 1][j][k + 1],
dp[i][j][k] * (tmpW1 + k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod);
add(dp[i + 1][j][k],
dp[i][j][k] * (w[t] + i - j - k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod);
} else if (!a[t] && w[t] >= i - j - k) {
if (j + 1 <= tmpW0)
add(dp[i + 1][j + 1][k],
dp[i][j][k] * (tmpW0 - j) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod);
add(dp[i + 1][j][k + 1],
dp[i][j][k] * (tmpW1 + k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod);
if (i - j - k + 1 <= w[t])
add(dp[i + 1][j][k],
dp[i][j][k] * (w[t] - i + j + k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod);
}
}
}
}
long long ans = 0;
for (int i = 0; i < 55; i++)
for (int j = 0; j < 55 && m - i - j >= 0; ++j) {
if (a[t]) {
add(ans, dp[m][i][j] * (m - i - j + w[t]) % Mod);
} else if (!a[t] && m - i - j <= w[t]) {
add(ans, dp[m][i][j] * (w[t] - m + i + j) % Mod);
}
}
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;
const long long Mod = 998244353;
long long qpw(long long a, long long b) {
long long ans = 1;
a = (a % Mod + Mod) % Mod;
while (b) {
if (b & 1) ans = ans * a % Mod;
a = a * a % Mod;
b >>= 1;
}
return ans;
}
long long w[55], a[55], W0, W1;
long long dp[55][55][55];
void add(long long &x, long long y) {
x += y;
if (x >= Mod) x -= Mod;
if (x < 0) x += Mod;
}
long long inv(long long x) { return qpw(x, Mod - 2); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> w[i];
if (a[i])
W1 += w[i];
else
W0 += w[i];
}
for (int t = 0; t < n; ++t) {
memset(dp, 0, sizeof dp);
dp[0][0][0] = 1;
int tmpW0 = W0, tmpW1 = W1;
if (a[t])
tmpW1 -= w[t];
else
tmpW0 -= w[t];
for (int i = 0; i < m; ++i) {
for (int j = 0; j <= i && j <= tmpW0; j++) {
for (int k = 0; k + j <= i; k++) {
if (a[t]) {
if (j + 1 <= tmpW0)
add(dp[i + 1][j + 1][k],
dp[i][j][k] * (tmpW0 - j) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod);
add(dp[i + 1][j][k + 1],
dp[i][j][k] * (tmpW1 + k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod);
add(dp[i + 1][j][k],
dp[i][j][k] * (w[t] + i - j - k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod);
} else if (!a[t] && w[t] >= i - j - k) {
if (j + 1 <= tmpW0)
add(dp[i + 1][j + 1][k],
dp[i][j][k] * (tmpW0 - j) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod);
add(dp[i + 1][j][k + 1],
dp[i][j][k] * (tmpW1 + k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod);
if (i - j - k + 1 <= w[t])
add(dp[i + 1][j][k],
dp[i][j][k] * (w[t] - i + j + k) % Mod *
inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod);
}
}
}
}
long long ans = 0;
for (int i = 0; i < 55; i++)
for (int j = 0; j < 55 && m - i - j >= 0; ++j) {
if (a[t]) {
add(ans, dp[m][i][j] * (m - i - j + w[t]) % Mod);
} else if (!a[t] && m - i - j <= w[t]) {
add(ans, dp[m][i][j] * (w[t] - m + i + j) % Mod);
}
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 52 + 5;
const int mod = 998244353;
long long qp(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
template <class T>
inline bool scan(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void out(T x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
struct mint {
int n;
mint(int n_ = 0) : n(n_) {}
};
mint operator+(mint a, mint b) { return (a.n += b.n) >= mod ? a.n - mod : a.n; }
mint operator-(mint a, mint b) { return (a.n -= b.n) < 0 ? a.n + mod : a.n; }
mint operator*(mint a, mint b) { return 1LL * a.n * b.n % mod; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }
istream &operator>>(istream &o, mint a) { return o >> a.n; }
int n, m;
mint dp[maxn][maxn][maxn];
int tp[maxn];
mint w[maxn];
mint inv[maxn * maxn];
int main() {
for (int i = 0; i < maxn * maxn; ++i) inv[i] = qp(i, mod - 2);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &tp[i]);
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &w[i].n);
}
int wgood = 0, wbad = 0;
for (int i = 1; i <= n; ++i) {
if (tp[i] == 1)
wgood += w[i].n;
else
wbad += w[i].n;
}
for (int i = 1; i <= n; ++i) {
memset(dp, 0, sizeof dp);
dp[0][0][0] = 1;
for (int step = 0; step <= m; ++step) {
for (int j = 0; j <= step; ++j) {
int k = step - j;
int up = tp[i] ? j : min(w[i].n, k);
mint invk = inv[wgood + wbad - k + j];
for (int hit = 0; hit <= up; ++hit) {
mint tmp = dp[hit][j][k] * invk;
if (tp[i]) {
dp[hit][j + 1][k] += tmp * (wgood + j - (w[i] + hit));
dp[hit][j][k + 1] += tmp * (wbad - k);
dp[hit + 1][j + 1][k] += tmp * (w[i] + hit);
} else {
dp[hit][j + 1][k] += tmp * (wgood + j);
dp[hit][j][k + 1] += tmp * (wbad - k - (w[i] - hit));
dp[hit + 1][j][k + 1] += tmp * (w[i] - hit);
}
}
}
}
mint res = 0;
for (int j = 0; j <= m; ++j) {
int k = m - j;
int up = tp[i] ? j : min(w[i].n, k);
for (int hit = 0; hit <= up; ++hit) {
if (tp[i])
res += hit * dp[hit][j][k];
else
res -= hit * dp[hit][j][k];
}
}
printf("%d\n", (res + w[i]).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;
const int maxn = 52 + 5;
const int mod = 998244353;
long long qp(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
template <class T>
inline bool scan(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void out(T x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
struct mint {
int n;
mint(int n_ = 0) : n(n_) {}
};
mint operator+(mint a, mint b) { return (a.n += b.n) >= mod ? a.n - mod : a.n; }
mint operator-(mint a, mint b) { return (a.n -= b.n) < 0 ? a.n + mod : a.n; }
mint operator*(mint a, mint b) { return 1LL * a.n * b.n % mod; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }
istream &operator>>(istream &o, mint a) { return o >> a.n; }
int n, m;
mint dp[maxn][maxn][maxn];
int tp[maxn];
mint w[maxn];
mint inv[maxn * maxn];
int main() {
for (int i = 0; i < maxn * maxn; ++i) inv[i] = qp(i, mod - 2);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &tp[i]);
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &w[i].n);
}
int wgood = 0, wbad = 0;
for (int i = 1; i <= n; ++i) {
if (tp[i] == 1)
wgood += w[i].n;
else
wbad += w[i].n;
}
for (int i = 1; i <= n; ++i) {
memset(dp, 0, sizeof dp);
dp[0][0][0] = 1;
for (int step = 0; step <= m; ++step) {
for (int j = 0; j <= step; ++j) {
int k = step - j;
int up = tp[i] ? j : min(w[i].n, k);
mint invk = inv[wgood + wbad - k + j];
for (int hit = 0; hit <= up; ++hit) {
mint tmp = dp[hit][j][k] * invk;
if (tp[i]) {
dp[hit][j + 1][k] += tmp * (wgood + j - (w[i] + hit));
dp[hit][j][k + 1] += tmp * (wbad - k);
dp[hit + 1][j + 1][k] += tmp * (w[i] + hit);
} else {
dp[hit][j + 1][k] += tmp * (wgood + j);
dp[hit][j][k + 1] += tmp * (wbad - k - (w[i] - hit));
dp[hit + 1][j][k + 1] += tmp * (w[i] - hit);
}
}
}
}
mint res = 0;
for (int j = 0; j <= m; ++j) {
int k = m - j;
int up = tp[i] ? j : min(w[i].n, k);
for (int hit = 0; hit <= up; ++hit) {
if (tp[i])
res += hit * dp[hit][j][k];
else
res -= hit * dp[hit][j][k];
}
}
printf("%d\n", (res + w[i]).n);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long i, i0, n, m, a[55], w[55], dp[55][55][55][55], dic[3055],
ddic[3055][3055];
long long inv(long long b) {
return b == 1 ? 1 : (mod - mod / b) * inv(mod % b) % mod;
}
int main() {
for (i = 1; i <= 3050; i++) dic[i] = inv(i);
for (i = 1; i <= 3050; i++) {
for (i0 = 1; i0 <= i; i0++) {
ddic[i0][i] = i0 * dic[i] % mod;
}
}
scanf("%lld %lld", &n, &m);
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
long long sum0 = 0, sum1 = 0;
for (i = 1; i <= n; i++) {
scanf("%lld", &w[i]);
if (a[i])
sum1 += w[i];
else
sum0 += w[i];
}
for (i = 1; i <= n; i++) {
if (a[i]) {
memset(dp, 0, sizeof(dp));
dp[0][0][0][0] = 1;
for (i0 = 0; i0 < m; i0++) {
for (int ia = 0; ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ia + ib + ic <= m && ic <= sum0; ic++) {
dp[i0 + 1][ia + 1][ib][ic] +=
dp[i0][ia][ib][ic] *
ddic[w[i] + ia][sum0 + sum1 + ia + ib - ic];
dp[i0 + 1][ia][ib + 1][ic] +=
dp[i0][ia][ib][ic] *
ddic[sum1 + ib - w[i]][sum0 + sum1 + ia + ib - ic];
dp[i0 + 1][ia][ib][ic + 1] +=
dp[i0][ia][ib][ic] *
ddic[sum0 - ic][sum0 + sum1 + ia + ib - ic];
dp[i0 + 1][ia + 1][ib][ic] %= mod;
dp[i0 + 1][ia][ib + 1][ic] %= mod;
dp[i0 + 1][ia][ib][ic + 1] %= mod;
}
}
}
}
long long ans = 0;
for (int ia = 0; ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ic + ia + ib <= m && ic <= sum0; ic++) {
ans += (ia + w[i]) * dp[m][ia][ib][ic];
ans %= mod;
}
}
}
printf("%lld\n", ans);
} else {
memset(dp, 0, sizeof(dp));
dp[0][0][0][0] = 1;
for (i0 = 0; i0 < m; i0++) {
for (int ia = 0; ia <= w[i] && ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ic + ib + ia <= m && ic <= sum0 - w[i]; ic++) {
dp[i0 + 1][ia + 1][ib][ic] +=
dp[i0][ia][ib][ic] *
ddic[w[i] - ia][sum0 + sum1 - ia + ib - ic];
dp[i0 + 1][ia][ib + 1][ic] +=
dp[i0][ia][ib][ic] *
ddic[sum1 + ib][sum0 + sum1 - ia + ib - ic];
dp[i0 + 1][ia][ib][ic + 1] +=
dp[i0][ia][ib][ic] *
ddic[sum0 - ic - w[i]][sum0 + sum1 - ia + ib - ic];
dp[i0 + 1][ia + 1][ib][ic] %= mod;
dp[i0 + 1][ia][ib + 1][ic] %= mod;
dp[i0 + 1][ia][ib][ic + 1] %= mod;
}
}
}
}
long long ans = 0;
for (int ia = 0; ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ia + ib + ic <= m; ic++) {
ans += (w[i] - ia) * dp[m][ia][ib][ic];
ans %= mod;
}
}
}
printf("%lld\n", ans);
}
}
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 long long mod = 998244353;
long long i, i0, n, m, a[55], w[55], dp[55][55][55][55], dic[3055],
ddic[3055][3055];
long long inv(long long b) {
return b == 1 ? 1 : (mod - mod / b) * inv(mod % b) % mod;
}
int main() {
for (i = 1; i <= 3050; i++) dic[i] = inv(i);
for (i = 1; i <= 3050; i++) {
for (i0 = 1; i0 <= i; i0++) {
ddic[i0][i] = i0 * dic[i] % mod;
}
}
scanf("%lld %lld", &n, &m);
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
long long sum0 = 0, sum1 = 0;
for (i = 1; i <= n; i++) {
scanf("%lld", &w[i]);
if (a[i])
sum1 += w[i];
else
sum0 += w[i];
}
for (i = 1; i <= n; i++) {
if (a[i]) {
memset(dp, 0, sizeof(dp));
dp[0][0][0][0] = 1;
for (i0 = 0; i0 < m; i0++) {
for (int ia = 0; ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ia + ib + ic <= m && ic <= sum0; ic++) {
dp[i0 + 1][ia + 1][ib][ic] +=
dp[i0][ia][ib][ic] *
ddic[w[i] + ia][sum0 + sum1 + ia + ib - ic];
dp[i0 + 1][ia][ib + 1][ic] +=
dp[i0][ia][ib][ic] *
ddic[sum1 + ib - w[i]][sum0 + sum1 + ia + ib - ic];
dp[i0 + 1][ia][ib][ic + 1] +=
dp[i0][ia][ib][ic] *
ddic[sum0 - ic][sum0 + sum1 + ia + ib - ic];
dp[i0 + 1][ia + 1][ib][ic] %= mod;
dp[i0 + 1][ia][ib + 1][ic] %= mod;
dp[i0 + 1][ia][ib][ic + 1] %= mod;
}
}
}
}
long long ans = 0;
for (int ia = 0; ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ic + ia + ib <= m && ic <= sum0; ic++) {
ans += (ia + w[i]) * dp[m][ia][ib][ic];
ans %= mod;
}
}
}
printf("%lld\n", ans);
} else {
memset(dp, 0, sizeof(dp));
dp[0][0][0][0] = 1;
for (i0 = 0; i0 < m; i0++) {
for (int ia = 0; ia <= w[i] && ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ic + ib + ia <= m && ic <= sum0 - w[i]; ic++) {
dp[i0 + 1][ia + 1][ib][ic] +=
dp[i0][ia][ib][ic] *
ddic[w[i] - ia][sum0 + sum1 - ia + ib - ic];
dp[i0 + 1][ia][ib + 1][ic] +=
dp[i0][ia][ib][ic] *
ddic[sum1 + ib][sum0 + sum1 - ia + ib - ic];
dp[i0 + 1][ia][ib][ic + 1] +=
dp[i0][ia][ib][ic] *
ddic[sum0 - ic - w[i]][sum0 + sum1 - ia + ib - ic];
dp[i0 + 1][ia + 1][ib][ic] %= mod;
dp[i0 + 1][ia][ib + 1][ic] %= mod;
dp[i0 + 1][ia][ib][ic + 1] %= mod;
}
}
}
}
long long ans = 0;
for (int ia = 0; ia <= m; ia++) {
for (int ib = 0; ib + ia <= m; ib++) {
for (int ic = 0; ia + ib + ic <= m; ic++) {
ans += (w[i] - ia) * dp[m][ia][ib][ic];
ans %= mod;
}
}
}
printf("%lld\n", ans);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 55;
const long long mod = 998244353;
int n, m;
int sum[2];
int a[MAXN], w[MAXN];
int memo[MAXN * 2][MAXN][MAXN][MAXN][2];
long long fastPow(long long x, long long p) {
long long answer = 1, current = x;
while (p != 0) {
if (p % 2 != 0) {
answer = (answer * current) % mod;
}
p /= 2;
current = (current * current) % mod;
}
return answer;
}
long long inv(long long x) { return fastPow(x, mod - 2); }
long long calcState(int curr, int diffGood, int diffBad, int turn, bool type) {
if (curr == 0) return 0;
if (turn == m) return curr;
if (memo[curr][diffGood][diffBad][turn][type] != -1) {
return memo[curr][diffGood][diffBad][turn][type];
}
int sumBad = sum[0] - diffBad;
int sumGood = sum[1] + diffGood;
if (type == false)
sumBad -= curr;
else if (type == true)
sumGood -= curr;
long long answer = 0;
if (sumBad != 0)
answer += (((sumBad * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr, diffGood, diffBad + 1, turn + 1, type)) %
mod;
if (sumGood != 0)
answer += (((sumGood * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr, diffGood + 1, diffBad, turn + 1, type)) %
mod;
if (curr != 0) {
if (type == false) {
answer += (((curr * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr - 1, diffGood, diffBad + 1, turn + 1, type)) %
mod;
} else {
answer += (((curr * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr + 1, diffGood + 1, diffBad, turn + 1, type)) %
mod;
}
}
answer %= mod;
memo[curr][diffGood][diffBad][turn][type] = answer;
return answer;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
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 = 0; i < n; i++) {
sum[a[i]] += w[i];
}
memset(memo, -1, sizeof(memo));
for (int i = 0; i < n; i++) {
cout << calcState(w[i], 0, 0, 0, a[i]) << '\n';
}
}
| ### 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 = 55;
const long long mod = 998244353;
int n, m;
int sum[2];
int a[MAXN], w[MAXN];
int memo[MAXN * 2][MAXN][MAXN][MAXN][2];
long long fastPow(long long x, long long p) {
long long answer = 1, current = x;
while (p != 0) {
if (p % 2 != 0) {
answer = (answer * current) % mod;
}
p /= 2;
current = (current * current) % mod;
}
return answer;
}
long long inv(long long x) { return fastPow(x, mod - 2); }
long long calcState(int curr, int diffGood, int diffBad, int turn, bool type) {
if (curr == 0) return 0;
if (turn == m) return curr;
if (memo[curr][diffGood][diffBad][turn][type] != -1) {
return memo[curr][diffGood][diffBad][turn][type];
}
int sumBad = sum[0] - diffBad;
int sumGood = sum[1] + diffGood;
if (type == false)
sumBad -= curr;
else if (type == true)
sumGood -= curr;
long long answer = 0;
if (sumBad != 0)
answer += (((sumBad * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr, diffGood, diffBad + 1, turn + 1, type)) %
mod;
if (sumGood != 0)
answer += (((sumGood * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr, diffGood + 1, diffBad, turn + 1, type)) %
mod;
if (curr != 0) {
if (type == false) {
answer += (((curr * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr - 1, diffGood, diffBad + 1, turn + 1, type)) %
mod;
} else {
answer += (((curr * inv(sumBad + sumGood + curr)) % mod) *
calcState(curr + 1, diffGood + 1, diffBad, turn + 1, type)) %
mod;
}
}
answer %= mod;
memo[curr][diffGood][diffBad][turn][type] = answer;
return answer;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
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 = 0; i < n; i++) {
sum[a[i]] += w[i];
}
memset(memo, -1, sizeof(memo));
for (int i = 0; i < n; i++) {
cout << calcState(w[i], 0, 0, 0, a[i]) << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dp[2555][105][55];
long long a[55], wi[55], cur, sp, sn, su, mo;
long long expo(long long base, long long exponent, long long mod = 998244353) {
long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
long long add(long long x, long long y, long long mod = 998244353) {
x += y;
if (x >= mod) x -= mod;
return x;
}
long long sub(long long x, long long y, long long mod = 998244353) {
x -= y;
if (x < 0) x += mod;
return x;
}
long long mul(long long x, long long y, long long mod = 998244353) {
return (x * 1ll * y) % mod;
}
long long solve(long long w, long long s, long long m) {
if (m == 0) return w;
if (w == 0) return 0;
if (dp[w][s][m] != -1) return dp[w][s][m];
long long ans;
long long left = (mo - m) - abs(w - wi[cur]);
long long curs = (s - w) - su;
long long snu = (left - curs) / 2, spu = (left + curs) / 2;
if (a[cur]) {
ans = mul(w, solve(w + 1, s + 1, m - 1));
if (sn - snu) ans = add(ans, mul(sn - snu, solve(w, s - 1, m - 1)));
if (sp + spu) ans = add(mul(sp + spu, solve(w, s + 1, m - 1)), ans);
ans = mul(ans, expo(s, 998244353 - 2));
} else {
ans = mul(w, solve(w - 1, s - 1, m - 1));
if (sn - snu) ans = add(ans, mul(sn - snu, solve(w, s - 1, m - 1)));
if (sp + spu) ans = add(ans, mul(sp + spu, solve(w, s + 1, m - 1)));
ans = mul(ans, expo(s, 998244353 - 2));
}
dp[w][s][m] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
long long n, m;
cin >> n >> m;
mo = m;
long long s = 0;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) {
cin >> wi[i];
s += wi[i];
if (a[i])
sp += wi[i];
else
sn += wi[i];
}
su = s;
for (long long i = 0; i < n; i++) {
for (long long l = max(0LL, s - m - 1LL); l < s + m + 1; l++)
for (long long j = max(0LL, wi[i] - m - 1); j < m + 1 + wi[i]; j++)
for (long long k = 0; k < m + 1; k++) dp[j][l][k] = -1;
cur = i;
if (a[i])
sp -= wi[i];
else
sn -= wi[i];
su -= wi[i];
cout << solve(wi[i], s, m) << '\n';
if (a[i])
sp += wi[i];
else
sn += wi[i];
su += wi[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 dp[2555][105][55];
long long a[55], wi[55], cur, sp, sn, su, mo;
long long expo(long long base, long long exponent, long long mod = 998244353) {
long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
long long add(long long x, long long y, long long mod = 998244353) {
x += y;
if (x >= mod) x -= mod;
return x;
}
long long sub(long long x, long long y, long long mod = 998244353) {
x -= y;
if (x < 0) x += mod;
return x;
}
long long mul(long long x, long long y, long long mod = 998244353) {
return (x * 1ll * y) % mod;
}
long long solve(long long w, long long s, long long m) {
if (m == 0) return w;
if (w == 0) return 0;
if (dp[w][s][m] != -1) return dp[w][s][m];
long long ans;
long long left = (mo - m) - abs(w - wi[cur]);
long long curs = (s - w) - su;
long long snu = (left - curs) / 2, spu = (left + curs) / 2;
if (a[cur]) {
ans = mul(w, solve(w + 1, s + 1, m - 1));
if (sn - snu) ans = add(ans, mul(sn - snu, solve(w, s - 1, m - 1)));
if (sp + spu) ans = add(mul(sp + spu, solve(w, s + 1, m - 1)), ans);
ans = mul(ans, expo(s, 998244353 - 2));
} else {
ans = mul(w, solve(w - 1, s - 1, m - 1));
if (sn - snu) ans = add(ans, mul(sn - snu, solve(w, s - 1, m - 1)));
if (sp + spu) ans = add(ans, mul(sp + spu, solve(w, s + 1, m - 1)));
ans = mul(ans, expo(s, 998244353 - 2));
}
dp[w][s][m] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
long long n, m;
cin >> n >> m;
mo = m;
long long s = 0;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) {
cin >> wi[i];
s += wi[i];
if (a[i])
sp += wi[i];
else
sn += wi[i];
}
su = s;
for (long long i = 0; i < n; i++) {
for (long long l = max(0LL, s - m - 1LL); l < s + m + 1; l++)
for (long long j = max(0LL, wi[i] - m - 1); j < m + 1 + wi[i]; j++)
for (long long k = 0; k < m + 1; k++) dp[j][l][k] = -1;
cur = i;
if (a[i])
sp -= wi[i];
else
sn -= wi[i];
su -= wi[i];
cout << solve(wi[i], s, m) << '\n';
if (a[i])
sp += wi[i];
else
sn += wi[i];
su += wi[i];
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
void err() { std::cout << std::endl; }
template <typename T, typename... Args>
void err(T a, Args... args) {
std::cout << a << ' ';
err(args...);
}
using namespace std;
const int mod = 998244353;
const int inf = 1 << 30;
const int maxn = 100000 + 5;
const int maxq = 3000 + 5;
long long qpow(long long x, long long n) {
long long r = 1;
while (n > 0) {
if (n & 1) r = r * x % mod;
n >>= 1;
x = x * x % mod;
}
return r;
}
long long imem[maxn * 2];
long long inv(int x) {
if (imem[x]) return imem[x];
return imem[x] = qpow(x, mod - 2);
}
void add(long long& x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
int n, m, a[maxn], w[maxn];
long long f[maxq][maxq];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
int sa = 0, sb = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", w + i);
if (a[i])
sa += w[i];
else
sb += w[i];
}
f[0][0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
int a = sa + j, b = sb - (i - j);
long long fm = inv(a + b);
add(f[i + 1][j + 1], f[i][j] * a % mod * fm % mod);
add(f[i + 1][j], f[i][j] * b % mod * fm % mod);
}
}
long long ea = 0, eb = 0;
for (int i = 0; i <= m; i++) {
;
add(ea, f[m][i] * (sa + i) % mod);
add(eb, f[m][i] * (sb - (m - i)) % mod);
};
long long iva = inv(sa), ivb = inv(sb);
for (int i = 1; i <= n; i++) {
if (a[i]) {
printf("%lld\n", 1ll * w[i] * iva % mod * ea % mod);
} else {
printf("%lld\n", 1ll * w[i] * ivb % mod * eb % mod);
}
}
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>
void err() { std::cout << std::endl; }
template <typename T, typename... Args>
void err(T a, Args... args) {
std::cout << a << ' ';
err(args...);
}
using namespace std;
const int mod = 998244353;
const int inf = 1 << 30;
const int maxn = 100000 + 5;
const int maxq = 3000 + 5;
long long qpow(long long x, long long n) {
long long r = 1;
while (n > 0) {
if (n & 1) r = r * x % mod;
n >>= 1;
x = x * x % mod;
}
return r;
}
long long imem[maxn * 2];
long long inv(int x) {
if (imem[x]) return imem[x];
return imem[x] = qpow(x, mod - 2);
}
void add(long long& x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
int n, m, a[maxn], w[maxn];
long long f[maxq][maxq];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
int sa = 0, sb = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", w + i);
if (a[i])
sa += w[i];
else
sb += w[i];
}
f[0][0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
int a = sa + j, b = sb - (i - j);
long long fm = inv(a + b);
add(f[i + 1][j + 1], f[i][j] * a % mod * fm % mod);
add(f[i + 1][j], f[i][j] * b % mod * fm % mod);
}
}
long long ea = 0, eb = 0;
for (int i = 0; i <= m; i++) {
;
add(ea, f[m][i] * (sa + i) % mod);
add(eb, f[m][i] * (sb - (m - i)) % mod);
};
long long iva = inv(sa), ivb = inv(sb);
for (int i = 1; i <= n; i++) {
if (a[i]) {
printf("%lld\n", 1ll * w[i] * iva % mod * ea % mod);
} else {
printf("%lld\n", 1ll * w[i] * ivb % mod * eb % mod);
}
}
return 0;
}
``` |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.