Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
inline long long mod(long long n) {
if (n < 0) {
return (n % MOD + MOD) % 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 = 103;
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';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 110;
long long kOper, n, cnt;
long long c[N];
struct Matrix {
long long x[N][N];
Matrix() {
for (int i = 0; i <= N - 1; ++i)
for (int j = 0; j <= N - 1; ++j) x[i][j] = 0;
}
void show() {
for (int i = 0; i <= cnt; ++i) {
for (int j = 0; j <= cnt; ++j) cout << x[i][j] << ' ';
cout << '\n';
}
}
} a, b;
long long px(long long a, long long q) {
if (q == 0) return 1;
long long tmp = px(a, q / 2);
tmp = (tmp * tmp) % MOD;
if (q & 1) tmp = (tmp * a) % MOD;
return tmp;
}
Matrix mul(Matrix &a, Matrix &b) {
Matrix c;
for (int i = 0; i <= cnt; ++i)
for (int j = 0; j <= cnt; ++j)
for (int k = 0; k <= cnt; ++k)
c.x[i][j] = (c.x[i][j] + a.x[i][k] * b.x[k][j]) % MOD;
return c;
}
Matrix px(Matrix a, long long q) {
if (q == 1) return a;
Matrix tmp = px(a, q / 2);
tmp = mul(tmp, tmp);
if (q & 1) tmp = mul(tmp, a);
return tmp;
}
long long sum(long long n) { return n * (n - 1) / 2 % MOD; }
void read() {
cin >> n >> kOper;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
cnt += (!c[i]);
}
int cur = 0;
for (int i = 1; i <= cnt; ++i) cur += (!c[i]);
a.x[0][cur] = 1;
for (int j = 0; j <= cnt; ++j) {
b.x[j][j] = (sum(cnt) + sum(n - cnt) + j * (cnt - j) +
(cnt - j) * (n - 2 * cnt + j)) %
MOD;
if (j > 0) b.x[j - 1][j] = (cnt - j + 1) * (cnt - j + 1) % MOD;
b.x[j + 1][j] = (j + 1) * (n - 2 * cnt + j + 1) % MOD;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
b = px(b, kOper);
a = mul(a, b);
long long sum = 0;
for (int i = 0; i <= cnt; ++i) sum = (sum + a.x[0][i]) % MOD;
cout << a.x[0][cnt] * px(sum, MOD - 2) % MOD << '\n';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long p = (long long)1e9 + 7;
int n, m, k, a[110];
long long f[110][110], C[110][110];
long long qmod(long long base, long long n) {
long long res = 1;
while (n) {
if (n & 1) {
res = res * base % p;
}
base = base * base % p;
n >>= 1;
}
return res;
}
struct node {
int len;
long long a[110][110];
node() {}
node(int len) {
this->len = len;
memset(a, 0, sizeof a);
}
node operator*(const node &other) const {
node res = node(len);
for (int i = 0; i <= len; ++i) {
for (int j = 0; j <= len; ++j) {
for (int k = 0; k <= len; ++k) {
(res.a[i][j] += a[i][k] * other.a[k][j] % p) %= p;
}
}
}
return res;
}
} base, res;
void qmod(node &res, node &base, long long n) {
while (n) {
if (n & 1) {
res = res * base;
}
base = base * base;
n >>= 1;
}
}
int main() {
memset(C, 0, sizeof C);
C[1][0] = C[1][1] = 1;
for (int i = 1; i <= 100; ++i) {
for (int j = 0; j <= i; ++j) {
(C[i + 1][j + 1] += C[i][j]) %= p;
(C[i + 1][j] += C[i][j]) %= p;
}
}
while (scanf("%d%d", &n, &k) != EOF) {
m = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
m += (a[i] == 0);
}
base = node(m);
res = node(m);
if (m == 0) {
res.a[0][0] = 1;
}
for (int i = 1, j = 0; i <= m; ++i) {
j += (a[i] == 1);
if (i == m) {
res.a[0][j] = 1;
}
}
for (int i = 0; i <= m; ++i) {
(base.a[i][i] += C[m][2] + C[n - m][2]) %= p;
(base.a[i][i] += i * (n - m - i) % p) %= p;
(base.a[i][i] += (m - i) * i % p) %= p;
if (i != 0) {
(base.a[i - 1][i] += (m - i + 1) * (n - m - i + 1) % p) %= p;
}
if (i != m) {
(base.a[i + 1][i] += (i + 1) * (i + 1) % p) %= p;
}
}
qmod(res, base, k);
long long a = res.a[0][0], b = 0;
for (int i = 0; i <= m; ++i) {
(b += res.a[0][i]) %= p;
}
printf("%lld\n", a * qmod(b, p - 2) % p);
}
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
vector<ll> v;
vector<vector<ll>> mat;
ll poww(ll x, ll y) {
if (y == 1) return x;
if (y % 2 == 0) {
ll powww = poww(x, y / 2);
return (powww * powww) % 1000000007;
} else {
return (poww(x, y - 1) % 1000000007) * x % 1000000007;
}
}
vector<vector<ll>> matmult(vector<vector<ll>> mat1, vector<vector<ll>> mat2) {
vector<vector<ll>> mat3;
mat3.resize((int)(mat1).size(), vector<ll>((int)(mat1).size()));
for (int i = 0; i < (int)(mat1).size(); ++i) {
for (int j = 0; j < (int)(mat1).size(); ++j) {
ll sum = 0;
for (int k = 0; k < (int)(mat1).size(); ++k) {
sum += (mat1)[i][k] * (mat2)[k][j] % 1000000007;
}
mat3[i][j] = sum % 1000000007;
}
}
return mat3;
}
vector<vector<ll>> powmat(vector<vector<ll>> mat, ll x) {
if (x == 1) return mat;
if (x % 2 == 0) {
vector<vector<ll>> tem = powmat(mat, x / 2);
return matmult(tem, tem);
}
return matmult(powmat(mat, x - 1), mat);
}
int main() {
std::ios::sync_with_stdio(false);
int anfl1 = 0;
int anz1 = 0;
ll x;
int k, n;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> x;
if (x == 1) anz1++;
v.push_back(x);
}
for (int i = 0; i < n - anz1; ++i) {
if (v[i] == 1) anfl1++;
}
ll l = n * (n - 1) / 2 + anz1 * (anz1 - n);
int mini = min(anz1, n - anz1);
if (anz1 == 0 || anz1 == n) {
cout << 1 << endl;
return 0;
}
mat.resize(mini + 1, vector<ll>(mini + 1));
for (int i = 0; i <= mini; ++i) {
for (int j = max(0, i - 1); j <= min(mini, i + 1); ++j) {
if (j == i) {
mat[i][j] = l + j * (n - 2 * j);
} else if (j + 1 == i) {
mat[i][j] = i * i;
} else if (j == i + 1) {
mat[i][j] = anz1 * (n - anz1) + i * (i - n);
}
}
}
mat = powmat(mat, k);
ll P = mat[anfl1][0];
ll Q = poww(n * (n - 1) / 2, k);
ll Q_ = poww(Q, 1000000005);
cout << (P * Q_) % 1000000007 << endl;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MOD = 1e9 + 7;
const int N = 100;
const int MAXN = N + 5;
int pow_mod(int x, int i) {
int res = 1;
while (i) {
if (i & 1) res = (long long)res * x % MOD;
x = (long long)x * x % MOD;
i >>= 1;
}
return res;
}
int n, k, a[MAXN], c;
struct Matrix {
int a[MAXN][MAXN];
Matrix() { memset(a, 0, sizeof(a)); }
} A, B;
Matrix operator*(Matrix X, Matrix Y) {
Matrix Z;
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= N; ++j) {
for (int k = 0; k <= N; ++k) {
Z.a[i][j] =
((long long)Z.a[i][j] + (long long)X.a[i][k] * Y.a[k][j] % MOD) %
MOD;
}
}
}
return Z;
}
Matrix matrix_pow(Matrix X, int i) {
Matrix Y;
for (int i = 0; i <= N; ++i) Y.a[i][i] = 1;
while (i) {
if (i & 1) Y = Y * X;
X = X * X;
i >>= 1;
}
return Y;
}
void Print(Matrix X) {
for (int i = 0; i <= 10; ++i) {
for (int j = 0; j <= 10; ++j) {
cout << X.a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
n = read();
k = read();
for (int i = 1; i <= n; ++i) a[i] = read(), c += (a[i] == 0);
int t = 0;
for (int i = 1; i <= c; ++i) t += (a[i] == 0);
A.a[0][t] = 1;
for (int i = 0; i <= c; ++i) {
if (i != 0) B.a[i - 1][i] = 1LL * (c - (i - 1)) * (c - (i - 1)) % MOD;
B.a[i][i] =
1LL * (1LL * i * (c - i) % MOD + 1LL * (c - i) * (n - c - c + i)) % MOD;
B.a[i][i] = (B.a[i][i] + 1LL * c * (c - 1) / 2 % MOD) % MOD;
B.a[i][i] = (B.a[i][i] + 1LL * (n - c) * (n - c - 1) / 2 % MOD) % MOD;
if (i != c) B.a[i + 1][i] = 1LL * (i + 1) * (n - c - c + i + 1) % MOD;
}
B = matrix_pow(B, k);
A = A * B;
int ans = A.a[0][c];
t = 0;
for (int i = 0; i <= c; ++i) t = ((long long)t + A.a[0][i]) % MOD;
ans = ((long long)ans * pow_mod(t, MOD - 2)) % MOD;
cout << ans << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
template <class _Tp>
void ckmax(_Tp &a, _Tp b) {
if (a < b) a = b;
}
template <class _Tp>
void ckmin(_Tp &a, _Tp b) {
if (a > b) a = b;
}
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
return (b == 0) ? (a) : (gcd(b, a % b));
}
int read() {
char ch = getchar();
bool f = 1;
int x = 0;
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = 0, ch = getchar();
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch & 15);
ch = getchar();
}
return f ? x : -x;
}
const int inf = 1000000000;
const long long Inf = 1000000000000000000ll;
const long long mod = 1000000007;
void add(int &x, int y) {
x += y;
if (x > mod) x -= mod;
}
int n, m, K, zero, C[150][150], a[150];
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return res;
}
struct Mat {
int s[150][150];
void clear() { memset(s, 0, sizeof(s)); }
void init() {
clear();
for (int i = 0; i <= m; ++i) s[i][i] = 1;
}
int *operator[](int x) { return s[x]; }
} A, B;
Mat operator*(Mat a, Mat b) {
Mat c;
c.clear();
for (int i = 0; i <= m; ++i)
for (int j = 0; j <= m; ++j)
for (int k = 0; k <= m; ++k)
c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % mod;
return c;
}
Mat qpow(Mat a, int b) {
Mat res;
res.init();
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int main() {
n = read();
K = read();
for (int i = 1; i <= n; ++i) a[i] = read(), zero += 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]) % mod;
int s = 0;
for (int i = 1; i <= n; ++i)
if (i <= zero && a[i]) ++s;
m = min(n - zero, zero);
B[0][s] = 1;
for (int i = 0; i <= m; ++i) {
add(A[i][i], (C[zero][2] + C[n - zero][2]) % mod);
add(A[i][i], 1ll * i * (n - zero - i) % mod);
add(A[i][i], 1ll * (zero - i) * i % mod);
if (i) add(A[i][i - 1], 1ll * i * i % mod);
if (i < m) add(A[i][i + 1], 1ll * (zero - i) * (n - zero - i) % mod);
}
B = B * qpow(A, K);
int ans = 1ll * B[0][0] * qpow(qpow(n * (n - 1) / 2, K), mod - 2) % mod;
printf("%d\n", ans);
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &AKNOI) {
T x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
AKNOI = flag * x;
}
inline void Inc(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
inline int Add(int x, int y) {
Inc(x, y);
return x;
}
int n, K, a[N], t, m, C[N][N];
inline int ksm(int x, int k) {
int ret = x;
--k;
while (k) {
if (k & 1) ret = 1LL * ret * x % MOD;
x = 1LL * x * x % MOD;
k >>= 1;
}
return ret;
}
struct Matrix {
int a[N][N];
int *operator[](int x) { return a[x]; }
} A, F;
Matrix operator*(Matrix a, Matrix b) {
Matrix ret;
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= m; ++j) {
long long tmp = 0;
for (int k = 0; k <= m; ++k) {
tmp = (tmp + 1LL * a[i][k] * b[k][j]) % MOD;
}
ret[i][j] = tmp;
}
}
return ret;
}
Matrix Ksm(Matrix x, int k) {
Matrix ret = x;
--k;
while (k) {
if (k & 1) ret = ret * x;
x = x * x;
k >>= 1;
}
return ret;
}
void init() {
read(n);
read(K);
for (int i = 1; i <= n; ++i) {
read(a[i]);
t += (a[i] == 0);
}
for (int i = 0; i <= n; ++i) {
C[i][0] = 1;
for (int j = 1; j <= i; ++j) {
C[i][j] = Add(C[i - 1][j], C[i - 1][j - 1]);
}
}
}
void solve() {
int c = 0;
for (int i = 1; i <= t; ++i) {
c += a[i];
}
A[0][c] = 1;
m = min(t, n - t);
for (int i = 0; i <= m; ++i) {
Inc(F[i][i], C[t][2]);
Inc(F[i][i], C[n - t][2]);
Inc(F[i][i], (t - i) * i);
Inc(F[i][i], i * (n - t - i));
if (i) Inc(F[i][i - 1], i * i);
if (i < m) Inc(F[i][i + 1], (t - i) * (n - t - i));
}
A = A * Ksm(F, K);
int ans = 1LL * A[0][0] * ksm(ksm(C[n][2], K), MOD - 2) % MOD;
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
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;
const int blocksize = 10;
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();
}
}
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]; }
Matrix operator*(Matrix a) {
int p = a.m;
vector<vector<long long>>& t = a.s;
Matrix r(n, p);
for (int i = 0; i < (int)(n); ++i) {
for (int k = 0; k < (int)(m); ++k) {
long long x = s[i][k];
for (int j = 0; j < (int)(p); ++j) {
r[i][j] += x * t[k][j] % mod;
if (r[i][j] >= mod) {
r[i][j] -= mod;
}
}
}
}
return r;
}
Matrix mul(Matrix a) {
vector<vector<long long>>& t = a.s;
Matrix r(n, n);
for (int ii = 0; ii < n; ii += blocksize) {
for (int jj = 0; jj < n; jj += blocksize) {
for (int kk = 0; kk < n; kk += blocksize) {
for (int i = ii; i < min(ii + blocksize, n); ++i) {
for (int j = jj; j < min(jj + blocksize, n); ++j) {
long long sum = 0;
for (int k = kk; k < min(kk + blocksize, n); ++k) {
sum += s[i][k] * t[k][j] % mod;
if (sum >= mod) {
sum -= mod;
}
}
r[i][j] += sum;
if (r[i][j] >= mod) {
r[i][j] -= mod;
}
}
}
}
}
}
return r;
}
};
Matrix power(Matrix a, long long b) {
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) {
r = r.mul(a);
}
b >>= 1;
a = a.mul(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;
bool check(int i, int p) { return i <= p && p - i <= n - p; }
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 (!check(i, p)) {
continue;
}
if (i >= 1 && check(i - 1, p)) {
b[i - 1][i] = (p - i + 1) * (p - i + 1) * iv % mod;
}
if (i < p && check(i + 1, 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;
ans = 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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
inline void Add(int &a, int b) { a = a + b >= mod ? a + b - mod : a + b; }
inline int ksm(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) res = 1ll * res * a % mod;
return res;
}
int N, k, n;
struct Matrix {
int a[110][110];
Matrix() { memset(a, 0, sizeof(a)); }
inline void init() {
for (int i = 0; i <= n; i++) a[i][i] = 1;
}
Matrix operator*(const Matrix &p) const {
Matrix ans;
for (int i = 0; i <= n; i++)
for (int k = 0; k <= n; k++)
for (int j = 0; j <= n; j++)
Add(ans.a[i][j], 1ll * a[i][k] * p.a[k][j] % mod);
return ans;
}
inline void print() {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) printf("%d ", a[i][j]);
putchar('\n');
}
}
};
inline Matrix ksm(Matrix a, int b) {
Matrix res;
res.init();
for (; b; b >>= 1, a = a * a)
if (b & 1) res = res * a;
return res;
}
Matrix Base;
inline void trans(int x, int y, int k) { Add(Base.a[x][y], k); }
int v[110];
int main() {
scanf("%d%d", &N, &k);
for (int i = 1; i <= N; i++) scanf("%d", v + i), n += (v[i] == 0);
if (n == 0) return puts("1"), 0;
for (int i = 0; i <= n; i++) {
int cnt1 = i, cnt0 = n - i;
int rcnt1 = n - i, rcnt0 = N - n - rcnt1;
if (rcnt0 < 0) continue;
int allv = 1ll * N * (N - 1) / 2 % mod;
trans(i, i,
(mod + 1 -
(1ll * cnt1 * rcnt0 + 1ll * cnt0 * rcnt1) % mod *
ksm(allv, mod - 2) % mod) %
mod);
if (i) trans(i, i - 1, 1ll * cnt1 * rcnt0 % mod * ksm(allv, mod - 2) % mod);
if (i < n)
trans(i, i + 1, 1ll * cnt0 * rcnt1 % mod * ksm(allv, mod - 2) % mod);
}
Matrix t = ksm(Base, k);
int cnt = 0;
for (int i = 1; i <= n; i++) cnt += v[i];
printf("%d\n", t.a[n - cnt][n]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using vb = vector<bool>;
using vc = vector<char>;
using vd = vector<double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vlli = vector<lli>;
using vvlli = vector<vlli>;
using pi = pair<int, int>;
using plli = pair<lli, lli>;
void readc(char& q) { scanf("%c", &q); }
char readc() {
char q;
scanf("%c", &q);
return q;
}
void readint(int& q) { scanf("%d", &q); }
int readint() {
int q;
scanf("%d", &q);
return q;
}
void readlong(lli& q) { scanf("%lld", &q); }
lli readlong() {
lli q;
scanf("%lld", &q);
return q;
}
void readdbl(double& q) { scanf("%lf", &q); }
double readdbl() {
double q;
scanf("%lf", &q);
return q;
}
void printint(int q) { printf("%d", q); }
void printlong(lli q) { printf("%lld", q); }
void printdbl(double q, int p = 6) { printf("%.*lf", p, q); }
void readln() { scanf("\n"); }
void println() { printf("\n"); }
void printsp() { printf(" "); }
void print(int q) { printint(q); }
void print(long q) { printlong((lli)q); }
void print(lli q) { printlong(q); }
void print(double q) { printdbl(q); }
void print(char c) { printf("%c", c); }
void print(pi q) {
print(q.first);
printsp();
print(q.second);
}
void print(vi q) {
for (int i = 0; i < q.size(); ++i) {
print(q[i]);
printsp();
}
}
void print(vlli q) {
for (int i = 0; i < q.size(); ++i) {
print(q[i]);
printsp();
}
}
void print(vd q) {
for (int i = 0; i < q.size(); ++i) {
print(q[i]);
printsp();
}
}
template <typename T>
void println(T q) {
print(q);
println();
}
template <typename T>
T maxim(T first, T second) {
return (first > second) ? first : second;
}
template <typename T>
T maxim(T first, T second, T third) {
return mndlim(maxim(first, second), third);
}
template <typename T>
T minim(T first, T second) {
return (first < second) ? first : second;
}
template <typename T>
T minim(T first, T second, T third) {
return minim(minim(first, second), third);
}
template <typename T>
T middle(T first, T second, T third) {
return first + second + third - maxim(first, second, third) -
minim(first, second, third);
}
template <typename T>
T abs(T arg) {
return arg < 0 ? -arg : arg;
}
const lli modulo = 1000000007;
struct Matrix {
Matrix(int dimension) {
size = dimension;
m = new lli*[size];
for (int i = 0; i < size; ++i) {
m[i] = new lli[size];
for (int j = 0; j < size; ++j) m[i][j] = 0ll;
}
}
Matrix(const Matrix& other) : size(other.size), m(nullptr) {
if (size) {
m = new lli*[size];
for (int i = 0; i < size; ++i) {
m[i] = new lli[size];
for (int j = 0; j < size; ++j) m[i][j] = other.m[i][j];
}
}
}
friend void swap(Matrix& first, Matrix& second) {
using std::swap;
swap(first.size, second.size);
for (int i = 0; i < first.size; ++i) swap(first.m[i], second.m[i]);
}
Matrix& operator=(Matrix matrix) {
swap(*this, matrix);
return *this;
}
void identity() {
for (int i = 0; i < size; ++i) m[i][i] = 1ll;
}
~Matrix() {
for (int i = 0; i < size; ++i) {
delete[] m[i];
}
delete[] m;
}
int size;
lli** m = nullptr;
};
Matrix multiply(const Matrix& a, const Matrix& b) {
Matrix result(a.size);
for (int i = 0; i < a.size; ++i)
for (int j = 0; j < a.size; ++j)
for (lli k = 0; k < a.size; ++k) {
result.m[i][j] =
(result.m[i][j] + (a.m[i][k] * b.m[k][j]) % modulo) % modulo;
}
return result;
}
Matrix power(const Matrix& a, lli p) {
Matrix result(a.size);
result.identity();
if (p == 0) return result;
if (p == 1) return a;
if (p % 2 == 1) result = a;
Matrix qres = power(a, p / 2);
return multiply(result, multiply(qres, qres));
}
lli gcd_extended(lli a, lli b, lli* x, lli* y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
lli x1, y1;
lli result = gcd_extended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return result;
}
lli reverse_modulo(lli a) {
lli x = 0ll, y = 0ll;
gcd_extended(a, modulo, &x, &y);
x = (x % modulo + modulo) % modulo;
return x;
}
int main() {
long n = readlong();
long k = readlong();
vi a(n);
for (int i = 0; i < n; ++i) a[i] = readint();
lli z = 0;
for (int i = 0; i < n; ++i)
if (a[i] == 0) z++;
Matrix A(z + 1);
for (int i = 0; i < z + 1; ++i) {
A.m[i][i] = n * (n - 1) / 2 - (z * z + 2 * i * i - 4 * z * i + i * n);
}
for (int i = 0; i < z; ++i) {
A.m[i][i + 1] = (i + 1) * (n - 2 * z + i + 1);
A.m[i + 1][i] = (z - i) * (z - i);
}
A = power(A, k);
int z0 = 0;
for (int i = 0; i < z; ++i)
if (a[i] == 0) z0++;
vlli d(z + 1);
d[z0] = 1;
vlli res(z + 1);
lli sum = 0;
for (int i = 0; i < z + 1; ++i) {
for (int j = 0; j < z + 1; ++j) {
res[i] = (res[i] + A.m[i][j] * d[j]) % modulo;
}
sum = (sum + res[i]) % modulo;
}
sum = reverse_modulo(sum);
cout << (res[z] * sum) % modulo << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 110;
int n, a[N], k, cnt;
template <typename T>
void gi(T &x) {
x = 0;
register char c = getchar(), pre = 0;
for (; c < '0' || c > '9'; pre = c, c = getchar())
;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10ll + (c ^ 48);
if (pre == '-') x = -x;
}
inline int add(const int &x, const int &y) {
return x + y < mod ? x + y : x + y - mod;
}
inline int sub(const int &x, const int &y) {
return x - y < 0 ? x - y + mod : x - y;
}
inline int mul(const int &x, const int &y) {
return (int)((long long)x * y % mod);
}
int ksm(int x, int y = mod - 2) {
int ss = 1;
for (; y; y >>= 1, x = mul(x, x))
if (y & 1) ss = mul(ss, x);
return ss;
}
struct mtrx {
int a[N][N];
inline mtrx operator*(const mtrx &yy) const {
mtrx res;
memset(res.a, 0, sizeof(res.a));
for (int i = (0), ied = (cnt); i <= ied; i++)
for (int j = (0), jed = (cnt); j <= jed; j++)
for (int k = (0), ked = (cnt); k <= ked; k++)
res.a[i][j] = add(res.a[i][j], mul(a[i][k], yy.a[k][j]));
return res;
}
};
mtrx f, g;
int main() {
gi(n), gi(k);
for (int i = (1), ied = (n); i <= ied; i++) gi(a[i]), cnt += (a[i] ^ 1);
int All = ksm(mul(mul(n, n - 1), ksm(2)));
for (int i = (0), ied = (cnt); i <= ied; i++) {
f.a[i][i] = 1;
if (i >= 1 && i <= n - cnt) {
f.a[i][i - 1] = mul(mul(i, i), All),
f.a[i][i] = sub(f.a[i][i], f.a[i][i - 1]);
}
if (i < cnt && i < n - cnt) {
f.a[i][i + 1] = mul(mul(n - cnt - i, cnt - i), All),
f.a[i][i] = sub(f.a[i][i], f.a[i][i + 1]);
}
}
g = f, --k;
for (; k; k >>= 1, f = f * f)
if (k % 2) g = g * f;
int sum = 0, ans = 0;
for (int i = (1), ied = (cnt); i <= ied; i++) sum += a[i];
printf("%d\n", g.a[sum][0]);
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
int n, k, m, nn;
int a[110], dp[110][110];
struct Node {
int t[110][110];
} init, ans;
inline int fpow(int x, int y) {
int cur_ans = 1;
while (y) {
if (y & 1) cur_ans = 1ll * cur_ans * x % 1000000007;
x = 1ll * x * x % 1000000007;
y >>= 1;
}
return cur_ans;
}
inline Node calc(Node x, Node y) {
Node cur_ans;
for (int i = 0; i <= nn; i++)
for (int j = 0; j <= nn; j++) cur_ans.t[i][j] = 0;
for (int i = 0; i <= nn; i++)
for (int j = 0; j <= nn; j++)
for (int p = 0; p <= nn; p++) {
cur_ans.t[i][j] =
(cur_ans.t[i][j] + 1ll * x.t[i][p] * y.t[p][j] % 1000000007) %
1000000007;
}
return cur_ans;
}
inline Node solve(Node x, int y) {
Node cur_ans;
for (int i = 0; i <= nn; i++)
for (int j = 0; j <= nn; j++) cur_ans.t[i][j] = 0;
for (int i = 0; i <= nn; i++) cur_ans.t[i][i] = 1;
while (y) {
if (y & 1) cur_ans = calc(cur_ans, x);
x = calc(x, x);
y >>= 1;
}
return cur_ans;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) m++;
}
int cur = 0;
for (int i = 1; i <= m; i++)
if (a[i] == 1) cur++;
int c1 = (1ll * (m - 1) * m / 2) % 1000000007;
int c2 = (1ll * (n - m - 1) * (n - m) / 2) % 1000000007;
nn = min(n - m, m);
for (int i = 0; i <= nn; i++) {
init.t[i][i] = (init.t[i][i] + (c1 + c2) % 1000000007) % 1000000007;
init.t[i][i] =
(init.t[i][i] + (1ll * (m - i) * i) % 1000000007) % 1000000007;
init.t[i][i] =
(init.t[i][i] + (1ll * (n - m - i) * i % 1000000007)) % 1000000007;
if (i + 1 <= nn)
init.t[i][i + 1] =
(init.t[i][i + 1] + 1ll * (m - i) * (n - m - i) % 1000000007) %
1000000007;
if (i - 1 >= 0)
init.t[i][i - 1] =
(init.t[i][i - 1] + 1ll * i * i % 1000000007) % 1000000007;
}
init = solve(init, k);
ans.t[0][cur] = 1;
ans = calc(ans, init);
int fn = 1ll * fpow(fpow(n * (n - 1) / 2, k), 1000000007 - 2) % 1000000007;
cout << 1ll * ans.t[0][0] * fn % 1000000007 << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long N = 1e2 + 7;
const long long mod = 1e9 + 7;
long long n, k;
void pr(long long x[N][N]) {
printf("******\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", x[i][j]);
}
printf("\n");
}
printf("******\n");
}
long long qpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
void mul(long long a[N][N], long long b[N][N]) {
long long c[N][N];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = 0;
for (int k = 0; k < n; k++) {
if (a[i][k] > 0 && b[k][j] > 0)
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
}
}
}
memcpy(a, c, sizeof(c));
}
long long a[N][N];
long long b[N][N];
long long qpow(long long x) {
while (x) {
if (x & 1) mul(a, b);
x >>= 1;
mul(b, b);
}
}
int ar[N];
int main() {
int t0 = 0, t1 = 0, tmp = 0;
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &ar[i]);
if (ar[i])
t1++;
else
t0++;
}
if (t0 == 0 || t1 == 0) {
printf("1\n");
return 0;
}
for (int i = n, j = 1; j <= t1; j++, i--) {
if (ar[i] == 1) tmp++;
}
a[0][tmp] = 1;
long long x = n * (n - 1) / 2;
b[0][0] = x - t1 * t1;
b[1][0] = t0 - (t1 - 1);
for (int i = 1; i < t1; i++) {
int j = t1 - i;
b[i][i] =
t0 * (t0 - 1) / 2 + t1 * (t1 - 1) / 2 + i * (t1 - i) + j * (t0 - j);
b[i - 1][i] = (t1 - i + 1) * (t1 - i + 1);
b[i + 1][i] = (i + 1) * (t0 - (t1 - (i + 1)));
}
b[t1][t1] = t0 * (t0 - 1) / 2 + t1 * (t1 - 1) / 2;
b[t1 - 1][t1] = 1;
qpow(k);
long long ans = a[0][t1];
ans = ans * qpow(qpow(x, k), mod - 2) % mod;
printf("%lld\n", ans);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
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 a[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.a[i][k] = (R.a[i][k] + 1ll * a[i][j] * P.a[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.a[i][j] = 0;
}
for (j = 0; j < c + 1; j++) {
T.a[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.a[j][j - 1] = ((c - j + 1) * (c - j + 1)) % 1000000007;
if (j != c) T.a[j][j + 1] = ((j + 1) * (n - 2 * c + j + 1)) % 1000000007;
}
matrix A = T ^ k;
p = A.a[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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
const long long Mod = 1e9 + 7;
const long long M = 110;
long long dp[M][M], n, k, t, a[M];
struct node {
long long a[M][M];
} ans, aa, res;
node mul(node a, node b) {
node c;
memset(c.a, 0, sizeof(c.a));
for (long long i = 0; i <= t; i++)
for (long long j = 0; j <= t; j++)
for (long long k = 0; k <= t; k++)
c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j]) % Mod;
return c;
}
long long poww(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % Mod;
a = a * a % Mod, b >>= 1;
}
return res;
}
long long inv(long long a) { return poww(a, Mod - 2); }
node poww2(node a, long long b) {
while (b) {
if (b & 1) res = mul(res, a);
a = mul(a, a), b >>= 1;
}
return res;
}
signed main() {
n = read(), k = read();
for (long long i = 1; i <= n; i++) a[i] = read(), t += a[i] ^ 1;
long long cnt = 0;
for (long long i = 1; i <= t; i++) cnt += a[i] ^ 1;
ans.a[0][cnt] = 1;
for (long long i = 0; i <= t; i++) res.a[i][i] = 1;
for (long long i = 0; i <= t; i++)
aa.a[i - 1][i] = (t - i + 1) * (t - i + 1) % Mod,
aa.a[i][i] = (t * (t - 1) / 2 + (n - t) * (n - t - 1) / 2 +
i * (t - i) + (t - i) * (n - 2 * t + i)) %
Mod,
aa.a[i + 1][i] = (i + 1) * (n - 2 * t + (i + 1)) % Mod;
aa = poww2(aa, k);
ans = mul(ans, aa);
cout << ans.a[0][t] * inv(poww(n * (n - 1) / 2, k)) % Mod;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | N, T = map(int, input().split())
A = [int(a) for a in input().split()]
if sum(A) > N//2:
A = [1-a for a in A][::-1]
K = sum(A)
S = sum(A[-K:])
M = K + 1
P = 10**9+7
inv = pow(N*(N-1)//2, P-2, P)
X = [[0]*M for _ in range(M)]
for i in range(M):
if i > 0: X[i-1][i] = ((K-i+1)**2*inv)%P
if i < M-1: X[i+1][i] = (N-2*K+i+1)*(i+1)*inv%P
X[i][i] = (1-((K-i)**2*inv)-(N-2*K+i)*(i)*inv)%P
def ddd(n):
for i in range(1, 100):
if (n*i%P) < 100:
return (n*i%P), i
return -1, -1
def poww(MM, n):
if n == 1:
return MM
if n % 2:
return mult(poww(MM, n-1), MM)
return poww(mult(MM,MM), n//2)
def mult(M1, M2):
Y = [[0] * M for _ in range(M)]
for i in range(M):
for j in range(M):
for k in range(M):
Y[i][j] += M1[i][k] * M2[k][j]
Y[i][j] %= P
return Y
X = poww(X, T)
print(X[S][K])
| PYTHON3 |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
struct mint {
int n;
mint(int n_ = 0) : n(n_ % MOD) {
if (n < 0) n += MOD;
}
};
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 &os, mint a) { return os << a.n; }
istream &operator>>(istream &is, mint &a) { return is >> a.n; }
mint inv(mint x) {
long long a = x.n, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
swap((a -= t * b), b);
swap((u -= t * v), v);
}
return mint(u);
}
mint operator^(mint a, long long n) {
mint r = 1;
while (n) {
if (n & 1) r *= a;
a *= a;
n >>= 1;
}
return r;
}
bool operator<(const mint &a, const mint &b) { return a.n < b.n; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
for (auto &vi : vec) os << vi << " ";
return os;
}
template <class T>
struct Matrix {
vector<vector<T>> val;
Matrix(int n = 1, int m = 1, T x = 0) { val.assign(n, vector<T>(m, x)); }
size_t size() const { return val.size(); }
vector<T> &operator[](int i) { return val[i]; }
const vector<T> &operator[](int i) const { return val[i]; }
friend ostream &operator<<(ostream &os, const Matrix<T> M) {
for (int i = 0; i < M.size(); ++i) os << M[i] << " \n"[i != M.size() - 1];
return os;
}
};
template <class T>
Matrix<T> operator^(Matrix<T> A, long long n) {
Matrix<T> R(A.size(), A.size());
for (int i = 0; i < A.size(); ++i) R[i][i] = 1;
while (n > 0) {
if (n & 1) R = R * A;
A = A * A;
n >>= 1;
}
return R;
}
template <class T>
Matrix<T> operator*(const Matrix<T> &A, const Matrix<T> &B) {
Matrix<T> R(A.size(), B[0].size());
for (int i = 0; i < A.size(); ++i)
for (int j = 0; j < B[0].size(); ++j)
for (int k = 0; k < B.size(); ++k) R[i][j] += A[i][k] * B[k][j];
return R;
}
template <class T>
vector<T> operator*(const Matrix<T> &A, vector<T> &B) {
vector<T> v(A.size());
for (int i = 0; i < A.size(); ++i)
for (int k = 0; k < B.size(); ++k) v[i] += A[i][k] * B[k];
return v;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &ai : a) cin >> ai;
int cnt0 = count(a.begin(), a.end(), 0);
int ng1 = count(a.begin(), a.begin() + cnt0, 1);
vector<mint> state(cnt0 + 1);
state[cnt0 - ng1] = 1;
Matrix<mint> trans(cnt0 + 1, cnt0 + 1);
mint all_inv = 2 * inv(n * (n - 1));
for (int ok0 = 0; ok0 <= cnt0; ok0++) {
mint dec = ok0 * (n + ok0 - 2 * cnt0) * all_inv;
mint inc = (cnt0 - ok0) * (cnt0 - ok0) * all_inv;
if (ok0 > 0) trans[ok0 - 1][ok0] = dec;
trans[ok0][ok0] += 1 - (dec + inc);
if (ok0 < cnt0) trans[ok0 + 1][ok0] = inc;
}
cout << ((trans ^ k) * state)[cnt0] << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
int n, k, v[102];
int cnt0, cnt1, N;
int m[102][102], Ans[102][102];
int lgPow(int a, int b) {
int ret = 1;
for (int i = 0; (1 << i) <= b; ++i, a = (1LL * a * a) % 1000000007)
if (b & (1 << i)) ret = (1LL * ret * a) % 1000000007;
return ret;
}
void mul(int a[102][102], int b[102][102]) {
int res[102][102];
for (int i = 0; i <= N; ++i)
for (int j = 0; j <= N; ++j) {
res[i][j] = 0;
for (int k = 0; k <= N; ++k)
res[i][j] = (1LL * res[i][j] + 1LL * a[i][k] * b[k][j]) % 1000000007;
}
memcpy(a, res, sizeof(res));
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &v[i]);
cnt1 += v[i];
cnt0 += (v[i] == 0);
}
int p = 0;
for (int i = 0; i < cnt0; ++i) p += v[i];
int P1 = ((cnt1 - 1) * cnt1) / 2, P0 = ((cnt0 - 1) * cnt0) / 2;
N = min(cnt0, cnt1);
for (int i = 0; i <= N; ++i) {
Ans[i][i] = 1;
m[i][i] = i * (cnt0 - i) + P0 + P1 + i * (cnt1 - i);
if (i > 0) m[i][i - 1] = i * i;
if (i < N) m[i][i + 1] = (cnt1 - i) * (cnt0 - i);
}
for (int i = 0; (1 << i) <= k; ++i, mul(m, m))
if (k & (1 << i)) mul(Ans, m);
int ans = lgPow((n * (n - 1)) / 2, 1000000007 - 1 - k);
ans = (1LL * ans * Ans[p][0]) % 1000000007;
printf("%d\n", ans);
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int p[105];
long long quickpow(long long a, long long x) {
long long res = 1;
while (x) {
if (x & 1) res = (res * a) % mod;
x >>= 1;
a = (a * a) % mod;
}
return res;
}
struct matrix {
long long mat[105][105];
matrix() { memset(mat, 0, sizeof(mat)); }
};
matrix mul(matrix a, matrix b) {
matrix c;
for (int i = 0; i < 105; i++)
for (int j = 0; j < 105; j++) {
c.mat[i][j] = 0;
for (int k = 0; k < 105; k++)
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j] % mod) % mod;
}
return c;
}
matrix pow(matrix a, long long x) {
matrix res;
for (int i = 0; i < 105; i++) res.mat[i][i] = 1;
while (x) {
if (x & 1) res = mul(res, a);
x >>= 1;
a = mul(a, a);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
int n, k, cnt = 0;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> p[i];
cnt += (p[i] == 0);
}
matrix trans, f;
f.mat[0][0] = 1;
long long all = n * (n - 1) % mod * quickpow(2, mod - 2) % mod;
for (int i = 0; i <= min(cnt, n - cnt); i++) {
long long temp = 0;
if (i) {
trans.mat[i][i - 1] = i * i * quickpow(all, mod - 2) % mod;
temp = (temp + trans.mat[i][i - 1]) % mod;
}
if (i < cnt) {
trans.mat[i][i + 1] =
(cnt - i) * (n - cnt - i) * quickpow(all, mod - 2) % mod;
temp = (temp + trans.mat[i][i + 1]) % mod;
}
trans.mat[i][i] = (1 - temp + mod) % mod;
}
f = mul(pow(trans, k), f);
int now = 0;
for (int i = 1; i <= cnt; i++) now += (p[i] == 1);
cout << f.mat[now][0];
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int mo = 1e9 + 7;
int n, K, kai1[N], m;
inline int fix(int x) {
if (x >= mo) return x - mo;
return x < 0 ? x + mo : x;
}
inline int hapow1(int x, int y) {
int daan = 1;
while (y) {
if (y & 1) daan = 1ll * daan * x % mo;
x = 1ll * x * x % mo;
y >>= 1;
}
return daan;
}
struct matrix {
int ma[N][N];
friend matrix operator*(matrix x, matrix y) {
matrix z;
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
z.ma[i][j] = 0;
for (int k = 0; k <= m; k++)
z.ma[i][j] = fix(z.ma[i][j] + 1ll * x.ma[i][k] * y.ma[k][j] % mo);
}
return z;
}
} kai, ans;
inline int read() {
char ch = getchar();
int x = 0, f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void hapow(int y) {
while (y) {
if (y & 1) ans = ans * kai;
kai = kai * kai;
y >>= 1;
}
}
inline int C(int x) { return x * (x - 1) / 2; }
int main() {
n = read();
K = read();
for (int i = 1; i <= n; i++) kai1[i] = read(), m += (kai1[i] == 0);
int hu = 0;
for (int i = 1; i <= m; i++) hu += (kai1[i] == 0);
ans.ma[0][hu] = 1;
int inv = hapow1(n * (n - 1) / 2, mo - 2);
for (int i = 0; i <= m; i++) {
kai.ma[i][i] = 1ll * inv *
(i * (m - i) + (m - i) * (n - 2 * m + i) + C(m) + C(n - m)) %
mo;
if (i != 0) kai.ma[i][i - 1] = 1ll * inv * i % mo * (n - 2 * m + i) % mo;
kai.ma[i][i + 1] = 1ll * inv * (m - i) % mo * (m - i) % mo;
}
hapow(K);
cout << ans.ma[0][m];
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 109;
const int mod = 1e9 + 7;
int n, a[N], k;
int cnt0, cnt1;
int b[N][N];
void jzc(int a[][N], int b[][N], int c) {
int ans[N][N];
for (int i = 0; i <= cnt0; i++)
for (int j = 0; j <= cnt0; j++) ans[i][j] = 0;
for (int i = 0; i <= cnt0; i++)
for (int j = 0; j <= cnt0; j++)
for (int k = 0; k <= cnt0; k++)
ans[i][j] = (ans[i][j] + 1ll * a[i][k] * b[k][j]) % mod;
for (int i = 0; i <= cnt0; i++)
for (int j = 0; j <= cnt0; j++) a[i][j] = ans[i][j];
}
void jzksm(int a[][N], int b, int c) {
int ans[N][N], base[N][N];
for (int i = 0; i <= cnt0; i++) {
for (int j = 0; j <= cnt0; j++) {
if (i == j)
ans[i][j] = 1;
else
ans[i][j] = 0;
base[i][j] = a[i][j];
}
}
while (b) {
if (b & 1) jzc(ans, base, c);
jzc(base, base, c);
b >>= 1;
}
for (int i = 0; i <= cnt0; i++)
for (int j = 0; j <= cnt0; j++) a[i][j] = ans[i][j];
}
void print() {
for (int i = 0; i <= cnt0; i++) {
for (int j = 0; j <= cnt0; j++) cout << b[i][j] << " ";
cout << endl;
}
cout << endl;
}
int poww(int a, int b, int c) {
int ans = 1, base = a;
while (b) {
if (b & 1) ans = 1ll * ans * base % c;
base = 1ll * base * base % c;
b >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], cnt0 += (a[i] == 0), cnt1 += (a[i] == 1);
int st = 0;
for (int i = 1; i <= cnt0; i++) st += (a[i] == 0);
for (int i = 0; i <= cnt0; i++) {
if (i) b[i - 1][i] = (cnt0 - i + 1) * (cnt0 - i + 1);
b[i][i] = cnt0 * (cnt0 - 1) / 2 + (cnt1 - 1) * (cnt1) / 2 + i * (cnt0 - i) +
(cnt0 - i) * (cnt1 - cnt0 + i);
if (i < cnt0) b[i + 1][i] = (i + 1) * (cnt1 - cnt0 + i + 1);
}
jzksm(b, k, mod);
int sum = n * (n - 1) / 2;
sum = poww(sum, k, mod);
cout << b[st][cnt0] * 1ll * poww(sum, mod - 2, mod) % mod << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
int n, v[104], k, nr0;
int** mat;
int _pow(int a, int b) {
if (b == 0) return 1;
if (b == 1) return a;
if (b % 2 == 0) return _pow(1LL * a * a % 1000000007, b / 2);
return 1LL * _pow(1LL * a * a % 1000000007, b / 2) * a % 1000000007;
}
int** mult(int** A, int** B) {
int** ans = new int*[nr0 + 1];
for (int i = 0; i <= nr0; i++) ans[i] = new int[nr0 + 1];
for (int i = 0; i <= nr0; i++)
for (int j = 0; j <= nr0; j++) ans[i][j] = 0;
for (int i = 0; i <= nr0; i++)
for (int j = 0; j <= nr0; j++) {
for (int k = 0; k <= nr0; k++) {
ans[i][j] = (1LL * A[i][k] * B[k][j] + ans[i][j]) % 1000000007;
}
}
return ans;
}
int** _pow2(int** mat, int k) {
int** mat2 = new int*[nr0 + 1];
for (int i = 0; i <= nr0; i++) mat2[i] = new int[nr0 + 1];
for (int i = 0; i <= nr0; i++)
for (int j = 0; j <= nr0; j++) mat2[i][j] = mat[i][j];
if (k == 1) return mat2;
int** mat3 = new int*[nr0 + 1];
for (int i = 0; i <= nr0; i++) mat3[i] = new int[nr0 + 1];
for (int i = 0; i <= nr0; i++)
for (int j = 0; j <= nr0; j++) mat3[i][j] = mat[i][j];
if (k % 2 == 0) {
mat3 = mult(mat3, mat2);
return _pow2(mat3, k / 2);
} else {
mat3 = mult(mat3, mat2);
mat3 = _pow2(mat3, k / 2);
return mult(mat3, mat2);
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> v[i];
if (v[i] == 0) nr0++;
}
if (nr0 == 0 || nr0 == n) {
cout << 1 << '\n';
return 0;
}
mat = new int*[nr0 + 1];
for (int i = 0; i <= nr0; i++) {
mat[i] = new int[nr0 + 1];
for (int j = 0; j <= nr0; j++) {
mat[i][j] = 0;
}
}
int mn = max(0, nr0 - (n - nr0));
for (int i = 0; i <= nr0; i++) {
if (i > mn)
mat[i - 1][i] = 1LL * (nr0 - (i - 1)) * (nr0 - (i - 1)) % 1000000007;
mat[i][i] = ((max(0LL, 1LL * nr0 * (nr0 - 1) / 2) +
max(0LL, 1LL * (n - nr0) * (n - nr0 - 1) / 2)) %
1000000007 +
1LL * (nr0 - i) * i) %
1000000007;
mat[i][i] =
(mat[i][i] + 1LL * (nr0 - i) * (n - nr0 - (nr0 - i))) % 1000000007;
if (i < nr0)
mat[i + 1][i] = 1LL * (i + 1) * (n - nr0 - (nr0 - i - 1)) % 1000000007;
}
int** ans = _pow2(mat, k);
for (int i = 0; i <= nr0; i++)
for (int j = 0; j <= nr0; j++) mat[i][j] = 0;
int x = 0;
for (int i = 1; i <= nr0; i++)
if (v[i] == 0) x++;
mat[1][x] = 1;
int** ans2 = mult(mat, ans);
int A = ans2[1][nr0], B = 0;
for (int i = 0; i <= nr0; i++) B = (B + ans2[1][i]) % 1000000007;
A = (1LL * A * _pow(B, 1000000007 - 2)) % 1000000007;
cout << A << '\n';
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 120;
const int mod = 1e9 + 7;
int n, k;
int um = 0;
int a[maxn];
struct Matrix {
int mat[maxn][maxn];
} ans, dt, zz;
void multi(Matrix &k1, Matrix k2) {
for (int i = 0; i <= um; i++)
for (int j = 0; j <= um; j++) zz.mat[i][j] = 0;
for (int k = 0; k <= um; k++) {
for (int i = 0; i <= um; i++) {
for (int j = 0; j <= um; j++) {
zz.mat[i][j] += 1ll * k1.mat[i][k] * k2.mat[k][j] % mod;
zz.mat[i][j] %= mod;
}
}
}
for (int i = 0; i <= um; i++)
for (int j = 0; j <= um; j++) k1.mat[i][j] = zz.mat[i][j];
}
void read() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
}
void fast_pow(int pw) {
int bit = 1;
for (int i = 0; i <= um; i++) ans.mat[i][i] = 1;
while (bit <= pw) {
if (bit & pw) {
multi(ans, dt);
}
bit <<= 1;
multi(dt, dt);
}
}
int fast_pow(int now, int pw) {
int bit = 1, ddt = now, res = 1;
while (bit <= pw) {
if (bit & pw) {
res = 1ll * res * ddt % mod;
}
ddt = 1ll * ddt * ddt % mod;
bit <<= 1;
}
return res;
}
void work() {
for (int i = 1; i <= n; i++)
if (a[i] == 1) um++;
for (int i = 0; i <= um; i++) {
int z1 = um - i, z0 = n - um - z1;
int l1 = i, l0 = um - i;
if (z1 < 0 || z0 < 0) continue;
dt.mat[i][i] = (1ll * um * (um - 1) / 2 % mod +
1ll * (n - um) * (n - um - 1) / 2 % mod) %
mod;
dt.mat[i][i] += (1ll * z1 * l1 % mod + 1ll * z0 * l0 % mod) % mod;
dt.mat[i][i] %= mod;
if (i + 1 <= um) dt.mat[i][i + 1] = 1ll * (i + 1) * (z0 + 1) % mod;
if (i - 1 >= 0) dt.mat[i][i - 1] = 1ll * (l0 + 1) * (z1 + 1) % mod;
}
fast_pow(k);
int im = 0;
for (int i = 0; i < um; i++) im += a[n - i];
int pm = fast_pow(1ll * n * (n - 1) / 2 % mod, k);
pm = fast_pow(pm, mod - 2);
int res = 1ll * ans.mat[um][im] * pm % mod;
printf("%d\n", res);
}
int main() {
read();
work();
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 105;
const int inv2 = 5e8 + 4;
int I[N][N], ans, sum[2], n, k, inv_tot, cnt;
int x[N];
long long fp(long long a, long long b) {
a %= MOD;
long long res = 1;
for (; b; b >>= 1, a = a * a % MOD) {
if (b & 1) res = a * res % MOD;
}
return res;
}
int calc(int x, int y) {
if (abs(x - y) > 1) return 0;
if (y == x) {
long long ret = (1LL * sum[1] * (sum[1] - 1) % MOD * inv2 % MOD +
1LL * sum[0] * (sum[0] - 1) % MOD * inv2 % MOD);
ret = ((ret + 1LL * y * (sum[0] - y) % MOD) % MOD +
1LL * y * (sum[1] - y) % MOD) %
MOD;
return ret * inv_tot % MOD;
} else if (x > y)
return 1LL * (sum[0] - y) * (sum[1] - y) % MOD * inv_tot % MOD;
else
return 1LL * y * y % MOD * inv_tot % MOD;
}
void matrix_mul(int (&a)[N][N], int (&b)[N][N], int n) {
int c[N][N] = {0};
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
if (!a[i][k]) continue;
for (int j = 0; j < n; j++) {
c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % MOD;
}
}
}
memcpy(a, c, sizeof(c));
}
void matrix_fp(int (&a)[N][N], long long k, int n) {
int c[N][N] = {0};
for (int i = 0; i < n; i++) c[i][i] = 1;
while (k) {
if (k & 1) matrix_mul(c, a, n);
matrix_mul(a, a, n);
k >>= 1;
}
memcpy(a, c, sizeof(c));
}
int main() {
scanf("%d%d", &n, &k);
inv_tot = fp(n * (n - 1) / 2, MOD - 2);
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
sum[x[i]]++;
}
for (int i = 0; i < sum[0]; i++) {
if (x[i] != 0) cnt++;
}
for (int i = 0; i <= sum[1]; i++) {
for (int j = 0; j <= sum[1]; j++) {
I[i][j] = calc(i, j);
}
}
matrix_fp(I, k, sum[1] + 1);
printf("%lld\n", I[0][cnt]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
template <int mod = MOD>
struct modBase {
modBase(int val = 0) : val((val % mod + mod) % mod) {}
int val;
modBase<mod> operator*(modBase<mod> o) {
return modBase<mod>((long long)val * o.val % mod);
}
modBase<mod> operator+(modBase<mod> o) {
return modBase<mod>((val + o.val) % mod);
}
};
template <class T>
T fexp(T x, long long e) {
T ans(1);
for (; e > 0; e /= 2) {
if (e & 1) ans = ans * x;
x = x * x;
}
return ans;
}
template <const size_t n, const size_t m, class T = modBase<>>
struct Matrix {
T v[n][m];
Matrix(int d = 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
v[i][j] = T(0);
}
if (i < m) {
v[i][i] = T(d);
}
}
}
template <size_t mm>
Matrix<n, mm, T> operator*(Matrix<m, mm, T> &o) {
Matrix<n, mm, T> ans;
for (int i = 0; i < n; i++) {
for (int j = 0; j < mm; j++) {
for (int k = 0; k < m; k++) {
ans.v[i][j] = ans.v[i][j] + v[i][k] * o.v[k][j];
}
}
}
return ans;
}
};
int pairs(int x) { return x * (x - 1) / 2; }
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int freq[2] = {0, 0};
int n, k;
std::cin >> n >> k;
std::vector<int> a(n, 0);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
freq[a[i]]++;
}
const int ms = 101;
Matrix<ms, ms> bas(0);
for (int i = 0; i <= freq[0]; i++) {
int z0 = i, o0 = freq[0] - i;
int z1 = freq[0] - z0, o1 = freq[1] - o0;
if (o0 < 0 || o1 < 0 || z0 < 0 || z1 < 0) continue;
if (z0 + z1 != freq[0] || o0 + o1 != freq[1]) continue;
if (z0 + o0 != freq[0] || z1 + o1 != freq[1]) continue;
auto p = fexp(modBase<>(pairs(n)), MOD - 2);
bas.v[i][i] = p * modBase<>(z0 * o0 + z1 * o1 + z0 * z1 + o0 * o1 +
pairs(z0) + pairs(z1) + pairs(o0) + pairs(o1));
if (z0 > 0 && o1 > 0) {
bas.v[i][i - 1] = p * modBase<>(z0 * o1);
}
if (o0 > 0 && z1 > 0) {
bas.v[i][i + 1] = p * modBase<>(z1 * o0);
}
}
auto ans = fexp(bas, k);
int z0 = 0;
for (int i = 0; i < freq[0]; i++) {
if (a[i] == 0) z0++;
}
std::cout << ans.v[z0][freq[0]].val << '\n';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
int N;
struct Mat {
long long n[55][55] = {};
Mat friend operator*(Mat a, Mat b) {
Mat c;
for (int j = 0; j <= N; j++)
for (int i = 0; i <= N; i++)
for (int k = 0; k <= N; k++)
c.n[i][k] = (c.n[i][k] + a.n[i][j] * b.n[j][k]) % 1000000007;
return c;
}
} A;
int num[105];
long long pow1(long long x, int y) {
long long ans = 1;
while (y) {
if (y % 2) ans = (ans * x) % 1000000007;
x = (x * x) % 1000000007;
y = y / 2;
}
return ans;
}
Mat pow2(int y) {
Mat ans;
for (int i = 0; i <= N; i++) ans.n[i][i] = 1;
while (y) {
if (y % 2) ans = ans * A;
A = A * A;
y = y / 2;
}
return ans;
}
int main() {
int n, k, sum = 0, now = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &num[i]);
if (num[i] == 0) sum++;
}
for (int i = sum + 1; i <= n; i++) {
if (num[i] == 0) now++;
}
N = min(sum, n - sum);
for (int i = 0; i <= N; i++) {
A.n[i][i] = n * (n - 1) / 2;
if (i < N) {
A.n[i][i + 1] = (sum - i) * (n - sum - i);
A.n[i][i] -= (sum - i) * (n - sum - i);
}
if (i > 0) {
A.n[i][i - 1] = i * i;
A.n[i][i] -= i * i;
}
}
A = pow2(k);
printf("%lld\n",
(A.n[now][0] * pow1(pow1(n * (n - 1) / 2, 1000000007 - 2), k)) %
1000000007);
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long powmod(long long a, long long k) {
long long ap = a, ans = 1;
while (k) {
if (k & 1) {
ans *= ap;
ans %= MOD;
}
ap = ap * ap;
ap %= MOD;
k >>= 1;
}
return ans;
}
long long inv(long long a) { return powmod(a, MOD - 2); }
vector<vector<long long>> matrixmul(int l, int m, int n,
vector<vector<long long>> a,
vector<vector<long long>> b) {
vector<vector<long long>> c;
for (int i = 0; i < l; i++) {
vector<long long> v;
for (int j = 0; j < n; j++) {
long long x = 0;
for (int k = 0; k < m; k++) {
x += (a[i][k] * b[k][j]);
x %= MOD;
}
v.push_back(x);
}
c.push_back(v);
}
return c;
}
vector<vector<long long>> matrixpow(int n, vector<vector<long long>> a,
long long k) {
vector<vector<long long>> ap = a, ans;
for (int i = 0; i < n; i++) {
vector<long long> v;
for (int j = 0; j < n; j++) {
if (i == j) {
v.push_back(1);
} else {
v.push_back(0);
}
}
ans.push_back(v);
}
while (k) {
if (k & 1) ans = matrixmul(n, n, n, ap, ans);
ap = matrixmul(n, n, n, ap, ap);
k >>= 1;
}
return ans;
}
int main() {
int n;
long long k;
cin >> n >> k;
int a[101];
int c = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 1) c++;
}
if (c == 0 || c == n) {
cout << 1 << endl;
return 0;
}
int t = 0;
for (int i = n - c; i < n; i++) {
if (a[i] == 1) t++;
}
vector<vector<long long>> mat;
long long invn = inv(n * (n - 1) / 2);
for (int i = 0; i <= c; i++) {
vector<long long> v(c + 1);
mat.push_back(v);
}
for (long long i = 0; i <= c; i++) {
long long x = i * (n - 2 * c + i) * invn % MOD;
long long y = (c - i) * (c - i) * invn % MOD;
if (i > 0) mat[i - 1][i] = x;
if (i < c) mat[i + 1][i] = y;
mat[i][i] = (1 - x - y + 2 * MOD) % MOD;
}
vector<vector<long long>> mp = matrixpow(c + 1, mat, k);
cout << mp[c][t] << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
struct MATRIX {
int n;
long long a[105][105];
MATRIX(int n) : n(n) {
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j) a[i][j] = 0;
}
MATRIX operator*(const MATRIX &b) {
MATRIX c(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]) % mod;
return c;
}
};
int n, k, a[105], zero, one;
inline int nC2(int n) { return (n * (n - 1) / 2) % mod; }
MATRIX matPow(MATRIX a, int n) {
MATRIX res(zero), t = a;
for (int i = 0; i <= zero; ++i) res.a[i][i] = 1;
while (n) {
if (n & 1) res = res * t;
t = t * t;
n >>= 1;
}
return res;
}
long long pow(long long a, int n) {
long long res = 1LL, t = a;
while (n) {
if (n & 1) res = res * t % mod;
t = t * t % mod;
n >>= 1;
}
return res;
}
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];
if (a[i] == 0)
++zero;
else
++one;
}
MATRIX mat(zero);
for (int zeroA = 0; zeroA <= zero; ++zeroA) {
int oneA = zero - zeroA, zeroB = oneA, oneB = one - oneA;
if (oneB < 0) continue;
if (zeroA > 0 && oneA < one && zeroB < zero)
mat.a[zeroA - 1][zeroA] = (oneA + 1) * (zeroB + 1) % mod;
mat.a[zeroA][zeroA] =
(nC2(zero) + nC2(one) + (oneA * zeroA) + (oneB * zeroB)) % mod;
if (zeroA < zero && oneB < one)
mat.a[zeroA + 1][zeroA] = (zeroA + 1) * (oneB + 1) % mod;
}
MATRIX res = matPow(mat, k);
int zeroA = 0;
for (int i = 1; i <= zero; ++i) zeroA += (a[i] == 0);
long long sum = 0LL;
for (int j = 0; j <= zero; ++j) sum = (sum + res.a[zeroA][j]) % mod;
cout << 1LL * res.a[zeroA][zero] * pow(sum, mod - 2) % mod;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 110;
const long long mod = 1e9 + 7;
int n;
long long k;
long long x, y;
int a[maxN];
struct SquareMatrix {
SquareMatrix(int _n) : n(_n) { data.assign(_n, vector<long long>(_n, 0)); }
vector<long long> &operator[](int i) { return data[i]; }
const vector<long long> &operator[](int i) const { return data[i]; }
SquareMatrix operator*(const SquareMatrix &other) const {
assert(n == other.n);
SquareMatrix ret(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
ret[i][j] = (ret[i][j] + data[i][k] * other[k][j]) % mod;
}
}
}
return ret;
}
vector<vector<long long>> data;
int n;
};
SquareMatrix quickpower(SquareMatrix m, int p) {
int n = m.n;
SquareMatrix ret(n);
for (int i = 0; i < n; ++i) ret[i][i] = 1;
while (p) {
if (p & 1) {
ret = ret * m;
}
m = m * m;
p >>= 1;
}
return ret;
}
long long powmod(long long base, long long times) {
long long res = 1;
while (times) {
if (times % 2) res = (res * base) % mod;
times /= 2;
base = (base * base) % mod;
}
return res;
}
int main() {
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
if (a[i] == 0) x++;
y = n - x;
long long tmp0 = 0;
for (int i = 1; i <= x; i++)
if (a[i] == 0) tmp0++;
long long tmp1 = y - x + tmp0;
if (x > y) {
swap(x, y);
swap(tmp0, tmp1);
}
SquareMatrix m(x + 1);
for (int i = 0; i <= x; i++) {
m[i][i] =
x * (x - 1) / 2 + y * (y - 1) / 2 + i * (x - i) + (x - i) * (y - x + i);
if (i > 0) m[i - 1][i] = (x - i + 1) * (x - i + 1);
if (i < x) m[i + 1][i] = (i + 1) * (y - x + i + 1);
}
m = quickpower(m, k);
long long zi = m[tmp0][x];
long long mu = 0;
for (int i = 0; i <= x; i++) mu = (mu + m[tmp0][i]) % mod;
cout << (zi * powmod(mu, mod - 2)) % mod << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
template <class T>
struct Matrix {
struct VirtualVector {
vector<T> &R;
int off;
VirtualVector(vector<T> &R, const int off) : R(R), off(off) {}
T &operator[](int k) { return R[off + k]; }
};
vector<T> MAT;
int N, M;
Matrix(int N, int M) {
this->N = N;
this->M = M;
MAT = vector<T>(N * M);
}
static Matrix<T> identity(int N) {
Matrix<T> A(N, N);
for (int i = 0; i < N; ++i) A[i][i] = 1;
return A;
}
Matrix<T> operator+(Matrix<T> &M) {
Matrix<T> A(this->rows(), M.cols());
for (int i = 0; i < this->rows(); ++i) {
for (int j = 0; j < this->cols(); ++j) {
A[i][j] = ((*this)[i][j] + M[i][j]);
}
}
return A;
}
Matrix<T> operator*(Matrix<T> &MA) {
Matrix<T> A(this->rows(), MA.cols());
if (this->cols() != MA.rows()) return A;
for (int i = 0; i < this->rows(); ++i) {
for (int j = 0; j < MA.cols(); ++j) {
A[i][j] = 0;
for (int k = 0; k < this->cols(); ++k) {
A[i][j] = (A[i][j] + MAT[i * M + k] * MA[k][j]);
}
}
}
return A;
}
Matrix<T> operator*(int &n) {
Matrix<T> A(this->rows(), this->cols());
for (int i = 0; i < this->rows(); ++i) {
for (int j = 0; j < this->cols(); ++j) {
A[i][j] = (1LL * n * (*this)[i][j]);
}
}
return A;
}
Matrix<T> pow(int n) {
Matrix<T> A = identity((*this).rows());
for (int b = (1 << 30); b >= 1; b >>= 1) {
A = A * A;
if (b & n) A = A * (*this);
}
return A;
}
VirtualVector operator[](int i) { return VirtualVector(MAT, i * M); }
int rows() { return N; }
int cols() { return M; }
string toString() {
string ans = "{\n";
for (int i = 0; i < this->rows(); ++i) {
ans += "[";
for (int j = 0; j < this->cols(); ++j) {
stringstream st;
string app = (j == 0 ? "" : " ");
st << (*this)[i][j];
app += st.str();
ans += app;
}
ans += "]\n";
}
ans += "}";
return ans;
}
};
template <long long MOD>
struct ModInt {
int n;
ModInt(const ModInt<MOD> &v) : n(v.n) {}
ModInt() : n(0) {}
ModInt(long long nn) {
if (nn < -MOD || nn >= MOD) nn %= MOD;
if (nn < 0) nn += MOD;
n = nn;
}
ModInt<MOD> operator+(const ModInt<MOD> &M) const {
int r = (n + M.n);
if (r >= MOD) r -= MOD;
return ModInt::safe(r);
}
ModInt<MOD> operator-(const ModInt<MOD> &M) const {
int r = (n - M.n);
if (r < 0) r += MOD;
return ModInt::safe(r);
}
ModInt<MOD> operator*(const ModInt<MOD> &M) const {
return ModInt::safe(((long long)n * M.n) % MOD);
}
ModInt<MOD> operator+=(const ModInt<MOD> &M) {
return ModInt::safe(n = ((*this) + (M)).n);
}
ModInt<MOD> operator-=(const ModInt<MOD> &M) {
return ModInt::safe(n = ((*this) - (M)).n);
}
ModInt<MOD> operator/(const ModInt<MOD> &B) const {
long long a = B.n, b = MOD;
long long r = a, o_r = b;
long long s = 0, o_s = 1;
long long t = 1, o_t = 0;
while (r != 0) {
long long q = o_r / r;
long long tem;
tem = r;
r = o_r - r * q;
o_r = tem;
tem = o_s;
o_s = o_s - s * q;
o_s = tem;
tem = t;
t = o_t - t * q;
o_t = tem;
}
return (*this) * ModInt(o_t);
}
ModInt<MOD> power(const ModInt<MOD - 1> &B) {
if (B.n == 0) return 1;
ModInt<MOD> sq = power(B.n / 2);
sq = sq * sq;
if (B.n & 1) sq = sq * (*this);
return sq;
}
inline static ModInt<MOD> safe(long long n) {
ModInt<MOD> m;
m.n = n;
return m;
}
friend ostream &operator<<(ostream &os, const ModInt &anInt) {
os << "n: " << anInt.n;
return os;
}
};
int main() {
int N, K;
cin >> N >> K;
vector<int> V(N);
for (int i = 0; i < N; ++i) {
cin >> V[i];
}
int L = count(V.begin(), V.end(), 0);
int R = count(V.begin(), V.end(), 1);
int X = count(V.begin(), V.begin() + L, 1);
Matrix<ModInt<1000000007> > MAT(min(L, R) + 1, min(L, R) + 1);
for (int onesLeft = 0; onesLeft <= min(L, R); ++onesLeft) {
ModInt<1000000007> tot = N * (N - 1) / 2;
ModInt<1000000007> probInc = ModInt<1000000007>(L - onesLeft) *
ModInt<1000000007>(R - onesLeft) / tot;
ModInt<1000000007> probDec =
ModInt<1000000007>(onesLeft) * ModInt<1000000007>(onesLeft) / tot;
ModInt<1000000007> probSame = ModInt<1000000007>(1) - probInc - probDec;
if (onesLeft < L && onesLeft < R) {
MAT[onesLeft + 1][onesLeft] = probInc;
}
if (onesLeft > 0) {
MAT[onesLeft - 1][onesLeft] = probDec;
}
MAT[onesLeft][onesLeft] = probSame;
}
Matrix<ModInt<1000000007> > ST(min(L, R) + 1, 1);
ST[X][0] = 1;
cout << (MAT.pow(K) * ST)[0][0].n << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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 < (MOD << 1))
return n - MOD;
else if (n < (MOD << 1) + MOD)
return n - (MOD << 1);
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';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, k, a[105], sum, Q;
struct Mat {
int s[105][105];
Mat() { memset(s, 0, sizeof s); }
Mat operator*(const Mat &B) {
Mat ret;
for (int k = 0; k <= sum; k++)
for (int i = 0; i <= sum; i++)
if (s[i][k])
for (int j = 0; j <= sum; j++)
if (B.s[k][j])
ret.s[i][j] = (ret.s[i][j] + 1ll * s[i][k] * B.s[k][j]) % mod;
return ret;
}
} f, g;
inline int ksm(int a, int b) {
int s = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) s = 1ll * s * a % mod;
return s;
}
int main() {
scanf("%d%d", &n, &k), Q = 1ll * (n - 1) * n / 2 % mod;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += (a[i] == 0);
int tmp = 0;
for (int i = 1; i <= sum; i++) tmp += (a[i] == 0);
f.s[0][tmp] = 1;
for (int i = 0, x, y; i <= sum; i++) {
x = y = 0;
if (i) g.s[i][i - 1] = x = 1ll * i * (n - sum - (sum - i)) % mod;
if (i < sum) g.s[i][i + 1] = y = 1ll * (sum - i) * (sum - i) % mod;
g.s[i][i] = (Q - x - y + mod) % mod;
}
for (tmp = k; tmp; tmp >>= 1, g = g * g)
if (tmp & 1) f = f * g;
printf("%d\n", 1ll * f.s[0][sum] * ksm(ksm(Q, k), mod - 2) % mod);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 102;
long long int mod = 1e9 + 7, b[N];
struct matrix {
long long int n, m;
long long int a[N][N];
matrix(int nn, int mm) {
n = nn;
m = mm;
}
void ide() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = (i == j);
}
}
}
void emp() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = 0;
}
}
}
};
void out(matrix o) {
cout << o.n << ' ' << o.m << '\n';
for (int i = 0; i < o.n; i++) {
for (int j = 0; j < o.m; j++) {
cout << o.a[i][j] << ' ';
}
cout << '\n';
}
cout << '\n';
}
long long int wop(long long int x, long long int y) {
long long int ret = 1;
for (; y >= 1; y /= 2) {
if (y & 1) ret *= x;
x *= x;
ret %= mod;
x %= mod;
}
return ret;
}
matrix operator*(matrix A, matrix B) {
matrix ret(A.n, B.m);
ret.emp();
for (int i = 0; i < A.n; i++) {
for (int j = 0; j < B.m; j++) {
for (int k = 0; k < A.m; k++) {
ret.a[i][j] += (A.a[i][k] * B.a[k][j]) % mod;
ret.a[i][j] %= mod;
}
}
}
return ret;
}
matrix mult(matrix x, long long int k) {
matrix ret(x.n, x.m);
ret.ide();
for (; k >= 1; k /= 2) {
if (k & 1) ret = ret * x;
x = x * x;
}
return ret;
}
long long int kasr(long long int sor, long long int mag) {
return (sor * wop(mag, mod - 2)) % mod;
}
int ze = 0, on, fr;
bool not_valid(int x) { return ze - x <= on; }
long long int chos(long long int x) { return (x * (x - 1)) / 2; }
int main() {
int t, k;
cin >> t >> k;
for (int i = 0; i < t; i++) {
cin >> b[i];
ze += (b[i] == 0);
}
on = t - ze;
fr = 0;
for (int i = 0; i < ze; i++) {
fr += b[i] == 0;
}
matrix mat(ze + 1, ze + 1);
mat.emp();
for (int i = 0; i <= ze; i++) {
if (!not_valid(i)) continue;
mat.a[i][i] += kasr((i * (ze - i)) + ((ze - i) * (on - (ze - i))), chos(t));
mat.a[i][i] %= mod;
mat.a[i][i] += kasr(chos(on), chos(t));
mat.a[i][i] %= mod;
mat.a[i][i] += kasr(chos(ze), chos(t));
mat.a[i][i] %= mod;
if (i != ze) {
mat.a[i][i + 1] += kasr((ze - i) * (ze - i), chos(t));
mat.a[i][i + 1] %= mod;
}
if (i - 1 >= 0) {
mat.a[i][i - 1] += kasr((on - (ze - i)) * i, chos(t));
mat.a[i][i - 1] %= mod;
}
}
matrix ans = mult(mat, k);
cout << (ans.a[fr][ze]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
int n, k, a[102], now, o, z, t, i;
struct M {
int a[102][102];
friend M operator*(M x, M y) {
M z;
memset(z.a, 0, sizeof(z.a));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
z.a[i][j] = (z.a[i][j] + 1ll * x.a[i][k] * y.a[k][j]) % P;
return z;
}
friend M operator^(M x, int y) {
M z;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) z.a[i][j] = i == j;
for (; y; y >>= 1, x = x * x)
if (y & 1) z = z * x;
return z;
}
} A;
int pw(int x, int y) {
int z = 1;
for (; y; y >>= 1, x = 1ll * x * x % P)
if (y & 1) z = 1ll * z * x % P;
return z;
}
int main() {
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) scanf("%d", &a[i]), z += !a[i];
o = n - z;
for (i = 1; i <= z; i++) now += !a[i];
for (i = 0; i <= z; i++) {
A.a[i][i] =
z * (z - 1) / 2 + o * (o - 1) / 2 + i * (z - i) + (z - i) * (o - z + i);
if (i < z) A.a[i][i + 1] = (z - i) * (z - i);
if (i) A.a[i][i - 1] = i * (o - z + i);
}
t = pw(pw(n * (n - 1) / 2, k), P - 2);
n = z + 1, A = A ^ k;
printf("%I64d", 1ll * A.a[now][z] * t % P);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
long long chu(long long a, long long b) {
long long res = 1, p = 1000000007 - 2;
while (p) {
if (p & 1) {
res = res * b % 1000000007;
}
b = b * b % 1000000007;
p >>= 1;
}
return a * res % 1000000007;
}
int n, x, y, i, j, k, ac, wa, n0, n1, sb[666], nn;
long long res, jb;
struct Mat {
long long mat[55][55] = {0};
void print() {
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++) {
printf("%lld ", mat[i][j]);
}
puts("");
}
puts("");
}
} a, ans;
Mat operator*(Mat A, Mat B) {
Mat temp;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
temp.mat[i][j] =
(temp.mat[i][j] + A.mat[i][k] * B.mat[k][j]) % 1000000007;
}
}
}
return temp;
}
Mat operator^(Mat A, int k) {
Mat base;
for (int i = 0; i <= n; i++) base.mat[i][i] = 1;
while (k) {
if (k & 1) base = base * A;
A = A * A;
k = k >> 1;
}
return base;
}
int main() {
scanf("%d%d", &n, &k);
jb = chu(1, n * (n - 1) / 2);
for (i = 1; i <= n; i++) {
scanf("%d", &sb[i]);
if (sb[i]) {
n1++;
} else {
n0++;
}
}
for (i = 1; i <= n0; i++) {
if (sb[i]) {
wa++;
} else {
ac++;
}
}
for (i = n0 + 1; i <= n; i++) {
if (sb[i]) {
ac++;
} else {
wa++;
}
}
ac /= 2;
wa /= 2;
n /= 2;
ans.mat[0][wa] = 1;
a.mat[0][1] = n0 * n1 * jb % 1000000007;
a.mat[0][0] = (1 - a.mat[0][1] + 1000000007) % 1000000007;
for (i = 1; i <= n - 1; i++) {
nn++;
n0 = max(0, n0 - 1);
n1 = max(0, n1 - 1);
a.mat[i][i - 1] = nn * nn * jb % 1000000007;
a.mat[i][i + 1] = n0 * n1 * jb % 1000000007;
a.mat[i][i] =
(1 - a.mat[i][i + 1] - a.mat[i][i - 1] + 1000000007 + 1000000007) %
1000000007;
}
nn++;
n0 = max(0, n0 - 1);
n1 = max(0, n1 - 1);
a.mat[n][n - 1] = nn * nn * jb % 1000000007;
a.mat[n][n] = (1 - a.mat[n][n - 1] + 1000000007) % 1000000007;
a = a ^ k;
ans = ans * a;
printf("%lld", ans.mat[0][0]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
long long int n, arr[105], hell = pow(10, 9) + 7;
long long int add(long long int a, long long int b) {
a += b;
if (a > hell) a -= hell;
return a;
}
long long int pw(long long int x, long long int y) {
long long int res = 1;
x %= hell;
while (y > 0) {
if (y & 1) res = (res * x) % hell;
y >>= 1;
x = (x * x) % hell;
}
return res;
}
void mul(vector<vector<long long int>> &a, vector<vector<long long int>> &b) {
long long int temp[n][n];
for (long long int i = 0; i < n; i++)
for (long long int j = 0; j < n; j++) {
temp[i][j] = 0;
for (long long int k = 0; k < n; k++)
temp[i][j] = add(temp[i][j], (a[i][k] * b[k][j]) % hell);
}
for (long long int i = 0; i < n; i++)
for (long long int j = 0; j < n; j++) a[i][j] = temp[i][j];
}
void exp(long long int y, vector<vector<long long int>> &res,
vector<vector<long long int>> &mat) {
while (y > 0) {
if (y & 1) mul(res, mat);
y >>= 1;
mul(mat, mat);
}
}
void solve() {
long long int k, cnt = 0;
cin >> n >> k;
for (long long int i = 1; i <= n; i++) {
cin >> arr[i];
if (!arr[i]) cnt++;
}
long long int inv = pw(n * (n - 1) / 2, hell - 2);
vector<vector<long long int>> res(n + 5, vector<long long int>(n + 5, 0));
vector<vector<long long int>> mat(n + 5, vector<long long int>(n + 5, 0));
for (long long int i = 0; i < n; i++) res[i][i] = 1;
for (long long int i = 0; i <= min(cnt, n - cnt); i++) {
if (i > 0) mat[i - 1][i] = (i * i * inv) % hell;
mat[i][i] =
(((cnt * (cnt - 1) + (n - cnt) * (n - cnt - 1)) / 2 + i * (n - 2 * i)) *
inv) %
hell;
mat[i + 1][i] = ((cnt - i) * (n - cnt - i) * inv) % hell;
}
exp(k, res, mat);
long long int tr = 0;
for (long long int i = 1; i <= cnt; i++)
if (arr[i]) tr++;
cout << res[0][tr];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int qc = 1;
for (long long int i = 1; i <= qc; i++) solve();
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int n, k, zero, one, cnt;
int a[105];
long long A[105][105], A_k[105][105];
void mult(long long M[105][105], long long N[105][105]) {
long long ret[105][105];
memset(ret, 0, sizeof ret);
for (int i = 0; i <= zero; ++i)
for (int j = 0; j <= zero; ++j)
if (M[i][j])
for (int k = 0; k <= zero; ++k)
ret[i][k] = (ret[i][k] + M[i][j] * N[j][k] % mod) % mod;
for (int i = 0; i <= zero; ++i)
for (int j = 0; j <= zero; ++j) M[i][j] = ret[i][j];
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i])
one++;
else
zero++;
}
if (zero == 0 || one == 0) {
cout << 1;
return 0;
}
for (int i = 0; i < zero; ++i)
if (a[i] == 0) cnt++;
;
for (int i = max(zero - one, 0); i <= zero; ++i) {
A[i][i] = zero * (zero - 1) / 2 + one * (one - 1) / 2 + i * (zero - i) +
(zero - i) * (one - zero + i);
if (i) A[i][i - 1] = (zero - i + 1) * (zero - i + 1);
if (zero - i) A[i][i + 1] = (i + 1) * (one - zero + i + 1);
}
for (int i = 0; i <= zero; ++i) A_k[i][i] = 1;
for (; k; k >>= 1) {
if (k & 1) mult(A_k, A);
mult(A, A);
}
long long ans = A_k[zero][cnt], down = 0;
for (int i = 0; i <= zero; ++i) down = (down + A_k[i][cnt]) % mod;
for (long long pow = mod - 2; pow; pow >>= 1) {
if (pow & 1) ans = ans * down % mod;
down = down * down % mod;
}
cout << ans;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
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;
mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7;
const int seed = 233;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int max_n = 100005;
namespace {
inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
inline int Sub(int x, int y) { return (x -= y) < 0 ? x + mod : x; }
inline int Mul(int x, int y) { return 1ll * x * y % mod; }
inline int Pow(int x, int y = mod - 2) {
int res = 1;
while (y) {
if (y & 1) res = 1ll * res * x % mod;
x = 1ll * x * x % mod;
y >>= 1;
}
return res;
}
} // namespace
struct mat {
int n, m;
int a[105][105];
mat(int _n = 0, int _m = 0) {
n = _n, m = _m;
memset(a, 0, sizeof(a));
}
mat operator*(mat b) {
mat c(n, b.m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < b.m; j++) {
c.a[i][j] = 0;
for (int k = 0; k < m; k++) {
c.a[i][j] = Add(c.a[i][j], Mul(a[i][k], b.a[k][j]));
}
}
}
return c;
}
void unit() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) a[i][j] = (i == j);
}
}
};
mat quick_pow(mat a, long long n) {
mat c(a.n, a.m);
c.unit();
while (n) {
if (n & 1) c = c * a;
a = a * a;
n >>= 1;
}
return c;
}
int C(int n) { return 1ll * n * (n - 1) / 2 % mod; }
int n, k;
int a[max_n];
int c[2];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", a + i), c[a[i]]++;
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 0 && i <= c[0]) cnt++;
if (a[i] == 1 && i > c[0]) cnt++;
}
mat s(n + 1, 1);
s.a[(n - cnt) / 2][0] = 1;
mat A(n + 1, n + 1);
for (int i = 0; i <= n; i++) {
if (i)
A.a[i][i - 1] =
1ll * (c[0] + mod - (i - 1)) * (c[1] + mod - (i - 1)) % mod;
if (i != n) A.a[i][i + 1] = (i + 1) * (i + 1);
A.a[i][i] = C(n) - i * i - (c[0] - i) * (c[1] - i);
A.a[i][i] += mod, A.a[i][i] %= mod;
}
A = quick_pow(A, k);
s = A * s;
long long y = Pow(C(n), k);
y = Pow(y);
y = 1ll * y * s.a[0][0] % mod;
printf("%lld\n", y);
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <class T>
inline bool ckmin(T &a, T b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool ckmax(T &a, T b) {
return b > a ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 102;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
int fact[mxN];
int add(int x, int y) {
x += y;
while (x >= MOD) x -= MOD;
while (x < 0) x += MOD;
return x;
}
void ad(int &x, int y) { x = add(x, y); }
int sub(int x, int y) {
x -= y;
while (x >= MOD) x -= MOD;
while (x < 0) x += MOD;
return x;
}
void sb(int &x, int y) { x = sub(x, y); }
int mul(int x, int y) { return ((int64_t)x * (int64_t)y) % MOD; }
void ml(int &x, int y) { x = mul(x, y); }
int binpow(int x, int y) {
int z = 1;
while (y > 0) {
if (y % 2 == 1) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
int inv(int x) { return binpow(x, MOD - 2); }
int divide(int x, int y) { return mul(x, inv(y)); }
void precalc() {
fact[0] = 1;
for (int i = 1; i < mxN; i++) fact[i] = mul(i, fact[i - 1]);
}
int choose(int n, int k) {
if (k > n) return 0;
return divide(fact[n], mul(fact[n - k], fact[k]));
}
template <class T, int N>
struct Mat {
array<array<T, N>, N> d{};
Mat operator*(const Mat &m) const {
Mat a;
for (int i = (0); i < (N); i++)
for (int j = (0); j < (N); j++)
for (int k = (0); k < (N); k++) {
ad(a.d[i][j], mul(d[i][k], m.d[k][j]));
}
return a;
};
vector<T> operator*(const vector<T> &vec) {
vector<T> ret(N);
for (int i = (0); i < (N); i++)
for (int j = (0); j < (N); j++) ret[i] += d[i][j] * vec[j];
return ret;
};
Mat operator^(ll p) {
Mat a, b(*this);
for (int i = (0); i < (N); i++) a.d[i][i] = 1;
while (p) {
if (p & 1) a = a * b;
b = b * b;
p /= 2;
}
return a;
};
};
int n, k;
int a[mxN];
int cnt[2];
Mat<int, mxN> M;
int cnt0;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = (0); i < (n); i++) {
cin >> a[i];
cnt[a[i]]++;
}
for (int i = (0); i < (cnt[0]); i++) {
if (a[i] == 0) cnt0++;
}
for (int i = (0); i < (cnt[0] + 1); i++) {
for (int j = (0); j < (cnt[0] + 1); j++) {
int c0 = i;
int d0 = cnt[0] - c0;
int c1 = cnt[0] - i;
int d1 = cnt[1] - c1;
if (min({c0, d0, c1, d1}) < 0) continue;
if (i == j) {
M.d[i][j] = c0 * d0 + c1 * d1 + cnt[0] * (cnt[0] - 1) / 2 +
(n - cnt[0]) * (n - cnt[0] - 1) / 2;
} else if (i + 1 == j) {
M.d[i][j] = c1 * d0;
} else if (i - 1 == j) {
M.d[i][j] = c0 * d1;
}
}
}
Mat<int, mxN> res = M ^ k;
int tot = 0;
for (int i = (0); i < (n + 1); i++) {
ad(tot, res.d[cnt0][i]);
}
if (tot == 0) {
cout << 0 << nl;
} else {
cout << divide(res.d[cnt0][cnt[0]], tot) << nl;
}
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const double eps = (double)1e-8;
const int maxn = (int)2e5 + 20;
int n, m;
int a[105];
struct mat {
int n;
int a[105][105];
mat operator*(const mat &t) const {
mat res;
res.n = n;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) res.a[i][j] = 0;
for (int i = 0; i <= n; i++)
for (int k = 0; k <= n; k++)
if (a[i][k])
for (int j = 0; j <= n; j++)
if (t.a[k][j])
res.a[i][j] =
((long long)a[i][k] * t.a[k][j] + res.a[i][j]) % MOD;
return res;
}
};
mat fp(mat a, int n) {
mat res;
res.n = a.n;
for (int i = 0; i <= res.n; i++)
for (int j = 0; j <= res.n; j++) res.a[i][j] = 0;
for (int i = 0; i <= res.n; i++) res.a[i][i] = 1;
while (n) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
int fp(int a, int n) {
int res = 1;
while (n) {
if (n & 1) res = ((long long)res * a) % MOD;
a = ((long long)a * a) % MOD;
n >>= 1;
}
return res;
}
void work() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
int t = 0;
for (int i = 1; i <= n; i++) t += !a[i];
int s = 0;
for (int i = 1; i <= t; i++) s += !a[i];
mat base;
base.n = t;
for (int i = 0; i <= t; i++)
for (int j = 0; j <= t; j++) base.a[i][j] = 0;
for (int i = 0; i <= t; i++) {
int a, b, c, d;
a = i;
b = t - a;
c = b;
d = n - a - b - c;
base.a[i][i] = t * (t - 1) / 2 + (n - t) * (n - t - 1) / 2 + a * c + b * d;
if (i != 0) base.a[i][i - 1] = a * d;
if (i != t) base.a[i][i + 1] = b * c;
}
mat ans = fp(base, m);
cout << ((long long)ans.a[s][t] * fp(fp(n * (n - 1) / 2, MOD - 2), m)) % MOD
<< endl;
}
int main() {
int tc = 1;
for (int ca = 1; ca <= tc; ca++) {
work();
}
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
long long ret = 1;
for (; b; b >>= 1, a = (a * a) % MMM)
if (b & 1) ret = (ret * a) % MMM;
return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
if (a == 0 || b == 0) return a + b;
return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int ddx[] = {-1, -2, 1, -2, 2, -1, 2, 1}, ddy[] = {-2, -1, -2, 1, -1, 2, 1, 2};
vector<vector<long long> > operator*(const vector<vector<long long> >& A,
const vector<vector<long long> >& B) {
int n = A.size(), m = A[0].size();
int N = B.size(), M = B[0].size();
vector<vector<long long> > C =
vector<vector<long long> >(n, vector<long long>(M));
if (m != N) return C;
for (int(i) = (0); (i) <= (n - 1); (i) += (1)) {
for (int(j) = (0); (j) <= (M - 1); (j) += (1)) {
for (int(k) = (0); (k) <= (m - 1); (k) += (1))
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
}
}
return C;
}
vector<vector<long long> > mul(vector<vector<long long> >& A, int k) {
if (k == 1) return A;
if (k & 1) return mul(A, k - 1) * A;
vector<vector<long long> > t = mul(A, k / 2);
return t * t;
}
int c0, c1;
int a[100];
int n, K;
int main() {
scanf("%d%d", &n, &K);
for (int(i) = (0); (i) <= (n - 1); (i) += (1)) scanf("%d", a + i);
for (int(i) = (0); (i) <= (n - 1); (i) += (1)) {
if (a[i] == 0)
c0++;
else
c1++;
}
long long p, q = n * (n - 1) / 2;
q = POW(q, K);
int T = min(c0, c1);
vector<vector<long long> > A =
vector<vector<long long> >(T + 1, vector<long long>(T + 1));
for (int(k) = (0); (k) <= (T); (k) += (1)) {
if (k < T) A[k][k + 1] = (c0 - k) * (c1 - k);
A[k][k] =
(c0 - k) * k + (c1 - k) * k + c0 * (c0 - 1) / 2 + c1 * (c1 - 1) / 2;
if (k > 0) A[k][k - 1] = k * k;
}
A = mul(A, K);
int k = 0;
for (int(i) = (0); (i) <= (c0 - 1); (i) += (1))
if (a[i] == 1) k++;
p = A[k][0];
printf("%lld\n", (p * POW(q, MOD - 2)) % MOD);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
long long n, k;
int arr[100];
long long len = 0;
long long us(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) {
ans *= a;
ans %= 1000000007;
}
b >>= 1;
a *= a;
a %= 1000000007;
}
return ans;
}
long long md(long long a) {
return ((a % 1000000007) + 1000000007) % 1000000007;
}
long long fa[101], fb[101], fc[101];
long long solarr[103];
typedef struct mtr {
long long mat[103][103];
} mtr;
mtr *nmat;
long long f(int num0, int remk) {
if (remk == 0) return num0 == len;
long long ans = 0;
ans += (fa[num0] * f(num0, remk - 1)) % 1000000007;
ans %= 1000000007;
ans += (fb[num0] * f(num0 - 1, remk - 1)) % 1000000007;
ans %= 1000000007;
ans += (fc[num0] * f(num0 + 1, remk - 1)) % 1000000007;
ans %= 1000000007;
return ans;
}
void carp(mtr *mat1, mtr *mat2) {
mtr *tmp = (mtr *)malloc(sizeof(mtr));
for (int i = 0; i < n + 3; i++) {
for (int j = 0; j < n + 3; j++) {
long long top = 0;
for (int k = 0; k < n + 3; k++) {
top += (mat1->mat[i][k] * mat2->mat[k][j]) % 1000000007;
top %= 1000000007;
}
tmp->mat[i][j] = top;
}
}
for (int i = 0; i < n + 3; i++) {
for (int j = 0; j < n + 3; j++) {
mat1->mat[i][j] = tmp->mat[i][j];
}
}
free(tmp);
}
void matexp(mtr *mat, long long us) {
mtr *tmp = (mtr *)malloc(sizeof(mtr));
for (int i = 0; i < n + 3; i++) {
for (int j = 0; j < n + 3; j++) {
tmp->mat[i][j] = i == j;
}
}
while (us) {
if (us & 1) {
carp(tmp, mat);
}
us >>= 1;
carp(mat, mat);
}
for (int i = 0; i < n + 3; i++) {
for (int j = 0; j < n + 3; j++) {
mat->mat[i][j] = tmp->mat[i][j];
}
}
free(tmp);
}
void printmat(mtr *mat) {
for (int i = 0; i < n + 3; i++) {
for (int j = 0; j < n + 3; j++) {
cout << nmat->mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == 0) len++;
}
for (int i = 0; i <= n; i++) {
long long l0 = i;
long long r0 = len - i;
long long l1 = len - i;
long long r1 = n - len - len + i;
if (l0 < 0 || r0 < 0 || l1 < 0 || r1 < 0) {
fa[i] = 0;
fb[i] = 0;
fc[i] = 0;
continue;
}
long long a = ((md(md(len * (len - 1)) * 500000004) +
md(md((n - len) * (n - len - 1)) * 500000004)) %
1000000007 +
md(r0 * l0) + md(r1 * l1)) %
1000000007;
long long b = ((i) * (n - len - len + i)) % 1000000007;
long long c = ((len - i) * (len - i)) % 1000000007;
fa[i] = a;
fb[i] = b;
fc[i] = c;
}
nmat = (mtr *)malloc(sizeof(mtr));
for (int i = 0; i < n + 3; i++) {
for (int j = 0; j < n + 3; j++) {
if (i == 0)
nmat->mat[i][j] = 0;
else if (i == n + 2)
nmat->mat[i][j] = 0;
else {
long long num = n + 1 - i;
if (num + j == n)
nmat->mat[i][j] = fc[num];
else if (num + j == n + 1)
nmat->mat[i][j] = fa[num];
else if (num + j == n + 2)
nmat->mat[i][j] = fb[num];
else
nmat->mat[i][j] = 0;
}
}
}
matexp(nmat, k % 1000000007);
solarr[0] = 0;
solarr[n + 2] = 0;
for (int i = 1; i < n + 2; i++) {
long long num = n + 1 - i;
solarr[i] = (num == len);
}
long long cur0 = 0;
for (int i = 0; i < len; i++)
if (arr[i] == 0) cur0++;
long long ind = n + 1 - cur0;
long long pay = 0;
for (int j = 0; j < n + 3; j++) {
pay += (nmat->mat[ind][j] * solarr[j]) % 1000000007;
pay %= 1000000007;
}
long long payda = us((n * (n - 1)) / 2, k % 1000000007);
pay *= us(payda, 1000000007 - 2);
pay %= 1000000007;
cout << pay << endl;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, k, sum;
int a[105];
struct matrix {
long long a[105][105];
int n;
matrix(int n = 0) : n(n) { memset(a, 0, sizeof(a)); }
void init() {
for (int i = 0; i <= n; ++i) a[i][i] = 1;
}
matrix operator*(matrix b) const {
matrix c(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] % mod) % mod;
return c;
}
} trans, base;
long long power(long long a, int b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
matrix power(matrix a, int b) {
matrix res(a.n);
res.init();
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int read() {
char c = getchar();
int x = 0;
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x;
}
void build() {
for (int j = 0; j <= sum; ++j) {
if (j) trans.a[j - 1][j] = 1ll * (sum - j + 1) * (sum - j + 1) % mod;
if (j != sum)
trans.a[j + 1][j] = 1ll * (j + 1) * (n - 2 * sum + j + 1) % mod;
trans.a[j][j] =
(1ll * j * (sum - j) % mod + 1ll * (sum - j) * (n - 2 * sum + j) % mod +
1ll * sum * (sum - 1) % mod * power(2, mod - 2) % mod +
1ll * (n - sum) * (n - sum - 1) % mod * power(2, mod - 2) % mod) %
mod;
}
}
int main() {
n = read();
k = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
sum += !a[i];
}
int total = 0;
for (int i = 1; i <= sum; ++i) total += !a[i];
base = trans = matrix(sum);
build();
base.a[0][total] = 1;
base = base * power(trans, k);
printf("%I64d\n",
base.a[0][sum] * power(power(n * (n - 1) / 2, mod - 2), k) % mod);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& s, vector<T> const& v) {
s << '{';
for (int i = 0; i < int(v.size()); i++) s << (i ? "," : "") << v[i];
return s << '}';
}
template <typename S, typename T>
ostream& operator<<(ostream& s, pair<S, T> const& p) {
return s << '(' << p.first << ',' << p.second << ')';
}
constexpr long long MOD = 1000000007;
long long ch2(long long n) { return ((n * (n - 1)) / 2) % MOD; }
struct MAT {
long long A[51][51] = {};
};
MAT operator*(MAT const& m1, MAT const& m2) {
MAT m;
for (int i = 0; i < int(51); i++)
for (int j = 0; j < int(51); j++)
for (int k = 0; k < int(51); k++) {
m.A[i][j] += m1.A[i][k] * m2.A[k][j];
m.A[i][j] %= MOD;
}
return m;
}
MAT identity() {
MAT m;
for (int i = 0; i < int(51); i++) m.A[i][i] = 1;
return m;
}
long long gx, gy, gd;
void gcd(long long a, long long b) {
if (b == 0) {
gx = 1;
gy = 0;
gd = a;
return;
}
gcd(b, a % b);
gx -= gy * (a / b);
swap(gx, gy);
}
long long modinv(long long x) {
gcd(x, MOD);
return gx % MOD;
}
int main() {
int N, K, N0 = 0, N1 = 0;
scanf("%d%d", &N, &K);
bool A[100] = {};
for (int n = 0; n < int(N); n++) {
char c;
scanf(" %c", &c);
A[n] = (c == '1');
if (A[n])
++N1;
else
++N0;
}
MAT m;
int MI = min(N0, N1);
long long ssame = (ch2(N0) + ch2(N1)) % MOD;
for (int i = 0; i < int(MI + 1); i++) {
long long& ss = m.A[i][i];
ss += ssame;
if (i > 0) {
m.A[i - 1][i] += (i * i) % MOD;
ss = (ss + i * (N0 - i)) % MOD;
ss = (ss + i * (N1 - i)) % MOD;
}
if (i < MI) {
m.A[i + 1][i] += ((N0 - i) * (N1 - i)) % MOD;
}
}
int ep = K;
MAT ans = identity();
while (ep) {
if (ep % 2) {
ans = ans * m;
ep--;
}
m = m * m;
ep /= 2;
}
int istate = 0;
for (int n = 0; n < int(N0); n++)
if (A[n]) ++istate;
long long ians = ans.A[0][istate];
long long den = 0;
for (int i = 0; i < int(MI + 1); i++) den = (den + ans.A[i][istate]) % MOD;
long long qans = ians * modinv(den);
qans = (qans % MOD + MOD) % MOD;
printf("%d\n", int(qans));
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
struct matrix {
vector<vector<ll>> mat;
matrix() {}
matrix(int n, ll v) : mat(n, vector<ll>(n)) {
for (int i = 0; i < n; ++i) mat[i][i] = v;
}
void resize(int n) {
mat.resize(n);
for (auto &it : mat) it.resize(n);
}
};
int n, k;
vector<int> a;
ll ones, zeros;
int min_in_place;
vector<int> decrease, increase, notchange;
int mat_size;
matrix base_mat;
ll mod_exp(ll b, ll e) {
ll res = 1;
while (e) {
if (e & 1) res = (res * b) % MOD;
b = (b * b) % MOD;
e >>= 1;
}
return res;
}
matrix mat_mul(matrix a, matrix b) {
matrix res(mat_size, 0);
for (int i = 0; i < mat_size; ++i) {
for (int j = 0; j < mat_size; ++j) {
for (int k = 0; k < mat_size; ++k) {
res.mat[i][j] = (res.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
}
}
}
return res;
}
matrix mat_exp(matrix b, ll e) {
matrix res(mat_size, 1);
while (e) {
if (e & 1) res = mat_mul(res, b);
b = mat_mul(b, b);
e >>= 1;
}
return res;
}
void build() {
increase.resize(ones + 1);
decrease.resize(ones + 1);
notchange.resize(ones + 1);
min_in_place = max(0, 2 * (int)ones - n);
mat_size = ones - min_in_place + 1;
base_mat.resize(mat_size);
for (int state = min_in_place; state <= ones; ++state) {
ll inv_nn1 = (mod_exp(n, MOD - 2) * mod_exp(n - 1, MOD - 2)) % MOD;
base_mat.mat[state - min_in_place][state - min_in_place] =
((ones * (ones - 1 + MOD) + zeros * (zeros - 1 + MOD) +
2 * state * (ones - state) +
2 * (ones - state) * (zeros - (ones - state) + MOD)) %
MOD) *
inv_nn1 % MOD;
if (state > min_in_place) {
base_mat.mat[state - min_in_place - 1][state - min_in_place] =
(2 * state * (zeros - (ones - state) + MOD) % MOD) * inv_nn1 % MOD;
}
if (state < ones) {
base_mat.mat[state - min_in_place + 1][state - min_in_place] =
2 * (ones - state) * (ones - state) * inv_nn1 % MOD;
}
}
}
int main() {
cin >> n >> k;
a.resize(n);
for (auto &it : a) {
cin >> it;
if (it)
++ones;
else
++zeros;
}
build();
matrix final_mat = mat_exp(base_mat, k);
int initial_state = 0;
for (int i = n - ones; i < n; ++i) initial_state += a[i];
cout << final_mat.mat[ones - min_in_place][initial_state - min_in_place]
<< "\n";
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MODULO = 1000000007;
int gcd(int a, int b, int& ax, int& bx) {
if (b == 0) {
ax = 1;
bx = 0;
return b;
}
int g = gcd(b, a % b, bx, ax);
bx = (bx - (long long)(a / b) * ax % MODULO + MODULO) % MODULO;
return g;
}
int inv(int x) {
int ax, bx;
gcd(x, MODULO, ax, bx);
return ax;
}
vector<vector<int>> matmul(const vector<vector<int>>& a,
const vector<vector<int>>& b) {
int n = (int)a.size();
vector<vector<int>> re(n, vector<int>(n));
for (int i = 0; i < n; ++i)
for (int k = 0; k < n; ++k) {
int tmp = 0;
for (int j = 0; j < n; ++j) {
tmp = (tmp + a[i][j] * (long long)b[j][k] % MODULO) % MODULO;
}
re[i][k] = tmp;
}
return re;
}
vector<vector<int>> pow(const vector<vector<int>>& a, int k) {
if (k == 1) {
return a;
}
vector<vector<int>> b = pow(a, k / 2);
b = matmul(b, b);
if (k % 2 == 1) b = matmul(a, b);
return b;
}
int main() {
std::ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
int invn = inv(n);
int invnm1 = inv(n - 1);
vector<int> a(n);
vector<int> count(2);
for (int i = 0; i < n; ++i) {
cin >> a[i];
++count[a[i]];
}
int misplaced = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == 0 && i >= count[0] || a[i] == 1 && i < count[0]) {
++misplaced;
}
}
int misplaced_pair = misplaced / 2;
int m = std::min(count[0], count[1]) + 1;
vector<vector<int>> matrix(m, vector<int>(m));
for (int i = 0; i < m; ++i) {
int total = 0;
if (i - 1 >= 0) {
int cur = (long long)i * i % MODULO * 2ll % MODULO * (long long)invn %
MODULO * (long long)invnm1 % MODULO;
matrix[i][i - 1] = cur;
total = (total + cur) % MODULO;
}
if (i + 1 < m) {
int cur = (long long)(count[0] - i) * (count[1] - i) % MODULO * 2ll %
MODULO * (long long)invn % MODULO * (long long)invnm1 % MODULO;
matrix[i][i + 1] = cur;
total = (total + cur) % MODULO;
}
matrix[i][i] = (1 - total + MODULO) % MODULO;
}
vector<vector<int>> p = pow(matrix, k);
cout << p[misplaced_pair][0] << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long maxn = (long long)100 + 5;
const long long mod = (long long)1e9 + 7;
struct mat {
long long a[maxn][maxn];
mat() { memset(a, 0, sizeof(a)); }
};
long long n, k;
long long zero = 0, one = 0;
long long now = 0;
long long quick_pow_mod(long long a, long long b, long long c) {
long long res = 1;
while (b) {
if (b & 1) {
res = (res * a) % c;
}
a = (a * a) % c;
b = b >> 1;
}
return res;
}
mat mul(mat a, mat b) {
mat res;
for (long long(i) = 0; (i) <= zero; ++(i)) {
for (long long j = 0; j <= zero; ++j) {
for (long long k = 0; k <= zero; ++k) {
res.a[i][j] = (res.a[i][j] + (a.a[i][k] * b.a[k][j]) % mod) % mod;
}
}
}
return res;
}
mat qpow(mat a, long long k) {
mat res;
for (long long i = 0; i <= zero; i++) {
res.a[i][i] = 1;
}
while (k) {
if (k & 1) {
res = mul(res, a);
}
a = mul(a, a);
k >>= 1;
}
return res;
}
long long a[maxn];
mat dp;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 0)
zero++;
else
one++;
}
for (long long i = 1; i <= zero; i++) {
if (a[i] == 0) now++;
}
for (long long i = 0; i <= zero; i++) {
dp.a[i][i] = (zero * (zero - 1) / 2 + one * (one - 1) / 2 +
(zero - i) * (one - zero + i) + i * (zero - i)) %
mod;
if (i - 1 >= 0) {
dp.a[i][i - 1] = ((zero - i + 1) * (zero - i + 1)) % mod;
}
if (i + 1 <= zero) {
dp.a[i][i + 1] = ((i + 1) * (one - zero + i + 1)) % mod;
}
}
mat res = qpow(dp, k);
long long ans = 0;
ans = (res.a[zero][now] *
quick_pow_mod(quick_pow_mod(n * (n - 1) / 2, k, mod), mod - 2, mod)) %
mod;
cout << ans << '\n';
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int INF = 110;
long long zero, size;
struct Matrix {
long long g[INF][INF];
Matrix() { memset(g, 0, sizeof(g)); }
Matrix operator*(const Matrix& a) {
Matrix cur;
for (int k = 0; k < size; k++) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cur.g[i][j] = (cur.g[i][j] + g[i][k] * a.g[k][j] % mod) % mod;
}
}
}
return cur;
}
};
Matrix mat, ans;
long long c[110][110];
int a[INF];
long long quick(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
void mquick(int b) {
for (int i = 0; i < size; i++) {
ans.g[i][i] = 1;
}
while (b) {
if (b & 1) ans = ans * mat;
mat = mat * mat;
b >>= 1;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 0; i <= 100; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0)
c[i][j] = 1;
else
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (!a[i]) zero++;
}
size = zero + 1;
long long revcn2 = quick(c[n][2], mod - 2);
for (long long i = 0; i <= zero; i++) {
int j = i;
mat.g[i][j] = (c[zero][2] + c[n - zero][2] + i * (zero - i) +
(zero - i) * (n - 2 * zero + i + mod) % mod) *
revcn2 % mod;
if (i > 0) mat.g[i][i - 1] = (zero - i + 1) * (zero - i + 1) * revcn2 % mod;
if (i < zero)
mat.g[i][i + 1] = (i + 1) * (n + i + 1 - 2 * zero) * revcn2 % mod;
}
mquick(k);
long long res = 0;
int count = 0;
for (int i = 0; i < zero; i++) {
if (!a[i]) count++;
}
res = ans.g[zero][count];
cout << res;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) f = ch ^ 45, ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? x : -x;
}
int n, m, c, inv;
const int mod = 1e9 + 7;
struct mat {
int a[105][105];
inline mat operator*(const mat &t) {
mat res;
for (int i = 0; i <= c; ++i)
for (int j = 0; j <= c; ++j) {
res.a[i][j] = 0;
for (int k = 0; k <= c; ++k)
res.a[i][j] = (res.a[i][j] + 1ll * a[i][k] * t.a[k][j]) % mod;
}
return res;
}
} A, B, C, I;
int a[105];
inline int ksm(int b, int x) {
int res = 1;
while (x) {
if (x & 1) res = 1ll * res * b % mod;
b = 1ll * b * b % mod;
x >>= 1;
}
return res;
}
inline mat qpow(mat b, int x) {
mat res = I;
while (x) {
if (x & 1) res = res * b;
b = b * b;
x >>= 1;
}
return res;
}
int main() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1; i <= n; ++i) c += (!a[i]);
int cnt = 0;
for (int i = 1; i <= c; ++i) cnt += (!a[i]);
A.a[0][cnt] = 1;
int inv = ksm(n * (n - 1) >> 1, mod - 2);
for (int i = 0; i <= c; ++i) {
I.a[i][i] = 1;
if (n - 2 * c + i < 0) continue;
B.a[i][i + 1] = 1ll * (c - i) * (c - i) * inv % mod;
B.a[i][i - 1] = 1ll * i * (n - 2 * c + i) * inv % mod;
B.a[i][i] = (mod + mod + 1 - B.a[i][i + 1] - B.a[i][i - 1]) % mod;
}
A = A * qpow(B, m);
cout << A.a[0][c];
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Codeforces Round #553 (Div. 2)
Problem F. Sonya and Informatics
:author: Kitchen Tong
:mail: [email protected]
Please feel free to contact me if you have any question
regarding the implementation below.
"""
__version__ = '1.8'
__date__ = '2019-04-21'
import sys
def binom_dp():
dp = [[-1 for j in range(110)] for i in range(110)]
def calculate(n, k):
if n < k:
return 0
if n == k or k == 0:
return 1
if dp[n][k] > 0:
return dp[n][k]
else:
dp[n][k] = calculate(n-1, k-1) + calculate(n-1, k)
return dp[n][k]
return calculate
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def multiply(A, B, mod):
if not hasattr(B[0], '__len__'):
C = [sum(aij * B[j] % mod for j, aij in enumerate(ai)) for ai in A]
else:
C = [[0 for col in range(len(B[0]))] for row in range(len(A))]
len_A = len(A)
len_B = len(B)
for row in range(len_A):
if sum(A[row]) == 0:
continue
for col in range(len_B):
C[row][col] = sum(A[row][k] * B[k][col]
for k in range(len_B)) % mod
return C
def memoize(func):
memo = {}
def wrapper(*args):
M, n, mod = args
if n not in memo:
memo[n] = func(M, n, mod)
return memo[n]
return wrapper
@memoize
def matrix_pow(M, n, mod):
# print(f'n is {n}')
if n == 2:
return multiply(M, M, mod)
if n == 1:
return M
sub_M = matrix_pow(M, n//2, mod)
if n % 2 == 0:
return multiply(sub_M, sub_M, mod)
return multiply(sub_M, matrix_pow(M, n - n//2, mod), mod)
def solve(n, k, a, binom, mod):
ones = sum(a)
zeros = n - ones
M = [[0 for col in range(zeros+1)] for row in range(zeros+1)]
for row in range(max(0, zeros-ones), zeros+1):
pre_zeros = row
pre_ones = zeros - pre_zeros
post_zeros = pre_ones
post_ones = ones - pre_ones
M[row][row] = (pre_ones * post_ones + pre_zeros * post_zeros
+ binom(zeros, 2) + binom(ones, 2))
if row > max(0, zeros-ones):
M[row-1][row] = pre_zeros * post_ones
if row < zeros:
M[row+1][row] = post_zeros * pre_ones
M = [matrix_pow(M, k, mod)[-1]]
b = [0] * (zeros + 1)
b[zeros - sum(a[:zeros])] = 1
C = multiply(M, b, mod)
return C[-1]
def main(argv=None):
mod = int(1e9) + 7
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
binom = binom_dp()
P = solve(n, k, a, binom, mod)
if P == 0:
print(0)
else:
Q = pow(binom(n, 2), k, mod)
print(P * modinv(Q, mod) % mod)
return 0
if __name__ == "__main__":
STATUS = main()
sys.exit(STATUS)
| PYTHON3 |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
int NN;
struct mat {
long long a[105][105];
mat() { memset(a, 0, sizeof(a)); }
mat operator*(const mat& m1) const {
mat ret;
for (int i = 0; i <= NN; i++) {
for (int j = 0; j <= NN; j++) {
for (int k = 0; k <= NN; k++) {
ret.a[i][j] = (ret.a[i][j] + a[i][k] * m1.a[k][j] % int(1e9 + 7)) %
int(1e9 + 7);
}
}
}
return ret;
}
void print() {
cout << "======== print" << endl;
for (int i = 0; i <= NN; i++) {
for (int j = 0; j <= NN; j++) cout << a[i][j] << " ";
cout << endl;
}
}
};
mat pow(mat x, long long n) {
mat ret;
for (int i = 0; i <= NN; i++) {
ret.a[i][i] = 1;
}
while (n) {
if (n & 1) ret = ret * x;
x = x * x;
n /= 2;
}
return ret;
}
long long qpow(long long x, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans = ans * x % int(1e9 + 7);
x = x * x % int(1e9 + 7);
n /= 2;
}
return ans;
}
int N, M, K;
int a[105];
long long fac[105], ifac[105], iN2;
long long comb(long long n, long long k) {
if (n < k) return 0;
return fac[n] * ifac[n - k] % int(1e9 + 7) * ifac[k] % int(1e9 + 7);
}
void init() {
fac[0] = ifac[0] = 1;
for (int i = 1; i <= N; i++) {
fac[i] = fac[i - 1] * i % int(1e9 + 7);
ifac[i] = qpow(fac[i], int(1e9 + 7) - 2);
}
iN2 = qpow(comb(N, 2), int(1e9 + 7) - 2);
}
int main() {
ios::sync_with_stdio(0);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> a[i];
if (a[i]) ++K;
}
int k = 0;
for (int i = 1; i <= N; i++) {
if (a[i] && i >= N - K + 1) ++k;
}
NN = K;
init();
mat A, res;
for (int i = 0; i <= NN; i++) {
if (i < K)
A.a[i][i + 1] = (K - i) * (K - i) % int(1e9 + 7) * iN2 % int(1e9 + 7);
if (i > 0)
A.a[i][i - 1] =
i * max(0, N - 2 * K + i) % int(1e9 + 7) * iN2 % int(1e9 + 7);
A.a[i][i] =
(1 - A.a[i][i + 1] - (i > 0 ? A.a[i][i - 1] : 0) + 2 * int(1e9 + 7)) %
int(1e9 + 7);
}
res = pow(A, M);
long long ans = res.a[k][K];
cout << ans << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MOD = 1e9 + 7;
const int N = 100;
const int MAXN = N + 5;
int pow_mod(int x, int i) {
int res = 1;
while (i) {
if (i & 1) res = (long long)res * x % MOD;
x = (long long)x * x % MOD;
i >>= 1;
}
return res;
}
int n, k, a[MAXN], c;
struct Matrix {
int a[MAXN][MAXN];
Matrix() { memset(a, 0, sizeof(a)); }
} A, B;
Matrix operator*(Matrix X, Matrix Y) {
Matrix Z;
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= N; ++j) {
for (int k = 0; k <= N; ++k) {
Z.a[i][j] =
((long long)Z.a[i][j] + (long long)X.a[i][k] * Y.a[k][j] % MOD) %
MOD;
}
}
}
return Z;
}
Matrix matrix_pow(Matrix X, int i) {
Matrix Y;
for (int i = 0; i <= N; ++i) Y.a[i][i] = 1;
while (i) {
if (i & 1) Y = Y * X;
X = X * X;
i >>= 1;
}
return Y;
}
void Print(Matrix X) {
for (int i = 0; i <= 10; ++i) {
for (int j = 0; j <= 10; ++j) {
cout << X.a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
n = read();
k = read();
for (int i = 1; i <= n; ++i) a[i] = read(), c += (a[i] == 0);
int t = 0;
for (int i = 1; i <= c; ++i) t += (a[i] == 0);
A.a[0][t] = 1;
for (int i = 0; i <= c; ++i) {
if (i != 0) B.a[i - 1][i] = 1LL * (c - (i - 1)) * (c - (i - 1)) % MOD;
B.a[i][i] =
1LL * (1LL * i * (c - i) % MOD + 1LL * (c - i) * (n - c - c + i)) % MOD;
B.a[i][i] = (B.a[i][i] + 1LL * c * (c - 1) / 2 % MOD) % MOD;
B.a[i][i] = (B.a[i][i] + 1LL * (n - c) * (n - c - 1) / 2 % MOD) % MOD;
if (i != c) B.a[i + 1][i] = 1LL * (i + 1) * (n - c - c + i + 1) % MOD;
}
B = matrix_pow(B, k);
A = A * B;
int ans = A.a[0][c];
t = 0;
for (int i = 0; i <= c; ++i) t = ((long long)t + A.a[0][i]) % MOD;
ans = ((long long)ans * pow_mod(t, MOD - 2)) % MOD;
cout << ans << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, M = 1e9 + 7;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1), EPS = 1e-9;
const int N = 101;
long long b[N];
long long o;
int n, k;
template <class T>
T fm(T a, T b) {
return (-b < a and a < b) ? a : a % b;
}
template <int m = M>
struct modArit {
modArit(int v = 0) : v(fm(v, m)) {}
modArit(long long v) : v(fm(v, (long long)m)) {}
int v;
modArit<m> operator+(modArit<m> r) const { return modArit<m>(v + r.v); }
modArit<m> operator-(modArit<m> r) const { return modArit<m>(v - r.v); }
modArit<m> operator*(modArit<m> r) const {
return modArit<m>((long long)v * r.v);
}
modArit<m> operator/(modArit<m> r) const { return *this * inv(r); }
modArit<m> operator/(int r) const { return modArit<m>(v / r); }
};
template <class T = modArit<>>
struct Matrix {
T a[N][N] = {};
Matrix(int d = 0) {
for (int i = 0; i < n; i++) a[i][i] = d;
}
Matrix<T> operator*(const Matrix<T> r) {
Matrix<T> ans;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
ans.a[i][j] = ans.a[i][j] + a[i][k] * r.a[k][j];
return ans;
}
};
template <class T>
T fexp(T b, long long e) {
T r = 1;
while (e) {
if (e & 1) r = r * b;
b = b * b, e /= 2;
}
return r;
}
template <int m>
modArit<m> inv(modArit<m> x) {
return fexp(x, m - 2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
auto f = inv<M>((n * (n - 1) / 2));
for (int i = 0; i < n; i++) {
cin >> b[i];
if (b[i]) o++;
}
Matrix<> m;
auto t = min(n - o, o);
modArit<> nn = n, oo = o;
for (int i = 0; i <= t; i++) {
modArit<> ii = i;
m.a[i][i] = (nn * (nn - 1) / 2 - ii * ii - (oo - ii) * (nn - oo - ii)) * f;
if (i > 0) m.a[i][i - 1] = ii * ii * f;
m.a[i][i + 1] = (oo - ii) * (nn - oo - ii) * f;
}
Matrix<> a = fexp(m, k);
int id = 0;
for (int i = 0; i < n - o; i++)
if (b[i]) id++;
cout << fexp(m, k).a[id][0].v << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
struct Matrix {
long long ma[110][110];
int n;
Matrix() {}
Matrix(int _n) {
n = _n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) ma[i][j] = 0;
}
Matrix operator*(const Matrix &b) const {
Matrix ret = Matrix(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) {
ret.ma[i][j] += ma[i][k] * b.ma[k][j];
ret.ma[i][j] %= MOD;
}
return ret;
}
void set_one() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) ma[i][j] = (i == j);
}
};
void Mapow(Matrix &a, Matrix &ans, long long n) {
ans.set_one();
while (n) {
if (n & 1) ans = ans * a;
a = a * a;
n >>= 1;
}
}
long long Pow(long long base, long long n) {
long long res = 1;
while (n) {
if (n & 1) res = res * base % MOD;
base = base * base % MOD;
n >>= 1;
}
return res;
}
int a[110];
int main() {
int n, k, cnt0 = 0, cnt1 = 0, cur = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i])
cnt1++;
else
cnt0++;
}
for (int i = 1; i <= cnt0; i++)
if (a[i]) cur++;
if (cnt0 == 0 || cnt1 == 0) return 0 * printf("1\n");
int lim = min(cnt0, cnt1);
Matrix a(lim + 1), ans(lim + 1);
long long f1 = Pow((n * (n - 1)) >> 1, MOD - 2);
for (int i = 0; i <= lim; i++) {
long long tmp = 0;
if (i < lim) {
a.ma[i][i + 1] = (cnt0 - i) * (cnt1 - i) % MOD * f1 % MOD;
tmp += a.ma[i][i + 1];
}
if (i > 0) {
a.ma[i][i - 1] = i * i % MOD * f1 % MOD;
tmp += a.ma[i][i - 1];
}
tmp %= MOD;
a.ma[i][i] = (1 + MOD - tmp) % MOD;
}
Mapow(a, ans, k);
printf("%lld\n", ans.ma[cur][0]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long N = 105;
const long long mod = 1e9 + 7;
long long mul[N][N][35], dp[N], ar[N], dp1[N];
long long binpow(long long x, long long y) {
long long tich = 1;
while (y) {
if (y % 2 == 1) {
tich *= x;
tich %= mod;
}
x *= x;
x %= mod;
y >>= 1;
}
return tich;
}
long long inv(long long x) { return binpow(x, mod - 2); }
signed main() {
long long n, i, j, k, l, m, now = 0;
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> ar[i];
if (ar[i] == 0) {
now++;
}
}
j = 0;
for (i = 1; i <= now; i++) {
if (ar[i] == 0) {
j++;
}
}
dp[j] = 1;
k = (inv(n) * inv(n - 1)) % mod;
for (i = 0; i <= now; i++) {
mul[i][i + 1][0] = max(0ll, (2 * (now - i) * (now - i) * k) % mod);
mul[i][i][0] =
(((now * (now - 1) + (n - now) * (n - now - 1) + 2 * i * (now - i) +
max(0ll, 2 * (now - i) * (n + i - 2 * now))) %
mod) *
k) %
mod;
if (i) {
mul[i][i - 1][0] = max(0ll, (2 * i * (n + i - 2 * now) * k) % mod);
}
}
for (i = 1; i <= 30; i++) {
for (j = 0; j <= now; j++) {
for (k = 0; k <= now; k++) {
for (l = 0; l <= now; l++) {
mul[j][k][i] += mul[j][l][i - 1] * mul[l][k][i - 1];
mul[j][k][i] %= mod;
}
}
}
}
for (i = 30; i >= 0; i--) {
if (m >= (1 << i)) {
m -= (1 << i);
for (j = 0; j <= now; j++) {
dp1[j] = 0;
}
for (j = 0; j <= now; j++) {
for (k = 0; k <= now; k++) {
dp1[j] += (dp[k] * mul[k][j][i]);
dp1[j] %= mod;
}
}
for (j = 0; j <= now; j++) {
dp[j] = dp1[j];
}
}
}
j = 0;
for (i = 0; i <= now; i++) {
j += dp[i];
j %= mod;
}
cout << (dp[now] * inv(j)) % mod;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 105, M = 1e9 + 7;
int n, k, a[N], ze;
struct mat {
int mt[N][N];
mat() { memset(mt, 0, sizeof(mt)); }
mat operator*(const mat &t) const {
mat ans;
for (int i = 0; i <= ze; i++)
for (int j = 0; j <= ze; j++)
for (int k = 0; k <= ze; k++)
ans.mt[i][j] = (1ll * mt[i][k] * t.mt[k][j] % M + ans.mt[i][j]) % M;
return ans;
}
} beg, ope, ans;
mat qpow(mat x, int y) {
mat sum;
for (int i = 0; i <= ze; i++) sum.mt[i][i] = 1;
while (y) {
if (y & 1) sum = sum * x;
y >>= 1, x = x * x;
}
return sum;
}
int qpow(int x, int y) {
int sum = 1;
while (y) {
if (y & 1) sum = 1ll * sum * x % M;
y >>= 1, x = 1ll * x * x % M;
}
return sum;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ze += (a[i] == 0);
}
int lc = 0;
for (int i = 1; i <= ze; i++) lc += (a[i] == 0);
beg.mt[lc][0] = 1;
for (int i = 0; i <= ze; i++) {
ope.mt[i][i] = (1ll * ze * (ze - 1) / 2 +
1ll * (n - ze) * (n - ze - 1) / 2 + 1ll * i * (ze - i)) %
M;
if (i >= 2 * ze - n)
ope.mt[i][i] = (ope.mt[i][i] + 1ll * (ze - i) * (n - 2 * ze + i) % M) % M;
if (i) ope.mt[i][i - 1] = 1ll * (ze - i + 1) * (ze - i + 1) % M;
if (i != ze && n - 2 * ze + i + 1 >= 0)
ope.mt[i][i + 1] = 1ll * (i + 1) * (n - 2 * ze + i + 1) % M;
}
ans = qpow(ope, k) * beg;
int res = 0;
for (int i = 0; i <= ze; i++) res = (res + ans.mt[i][0]) % M;
res = 1ll * qpow(res, M - 2) * ans.mt[ze][0] % M;
printf("%d\n", res);
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 102;
long long int mod = 1e9 + 7, b[N];
struct matrix {
long long int n, m;
long long int a[N][N];
matrix(int nn, int mm) {
n = nn;
m = mm;
}
void ide() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = (i == j);
}
}
}
void emp() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = 0;
}
}
}
};
void out(matrix o) {
cout << o.n << ' ' << o.m << '\n';
for (int i = 0; i < o.n; i++) {
for (int j = 0; j < o.m; j++) {
cout << o.a[i][j] << ' ';
}
cout << '\n';
}
cout << '\n';
}
long long int wop(long long int x, long long int y) {
long long int ret = 1;
for (; y >= 1; y /= 2) {
if (y & 1) ret *= x;
x *= x;
ret %= mod;
x %= mod;
}
return ret;
}
matrix operator*(matrix A, matrix B) {
matrix ret(A.n, B.m);
ret.emp();
for (int i = 0; i < A.n; i++) {
for (int j = 0; j < B.m; j++) {
for (int k = 0; k < A.m; k++) {
ret.a[i][j] += (A.a[i][k] * B.a[k][j]) % mod;
ret.a[i][j] %= mod;
}
}
}
return ret;
}
matrix mult(matrix x, long long int k) {
matrix ret(x.n, x.m);
ret.ide();
for (; k >= 1; k /= 2) {
if (k & 1) ret = ret * x;
x = x * x;
}
return ret;
}
long long int kasr(long long int sor, long long int mag) {
return (sor * wop(mag, mod - 2)) % mod;
}
int ze = 0, on, fr;
bool not_valid(int x) { return ze - x <= on; }
long long int chos(long long int x) { return (x * (x - 1)) / 2; }
int main() {
int t, k;
cin >> t >> k;
for (int i = 0; i < t; i++) {
cin >> b[i];
ze += (b[i] == 0);
}
on = t - ze;
fr = 0;
for (int i = 0; i < ze; i++) {
fr += b[i] == 0;
}
matrix mat(ze + 1, ze + 1);
mat.emp();
for (int i = 0; i <= ze; i++) {
if (!not_valid(i)) continue;
mat.a[i][i] += kasr(
chos(ze) + chos(on) + (i * (ze - i)) + ((ze - i) * (on - (ze - i))),
chos(t));
mat.a[i][i] %= mod;
if (i != ze) {
mat.a[i][i + 1] += kasr((ze - i) * (ze - i), chos(t));
mat.a[i][i + 1] %= mod;
}
if (i - 1 >= 0) {
mat.a[i][i - 1] += kasr((on - (ze - i)) * i, chos(t));
mat.a[i][i - 1] %= mod;
}
}
matrix ans = mult(mat, k);
cout << (ans.a[fr][ze]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
namespace debug {
template <class T1, class T2>
void pr(const pair<T1, T2> &x);
template <class T, size_t SZ>
void pr(const array<T, SZ> &x);
template <class T>
void pr(const vector<T> &x);
template <class T>
void pr(const set<T> &x);
template <class T1, class T2>
void pr(const map<T1, T2> &x);
template <class T>
void pr(const T &x) {
cout << x;
}
template <class T, class... Ts>
void pr(const T &first, const Ts &...rest) {
pr(first), pr(rest...);
}
template <class T1, class T2>
void pr(const pair<T1, T2> &x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void prIn(const T &x) {
pr("{");
bool fst = 1;
for (auto &a : x) {
pr(fst ? "" : ", ", a), fst = 0;
}
pr("}");
}
template <class T, size_t SZ>
void pr(const array<T, SZ> &x) {
prIn(x);
}
template <class T>
void pr(const vector<T> &x) {
prIn(x);
}
template <class T>
void pr(const set<T> &x) {
prIn(x);
}
template <class T1, class T2>
void pr(const map<T1, T2> &x) {
prIn(x);
}
void ps() { pr("\n"); }
template <class Arg, class... Args>
void ps(const Arg &first, const Args &...rest) {
pr(first, " ");
ps(rest...);
}
} // namespace debug
using namespace debug;
const int MOD = 1000000007;
namespace binexp {
long long bPow(long long b, long long p = MOD - 2) {
b %= MOD;
long long c = 1;
while (p) {
if (p & 1) {
c = c * b % MOD;
}
b = b * b % MOD;
p >>= 1;
}
return c;
}
} // namespace binexp
using namespace binexp;
int N, K;
int a[105];
long long mat[105][105];
void mult(long long a[105][105], long long b[105][105],
long long res[105][105]) {
memset(res, 0, 8 * 105 * 105);
for (int i = 0; i < 105; i++) {
for (int j = 0; j < 105; j++) {
for (int k = 0; k < 105; k++) {
res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % MOD;
}
}
}
}
long long exp(int p, int x, int y) {
long long c[105][105];
memset(c, 0, sizeof(c));
for (int i = 0; i < 105; i++) {
c[i][i] = 1;
}
long long b[105][105];
memcpy(b, mat, sizeof(mat));
long long tmp[105][105];
while (p > 0) {
if (p & 1) {
mult(c, b, tmp);
memcpy(c, tmp, sizeof(tmp));
}
mult(b, b, tmp);
memcpy(b, tmp, sizeof(tmp));
p >>= 1;
}
return c[x][y];
}
int main() {
cin >> N >> K;
int cnt = 0;
for (int i = 0; i < N; i++) {
cin >> a[i];
if (a[i] == 0) {
cnt++;
}
}
int oo = 0;
for (int i = 0; i < cnt; i++) {
if (a[i] == 1) {
oo++;
}
}
memset(mat, 0, sizeof(mat));
for (int i = 0; i <= min(cnt, N - cnt); i++) {
if (i + 1 <= N) {
mat[i][i + 1] = 1LL * (cnt - i) * (N - cnt - i) % MOD;
}
if (i - 1 >= 0) {
mat[i][i - 1] = 1LL * i * i % MOD;
}
mat[i][i] = (1LL * cnt * (cnt - 1) / 2) % MOD;
mat[i][i] = (mat[i][i] + 1LL * (N - cnt) * (N - cnt - 1) / 2) % MOD;
mat[i][i] = (mat[i][i] + 1LL * i * (N - cnt - i)) % MOD;
mat[i][i] = (mat[i][i] + 1LL * (cnt - i) * (i)) % MOD;
}
long long num = exp(K, oo, 0);
long long ch2 = (N * (N - 1LL) / 2) % MOD;
long long den = bPow(ch2, K);
long long iDen = bPow(den);
cout << num * iDen % MOD << endl;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
const long long MOD = 1e9 + 7;
int a[N];
struct Matrix {
long long a[N][N];
};
long long fpow(long long a, long long b) {
long long rtn = 1;
while (b) {
if (b & 1) rtn = (rtn * a) % MOD;
b >>= 1;
a = (a * a) % MOD;
}
return rtn;
}
void mmul(Matrix& a, Matrix b, int n) {
Matrix rtn;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
rtn.a[i][j] = 0;
for (int k = 0; k < n; ++k)
rtn.a[i][j] = (rtn.a[i][j] + a.a[i][k] * b.a[k][j] % MOD) % MOD;
}
}
a = rtn;
}
void mfpow(Matrix& mat, int k, int n) {
Matrix rtn;
for (int i = 0; i < n; ++i) {
rtn.a[i][i] = 1;
}
while (k) {
if (k & 1) mmul(rtn, mat, n);
mmul(mat, mat, n);
k >>= 1;
}
mat = rtn;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int n1 = 0, WA = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i]) n1++;
}
for (int i = 0; i < n; ++i) {
if (i < n - n1 && a[i]) WA++;
}
int n0 = n - n1;
long long invc = fpow(1LL * n * (n - 1) / 2, MOD - 2);
int top = min(n1, n0);
Matrix mat;
for (int i = 0; i <= top; ++i) {
long long pre = 0;
for (int j = 0; j <= top; ++j) {
if (i == j + 1) {
pre += 1LL * (n0 - i + 1) * (n1 - i + 1);
mat.a[i][j] = 1LL * (n0 - i + 1) * (n1 - i + 1) * invc % MOD;
} else if (i == j - 1) {
pre += 1LL * (i + 1) * (i + 1);
mat.a[i][j] = 1LL * (i + 1) * (i + 1) * invc % MOD;
}
}
mat.a[i][i] =
1LL * (n * (n - 1) / 2 - i * i - (n0 - i) * (n1 - i)) * invc % MOD;
}
mfpow(mat, k, top + 1);
cout << mat.a[0][WA] << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
new Main().go();
}
PrintWriter out;
Reader in;
BufferedReader br;
Main() throws IOException {
try {
//br = new BufferedReader( new FileReader("input.txt") );
//in = new Reader("input.txt");
in = new Reader("input.txt");
out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) );
}
catch (Exception e) {
//br = new BufferedReader( new InputStreamReader( System.in ) );
in = new Reader();
out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) );
}
}
void go() throws Exception {
//int t = in.nextInt();
int t = 1;
while (t > 0) {
solve();
t--;
}
out.flush();
out.close();
}
int inf = 2000000000;
int mod = 1000000007;
double eps = 0.000000001;
int n;
int m;
ArrayList<Pair>[] g;
void solve() throws IOException {
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
int cnt1 = 0;
for (int i = 0; i < n; i++) {
cnt1 += a[i];
}
int cnt0 = n - cnt1;
int pref1 = 0;
for (int i = 0; i < cnt0; i++)
pref1 += a[i];
int pref0 = cnt0 - pref1;
int[][] cost = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
if (i > cnt0) continue;
cost[i][i] = cnt1 * (cnt1 - 1) / 2;
cost[i][i] += cnt0 * (cnt0 - 1) / 2;
cost[i][i] += i * (cnt0 - i);
cost[i][i] += (cnt0 - i) * (cnt1 - (cnt0 - i));
if (i - 1 >= 0) {
int cnt = cnt0 - (i - 1);
if (cnt <= cnt1)
cost[i - 1][i] = cnt * cnt;
}
if (i + 1 <= cnt0) {
int onesPref = cnt0 - (i + 1);
int onesSuf = cnt1 - onesPref;
if (onesPref + onesSuf == cnt1)
cost[i + 1][i] = (i + 1) * onesSuf;
}
}
int[][] c = pow(cost, k);
int[] d = new int[n + 1];
d[pref0] = 1;
int[] res = new int[n + 1];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
res[i] = (res[i] + d[j] * c[j][i]) % mod;
long sum = 0;
for (int i = 0; i <= n; i++)
sum = (sum + res[i]) % mod;
System.err.println(res[cnt0]);
long ans = (long) res[cnt0] * pow(sum, mod - 2);
ans %= mod;
out.println(ans);
}
int[][] mult(int[][] a, int[][] b) {
int n = a[0].length;
int[][] c = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
c[i][j] = (int)((c[i][j] + (long) a[i][k] * b[k][j]) % mod);
return c;
}
int[][] pow(int[][] a, int n) {
if (n == 1)
return a;
if (n % 2 == 1)
return mult(a, pow(a, n - 1));
int[][] res = pow(a, n / 2);
return mult(res, res);
}
long pow(long x, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return (x * pow(x, n - 1)) % mod;
long res = pow(x, n / 2);
return (res * res) % mod;
}
class Pair implements Comparable<Pair> {
int a;
int b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair p) {
if (a != p.a)
return Integer.compare(a, p.a);
else
return Integer.compare(b, p.b);
}
}
class Item {
int a;
int b;
int c;
Item(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
class Reader {
BufferedReader br;
StringTokenizer tok;
Reader(String file) throws IOException {
br = new BufferedReader( new FileReader(file) );
}
Reader() throws IOException {
br = new BufferedReader( new InputStreamReader(System.in) );
}
String next() throws IOException {
while (tok == null || !tok.hasMoreElements())
tok = new StringTokenizer(br.readLine());
return tok.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.valueOf(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.valueOf(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return br.readLine();
}
}
static class InputReader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public InputReader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public InputReader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
} | JAVA |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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 < (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) {
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';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int N = 105, MOD = 1e9 + 7;
struct mat {
int a[N][N];
mat() { memset(a, 0, sizeof(a)); }
};
int n, cnt[2];
mat operator*(const mat &a, const mat &b) {
mat c = mat();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
(c.a[i][k] += 1LL * a.a[i][j] * b.a[j][k] % MOD) %= MOD;
}
}
}
return c;
}
mat pow(mat x, int m) {
mat res = mat();
for (int i = 0; i < n; ++i) res.a[i][i] = 1;
while (m) {
if (m & 1) res = res * x;
x = x * x;
m >>= 1;
}
return res;
}
int npow(int x, int m) {
int res = 1;
while (m) {
if (m & 1) res = 1LL * res * x % MOD;
x = 1LL * x * x % MOD;
m >>= 1;
}
return res;
}
int a[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
cnt[a[i]]++;
}
int x0 = 0;
for (int i = n - cnt[1]; i < n; ++i) {
x0 += (a[i] == 1);
}
mat p0 = mat();
p0.a[0][x0] = 1;
int ans = npow(npow(n * (n - 1) / 2, m), MOD - 2);
mat p = mat();
::n = cnt[1] + 1;
for (int i = 0; i <= cnt[1]; ++i) {
vector<int> x = {i, cnt[1] - i, cnt[1] - i, cnt[0] - cnt[1] + i};
for (int a = 0; a < 4; ++a) {
for (int b = a + 1; b < 4; ++b) {
int j = i;
if (a == 0 && b == 3) j--;
if (a == 1 && b == 2) j++;
if (j >= 0 && j <= cnt[1]) (p.a[i][j] += x[a] * x[b]) %= MOD;
}
(p.a[i][i] += x[a] * (x[a] - 1) / 2) %= MOD;
}
}
mat res = p0 * pow(p, m);
ans = 1LL * res.a[0][cnt[1]] * ans % MOD;
printf("%d\n", ans);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
const int mod = 1e9 + 7;
struct Matrix {
int val[maxn][maxn];
int height, width;
Matrix(int h, int w) {
height = h;
width = w;
memset(val, 0, sizeof val);
}
Matrix operator*(const Matrix& other) const {
assert(width == other.height);
Matrix ret(height, other.width);
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= other.width; j++) {
for (int k = 1; k <= width; k++) {
(ret.val[i][j] += 1ll * val[i][k] * other.val[k][j] % mod) %= mod;
}
}
}
return ret;
}
void print() {
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= width; j++) {
printf("%d ", val[i][j]);
}
puts("");
}
}
};
int C[maxn][maxn];
int n, k;
int a[maxn];
int ans[maxn];
int powmod(int x, int y) {
int res = 1;
while (y) {
if (y & 1) {
res = 1ll * res * x % mod;
}
y >>= 1;
x = 1ll * x * x % mod;
}
return res;
}
Matrix powmod(Matrix trans, int y) {
Matrix res(trans.height, trans.width);
for (int i = 1; i <= trans.height; i++) {
res.val[i][i] = 1;
}
while (y) {
if (y & 1) {
res = res * trans;
}
y >>= 1;
trans = trans * trans;
}
return res;
}
int main() {
C[0][0] = 1;
for (int i = 1; i < maxn; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
}
scanf("%d%d", &n, &k);
int cnt0 = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
cnt0 += a[i] == 0;
}
int my0 = 0;
for (int i = 1; i <= cnt0; i++) {
my0 += a[i] == 0;
}
Matrix trans(cnt0 + 1, cnt0 + 1);
for (int i = 0; i <= cnt0; i++) {
if (cnt0 - i > n - cnt0) continue;
if (i - 1 >= 0 && cnt0 - i + 1 <= n - cnt0) {
trans.val[i + 1][i] = 1ll * (cnt0 - i + 1) * (cnt0 - i + 1) % mod;
}
trans.val[i + 1][i + 1] =
1ll *
(C[cnt0][2] + C[n - cnt0][2] + 1ll * i * (cnt0 - i) % mod +
1ll * (cnt0 - i) * (n - 2 * cnt0 + i) % mod) %
mod;
if (i + 1 <= cnt0 && n - 2 * cnt0 + i + 1 > 0) {
trans.val[i + 1][i + 2] = 1ll * (i + 1) * (n - 2 * cnt0 + i + 1) % mod;
}
}
Matrix final_trans = powmod(trans, k);
for (int i = 1; i <= cnt0 + 1; i++) {
ans[i - 1] = final_trans.val[i][my0 + 1];
}
int p = ans[cnt0], q = 0;
for (int i = 0; i <= cnt0; i++) {
(q += ans[i]) %= mod;
}
cout << 1ll * p * powmod(powmod(C[n][2], k), mod - 2) % mod << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 110;
struct MAT {
long long a[MAXN][MAXN];
MAT() {
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++) a[i][j] = 0;
}
void relax() {
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++) a[i][j] %= mod;
}
} T;
void zarb(MAT &m1, MAT &m2) {
MAT tmp;
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++)
for (int k = 0; k < MAXN; k++)
tmp.a[i][j] += m1.a[i][k] * m2.a[k][j] % mod;
tmp.relax();
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++) m1.a[i][j] = tmp.a[i][j];
}
void tavan(MAT &M, long long x) {
if (x == 0) {
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++) M.a[i][j] = (i == j);
return;
}
MAT tmp;
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++) tmp.a[i][j] = M.a[i][j];
x--;
for (; x; x >>= 1, zarb(tmp, tmp))
if (x & 1) zarb(M, tmp);
}
long long powmod(long long a, long long b) {
if (!b) return 1;
if (b & 1) return a * powmod(a * a % mod, b >> 1) % mod;
return powmod(a * a % mod, b >> 1);
}
long long n, m, k, u, v, x, y, t, c0, c1, ans, bad;
int A[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> A[i];
if (A[i])
c1++;
else
c0++;
}
for (int i = 1; i <= c0; i++) bad += A[i];
if (!c1 || !c0) return cout << 1 << '\n', 0;
cerr << "bad" << '=' << bad << endl;
for (long long i = 0; i <= n; i++) {
T.a[i][i] = (c1 * (c1 - 1) + c0 * (c0 - 1)) / 2 - 2 * i * i + n * i;
if (i) T.a[i][i - 1] = i * i;
if (i < n) T.a[i][i + 1] = (c1 - i) * (c0 - i);
}
tavan(T, k);
ans = (T.a[bad][0] + mod) % mod;
cout << (ans * powmod(n * (n - 1) / 2, mod - k - 1)) % mod << '\n';
cerr << "ans" << '=' << ans << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long maxm = 105;
const long long mod = 1e9 + 7;
long long a[maxm];
struct Node {
long long a[maxm][maxm];
Node operator*(const Node& x) const {
Node ans;
for (long long i = 0; i < maxm; i++)
for (long long j = 0; j < maxm; j++) ans.a[i][j] = 0;
for (long long k = 0; k < maxm; k++) {
for (long long i = 0; i < maxm; i++) {
for (long long j = 0; j < maxm; j++) {
ans.a[i][j] = (ans.a[i][j] + a[i][k] * x.a[k][j]) % mod;
}
}
}
return ans;
}
};
long long ppow(long long a, long long b, long long mod) {
long long ans = 1 % mod;
a %= mod;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
Node Node_pow(Node a, long long b) {
Node ans;
for (long long i = 0; i < maxm; i++)
for (long long j = 0; j < maxm; j++) ans.a[i][j] = (i == j);
while (b) {
if (b & 1) ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
signed main() {
long long n, k;
cin >> n >> k;
long long x = 0;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
if (!a[i]) x++;
}
Node base;
for (long long i = 0; i < maxm; i++)
for (long long j = 0; j < maxm; j++) base.a[i][j] = 0;
for (long long i = 0; i <= x; i++) {
base.a[i][i] += x * (x - 1) / 2;
base.a[i][i] += (n - x) * (n - x - 1) / 2;
base.a[i][i] += i * (x - i) + (x - i) * (n - x - x + i);
base.a[i][i] %= mod;
if (i > 0) {
base.a[i - 1][i] += (x - (i - 1)) * (x - (i - 1));
base.a[i - 1][i] %= mod;
}
if (i < x) {
base.a[i + 1][i] += (i + 1) * (n - x - x + (i + 1));
base.a[i + 1][i] %= mod;
}
}
long long inv = ppow(n * (n - 1) / 2, mod - 2, mod);
for (long long i = 0; i <= x; i++) {
for (long long j = 0; j <= x; j++) {
base.a[i][j] *= inv;
base.a[i][j] %= mod;
}
}
Node temp = Node_pow(base, k);
long long c = 0;
for (long long i = 1; i <= x; i++)
if (!a[i]) c++;
Node ans;
for (long long i = 0; i < maxm; i++)
for (long long j = 0; j < maxm; j++) ans.a[i][j] = 0;
ans.a[0][c] = 1;
ans = ans * temp;
cout << ans.a[0][x] << endl;
return 0;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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]);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | import sys; input=sys.stdin.readline
# print(input())
N, T = map(int, input().split())
A = [int(a) for a in input().split()]
if sum(A) > N//2:
A = [1-a for a in A][::-1]
K = sum(A)
S = sum(A[-K:])
M = K + 1
P = 10**9+7
inv = pow(N*(N-1)//2, P-2, P)
X = [[0]*M for _ in range(M)]
for i in range(M):
if i > 0: X[i-1][i] = ((K-i+1)**2*inv)%P
if i < M-1: X[i+1][i] = (N-2*K+i+1)*(i+1)*inv%P
X[i][i] = (1-((K-i)**2*inv)-(N-2*K+i)*(i)*inv)%P
# def ddd(n):
# for i in range(1, 100):
# if (n*i%P) < 100:
# return (n*i%P), i
# return -1, -1
def poww(MM, n):
if n == 1:
return MM
if n % 2:
return mult(poww(MM, n-1), MM)
return poww(mult(MM,MM), n//2)
def mult(M1, M2):
Y = [[0] * M for _ in range(M)]
for i in range(M):
for j in range(M):
for k in range(M):
Y[i][j] += M1[i][k] * M2[k][j]
Y[i][j] %= P
return Y
X = poww(X, T)
print(X[S][K])
| PYTHON3 |
1151_F. Sonya and Informatics | 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. | 2 | 12 | //package round553;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class F {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), K = ni();
int[] a = na(n);
int o = 0;
for(int i = 0;i < n;i++)o += a[i];
int s = 0;
for(int i = 0;i < o;i++)s += a[n-1-i];
// o-i/n-o i/o
int[][] M = new int[o+1][o+1];
long all = n*(n-1);
long iall = invl(all, mod);
for(int i = 0;i <= o;i++){
for(int j = -1;j <= 1;j++){
int t = i+j;
if(t >= 0 && t <= o){
if(j == 1){
M[t][i] = (int)(2*(o-i)*(o-i)*iall%mod);
}else if(j == -1){
M[t][i] = (int)(2*((n-o)-(o-i))*i*iall%mod);
}else{
M[t][i] = (int)((all-2*(o-i)*(o-i)-2*((n-o)-(o-i))*i)*iall%mod);
}
}
}
}
int[] v = new int[o+1];
v[s] = 1;
out.println(pow(M, v, K)[o]);
}
public static long invl(long a, long mod) {
long b = mod;
long p = 1, q = 0;
while (b > 0) {
long c = a / b;
long d;
d = a;
a = b;
b = d % b;
d = p;
p = q;
q = d - c * q;
}
return p < 0 ? p + mod : p;
}
///////// begin
public static final int mod = 1000000007;
public static final long m2 = (long)mod*mod;
public static final long BIG = 8L*m2;
// A^e*v
public static int[] pow(int[][] A, int[] v, long e)
{
for(int i = 0;i < v.length;i++){
if(v[i] >= mod)v[i] %= mod;
}
int[][] MUL = A;
for(;e > 0;e>>>=1) {
if((e&1)==1)v = mul(MUL, v);
MUL = p2(MUL);
}
return v;
}
// int matrix*int vector
public static int[] mul(int[][] A, int[] v)
{
int m = A.length;
int n = v.length;
int[] w = new int[m];
for(int i = 0;i < m;i++){
long sum = 0;
for(int k = 0;k < n;k++){
sum += (long)A[i][k] * v[k];
if(sum >= BIG)sum -= BIG;
}
w[i] = (int)(sum % mod);
}
return w;
}
// int matrix^2 (be careful about negative value)
public static int[][] p2(int[][] A)
{
int n = A.length;
int[][] C = new int[n][n];
for(int i = 0;i < n;i++){
long[] sum = new long[n];
for(int k = 0;k < n;k++){
for(int j = 0;j < n;j++){
sum[j] += (long)A[i][k] * A[k][j];
if(sum[j] >= BIG)sum[j] -= BIG;
}
}
for(int j = 0;j < n;j++){
C[i][j] = (int)(sum[j] % mod);
}
}
return C;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new F().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
1151_F. Sonya and Informatics | 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. | 2 | 12 | import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.TreeSet;
import java.util.function.BiFunction;
public class Main{
static int n;
static int mod = 1000000007;
static long[][] A;
public static void main(String[] args){
FastScanner sc = new FastScanner();
Mathplus mp = new Mathplus();
PrintWriter out = new PrintWriter(System.out);
n = sc.nextInt();
int k = sc.nextInt();
int num = 0;
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
num+=a[i];
}
int d = 0;
for(int i=0;i<n-num;i++){
if(a[i]==1) d ++;
}
long alle = n*(n-1)/2;
long mot = mp.rev(alle);
A = new long[n+1][n+1];
for(int i=0;i<=n;i++){
if(i!=0){
A[i][i-1] = i * i * mot%mod;
}
if(i!=n){
A[i][i+1] =Math.max(0,(n-num-i)) * Math.max(0,num-i) * mot % mod;
}
A[i][i] = Math.max(alle - i * i-Math.max(0,n-num-i) * Math.max(0,num-i),0)*mot%mod;
}
long[][] ans = pow(A,k);
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
if(A[i][j]<0){
System.out.println("out"+i+" "+j);
}
}
}
System.out.println(ans[d][0]);
}
private static long[][] pow(long[][] a2, long k) {
if(k==1){
return a2;
}else{
if(k%2==0){
long[][] val = pow(a2,k/2);
return mul(val,val);
}else{
return mul(pow(a2,k-1),A);
}
}
}
private static long[][] mul(long[][] a, long[][] b) {
long[][] c = new long[n+1][n+1];
for(int i=0;i<n+1;i++){
for(int j=0;j<n+1;j++){
for(int k=0;k<n+1;k++){
c[i][j] += a[i][k] * b[k][j];
c[i][j] %= mod;
}
}
}
return c;
}
}
class SegmentTree<T,E>{
int N;
BiFunction<T,T,T> f;
BiFunction<T,E,T> g;
T d1;
ArrayList<T> dat;
SegmentTree(BiFunction<T,T,T> F,BiFunction<T,E,T> G,T D1,T[] v){
int n = v.length;
f = F;
g = G;
d1 = D1;
init(n);
build(v);
}
void init(int n) {
N = 1;
while(N<n)N*=2;
dat = new ArrayList<T>();
}
void build(T[] v) {
for(int i=0;i<2*N;i++) {
dat.add(d1);
}
for(int i=0;i<v.length;i++) {
dat.set(N+i-1,v[i]);
}
for(int i=N-2;i>=0;i--) {
dat.set(i,f.apply(dat.get(i*2+1),dat.get(i*2+2)));
}
}
void update(int k,E a) {
k += N-1;
dat.set(k,g.apply(dat.get(k),a));
while(k>0){
k = (k-1)/2;
dat.set(k,f.apply(dat.get(k*2+1),dat.get(k*2+2)));
}
}
T query(int a,int b, int k, int l ,int r) {
if(r<=a||b<=l) return d1;
if(a<=l&&r<=b) return dat.get(k);
T vl = query(a,b,k*2+1,l,(l+r)/2);
T vr = query(a,b,k*2+2,(l+r)/2,r);
return f.apply(vl, vr);
}
T query(int a,int b){
return query(a,b,0,0,N);
}
}
class LazySegmentTree<T,E> extends SegmentTree<T,E>{
BiFunction<E,E,E> h;
BiFunction<E,Integer,E> p = (E a,Integer b) ->{return a;};
E d0;
ArrayList<E> laz;
LazySegmentTree(BiFunction<T,T,T> F,BiFunction<T,E,T> G,BiFunction<E,E,E> H,T D1,E D0,T[] v){
super(F,G,D1,v);
int n = v.length;
h = H;
d0 = D0;
Init(n);
}
void build() {
}
void Init(int n){
laz = new ArrayList<E>();
for(int i=0;i<2*N;i++) {
laz.add(d0);
}
}
void eval(int len,int k) {
if(laz.get(k).equals(d0)) return;
if(k*2+1<N*2-1) {
laz.set(k*2+1,h.apply(laz.get(k*2+1),laz.get(k)));
laz.set(k*2+2,h.apply(laz.get(k*2+2),laz.get(k)));
}
dat.set(k,g.apply(dat.get(k), p.apply(laz.get(k), len)));
laz.set(k,d0);
}
T update(int a,int b,E x,int k,int l,int r) {
eval(r-l,k);
if(r<=a||b<=l) {
return dat.get(k);
}
if(a<=l&&r<=b) {
laz.set(k,h.apply(laz.get(k),x));
return g.apply(dat.get(k),p.apply(laz.get(k),r-l));
}
T vl = update(a,b,x,k*2+1,l,(l+r)/2);
T vr = update(a,b,x,k*2+2,(l+r)/2,r);
dat.set(k,f.apply(vl,vr));
return dat.get(k);
}
T update(int a,int b,E x) {
return update(a,b,x,0,0,N);
}
T query(int a,int b,int k,int l,int r) {
eval(r-l,k);
if(r<=a||b<=l) return d1;
if(a<=l&&r<=b) return dat.get(k);
T vl = query(a,b,k*2+1,l,(l+r)/2);
T vr = query(a,b,k*2+2,(l+r)/2,r);
return f.apply(vl, vr);
}
T query(int a,int b){
return query(a,b,0,0,N);
}
}
class UnionFindTree {
int[] root;
int[] rank;
int[] size;
UnionFindTree(int N){
root = new int[N];
rank = new int[N];
size = new int[N];
for(int i=0;i<N;i++){
root[i] = i;
size[i] = 1;
}
}
public int find(int x){
if(root[x]==x){
return x;
}else{
return find(root[x]);
}
}
public void unite(int x,int y){
x = find(x);
y = find(y);
if(x==y){
return;
}else{
if(rank[x]<rank[y]){
root[x] = y;
size[y] += size[x];
}else{
root[y] = x;
size[x] += size[y];
if(rank[x]==rank[y]){
rank[x]++;
}
}
}
}
public boolean same(int x,int y){
return find(x)==find(y);
}
}
class ParticalEternalLastingUnionFindTree extends UnionFindTree{
int[] time;
int now;
ParticalEternalLastingUnionFindTree(int N){
super(N);
time = new int[N];
for(int i=0;i<N;i++) {
time[i] = 1000000007;
}
}
public int find(int t,int i) {
if(time[i]>t) {
return i;
}else {
return find(t,root[i]);
}
}
public void unite(int x,int y,int t) {
now = t;
x = find(t,x);
y = find(t,y);
if(x==y)return;
if(rank[x]<rank[y]){
root[x] = y;
size[y] += size[x];
time[x] = t;
}else{
root[y] = x;
size[x] += size[y];
if(rank[x]==rank[y]){
rank[x]++;
}
time[y] = t;
}
}
public int sametime(int x,int y) {
if(find(now,x)!=find(now,y)) return -1;
int ok = now;
int ng = 0;
while(ok-ng>1) {
int mid = (ok+ng)/2;
if(find(mid,x)==find(mid,y)) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
}
class Graph {
ArrayList<Edge>[] list;
int size;
TreeSet<LinkEdge> Edges = new TreeSet<LinkEdge>(new LinkEdgeComparator());
@SuppressWarnings("unchecked")
Graph(int N){
size = N;
list = new ArrayList[N];
for(int i=0;i<N;i++){
list[i] = new ArrayList<Edge>();
}
}
void addEdge(int a,int b){
list[a].add(new Edge(b,1));
}
void addWeightedEdge(int a,int b,long c){
list[a].add(new Edge(b,c));
}
void addEgdes(int[] a,int[] b){
int size = a.length;
for(int i=0;i<size;i++){
list[a[i]].add(new Edge(b[i],1));
}
}
void addWeighterEdges(int[] a ,int[] b ,int[] c){
int size = a.length;
for(int i=0;i<size;i++){
list[a[i]].add(new Edge(b[i],c[1]));
}
}
long[] bfs(int s){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
L[s] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
long c = e.cost;
if(L[w]==-1){
L[w] = L[v] + c;
Q.add(w);
}
}
}
return L;
}
long[] dijkstra(int s){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
int[] visited = new int[size];
L[s] = 0;
PriorityQueue<Pair> Q = new PriorityQueue<Pair>(new SampleComparator());
Q.add(new Pair(0,s));
while(!Q.isEmpty()){
Pair C = Q.poll();
if(visited[(int)C.b]==0){
L[(int)C.b] = C.a;
visited[(int) C.b] = 1;
for(Edge D:list[(int) C.b]){
Q.add(new Pair(L[(int)C.b]+D.cost,D.to));
}
}
}
return L;
}
long[] maxtra(int s,long l){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
int[] visited = new int[size];
L[s] = -1;
;
PriorityQueue<Pair> Q = new PriorityQueue<Pair>(new SampleComparator());
Q.add(new Pair(l,s));
while(!Q.isEmpty()){
Pair C = Q.poll();
if(visited[(int)C.b]==0){
L[(int)C.b] = C.a;
visited[(int) C.b] = 1;
for(Edge D:list[(int) C.b]){
Q.add(new Pair(Math.max(L[(int)C.b],D.cost),D.to));
}
}
}
return L;
}
long Kruskal(){
long ans = 0;
for(int i=0;i<size;i++) {
for(Edge e:list[i]) {
Edges.add(new LinkEdge(e.cost,i,e.to));
}
}
UnionFindTree UF = new UnionFindTree(size);
for(LinkEdge e:Edges){
if(e.a>=0&&e.b>=0) {
if(!UF.same(e.a,e.b)){
ans += e.L;
UF.unite(e.a,e.b);
}
}
}
return ans;
}
ArrayList<Integer> Kahntsort(){
ArrayList<Integer> ans = new ArrayList<Integer>();
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
int[] in = new int[size];
for(int i=0;i<size;i++) {
for(Edge e:list[i]) {
in[e.to]++;
}
}
for(int i=0;i<size;i++) {
if(in[i]==0) {
q.add(i);
}
}
while(!q.isEmpty()) {
int v = q.poll();
ans.add(v);
for(Edge e:list[v]) {
in[e.to]--;
if(in[e.to]==0) {
q.add(e.to);
}
}
}
for(int i=0;i<size;i++) {
if(in[i]>0)return new ArrayList<Integer>();
}
return ans;
}
RootedTree dfsTree(int i) {
int[] used = new int[size];
RootedTree r = new RootedTree(size);
dfsTree(i,used,r);
return r;
}
private void dfsTree(int i, int[] used, RootedTree r) {
used[i] = 1;
for(Edge e:list[i]) {
if(used[e.to]==0) {
r.list[i].add(e);
used[e.to] = 1;
dfsTree(i,used,r);
}
}
}
}
class Tree extends Graph{
public Tree(int N) {
super(N);
}
long[] tyokkei(){
long[] a = bfs(0);
System.out.println();
int maxdex = -1;
long max = 0;
for(int i=0;i<size;i++){
if(max<a[i]){
max = a[i];
maxdex = i;
}
}
long[] b = bfs(maxdex);
System.out.println();
int maxdex2 = -1;
long max2 = 0;
for(int i=0;i<size;i++){
if(max2<b[i]){
max2 = b[i];
maxdex2 = i;
}
}
long[] ans = {max2,maxdex,maxdex2};
return ans;
}
}
class RootedTree extends Graph{
RootedTree(int N){
super(N);
}
}
class LinkEdge{
long L;
int a ;
int b;
LinkEdge(long l,int A,int B){
L = l;
a = A;
b = B;
}
public boolean equals(Object o){
LinkEdge O = (LinkEdge) o;
if(O.a==this.a&&O.b==this.b&&O.L==this.L){
return true;
}else{
return false;
}
}
public int hashCode(){
return Objects.hash(L,a,b);
}
}
class Edge{
int to;
long cost;
Edge(int a,long b){
to = a;
cost = b;
}
}
class LinkEdgeComparator implements Comparator<LinkEdge>{
public int compare(LinkEdge P, LinkEdge Q) {
long temp = P.L-Q.L;
if(temp==0){
if(P.a>Q.a){
return 1;
}else{
if(P.b>Q.b){
return 1;
}else{
return -1;
}
}
}
if(temp>=0){
return 1;
}else{
return -1;
}
}
}
class Pair{
long a;
long b;
Pair(long p,long q){
this.a = p;
this.b = q;
}
public boolean equals(Object o){
Pair O = (Pair) o;
if(O.a==this.a&&O.b==this.b){
return true;
}else{
return false;
}
}
public int hashCode(){
return Objects.hash(a,b);
}
}
class SampleComparator implements Comparator<Pair>{
public int compare(Pair P, Pair Q) {
long temp = P.a-Q.a;
if(temp==0){
if(P.b>Q.b){
return 1;
}else{
return -1;
}
}
if(temp>=0){
return 1;
}else{
return -1;
}
}
}
class LongIntPair{
long a;
int b;
LongIntPair(long p,int q){
this.a = p;
this.b = q;
}
public boolean equals(Object o){
Pair O = (Pair) o;
if(O.a==this.a&&O.b==this.b){
return true;
}else{
return false;
}
}
public int hashCode(){
return Objects.hash(a,b);
}
}
class LongIntSampleComparator implements Comparator<LongIntPair>{
public int compare(LongIntPair P, LongIntPair Q) {
long temp = P.a-Q.a;
if(temp==0){
if(P.b>Q.b){
return 1;
}else{
return -1;
}
}
if(temp>=0){
return 1;
}else{
return -1;
}
}
}
class FastScanner {
private final java.io.InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
public boolean hasNext() { skipUnprintable(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return (minus ? -n : n);
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return (int) (minus ? -n : n);
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
}
class Mathplus{
int mod = 1000000007;
long[] fac = new long[1000001];
long[][] combt = new long[2001][2001];
long[][] permt = new long[2001][2001];
boolean isBuild = false;
boolean isBuildc = false;
boolean isBuildp = false;
int mindex = -1;
int maxdex = -1;
void buildFac(){
fac[0] = 1;
for(int i=1;i<=1000000;i++){
fac[i] = (fac[i-1] * i)%mod;
}
isBuild = true;
}
void buildComb() {
for(int i=0;i<=2000;i++) {
combt[i][0] = 1;
}
for(int j=1;j<=2000;j++) {
combt[j][j] = 1;
for(int i=j+1;i<=2000;i++) {
combt[i][j] = combt[i-1][j-1] + combt[i-1][j];
if(combt[i][j]>=mod)combt[i][j]-=mod;
}
}
isBuildc = false;
}
void buildPerm() {
for(int i=0;i<=2000;i++) {
permt[i][0] = 1;
}
if(!isBuild)buildFac();
for(int i=1;i<=2000;i++) {
for(int j=1;j<=i;j++) {
permt[i][j] = permt[i][j-1]*(i-j+1);
permt[i][j] %= mod;
}
}
isBuildp = true;
}
public int isBigger(int[] d, int i) {
int ok = d.length;
int ng = -1;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d[mid]>i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isSmaller(int[] d, int i) {
int ok = -1;
int ng = d.length;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d[mid]<i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public HashSet<Integer> primetable(int m) {
HashSet<Integer> pt = new HashSet<Integer>();
for(int i=2;i<=m;i++) {
boolean b = true;
for(int d:pt) {
if(i%d==0) {
b = false;
break;
}
}
if(b) {
pt.add(i);
}
}
return pt;
}
long max(long[] a){
long max = 0;
for(int i=0;i<a.length;i++){
if(max<a[i]){
max =a[i];
maxdex = i;
}
}
return max;
}
int max(int[] a){
int max = 0;
for(int i=0;i<a.length;i++){
if(max<a[i]){
max =a[i];
maxdex = i;
}
}
return max;
}
long min(long[] a){
long min = Long.MAX_VALUE;
for(int i=0;i<a.length;i++){
if(min>a[i]){
min =a[i];
mindex = i;
}
}
return min;
}
int min(int[] a){
int min = Integer.MAX_VALUE;
for(int i=0;i<a.length;i++){
if(min>a[i]){
min =a[i];
mindex = i;
}
}
return min;
}
long sum(long[] a){
long sum = 0;
for(int i=0;i<a.length;i++){
sum += a[i];
}
return sum;
}
long sum(int[] a){
long sum = 0;
for(int i=0;i<a.length;i++){
sum += a[i];
}
return sum;
}
long gcd(long a, long b){
if(a<b){
a^=b;
b^=a;
a^=b;
}
if(a%b==0){
return b;
}else{
return gcd(b,a%b);
}
}
long lcm(long a, long b){
return a / gcd(a,b) * b;
}
public long perm(int a,int num) {
if(a<2000&&num<2000) {
if(!isBuildp) {
buildPerm();
isBuildp = true;
}
return permt[a][num];
}
if(!isBuild) {
buildFac();
}
return fac[a] * (rev(fac[a-num]))%mod;
}
public long comb(int a,int num){
if(a<2000&&num<2000) {
if(!isBuildc) {
buildComb();
isBuildc = true;
}
return combt[a][num];
}
if(!isBuild){
buildFac();
}
return fac[a] * (rev(fac[num])*rev(fac[a-num])%mod)%mod;
}
long rev(long l) {
return pow(l,mod-2);
}
long pow(long l, int i) {
if(i==0){
return 1;
}else{
if(i%2==0){
long val = pow(l,i/2);
return val * val % mod;
}else{
return pow(l,i-1) * l % mod;
}
}
}
}
| JAVA |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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';
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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]];
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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";
}
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | M = 10 ** 9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
z, o = a.count(0), a.count(1)
d = pow(n * (n - 1) // 2, M - 2, M)
if z > o:
o, z = z, o
a = [1 - x for x in a][::-1]
res = [[0] * (z + 1) for i in range(z + 1)]
tf = [[0] * (z + 1) for i in range(z + 1)]
for i in range(z + 1):
res[i][i] = 1
tf[i][i] = (z * (z - 1) // 2 + o * (o - 1) // 2 + i * (z - i) + (z - i) * (o - z + i)) * d % M
if i < z: tf[i + 1][i] = (z - i) * (z - i) * d % M
if i: tf[i - 1][i] = i * (o - z + i) * d % M
def mul(a, b):
t = [[0] * (z + 1) for i in range(z + 1)]
for i in range(z + 1):
for k in range(z + 1):
for j in range(z + 1):
t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % M
return t
while k:
if k & 1:
res = mul(res, tf)
tf = mul(tf, tf)
k >>= 1
print(res[-1][a[:z].count(0)]) | PYTHON3 |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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);
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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;
}
| CPP |
1151_F. Sonya and Informatics | 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. | 2 | 12 | #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';
}
| CPP |
Subsets and Splits