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
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int MOD = 1e9 + 7; vector<int> g[N + N]; int mat[N][N], p[N + N], sub[N + N], vis[N + N]; int ans[N + N], instack[N + N], go = true, ct = 0; vector<pair<int, int>> extime; void putNumbers(int node) { vis[node] = 1; instack[node] = true; ct++; for (auto it : g[node]) { if (!vis[it]) { putNumbers(it); } else if (instack[it]) go = false; } instack[node] = false; extime.push_back({ct, node}); ct++; } void getAns(int node) { vis[node] = 1; ans[node] = max(ans[node], 1); for (auto it : g[node]) if (!vis[it]) { ans[it] = max(ans[it], ans[node] + 1); } } int root(int u) { while (p[u] != u) { p[u] = p[p[u]]; u = p[u]; } return u; } void merge(int uu, int vv) { int u = root(uu), v = root(vv); if (u == v) return; if (sub[u] < sub[v]) swap(u, v); p[v] = u; sub[u] += sub[v]; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, i, j; cin >> n >> m; for (i = 1; i <= n + m; i++) { p[i] = i; sub[i] = 1; } for (i = 1; i <= n; i++) { string s; cin >> s; for (j = 0; j < m; j++) { if (s[j] == '<') mat[i][j + 1] = -1; else if (s[j] == '=') { merge(i, n + j + 1); mat[i][j + 1] = 0; } else mat[i][j + 1] = 1; } } for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { int u = root(i), v = root(n + j); if (mat[i][j] < 0) { if (u == v) { cout << "No\n"; return 0; } g[u].push_back(v); } if (mat[i][j] > 0) { if (u == v) { cout << "No\n"; return 0; } g[v].push_back(u); } } for (i = 1; i <= n + m; i++) if (!vis[i] && p[i] == i) { putNumbers(i); if (!go) { cout << "No\n"; return 0; } } sort(extime.begin(), extime.end()); reverse(extime.begin(), extime.end()); memset(vis, 0, sizeof(vis)); for (auto it : extime) if (!vis[it.second]) { getAns(it.second); } cout << "Yes\n"; for (i = 1; i <= n + m; i++) { cout << ans[root(i)] << " "; if (i == n) cout << '\n'; } cout << '\n'; return 0; }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 5; namespace DSU { int fa[MAXN << 1]; void init(int n) { for (int i = 1; i <= n; i++) fa[i] = i; } int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); } bool Union(int x, int y) { x = Find(x), y = Find(y); if (x == y) return 0; return fa[x] = y, 1; } } // namespace DSU using namespace DSU; char s[MAXN][MAXN]; int n, m; vector<int> E[MAXN << 1], G[MAXN << 1]; int ans[MAXN << 1], in[MAXN << 1], res[MAXN << 1]; bool vis[MAXN << 1]; void bfs() { queue<int> Q; memset(vis, false, sizeof(vis)); for (int i = 1; i <= n + m; i++) { int x = Find(i); if (vis[x]) continue; in[x] = E[x].size(); vis[x] = true; if (in[x] == 0) { ans[x] = 1; Q.push(x); } } while (!Q.empty()) { int x = Q.front(); Q.pop(); for (auto v : G[x]) { in[v]--; if (in[v] == 0) { ans[v] = ans[x] + 1; Q.push(v); } } } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); init(n + m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (s[i][j] == '=') { Union(i, n + j); } } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x = Find(i), y = Find(n + j); if (s[i][j] == '>') E[x].push_back(y), G[y].push_back(x); if (s[i][j] == '<') E[y].push_back(x), G[x].push_back(y); } bfs(); for (int i = 1; i <= n + m; i++) { int x = Find(i); if (ans[x] == 0) return 0 * puts("No"); res[i] = ans[x]; } puts("Yes"); for (int i = 1; i <= n; i++) printf("%d ", res[i]); puts(""); for (int i = n + 1; i <= n + m; i++) printf("%d ", res[i]); puts(""); return 0; }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; char s[1005][1005]; int vis[3 * 1005], fa[3 * 1005], in[3 * 1005], ans[3 * 1005]; vector<int> v[3 * 1005]; int findd(int x) { return x == fa[x] ? x : fa[x] = findd(fa[x]); } int main() { int n, m, i, j; cin >> n >> m; for (i = 0; i < n; i++) { scanf("%s", s[i]); } for (i = 0; i < n + m; i++) fa[i] = i; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (s[i][j] == '=') { int fi = findd(i); int fj = findd(j + n); if (fi != fj) { fa[fi] = fj; } } } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (s[i][j] == '>') { int fi = findd(i); int fj = findd(j + n); v[fj].push_back(fi); in[fi]++; } else if (s[i][j] == '<') { int fi = findd(i); int fj = findd(j + n); v[fi].push_back(fj); in[fj]++; } } } int no = 0; while (1) { no++; int flag = 0; for (i = 0; i < n + m; i++) { int fi = findd(i); if (vis[i] == 0 && in[fi] == 0) { ans[i] = no; vis[i] = 1; flag = 1; } } for (i = 0; i < n + m; i++) { int fi = findd(i); if (ans[i] == no && fi == i) { for (j = 0; j < v[i].size(); j++) { in[v[i][j]]--; } } } if (flag == 0) break; } int flag = 1; for (i = 0; i < n + m; i++) { if (ans[i] == 0) { flag = 0; break; } } if (flag == 1) { cout << "YES" << endl; for (i = 0; i < n; i++) { cout << ans[i] << " "; } cout << endl; for (i = n; i < n + m; i++) { cout << ans[i] << " "; } cout << endl; } else { cout << "NO" << endl; } }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10; const int INF = 0x3f3f3f3f; char mp[N][N]; int sec[N], fir[N]; int num[3]; int b[N + N]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { getchar(); scanf("%s", mp[i] + 1); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (mp[i][j] == '>') sec[j]--; else if (mp[i][j] == '<') sec[j]++; } } int flag = 1; for (int j = 1; j <= m; j++) sec[j] *= 1000; for (int i = 1; i <= n; i++) { num[0] = num[1] = INF; num[2] = -INF; for (int j = 1; j <= m; j++) { if (mp[i][j] == '=') { if (num[0] == INF) num[0] = sec[j]; else if (num[0] != sec[j]) flag = 0; } else if (mp[i][j] == '<') { num[1] = min(sec[j], num[1]); } else { num[2] = max(sec[j], num[2]); } } if (!flag) break; if (num[2] >= num[1]) flag = 0; else if (num[0] != INF) { if (num[0] < num[1] && num[0] > num[2]) fir[i] = num[0]; else flag = 0; } else fir[i] = num[2] + 1; if (!flag) break; } if (flag) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (fir[i] > sec[j]) { if (mp[i][j] != '>') { flag = 0; break; } } else if (fir[i] == sec[j]) { if (mp[i][j] != '=') { flag = 0; break; } } else { if (mp[i][j] != '<') { flag = 0; break; } } } if (!flag) break; } int tot = 0; for (int i = 1; i <= n; i++) b[++tot] = fir[i]; for (int j = 1; j <= m; j++) b[++tot] = sec[j]; sort(b + 1, b + 1 + tot); int cnt = unique(b + 1, b + tot + 1) - b - 1; for (int i = 1; i <= n; i++) fir[i] = lower_bound(b + 1, b + 1 + cnt, fir[i]) - b; for (int j = 1; j <= m; j++) sec[j] = lower_bound(b + 1, b + 1 + cnt, sec[j]) - b; } if (!flag) printf("No\n"); else { printf("Yes\n"); for (int i = 1; i <= n; i++) if (i == 1) printf("%d", fir[i]); else printf(" %d", fir[i]); printf("\n"); for (int j = 1; j <= m; j++) if (j == 1) printf("%d", sec[j]); else printf(" %d", sec[j]); printf("\n"); } }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 10; char s[MAXN][MAXN]; vector<int> G[MAXN << 1]; int indec[MAXN << 1]; int ans[MAXN << 1]; bool vis[MAXN << 1]; struct DSU { int p[MAXN << 1]; int sz; void init(int n) { sz = n; for (int i = 1; i <= n; i++) { p[i] = i; } } int findp(int x) { return x == p[x] ? x : p[x] = findp(p[x]); } void unite(int x, int y) { x = findp(x); y = findp(y); if (x == y) return; sz--; p[x] = y; } } dsu; void dfs(int u) { vis[u] = true; for (auto v : G[u]) { ans[v] = max(ans[v], ans[u] + 1); indec[v]--; if (indec[v] == 0) { dfs(v); } } } int main() { int n, m; scanf("%d%d", &n, &m); dsu.init(n + m); for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i][j] == '=') { dsu.unite(i, n + j); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int x = dsu.findp(i); int y = dsu.findp(n + j); if (s[i][j] == '<') { if (x == y) { puts("No"); return 0; } G[x].push_back(y); indec[y]++; } else if (s[i][j] == '>') { if (x == y) { puts("No"); return 0; } G[y].push_back(x); indec[x]++; } } } for (int i = 1; i <= n + m; i++) { int x = dsu.findp(i); if (indec[x] == 0) { ans[x] = 1; } } for (int i = 1; i <= n + m; i++) { if (ans[i] == 1) { dfs(i); } } int cnt = 0; for (int i = 1; i <= n + m; i++) { if (vis[i]) { cnt++; } } if (cnt != dsu.sz) { puts("No"); return 0; } puts("Yes"); for (int i = 1; i <= n; i++) { printf("%d ", ans[dsu.findp(i)]); } puts(""); for (int i = 1; i <= m; i++) { printf("%d ", ans[dsu.findp(n + i)]); } return 0; }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; const long long Mod = 7 + 1e9; const long long INF32 = 5 + 2e9; const long long INF64 = 1 + 1e18; const long long MAX = 5 + 1e3; int N, M, parent[2 * MAX], dp[2 * MAX]; vector<long long> G[2 * MAX]; set<int> GG[2 * MAX]; bool eq[2 * MAX][2 * MAX]; int vis[2 * MAX]; void merge(int a, int b) { parent[b] = a; } bool IsCycled(int u) { vis[u] = 1; for (int v : GG[u]) if (vis[parent[v]] == 2) continue; else if (vis[parent[v]] == 1 || IsCycled(parent[v])) return 1; vis[u] = 2; return 0; } int Try(int u) { int &r = dp[u]; if (r != -1) return r; r = 1; for (int v : GG[u]) r = max(r, Try(v) + 1); return r; } char inp[2001][2001]; int main() { cin >> N >> M; for (int i = 0; i < N; i++) for (int j = N; j < N + M; j++) { char c; cin >> c; inp[i][j] = c; if (c == '<') G[j].push_back(i); else if (c == '>') G[i].push_back(j); else eq[i][j] = 1; } for (int i = 0; i < N + M; i++) parent[i] = i; for (int j = N; j < N + M; j++) { int f = -1; for (int i = 0; i < N; i++) if (eq[i][j]) f == -1 ? f = i : eq[f][i] = 1; } for (int i = 0; i < N + M; i++) { if (parent[i] == i) merge(i, i); for (int j = i + 1; j < N + M; j++) if (eq[i][j]) merge(parent[i], j); } for (int i = 0; i < N; i++) { for (int j = N; j < N + M; j++) { char c = inp[i][j]; if (c == '<') GG[parent[j]].insert(parent[i]); else if (c == '>') GG[parent[i]].insert(parent[j]); } } for (int i = 0; i < N + M; i++) if (!vis[parent[i]]) { if (IsCycled(parent[i])) return cout << "No\n", 0; } memset(dp, -1, sizeof(dp)); cout << "Yes\n"; for (int i = 0; i < N; i++) cout << Try(parent[i]) << ' '; cout << endl; for (int i = N; i < N + M; i++) cout << Try(parent[i]) << ' '; cout << endl; return 0; }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run(){ work(); out.flush(); } long mod=1000000007; long gcd(long a,long b) { return a==0?b:b>=a?gcd(b%a,a):gcd(b,a); } void work() { int n=in.nextInt(),m=in.nextInt(); int[][] A=new int[n][m]; int[][] count=new int[n][2]; boolean[] is=new boolean[n]; for(int i=0;i<n;i++) { String s=in.next(); for(int j=0;j<m;j++) { if(s.charAt(j)=='<') { A[i][j]=-1; count[i][0]++; }else if(s.charAt(j)=='>') { A[i][j]=1; count[i][1]++; } } } int[] row=new int[n]; int[] col=new int[m]; int[][] B=new int[n][]; for(int i=0;i<n;i++) { B[i]=new int[] {i,count[i][0],count[i][1]}; } Arrays.sort(B,new Comparator<int[]>() { public int compare(int[] A1,int[] A2) { if(A1[1]==A2[1]) { return A1[2]-A2[2]; } return A2[1]-A1[1]; } }); int cur=1; for(int i=0;i<n;i++) { int idx=B[i][0]; if(i>=1) { int pre=B[i-1][0]; boolean f=false; for(int j=0;j<m;j++) { if(A[idx][j]!=A[pre][j])f=true; } if(!f) { row[idx]=row[pre]; continue; } } boolean f=false; for(int j=0;j<m;j++) { if(A[idx][j]==1&&col[j]==0) { f=true; col[j]=cur; }else if(A[idx][j]==-1||A[idx][j]==0){ if(col[j]>0) { out.println("No"); return; } } } if(f)cur++; row[idx]=cur; for(int j=0;j<m;j++) { if(A[idx][j]==0) { col[j]=row[idx]; } } cur++; } for(int i=0;i<m;i++) { if(col[i]==0) { col[i]=cur; } } out.println("Yes"); for(int i=0;i<n;i++) { out.print(row[i]+" "); } out.println(); for(int i=0;i<m;i++) { out.print(col[i]+" "); } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { if(st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
JAVA
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> #pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math") using namespace std; inline int POW(int a, int b = 1000000007 - 2, int ans = 1) { for (; b; b >>= 1, a = (long long)a * a % 1000000007) if (b & 1) ans = (long long)ans * a % 1000000007; return ans; } inline void rd(int &X) { X = 0; int w = 0; char ch = 0; while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); X = w ? -X : X; } int n, m; char a[2005][2005]; int f[2005]; int head[2005], cnt, d[2005], v[2005]; struct nd { int nxt, to; } e[2005 * 2005]; int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } void add(int x, int y) { x = find(x); y = find(y); e[++cnt] = (nd){head[y], x}; head[y] = cnt; d[x]++; } int dis[2005]; signed main() { cin >> n >> m; for (int i = 1; i <= n + m; i++) f[i] = i; for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] == '=') f[find(i)] = find(j + n); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] == '>') add(i, j + n); else if (a[i][j] == '<') add(j + n, i); queue<int> q; for (int i = 1; i <= n + m; i++) if (!d[find(i)] and !v[find(i)]) q.push(find(i)), v[find(i)] = 1; while (q.size()) { int x = q.front(); q.pop(); for (int y, i = head[x]; (y = e[i].to); i = e[i].nxt) { dis[y] = max(dis[y], dis[x] + 1); if (--d[y] == 0) q.push(y); } } for (int i = 1; i <= n + m; i++) if (d[i]) return cout << "No", 0; cout << "Yes\n"; for (int i = 1; i <= n; i++) printf("%d ", dis[find(i)] + 1); cout << endl; for (int i = 1; i <= m; i++) printf("%d ", dis[find(i + n)] + 1); }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 1e3 + 5; int n, m, par[2 * N], sz[2 * N]; int id[2 * N], ans[2 * N]; vector<int> g[2 * N]; bool vis[2 * N], in[2 * N]; char a[N][N]; int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); } void join(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (sz[u] < sz[v]) swap(u, v); par[v] = u, sz[u] += sz[v]; } void dfs(int node) { vis[node] = true; int c = 0; for (auto &i : g[node]) { if (!vis[i]) dfs(i); c = max(ans[find(i)], c); } ans[find(node)] = c + 1; } void dfs2(int node) { vis[node] = true; in[node] = true; for (auto &i : g[node]) { if (in[i]) { cout << "No"; exit(0); } if (!vis[i]) dfs2(i); } in[node] = false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; par[i] = i, sz[i] = 1; } for (int i = 0; i < m; i++) { par[n + i] = n + i; sz[n + i] = 1; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[i][j] == '=') join(i, n + j); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (a[i][j] == '=') continue; int u = find(i), v = find(n + j); if (u == v) return cout << "No", 0; if (a[i][j] == '<') g[v].push_back(u), id[u]++; else g[u].push_back(v), id[v]++; } for (int i = 0; i < n + m; i++) { dfs2(i); memset(vis, 0, sizeof vis); } for (int i = 0; i < n + m; i++) if (i == find(i) && !vis[i] && !id[i]) dfs(i); cout << "Yes\n"; for (int i = 0; i < n + m; i++) { if (i == n) cout << '\n'; cout << ans[find(i)] << " "; } return 0; }
CPP
1131_D. Gourmet choice
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n × m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then a_{ij} is equal to ">", in the opposite case a_{ij} is equal to "<". Dishes also may be equally good, in this case a_{ij} is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if a_{ij} is "<", then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if a_{ij} is ">", then it should be greater, and finally if a_{ij} is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers n and m (1 ≤ n, m ≤ 1000) — the number of dishes in both days. Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is a_{ij}. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set. Examples Input 3 4 &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; Output Yes 2 2 2 1 1 1 1 Input 3 3 &gt;&gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt; Output Yes 3 1 3 2 2 2 Input 3 2 == =&lt; == Output No Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 2, for all dishes of the first day. In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.
2
10
#include <bits/stdc++.h> using namespace std; struct Edge { int v, w; } edge[1000000 * 2 + 10]; int d[1010 * 2], cnt[1010 * 2]; vector<int> G[1010 * 2]; bool vis[1010 * 2]; bool spfa(int n, int m) { memset(cnt, 0, sizeof(cnt)); memset(d, 0, sizeof(d)); memset(vis, true, sizeof(vis)); queue<int> qu; for (int i = 1; i <= n + m; ++i) qu.push(i); while (!qu.empty()) { int v = qu.front(); qu.pop(); vis[v] = false; for (int i = 0; i < G[v].size(); ++i) { Edge &e = edge[G[v][i]]; if (d[e.v] > d[v] + e.w) { d[e.v] = d[v] + e.w; if (!vis[e.v]) { vis[e.v] = true; cnt[e.v]++; if (cnt[e.v] > n + m) return false; qu.push(e.v); } } } } return true; } int main(void) { int n, m; char op; scanf("%d%d", &n, &m); int edge_cnt = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf(" %c", &op); if (op == '=') { edge[edge_cnt].v = j + n; edge[edge_cnt].w = 0; G[i].push_back(edge_cnt++); edge[edge_cnt].v = i; edge[edge_cnt].w = 0; G[j + n].push_back(edge_cnt++); } else if (op == '>') { edge[edge_cnt].v = j + n; edge[edge_cnt].w = -1; G[i].push_back(edge_cnt++); } else { edge[edge_cnt].v = i; edge[edge_cnt].w = -1; G[j + n].push_back(edge_cnt++); } } } if (!spfa(n, m)) printf("NO\n"); else { printf("YES\n"); int minx = 0x3f3f3f3f; for (int i = 1; i <= n + m; ++i) minx = min(minx, d[i]); int add = minx <= 0 ? 1 - minx : 0; for (int i = 1; i <= n; ++i) printf("%d ", d[i] + add); printf("\n"); for (int i = n + 1; i <= n + m; ++i) printf("%d ", d[i] + add); printf("\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 N = 100 + 10; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; long long qpow(long long n, long long k) { long long res = 1; while (k) { if (k & 1) res = res * n % mod; n = n * n % mod; k >>= 1; } return res; } struct matrix { long long a[N][N]; int n; matrix() {} matrix(int k) { n = k; memset(a, 0, sizeof(a)); } void init() { for (int i = 0; i < n; i++) a[i][i] = 1; } 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; } matrix operator^(long long k) { matrix A = (*this), res(n); res.init(); while (k) { if (k & 1) res = res * A; A = A * A; k >>= 1; } return res; } void print() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << a[i][j] << ' '; } cout << endl; } } }; int arr[N]; int main() { int n, k; cin >> n >> k; int a = 0, b = 0; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) if (arr[i]) b++; else a++; matrix x(a + 1); long long t = (n - 1) * n / 2; t = qpow(t, mod - 2); for (int i = 0; i <= a; i++) { if (b - a + i < 0) continue; x.a[i][i] = 1ll * (a * (a - 1) / 2 + b * (b - 1) / 2 + i * (a - i) + (a - i) * (b - a + i)) % mod * t % mod; if (i + 1 <= a) x.a[i][i + 1] = 1ll * (a - i) * (a - i) * t % mod; if (i - 1 >= 0) x.a[i][i - 1] = 1ll * i * (b - a + i) * t % mod; } x = x ^ (k); int s = 0; for (int i = 1; i <= a; i++) if (arr[i] == 0) s++; cout << x.a[s][a] << 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 = (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; const int N = 110; const int mo = 1e9 + 7; int n, k, a[N], num[3], nn, f = 0; long long ans, now[N][N], dp[N][N], aa[N]; long long po(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; long long c = po(a, b / 2); if (b & 1) return c * c % mo * a % mo; else return c * c % mo; } long long tmp[N][N]; void multi(long long a[][N], long long b[][N], int n) { memset(tmp, 0, sizeof tmp); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j] % mo) % mo; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = tmp[i][j]; } long long res[N][N]; void Pow(long long a[][N], long long n) { memset(res, 0, sizeof res); for (int i = 0; i < num[0] + 1; i++) res[i][i] = 1; while (n) { if (n & 1) multi(res, a, num[0] + 1); multi(a, a, num[0] + 1); n >>= 1; } } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), num[a[i]]++; if (num[0] < num[1]) { for (int i = 1; i <= num[0]; i++) if (!a[i]) nn++; } else { swap(num[0], num[1]); for (int i = n; i >= n - num[0] + 1; i--) if (a[i]) nn++; } for (int j = 0; j <= num[0]; j++) { now[j][j] += j * (num[0] - j); now[j][j] += (num[0] - j) * (num[1] - num[0] + j); now[j][j] += num[0] * (num[0] - 1) / 2ll; now[j][j] += num[1] * (num[1] - 1) / 2ll; if (j + 1 <= num[0]) now[j][j + 1] = (j + 1) * (num[1] - (num[0] - j - 1)); if (j) now[j][j - 1] = (num[0] - j + 1) * (num[0] - j + 1); } Pow(now, k); for (int i = 0; i <= num[0]; i++) ans = (ans + res[i][nn]) % mo; printf("%lld\n", res[num[0]][nn] * po(ans, mo - 2) % mo); 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> bool debug = 0; const long long MOD = 1000000007; const double PI = acos(-1.0); const double eps = 1e-9; using namespace std; long long modmul(long long a, long long b) { return (a * b) % MOD; } long long modsum(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; } long long modsub(long long a, long long b) { return (((a % MOD) + (b % MOD)) + MOD) % MOD; } long long div2(long long a) { return (a * 500000004LL) % MOD; } template <typename T> struct matrix { static const int MAXDIM = 100; int n, m; T mat[matrix::MAXDIM][matrix::MAXDIM]; matrix(int n, int m) : n(n), m(m) { assert(n < MAXDIM && m < MAXDIM); } matrix() : n(0), m(0) {} static void writeIdent(matrix &I) { assert(I.n == I.m); for (int i = (0); i < (I.n); i++) for (int j = (0); j < (I.m); j++) { I.mat[i][j] = 0; if (i == j) I.mat[i][j] = 1; } } static matrix mul(matrix &a, matrix &b, matrix &r) { assert(a.m == b.n); r.n = a.n; r.m = b.m; for (int i = (0); i < (a.n); i++) for (int j = (0); j < (b.m); j++) { r.mat[i][j] = 0; for (int k = (0); k < (a.m); k++) r.mat[i][j] = modsum(r.mat[i][j], modmul(a.mat[i][k], b.mat[k][j])); } return r; } static matrix pow(matrix &b, long long e, matrix &raux) { assert(b.n == b.m); matrix res(b.n, b.m); matrix::writeIdent(res); while (e > 0) { if (e & 1LL) res.cpy(matrix::mul(res, b, raux)); e >>= 1; b.cpy(matrix::mul(b, b, raux)); } return res; } void cpy(const matrix &b) { this->n = b.n; this->m = b.m; for (int i = (0); i < (b.n); i++) for (int j = (0); j < (b.m); j++) this->mat[i][j] = b.mat[i][j]; } void print() { const char s1[] = "%lld ", s2[] = "%d "; const char *s = is_same<T, long long>::value ? s1 : s2; for (int i = (0); i < (this->n); i++) { for (int j = (0); j < (this->m); j++) printf(s, this->mat[i][j]); printf("\n"); } printf("\n"); } }; int n; long long totInv; int z, u; long long fastPow(long long b, int e) { long long res = 1; while (e > 0) { if (e & 1) res = modmul(res, b); b = modmul(b, b); e >>= 1; } return res; } inline long long z0(long long z1) { return z - z1; } inline long long u1(long long z1) { return u - z1; } inline long long u0(long long z1) { return z1; } long long probInv(long long x) { return modmul(totInv, x); } long long fa(long long z1) { return probInv(modmul(z1, u0(z1))); } long long fb(long long z1) { return probInv( modsum(modmul(u1(z1), z1), modsum(modmul(u0(z1), z0(z1)), modsum(div2(modmul(z, z - 1)), div2(modmul(u, u - 1)))))); } long long fc(long long z1) { return probInv(modmul(z0(z1), u1(z1))); } long long calc(int k, int z1) { int sz = min(z, u) + 1; matrix<long long> b(sz, sz); for (int i = (0); i < (sz); i++) { if (i - 1 >= 0) b.mat[i][i - 1] = fa(i); b.mat[i][i] = fb(i); if (i + 1 < sz) b.mat[i][i + 1] = fc(i); } matrix<long long> aux, res; res = matrix<long long>::pow(b, k, aux); return res.mat[z1][0]; } int main() { int k; cin >> n >> k; totInv = fastPow(div2(modmul(n, n - 1)), MOD - 2); int v[110]; for (int i = (0); i < (n); i++) { cin >> v[i]; if (v[i] == 0) z++; else u++; } int z1 = 0; for (int i = (0); i < (n); i++) { if (i >= z) break; if (v[i] == 1) z1++; } cout << calc(k, z1) << 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; mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); long long nn, MOD = 1e9 + 7; vector<vector<long long> > matMul(vector<vector<long long> > &a, vector<vector<long long> > &b) { vector<vector<long long> > c(nn, vector<long long>(nn, 0)); int k; for (int i = 0; i < nn; i++) for (int j = 0; j < nn; j++) for (k = 0; k < nn; k++) { c[i][j] += (a[i][k] % MOD) * (b[k][j] % MOD); c[i][j] %= MOD; } return c; } vector<vector<long long> > matPow(vector<vector<long long> > base, long long p) { vector<vector<long long> > ans(nn, vector<long long>(nn, 0)); int i, j; for (i = 0; i < nn; i++) for (j = 0; j < nn; j++) ans[i][j] = (i == j); while (p) { if (p & 1) ans = matMul(ans, base); base = matMul(base, base); p >>= 1; } return ans; } inline long long pw(long long a, long long b) { if (b == 0) return 1; long long v = pw(a, b / 2); v = (v * v) % MOD; if (b & 1) v = (v * a) % MOD; return v; } int main() { ios::sync_with_stdio(0); int n, k; cin >> n >> k; vector<long long> al(n); for (auto &e : al) cin >> e; int toto = 0, totz, nowz = 0; for (long long i = 0; i < n; i++) toto += al[i]; totz = n - toto; for (long long i = 0; i < totz; i++) nowz += al[i] == 0; vector<vector<long long> > mat(totz + 1, vector<long long>(totz + 1, 0)); for (long long i = 0; i < totz + 1; i++) { if (toto < totz - i) continue; if (i && toto >= totz - (i - 1)) mat[i - 1][i] = (totz - (i - 1)) * (totz - (i - 1)); if (i < totz && toto >= totz - (i + 1)) mat[i + 1][i] = (i + 1) * (toto - (totz - (i + 1))); mat[i][i] = ((totz * (totz - 1)) / 2 + (toto * (toto - 1) / 2)) % MOD; (mat[i][i] += i * (totz - i) + (totz - i) * (toto - (totz - i))) %= MOD; } nn = totz + 1; vector<vector<long long> > ans = matPow(mat, k); vector<vector<long long> > bse(totz + 1, vector<long long>(totz + 1, 0)); bse[0][nowz] = 1; vector<vector<long long> > final = matMul(bse, ans); long long des = final[0][totz], total = 0; for (auto &x : final) for (auto &c : x) (total += c) %= MOD; cout << (des * pw(total, 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; 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 = 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 MAXN = 105; const int MOD = 1e9 + 7; struct Mat { long long a[MAXN][MAXN]; int n; Mat(int n) : n(n) { memset(a, 0, sizeof(a)); } }; Mat operator*(Mat a, Mat b) { Mat c(a.n); for (int i = 0; i <= a.n; i++) { for (int j = 0; j <= a.n; j++) { for (int k = 0; k <= a.n; k++) { c.a[i][j] += a.a[i][k] * b.a[k][j] % MOD; c.a[i][j] %= MOD; } } } return c; } Mat exp_mod(Mat a, long long b) { Mat ret(a.n); for (int i = 0; i <= a.n; i++) { ret.a[i][i] = 1; } while (b) { if (b & 1) ret = ret * a; a = a * a; b >>= 1; } return ret; } long long exp_mod(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; } int a[MAXN]; int c0, c1, cnt; int main(void) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i]) c1++; else c0++; } cnt = c1; for (int i = n - c1 + 1; i <= n; i++) { if (a[i]) cnt--; } Mat A(n); for (int i = 0; i <= n; i++) { A.a[i][i] = c0 * (c0 - 1) / 2 + c1 * (c1 - 1) / 2; if (c0 > i) A.a[i][i] += (c0 - i) * i; if (c1 > i) A.a[i][i] += (c1 - i) * i; if (i < n) { if (c1 > i && c0 > i) A.a[i][i + 1] = (c1 - i) * (c0 - i); } if (i > 0) { A.a[i][i - 1] = i * i; } } A = exp_mod(A, k); long long ans = A.a[cnt][0] * exp_mod(exp_mod(n * (n - 1) / 2, 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; void guan() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const int maxn = 123456; const int mod = 1e9 + 7; int a[maxn]; int dp[110][110][110]; class Matrix { public: long long m[105][105]; Matrix() { memset(m, 0, sizeof(m)); ; } Matrix(int mk) { memset(m, 0, sizeof(m)); ; for (int i = 0; i <= 200; i++) m[i][i] = 1; } Matrix(int a[][205]) { for (int i = 0; i <= 100; i++) for (int j = 0; j <= 100; j++) m[i][j] = a[i][j]; } long long* operator[](int pos) { return m[pos]; } }; Matrix mul(Matrix a, Matrix b) { Matrix c; for (int i = 0; i <= 100; i++) for (int j = 0; j <= 100; j++) for (int k = 0; k <= 100; k++) { c[i][j] += a[i][k] * b[k][j] % mod; c[i][j] %= mod; } return c; } long long qpow(long long a, long long n) { long long sum = 1; while (n) { if (n & 1) sum = sum * a % mod; a = a * a % mod; n >>= 1; } return sum; } Matrix mul_qpow(Matrix a, long long n) { Matrix e(1); while (n) { if (n & 1) e = mul(e, a); a = mul(a, a); n >>= 1; } return e; } void DP(int L, int R, int n, int K) { int one = 0; for (int i = 1; i <= L; i++) if (a[i]) one++; dp[one][R - one][0] = 1; long long Q = qpow(n * (n - 1) / 2, K); Q = qpow(Q, mod - 2); Matrix ans, mat; ans[0][R - one] = 1; for (int c = 0; c <= R; c++) { int i = R - c, j = c; if (i > L) continue; if (i < L && j > 0) mat[c - 1][c] = (i + 1) * (R - j + 1) % mod; if (i > 0 && j < R) mat[c + 1][c] = (L - i + 1) * (j + 1) % mod; mat[c][c] = (1ll * 2 * i * j + L * R - i * R - j * L + L * (L - 1) / 2 + R * (R - 1) / 2); } mat = mul_qpow(mat, K); ans = mul(ans, mat); cout << ans[0][R] % mod * Q % mod << endl; } void solve(int n, int k) { int one = 0; for (int i = 1; i <= n; i++) if (a[i]) one++; int R = one, L = n - one; memset(dp, 0, sizeof(dp)); ; DP(L, R, n, k); } int main() { guan(); int T = 1; int n, k; while (cin >> n >> k) { for (int i = 1; i <= n; i++) cin >> a[i]; solve(n, k); } 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.util.*; import java.io.*; public class F553 { public static void main(String[] args) throws Exception { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); long k = sc.nextLong(); int [] arr = new int[n]; int a = 0; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); if (arr[i] == 0) a++; } int b = n - a; initFac(104); dimension = a + 1; memo = new long[maxPow][dimension][dimension]; long [][] matrix = new long[dimension][dimension]; for (int i = 0; i < dimension; i++) { // dp[i] = ... long choose = (n * (n - 1)) / 2; if (i > 0) matrix[i][i - 1] = ((i * (b - a + i)) * inv(choose)) % mod; if (i < a) matrix[i][i + 1] = ((a - i) * (a - i) * inv(choose)) % mod; long num = choose - (i * (b - a + i) + (a - i) * (a - i)); matrix[i][i] = (num * inv(choose)) % mod; } powers(matrix); int start = 0; for (int i = 0; i < a; i++) { if (arr[i] == 0) start++; } long [][] res = exp(matrix, k); long [] vector = new long[dimension]; vector[a] = 1; vector = transform(vector, res); out.println(vector[start]); out.close(); } static long mod = (long) 1e9 + 7; static long [][][] memo; static int maxPow = 30; static int dimension; static long [][] multiply(long [][] a, long [][] b) throws Exception { if (a[0].length != b.length) { throw new Exception("Matrices are not compatible."); } long [][] ret = new long[a.length][b[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < b[0].length; j++) { long product = 0; for (int k = 0; k < a.length; k++) { product = (product + a[i][k] * b[k][j]) % mod; } ret[i][j] = product; } } return ret; } static long[][] exp(long [][] a, long pow) throws Exception { long [][] ret = getIdentity(dimension); for (int i = 0; i < maxPow; i++) { if (((pow) & (1L << i)) >> i == 1) { ret = multiply(ret, memo[i]); } } return ret; } static void powers(long [][] a) throws Exception { for (int i = 0; i < maxPow; i++) { memo[i] = a; a = square(a); } } static long [][] square(long [][] a) throws Exception { return multiply(a, a); } static long [][] getIdentity(int dimension) { long [][] I = new long[dimension][dimension]; for (int i = 0; i < dimension; i++) { I[i][i] = 1; } return I; } static long[] transform(long [] vector, long [][] matrix) throws Exception { if (vector.length != matrix[0].length) { throw new Exception("Dimensions are not compatible."); } long [] res = new long[vector.length]; for (int i = 0; i < vector.length; i++) { long sum = 0; for (int j = 0; j < vector.length; j++) { sum = (sum + matrix[i][j] * vector[j]) % mod; } res[i] = sum; } return res; } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static long[] fac; static void initFac(long n) { fac = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; } } static long nck(int n, int k) { if (n < k) return 0; long den = inv((int) (fac[k] * fac[n - k] % mod)); return fac[n] * den % mod; } static long pow(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) ans = ans * b % mod; e >>= 1; b = b * b % mod; } return ans; } static long inv(long x) { return pow(x, mod - 2); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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 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++) for (int j = 0; j <= n; 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> #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]; 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'; 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 str[101]; int dp[101][101][101]; long long num[51][51]; int n, m; int b, w; namespace MI { void A(long long &a, long long b) { a += b; if (a >= mod) a -= mod; } long long qmul(long long a, long long b) { long long res = 0; while (b) { if (b & 1) A(res, a); A(a, a); b >>= 1; } return res; } long long qpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = qmul(res, a); a = qmul(a, a); b >>= 1; } return res; } }; // namespace MI namespace Mat_pow { struct Mat { long long m[101][101]; }; Mat a, e; long long n, p; Mat Mul(Mat x, Mat y) { Mat c; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) c.m[i][j] = 0; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) c.m[i][j] = c.m[i][j] % mod + x.m[i][k] * y.m[k][j] % mod; return c; } Mat pow(Mat x, long long y) { Mat ans = e; while (y) { if (y & 1) ans = Mul(ans, x); x = Mul(x, x); y >>= 1; } return ans; } void init() { for (int i = 0; i <= n; i++) { e.m[i][i] = 1; } } void print() { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) cout << a.m[i][j] << " "; cout << endl; } } void inp(int N, int mod) { n = N; p = mod; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { a.m[i][j] = num[i][j]; } } init(); } long long func(long long k, int s) { a = pow(a, k); return a.m[0][s]; } }; // namespace Mat_pow int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> str[i]; } int s = 0; b = 0; w = 0; for (int i = 0; i < n; i++) { if (str[i] == 0) b++; else w++; } for (int i = 0; i < b; i++) { if (str[i] == 1) s++; } if (min(w, b) == 0) { cout << 1 << endl; return 0; } for (int i = 0; i <= min(w, b); i++) { if (i) num[i - 1][i] = i * i * 2, num[i][i] -= num[i - 1][i]; if (i < min(w, b)) num[i + 1][i] = (w - i) * (b - i) * 2, num[i][i] -= num[i + 1][i]; num[i][i] += n * (n - 1); } Mat_pow::inp(min(w, b), mod); long long res = Mat_pow::func(m, s); cout << res * MI::qpow(MI::qpow(n * n - n, mod - 2), m) % 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; const int N = 200; const int mod = 1e9 + 7; int inf = 1000000000; unsigned long long g[N][N], res[N][N]; unsigned long long dp[N]; int a[N]; int n; unsigned long long k; void mul(unsigned long long a[N][N], unsigned long long b[N][N], unsigned long long c[N][N]) { unsigned long long res[N][N]; memset(res, 0, sizeof(res)); for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % mod; } } } for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) c[i][j] = res[i][j]; } void pw(unsigned long long a[N][N], unsigned long long b, unsigned long long c[N][N]) { unsigned long long res[N][N]; memset(res, 0, sizeof(res)); for (int i = 0; i < n; ++i) res[i][i] = 1; while (b > 0) { if (b & 1) mul(res, a, res); mul(a, a, a); b >>= 1; } for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { c[i][j] = res[i][j]; } } unsigned long long pw(unsigned long long a, unsigned long long b) { unsigned long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } int main() { cin >> n >> k; int x = 0, y = 0; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); if (a[i] == 0) x++; else y++; } int num = 0; for (int i = 0; i < n; ++i) { if (i >= x && a[i] == 1) num++; } memset(dp, 0, sizeof(dp)); dp[num] = 1; memset(g, 0, sizeof(g)); for (int i = 0; i <= y; ++i) { g[i][i] = max(0, (n * (n - 1) / 2) - i * (x - (y - i)) - (y - i) * (y - i)); if (i) g[i][i - 1] = max(0, (y - i + 1) * (y - i + 1)); if (i + 1 <= y) g[i][i + 1] = max(0, (i + 1) * (x - (y - i - 1))); } n = y + 1; pw(g, k, res); unsigned long long tot = 0; for (int i = 0; i <= y; ++i) { dp[i] = (res[i][num]) % mod; tot = (tot + res[i][num]) % mod; } unsigned long long ans = (dp[y] * (pw(tot, mod - 2))) % mod; 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; inline void read(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void read(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void readc(char &x) { char c; while (((c = getchar()) == ' ') || c == '\n') ; x = c; } const int mod = 1e9 + 7; int n, m, i, j, c0, t, a[105]; struct mat { int a[105][105]; } g; mat mul(mat x, mat y) { mat z; int i, j, k; for (((i)) = (0); ((i)) <= (((int)(c0 + 1)) - 1); ((i))++) for (((j)) = (0); ((j)) <= (((int)(c0 + 1)) - 1); ((j))++) { z.a[i][j] = 0; for (((k)) = (0); ((k)) <= (((int)(c0 + 1)) - 1); ((k))++) { z.a[i][j] = (z.a[i][j] + 1ll * x.a[i][k] * y.a[k][j]) % mod; } } return z; } mat pw(mat x, int y) { mat z; int i, j; for (((i)) = (0); ((i)) <= (((int)(c0 + 1)) - 1); ((i))++) for (((j)) = (0); ((j)) <= (((int)(c0 + 1)) - 1); ((j))++) { z.a[i][j] = (i == j); } while (y) { if (y & 1) { z = mul(z, x); } x = mul(x, x); y /= 2; } return z; } int pw(int x, int y) { int z = 1; while (y) { if (y & 1) z = 1ll * z * x % mod; x = 1ll * x * x % mod; y /= 2; } return z; } int main() { read(n); read(m); for (((i)) = (1); ((i)) <= ((n)); ((i))++) { read(a[i]); if (a[i] == 0) c0++; } for (((i)) = (1); ((i)) <= ((c0)); ((i))++) t += (a[i] == 0); for (((i)) = (0); ((i)) <= (((int)(c0 + 1)) - 1); ((i))++) { int l0 = i; int r0 = c0 - i; int l1 = c0 - i; int r1 = n - l0 - l1 - r0; int s = n * (n - 1) / 2; if (i) { g.a[i][i - 1] = l0 * r1; s -= l0 * r1; } if (i < c0) { g.a[i][i + 1] = l1 * r0; s -= l1 * r0; } g.a[i][i] = s; } g = pw(g, m); cout << 1ll * g.a[t][c0] * pw(n * (n - 1) / 2, mod - 1 - m) % 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; const int iinf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3f3f3f3f3f; const int N = 100; const int mod = 1e9 + 7; int n, k, a[N], zn, cn, izn; int Pow(int a, int first) { int res = 1; for (; first; a = 1ll * a * a % mod, first >>= 1) if (first & 1) res = 1ll * res * a % mod; return res; } struct Matrix { int n, m; vector<vector<int>> arr; Matrix(int n, int m) : n(n), m(m) { arr.resize(n); for (int i = (0), I = (n); i < I; i++) arr[i].assign(m, 0); } vector<int>& operator[](int i) { return arr[i]; } friend Matrix operator*(Matrix p, Matrix q) { Matrix res(p.n, q.m); for (int mid = (0), I = (p.m); mid < I; mid++) for (int i = (0), I = (p.n); i < I; i++) for (int j = (0), I = (q.m); j < I; j++) (res[i][j] += 1ll * p[i][mid] * q[mid][j] % mod) %= mod; return res; } }; Matrix Pow(Matrix a, int first) { Matrix res(a.n, a.n); for (int i = (0), I = (a.n); i < I; i++) res[i][i] = 1; for (; first; a = a * a, first >>= 1) if (first & 1) res = res * a; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = (0), I = (n); i < I; i++) cin >> a[i], zn += (a[i] == 0); for (int i = (0), I = (zn); i < I; i++) cn += (a[i] == 0); Matrix f(1, zn + 1), g(zn + 1, zn + 1); f[0][cn] = 1, izn = Pow(n * (n - 1) / 2, mod - 2); for (int c = (0), I = (zn + 1); c < I; c++) { g[c][c] = 1; if (c < zn) g[c][c + 1] = 1ll * (zn - c) * (zn - c) % mod * izn % mod, (g[c][c] += mod - g[c][c + 1]) %= mod; if (c > 0) g[c][c - 1] = 1ll * c * (n - 2 * zn + c) % mod * izn % mod, (g[c][c] += mod - g[c][c - 1]) %= mod; } f = f * Pow(g, k); cout << f[0][zn] << '\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 java.io.*; import java.util.*; public class Main implements Runnable{ public static void main(String[] args){ //new Thread(null,new Main(),"Thread-1",1024*1024*10).start(); new Main().run(); } int[]a; int tot=0; long[]inv; int mod=1000000007; private void solve() throws Exception { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n=in.nextInt(),k=in.nextInt(); a=new int[n]; inv=getInvs(10000,mod); for(int i=0;i<n;i++){ a[i]=in.nextInt(); if(a[i]==1)tot++; } int where=0; for(int i=0;i<n-tot;i++){ if(a[i]==1)where++; } int len=Math.min(n-tot,tot); long[][]matrix=new long[len+1][len+1]; long all=(long)n*(n-1)*inv[2]%mod; long invall=inv[(int)all]; for(int i=0;i<=len;i++){ long cnt=0; if(i>0){ matrix[i-1][i]=i*i*invall%mod; cnt+=i*i; } if(i<len){ matrix[i+1][i]=(tot-i)*(n-tot-i)*invall%mod; cnt+=(tot-i)*(n-tot-i); } matrix[i][i]=((all-cnt)%mod+mod)%mod*invall%mod; } long[][]ans=pow(matrix,k); out.println(ans[0][where]); out.flush(); } public long[][]pow(long[][]a,int b){ long[][]ans=new long[a.length][a.length]; for(int i=0;i<a.length;i++)ans[i][i]=1; while(b>0){ if((b&1)==1)ans=modMul(ans,a); a=modMul(a,a); b>>=1; } return ans; } public long[][] modMul(long[][]a,long[][]b){ int row=a.length,column=b[0].length; int len=b.length; long[][] ans=new long[row][column]; for(int i=0;i<row;i++){ for(int j=0;j<len;j++){ if(a[i][j]==0)continue;//稀疏矩阵优化 for(int k=0;k<column;k++){ ans[i][k]+=a[i][j]*b[j][k]%mod; ans[i][k]%=mod; } } } return ans; } public static long[] getInvs(int n,int mod){ long[]inv=new long[n+1]; inv[1]=1; for(int i=2;i<inv.length;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; return inv; } @Override public void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); } } class InputReader{ StreamTokenizer tokenizer; public InputReader(InputStream stream){ tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream))); tokenizer.ordinaryChars(33,126); tokenizer.wordChars(33,126); } public String next() throws IOException { tokenizer.nextToken(); return tokenizer.sval; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean hasNext() throws IOException { int res=tokenizer.nextToken(); tokenizer.pushBack(); return res!=tokenizer.TT_EOF; } } }
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.util.*; import java.io.*; public class EdC { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; static int y; public static void main(String[] omkar) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); int n = sc.nextInt(); long k = sc.nextLong(); int[] array = readArrayInt(n); y = 0; int initial = 0; for(int j =0;j<n;j++) if (array[j] == 1) y++; for(int j =n-y;j<n;j++) if (array[j] == 1) initial++; long[][] matrix = new long[y+1][y+1]; for(int j = 0;j<=y;j++){ long s = n; s*=(long)(n-1); s/=2; if (j-1 >=0){ matrix[j-1][j]=j; matrix[j-1][j]%=mod; matrix[j-1][j]*=(long)(n-2*y+j); matrix[j-1][j]%=mod; s-=matrix[j-1][j]; s%=mod; s+=mod; s%=mod; } if (j+1 <= y){ matrix[j+1][j]=(y-j); matrix[j+1][j]%=mod; matrix[j+1][j]*=(long)(y-j); matrix[j+1][j]%=mod; s-=matrix[j+1][j]; s%=mod; s+=mod; s%=mod; } matrix[j][j] = s; } long[][] product = power(matrix, k); long ans = product[y][initial]; ans%=mod; long nC2 = n; nC2*=(long)(n-1); nC2/=2L; long div = power(nC2, mod-2); ans*=power(div, k); ans%=mod; out.println(ans); out.close(); } public static long[][] mul(long[][] m1, long[][] m2){ long[][] ans = new long[y+1][y+1]; for(int j =0;j<=y;j++){ for(int k = 0;k<=y;k++){ for(int l = 0;l<=y;l++){ ans[j][l]+=m1[j][k]*m2[k][l]; ans[j][l]%=mod; } } } return ans; } public static long[][] power(long[][] m1, long k){ if (k == 0){ long[][] mst = new long[y+1][y+1]; for(int j =0 ;j<=y;j++){ mst[j][j] = 1L; } return mst; } if (k == 1) return m1; long [][]ks = power(m1, k/2L); if (k%2 == 0) return mul(ks, ks); else return mul(mul(ks, ks), m1); } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<Integer>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
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") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimization("unroll-loops") using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\t"; err(++it, args...); } template <typename T1, typename T2> ostream& operator<<(ostream& c, pair<T1, T2>& v) { c << "(" << v.first << "," << v.second << ")"; return c; } template <template <class...> class TT, class... T> ostream& operator<<(ostream& out, TT<T...>& c) { out << "{ "; for (auto& x : c) out << x << " "; out << "}"; return out; } const int LIM = 1e5 + 5, MOD = 1e9 + 7; const long double EPS = 1e-9; const long long MAX_N = 105; struct Matrix { long long mat[MAX_N][MAX_N]; }; Matrix matMul(Matrix a, Matrix b) { Matrix ans; int i, j, k; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) for (ans.mat[i][j] = k = 0; k < MAX_N; k++) { ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD; ans.mat[i][j] %= MOD; } return ans; } Matrix matPow(Matrix base, long long p) { p %= MOD; Matrix ans; long long i, j; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j); while (p) { if (p & 1) ans = matMul(ans, base); base = matMul(base, base); p >>= 1; } return ans; } int fpow(int a, int p) { if (p == 0) return 1; long long z = fpow(a, p / 2); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> v; for (int i = 0; i < n; ++i) { int x; cin >> x; v.push_back(x); } reverse(v.begin(), v.end()); long long tot = 0; for (int i = 0; i < n; ++i) { if (v[i]) tot++; } long long cnt = 0; for (int i = 0; i < tot; ++i) { if (v[i]) cnt++; } Matrix m; for (int i = 0; i < tot + 1; ++i) { for (int j = 0; j < tot + 1; ++j) { if (i + n - tot < tot) { m.mat[i][j] = 0; continue; } if (abs(i - j) > 1) m.mat[i][j] = 0; else if (i == j) { m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 + i * (tot - i) + (tot - i) * (n - 2 * tot + i); } else if (i - j == 1) { m.mat[i][j] = (i) * (n - 2 * tot + i); } else if (j - i == 1) { m.mat[i][j] = (tot - i) * (tot - i); } m.mat[i][j] %= MOD; } } Matrix ans = matPow(m, k); long long a1 = 0, a2 = 0; a1 = ans.mat[cnt][tot]; for (int i = 0; i < tot + 1; ++i) { a2 += ans.mat[cnt][i]; a2 %= MOD; } long long ansf = 0; ansf = a1; ansf *= fpow(a2, MOD - 2); cout << (ansf) % MOD << '\n'; return 0; }
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> const int M = 105; const int MOD = 1e9 + 7; long long read() { long long x = 0, f = 1; char c; while ((c = getchar()) < '0' || c > '9') { if (c == '-') f = -1; } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } long long n, m, k, t, t1, t2, a[M]; struct Matrix { long long n, m, a[M][M]; Matrix() { n = m = 0; memset(a, 0, sizeof a); } void clear() { memset(a, 0, sizeof a); } Matrix operator*(const Matrix &b) const { Matrix r; r.n = n; r.m = b.m; for (long long i = 0; i <= n; i++) for (long long j = 0; j <= m; j++) for (long long k = 0; k <= b.m; k++) r.a[i][k] = (r.a[i][k] + a[i][j] * b.a[j][k]) % MOD; return r; } void print() { for (long long i = 1; i <= n; i++, puts("")) for (long long j = 1; j <= m; j++) printf("%lld ", a[i][j]); } } A, F; Matrix qkpow(Matrix a, long long b) { Matrix r; r.n = r.m = a.n; for (long long i = 1; i <= a.n; i++) r.a[i][i] = 1; while (b > 0) { if (b & 1) r = r * a; a = a * a; b >>= 1; } return r; } long long fast(long long a, long long b) { long long r = 1; while (b > 0) { if (b & 1) r = r * a % MOD; a = a * a % MOD; b >>= 1; } return r; } signed main() { n = read(); k = read(); for (long long i = 1; i <= n; i++) { a[i] = read(); if (!a[i]) m++; } for (long long i = 1; i <= m; i++) if (!a[i]) t++; A.n = A.m = F.n = m; F.m = 1; if (m == 0 || m == n) { puts("1"); return 0; } t1 = m * (m - 1) / 2; t2 = (n - m) * (n - m - 1) / 2; long long inv = fast(n * (n - 1) / 2, MOD - 2); for (long long i = 0; i <= m; i++) { if (i > 0) A.a[i][i - 1] = (m - (i - 1)) * (m - (i - 1)); A.a[i][i] = t1 + t2 + i * (m - i) + (m - i) * (n - 2 * m + i); if (i < m) A.a[i][i + 1] = (i + 1) * (n - 2 * m + (i + 1)); } F.a[t][1] = 1; A = qkpow(A, k); F = A * F; printf("%lld\n", F.a[m][1] * fast(inv, 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; 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) { if (!a[i][k] || !b[k][j]) continue; t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]); } } } for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { a[i][j] = t[i][j]; } } } void pw(long long p) { for (long long i = 0; i < N; ++i) ans[i][i] = 1; for (long long i = 0; (1 << i) <= p; ++i) { if ((p >> i) & 1) add(ans, m); add(m, m); } } bool a[N]; long long cnt[2]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; for (long long i = 0; i < n; ++i) { cin >> a[i]; ++cnt[a[i]]; } long long l = cnt[0]; long long r = n - l; long long op = n * (n - 1) / 2; for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) { long long l0 = l - l1; long long r0 = cnt[0] - l0; long long r1 = cnt[1] - l1; m[l1][l1] = op; if (l1) { m[l1][l1 - 1] = mod(l1 * r0); m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]); } m[l1][l1 + 1] = mod(l0 * r1); m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]); } pw(k); long long sum = 0; for (long long i = 0; i < l; ++i) sum += a[i]; cout << dv(ans[sum][0], fp(op, k)) << '\n'; }
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; int a[maxn]; long long dp[maxn]; struct mat { long long a[maxn][maxn]; int l, r; void init(int _l, int _r) { memset(a, 0, sizeof(a)); l = _l; r = _r; } }; mat operator*(mat X, mat Y) { mat Z; Z.l = X.l; Z.r = Y.r; for (int i = 0; i < X.l; i++) for (int j = 0; j < Y.r; j++) { Z.a[i][j] = 0; for (int k = 0; k < X.r; k++) Z.a[i][j] = (Z.a[i][j] + X.a[i][k] * Y.a[k][j] % MOD) % MOD; } return Z; } int n, k, cnt = 0, pre = 0; mat trans, iden; inline long long f10(int j) { return (cnt - j) * (cnt - j); } inline long long f01(int j) { return (n + j - 2 * cnt) * j; } inline long long f_eq(int j) { return n * (n - 1) / 2 - f10(j) - f01(j); } void debug(mat trans) { for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= cnt; j++) cout << trans.a[i][j] << " "; cout << endl; } } void init() { trans.init(cnt + 1, cnt + 1); iden.init(cnt + 1, cnt + 1); for (int j = 0; j <= cnt; j++) { iden.a[j][j] = 1; if (n + j - 2 * cnt < 0) continue; trans.a[j][j] = f_eq(j); if (j > 0) trans.a[j][j - 1] = f01(j); if (j < cnt) trans.a[j][j + 1] = f10(j); } } void mult() { while (k) { if (k & 1) iden = iden * trans; trans = trans * trans; k >>= 1; } } long long _pow(long long a, int k) { long long base = a, ans = 1; while (k) { if (k & 1) ans = ans * base % MOD; base = base * base % MOD; k >>= 1; } return ans; } long long cpow(long long b, long long e) { long long ret = 1LL; while (e) { ret = e & 1 ? ret * b % MOD : ret; b = b * b % MOD; e >>= 1; } return ret; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (!a[i]) cnt++; } for (int i = 0; i < cnt; i++) if (!a[i]) pre++; init(); mult(); long long P = 0, Q = 0; for (int i = 0; i <= cnt; i++) Q = (Q + iden.a[pre][i] % MOD) % MOD; P = iden.a[pre][cnt] % MOD; long long Q_1 = _pow(Q, MOD - 2); cout << P * Q_1 % 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; const int N = 105; const int mod = 1e9 + 7; int n, k, m, sta, a[N]; int fastpow(int x, int y) { int res = 1; while (y) { if (y & 1) res = 1ll * res * x % mod; x = 1ll * x * x % mod; y >>= 1; } return res; } struct mat { int a[N][N]; mat() { memset(a, 0, sizeof(a)); } int *operator[](int x) { return a[x]; } mat operator*(mat b) { mat c; for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) for (int k = 0; k <= m; ++k) c[i][k] = (c[i][k] + 1ll * a[i][j] * b[j][k]) % mod; return c; } } S, T; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), m += a[i]; for (int i = n - m + 1; i <= n; ++i) sta += a[i]; S[0][sta] = 1; for (int i = max(0, m + m - n), p, q, inv = fastpow(n * (n - 1) >> 1, mod - 2); i <= m; ++i) { p = 1ll * (n - m - m + i) * i * inv % mod; q = 1ll * (m - i) * (m - i) * inv % mod; if (i) T[i][i - 1] = p; if (i < m) T[i][i + 1] = q; T[i][i] = (mod + mod + 1 - p - q) % mod; } while (k) { if (k & 1) S = S * T; T = T * T; k >>= 1; } printf("%d\n", S[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> 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 = 102; 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 = 105, LOGK = 31; const long long MOD = 1e9 + 7; long long n, k, tab[N], l, dp[LOGK][N][N], pocz, ost[N][N], ost2[N][N]; long long pot(long long a, long long p) { if (p == 0) return 1; long long w = pot(a, p / 2); w = w * w % MOD; if (p % 2) w = w * a % MOD; return w; } int main() { scanf("%lld%lld", &n, &k); for (int i = 1; i <= n; i++) { scanf("%lld", &tab[i]); if (tab[i] == 0) l++; } for (int i = 1; i <= l; i++) if (tab[i] == 0) pocz++; int mini = max((long long)0, 2 * l - n); for (long long i = mini; i <= l; i++) { dp[0][i][i] = l * (l - 1) / 2 + (n - l) * (n - l - 1) / 2 + i * (l - i) + (l - i) * (n - 2 * l + i); if (i != mini) dp[0][i][i - 1] = i * (n - 2 * l + i); if (i != l) dp[0][i][i + 1] = (l - i) * (l - i); } for (int i = 1; i < LOGK; i++) { for (int p = mini; p <= l; p++) for (int k = mini; k <= l; k++) for (int j = mini; j <= l; j++) dp[i][p][k] = (dp[i][p][k] + dp[i - 1][p][j] * dp[i - 1][j][k]) % MOD; } for (int i = mini; i <= l; i++) ost[i][i] = 1; for (int i = 0; i < LOGK; i++) if (k & (1 << i)) { for (int p = mini; p <= l; p++) for (int k = mini; k <= l; k++) { ost2[p][k] = ost[p][k]; ost[p][k] = 0; } for (int p = mini; p <= l; p++) for (int k = mini; k <= l; k++) for (int j = mini; j <= l; j++) ost[p][k] = (ost[p][k] + ost2[p][j] * dp[i][j][k]) % MOD; } long long sum = pot(n * (n - 1) / 2, k); long long ans = ost[pocz][l]; ans = ans * pot(sum, MOD - 2) % MOD; printf("%lld", 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
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static final long MOD = 1_000_000_007 , INF = 1_000_000_000_000_000L; static final int INf = 1_000_000_000; static FastReader reader; static PrintWriter writer; public static void main(String[] args) { Thread t = new Thread(null, new O(), "Integer.MAX_VALUE", 100000000); t.start(); } static class O implements Runnable { public void run() { try { magic(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } static class FastReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(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[1000000]; 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(); } } static void magic() throws IOException { reader = new FastReader(); writer = new PrintWriter(System.out, true); int n = reader.nextInt(), k = reader.nextInt(); int arr[] = new int[n], curr = 0, x = 0; for(int i=0;i<n;++i) { arr[i] = reader.nextInt(); if(arr[i]==0) { ++curr; } } for(int i=0;i<curr;++i) { if(arr[i]==0) { ++x; } } //writer.println("curr: "+curr+" x: "+x); long a[] = new long[curr+1]; a[x] = 1; long M[][] = new long[curr+1][curr+1]; for(int i=0;i<=curr;++i) { M[i][i] = C2(curr) + C2(n-curr) + i*(curr-i) + (curr-i) * (n-curr-(curr-i)); if(i>0) { M[i-1][i] = (curr-i+1)*(curr-i+1); } if(i+1<=curr) { M[i+1][i] = (i+1) * (n-curr-(curr-(i+1))); } } M = exponentiateMatrix(M, curr+1,k); long ans[] = new long[curr+1]; for(int i=0;i<=curr;++i) { for(int j=0;j<=curr;++j) { ans[i] += a[j]*M[j][i]; ans[i] %= MOD; } } long nr = ans[curr]; long dr = 0; for(int i=0;i<=curr;++i) { dr+=ans[i]; dr%=MOD; } writer.println((nr*modpow(dr, MOD-2))%MOD); } /** * * @param a * @param b * @param k * @return the product of the two (k*k) matrices a and b */ static long[][] multiply(long a[][], long b[][], int k) { long c[][] = new long[k][k]; for(int i=0;i<k;++i) { for(int j=0;j<k;++j) { for(int K=0;K<k;++K) { c[i][j] = (c[i][j] + a[i][K]*b[K][j])%MOD; } } } return c; } /** * * @param mat * @param k * @param n * @return Finds the matrix: mat^n, here mat is a (k*k) matrix */ static long[][] exponentiateMatrix(long mat[][], int k, long n) { if(n==1) { return mat; } if(n%2==0) { long half[][] = exponentiateMatrix(mat, k, n/2); return multiply(half, half, k); } return multiply(mat, exponentiateMatrix(mat,k, n-1), k); } static long C2(long n) { if(n<2) { return 0; } return (n*(n-1))/2; } static long modpow(long x, long y) { if(y==0) { return 1; } if((y&1)==0) { return modpow((x*x)%MOD, y>>1); } return (x*modpow(x, y-1))%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> using namespace std; class Solution { public: int solve(vector<int>& nums, int k) { int n = nums.size(); if (n < 2) { return 0; } long long P = 0, Q = 0; long long p = invM(n * (n - 1) / 2); int zeros = 0, ones = 0; for (int i = 0; i < n; ++i) { if (nums[i] == 0) { ++zeros; } else { ++ones; } } int inPlace = 0; for (int i = 0; i < zeros; ++i) { if (nums[i] == 0) { ++inPlace; } } int m = zeros; vector<int> ans(m + 1, 0); ans[inPlace] = 1; vector<vector<int>> mat(m + 1, vector<int>(m + 1, 0)); for (int i = 0; i <= m; ++i) { int c00 = i; int c01 = zeros - i; int c10 = c01; int c11 = ones - c01; if (c01 < 0 || c11 < 0) { continue; } for (int j = -1; j <= 1; ++j) { int ni = i + j; if (ni >= 0 && ni <= m) { int cnt = 0; int& val = mat[ni][i]; if (zeros - ni > ones) { continue; } if (j == -1) { val += c00 * c11; } else if (j == 0) { val += n * (n - 1) / 2 - c00 * c11 - c01 * c10; } else { val += c01 * c10; } } } } for (int i = 0; i <= m; ++i) { for (int j = 0; j <= m; ++j) { if (mat[i][j] != 0) { mat[i][j] = mulM(mat[i][j], p); } } } auto M = matPow(mat, k); ans = matMulVec(M, ans); return ans[zeros]; } private: const int M = 1000000007; long long normalize(long long a) { if (a >= -M && a < (M << 1)) { while (a >= M) { a -= M; } while (a < 0) { a += M; } return a; } a %= M; if (a < 0) { a += M; } return a; } long long addM(long long a, long long b) { return normalize(a + b); } long long mulM(long long a, long long b) { return normalize(a * b); } long long invM(long long a) { long long b = M; long long u = 0, v = 1; a = normalize(a); while (a != 0) { long long d = b / a; b -= d * a; u -= d * v; swap(a, b); swap(u, v); } u = normalize(u); return u; } long long divM(long long a, long long b) { assert(b != 0); return mulM(a, invM(b)); } vector<vector<int>> matMul(vector<vector<int>>& A, vector<vector<int>>& B) { int n = A.size(); int l = A[0].size(); int m = B[0].size(); vector<vector<int>> res(n, vector<int>(m, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < l; ++j) { for (int k = 0; k < m; ++k) { res[i][k] = addM(res[i][k], mulM(A[i][j], B[j][k])); } } } return res; } vector<vector<int>> matPow(vector<vector<int>> A, int e) { int n = A.size(); assert(n == A[0].size()); vector<vector<int>> res(n, vector<int>(n, 0)); for (int i = 0; i < n; ++i) { res[i][i] = 1; } while (e > 0) { if (e & 0x1) { res = matMul(res, A); } e >>= 1; A = matMul(A, A); } return res; } vector<int> matMulVec(vector<vector<int>>& A, vector<int>& V) { int n = A.size(); int m = A[0].size(); assert(m == V.size()); vector<int> res(n, 0); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { res[i] = addM(res[i], mulM(A[i][j], V[j])); } } return res; } }; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); Solution sol; int n; cin >> n; int k; cin >> k; vector<int> nums(n); for (int i = 0; i < n; ++i) { cin >> nums[i]; } cout << sol.solve(nums, k) << "\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 mod = 1e9 + 7; long long add(long long a, long long b) { return (a + b) % mod; } long long mul(long long a, long long b) { return a * b % mod; } long long qpow(long long a, long long b = mod - 2) { long long ret = 1; while (b) { if (b & 1) ret = mul(ret, a); a = mul(a, a); b >>= 1; } return ret; } const int maxm = 105, inv_2 = qpow(2); long long C(long long a) { return mul(inv_2, mul(a, add(a, mod - 1))); } int n, k, len_a, len_b; int num[maxm]; struct matrix { long long num[maxm][maxm]; }; matrix mul(matrix a, matrix b) { matrix c; memset(c.num, 0, sizeof(c.num)); for (int i = 0; i <= len_b; i++) for (int j = 0; j <= len_b; j++) if (a.num[i][j]) for (int k = 0; k <= len_b; k++) c.num[i][k] = add(c.num[i][k], mul(a.num[i][j], b.num[j][k])); return c; } matrix qpow(matrix a, long long b) { matrix e; for (int i = 0; i <= len_b; i++) for (int j = 0; j <= len_b; j++) e.num[i][j] = (i == j); while (b) { if (b & 1) e = mul(e, a); a = mul(a, a); b >>= 1; } return e; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", num + i); if (num[i]) len_b++; else len_a++; } matrix base; for (int i = 0; i <= len_b; i++) { for (int j = max(0, i - 1); j <= min(len_b, i + 1); j++) { int b1 = j, b0 = len_b - j, a0 = len_a - b0, a1 = len_a - a0; if (i == j - 1) base.num[i][j] = a0 * b1; else if (i == j + 1) base.num[i][j] = a1 * b0; else { base.num[i][j] = add(base.num[i][j], C(a0)); base.num[i][j] = add(base.num[i][j], C(a1)); base.num[i][j] = add(base.num[i][j], C(b0)); base.num[i][j] = add(base.num[i][j], C(b1)); base.num[i][j] = add(base.num[i][j], mul(a0, a1)); base.num[i][j] = add(base.num[i][j], mul(a0, b0)); base.num[i][j] = add(base.num[i][j], mul(a1, b1)); base.num[i][j] = add(base.num[i][j], mul(b0, b1)); } } } int cnt = 0; for (int i = len_a; i < n; i++) if (num[i]) cnt++; matrix ano; memset(ano.num, 0, sizeof(ano.num)); ano.num[cnt][0] = 1; base = mul(qpow(base, k), ano); long long ans = base.num[len_b][0]; ans = mul(ans, qpow(qpow(C(n), k))); 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; const int md = (int)(1e9 + 7); const int N = 110; int a[N], sz; struct mat { int a[N][N]; void init() { memset(a, 0, sizeof(a)); for (int i = 0; i < sz; i++) { a[i][i] = 1; } } void print() { for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { printf("%d ", a[i][j]); } puts(""); } } }; mat operator*(mat x, mat y) { mat z; memset(z.a, 0, sizeof(z.a)); for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { for (int k = 0; k < sz; k++) { z.a[i][j] += 1ll * x.a[i][k] * y.a[k][j] % md; if (z.a[i][j] >= md) { z.a[i][j] -= md; } } } } return z; } mat operator^(mat a, int b) { mat c; c.init(); while (b) { if (b & 1) { c = c * a; } a = a * a; b >>= 1; } return c; } int pow_mod(int a, int b) { int ret = 1; while (b) { if (b & 1) { ret = 1LL * ret * a % md; } b >>= 1; a = 1LL * a * a % md; } return ret; } int inv(int x) { return pow_mod(x, md - 2); } int main() { int n, k; scanf("%d%d", &n, &k); int zero = 0, one = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == 0) { zero++; } else { one++; } } int start = 0; for (int i = 0; i < zero; i++) { if (0 == a[i]) { start++; } } int aim = zero; sz = zero + 1; mat trans; memset(trans.a, 0, sizeof(trans.a)); for (int i = 0; i <= zero; i++) { if (one - zero + i >= 0) { trans.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) { trans.a[i][i - 1] = i * (one - zero + i); } if (i + 1 <= zero) { trans.a[i][i + 1] = (zero - i) * (zero - i); } } trans = trans ^ k; int ret = 1LL * trans.a[start][aim] * pow_mod(2, k) % md * pow_mod(inv(n * (n - 1)), k) % md; cout << ret << 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 unsigned long long MOD = 1e9 + 7, N = 100; unsigned long long k, n, c, cnt; int a[110]; struct node { unsigned long long f[110][110]; } t, tt, res; void matrix(node &t) { memset(t.f, 0, sizeof t.f); for (unsigned long long i = 0; i <= N; ++i) t.f[i][i] = 1; } void mul(node &t, node &y, node &z) { memset(z.f, 0, sizeof z.f); for (unsigned long long i = 0; i <= N; ++i) for (unsigned long long j = 0; j <= N; ++j) for (unsigned long long k = 0; k <= N; ++k) z.f[i][j] = (z.f[i][j] + t.f[i][k] * y.f[k][j] % MOD) % MOD; } node matrixpow(node X, unsigned long long n) { node Y, ttt; matrix(Y); while (n) { if (n & 1) mul(X, Y, ttt), Y = ttt; mul(X, X, ttt); X = ttt; n >>= 1; } return Y; } unsigned long long ksm(unsigned long long x, unsigned long long n) { unsigned long long res = 1; while (n) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } int main() { scanf("%llu %llu", &n, &k); for (unsigned long long i = 1; i <= n; ++i) scanf("%d", &a[i]), c += (a[i] == 0); for (unsigned long long i = 1; i <= c; ++i) cnt += (a[i] == 0); t.f[0][cnt] = 1; unsigned long long o1 = 1, o2 = 2; for (unsigned long long i = 0; i <= c; ++i) { if (i != 0) tt.f[i - 1][i] = (c - (i - o1)) * (c - (i - o1)) % MOD; tt.f[i][i] = (i * (c - i) % MOD + (c - i) * (n - c - c + i)) % MOD; tt.f[i][i] = (tt.f[i][i] + (c * (c - o1) / o2) % MOD) % MOD; tt.f[i][i] = (tt.f[i][i] + ((n - c) * (n - c - o1) / o2) % MOD) % MOD; if (i != c) tt.f[i + o1][i] = (i + o1) * (n - c - c + i + o1) % MOD; } tt = matrixpow(tt, k); node ttt; mul(t, tt, ttt); unsigned long long ans = ttt.f[0][c], p = 0; for (int i = 0; i <= c; ++i) p = (p + ttt.f[0][i]) % MOD; ans = 1ll * ans * ksm(p, MOD - 2) % MOD; printf("%llu", 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; using ll = long long; const ll mod = 1000000007; struct Matrix { vector<vector<ll>> v; int n, m; Matrix(vector<vector<ll>>& t) { n = t.size(); m = t[0].size(); v = t; } Matrix(int p, int q) { v = vector<vector<ll>>(p, vector<ll>(q, 0)); n = p, m = q; } Matrix() { n = m = -1; } }; ll mexp(ll a, ll b) { if (!b) return 1; if (b & 1) return a * mexp(a * a % mod, b >> 1) % mod; else return mexp(a * a % mod, b >> 1); } Matrix operator*(const Matrix& A, const Matrix& B) { if (A.m != B.n) return Matrix(); Matrix ret(A.n, B.m); for (int i = 0; i < A.n; i++) { for (int j = 0; j < B.m; j++) { for (int l = 0; l < A.m; l++) { ret.v[i][j] += A.v[i][l] * B.v[l][j]; ret.v[i][j] %= mod; } } } return ret; } using ll = long long; int n, k; int a[103]; Matrix I; Matrix mexp(const Matrix& A, int e) { if (e == 0) return I; if (e & 1) return A * mexp(A * A, e >> 1); else return mexp(A * A, e >> 1); } int main() { scanf("%d%d", &n, &k); int z = 0; int cur = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); z += !a[i]; } for (int i = 0; i < z; i++) cur += !a[i]; Matrix X(z + 1, 1); X.v[cur][0] = 1; ll denom = 1; denom = mexp(n, mod - 2) * mexp(n - 1, mod - 2) % mod; denom = 2 * denom % mod; Matrix P(z + 1, z + 1); for (int i = std::max(2 * z - n, 0); i <= z; i++) { if (i + 1 <= z) P.v[i + 1][i] = (z - i) * (z - i) * denom % mod; if (i - 1 >= std::max(2 * z - n, 0)) P.v[i - 1][i] = i * (n - 2 * z + i) * denom % mod; P.v[i][i] = (n * (n - 1) / 2 - i * (n - 2 * z + i) - (z - i) * (z - i)) * denom % mod; } I = Matrix(z + 1, z + 1); for (int i = 0; i <= z; i++) I.v[i][i] = 1; X = mexp(P, k) * X; printf("%lld\n", X.v[z][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; 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("%I64d\n", ((long long)ret.a[0][sn] * q_pow(q_pow(n * (n - 1) >> 1, mod - 2), K) % mod + mod) % 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> void out(T x) { cout << x << endl; exit(0); } using ll = long long; const ll mod = 1e9 + 7; const int maxn = 110; using matrix = vector<vector<ll>>; void add(ll& x, ll y) { if (x >= mod) x %= mod; if (y >= mod) y %= mod; x += y; if (x >= mod) x %= mod; } void mul(ll& x, ll y) { if (x >= mod) x %= mod; if (y >= mod) y %= mod; x += y; if (x >= mod) x %= mod; } matrix mul(matrix a, matrix b) { int n = a.size(); assert(n > 0); int m = a[0].size(); assert(m == int(b.size())); int p = b[0].size(); matrix res(n, vector<ll>(p)); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { for (int k = 0; k < m; k++) { ll cur = a[i][k] * b[k][j] % mod; add(res[i][j], cur); } } } return res; } matrix matpw(matrix mat, ll k) { int n = mat.size(); assert(mat.size() && mat.size() == mat[0].size()); matrix res(n, vector<ll>(n)); for (int i = 0; i < n; i++) { res[i][i] = 1; } while (k > 0) { if (k % 2) { res = mul(res, mat); } mat = mul(mat, mat); k = k / 2; } return res; } ll pw(ll x, ll y) { ll res = 1; while (y) { if (y % 2) res = res * x % mod; x = x * x % mod; y = y / 2; } return ((res % mod) + mod) % mod; } int n, k; int a[maxn]; ll dp[maxn][maxn]; int cnt[2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; } int zero = cnt[0]; int have = 0; for (int i = 0; i < zero; i++) { have += (a[i] == 0); } matrix M = vector<vector<ll>>(zero + 1, vector<ll>(zero + 1, 0)); for (int j = 0; j <= zero; j++) { ll ways = 1ll * n * (n - 1) / 2; ll inc = (cnt[0] - j) * (cnt[0] - j); ll dec = j * (cnt[1] - (cnt[0] - j)); ll noop = ways - inc - dec; ll den = pw(ways, mod - 2); if (j > 0) { M[j][j - 1] = dec * den % mod; } if (j < zero) { M[j][j + 1] = inc * den % mod; } M[j][j] = noop * den % mod; } M = matpw(M, k); cout << M[have][zero] << endl; return 0; dp[0][have] = 1; for (int i = 0; i < k; i++) { for (int j = 0; j <= zero; j++) { if (dp[i][j] == 0) continue; ll ways = 1ll * n * (n - 1) / 2; ll inc = (cnt[0] - j) * (cnt[0] - j); ll dec = j * (cnt[1] - (cnt[0] - j)); ll noop = ways - inc - dec; assert(noop >= 0); ll den = pw(ways, mod - 2); if (j > 0) { dp[i + 1][j - 1] += dp[i][j] * dec % mod * den % mod; dp[i + 1][j - 1] %= mod; } if (j < zero) { dp[i + 1][j + 1] += dp[i][j] * inc % mod * den % mod; dp[i + 1][j + 1] %= mod; } dp[i + 1][j] += dp[i][j] * noop % mod * den % mod; dp[i + 1][j] %= mod; } } cout << dp[k][cnt[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 int N = 1e2 + 10; const long long mod = 1e9 + 7; long long qpow(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; } struct Matrix { static const int N = 110; long long a[N][N], n; Matrix(int siz) { n = siz; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = 0; } Matrix operator*(const Matrix& A) const { Matrix res = Matrix(n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) res.a[i][j] = (res.a[i][j] + 1ll * A.a[i][k] * a[k][j] % mod) % mod; return res; } }; Matrix power(Matrix A, long long k) { Matrix res = Matrix(A.n); for (int i = 0; i < res.n; i++) res.a[i][i] = 1; while (k) { if (k & 1) res = res * A; A = A * A; k >>= 1; } return res; } long long calc(long long x) { return x * (x - 1) / 2 % mod; } int a[N]; int main() { long long n, k; scanf("%lld%lld", &n, &k); int m = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (!a[i]) m++; } int now = 0; for (int i = 1; i <= m; i++) if (!a[i]) now++; Matrix A = Matrix(m + 1); long long inv = qpow(calc(n), mod - 2); for (int i = 0; i <= m; i++) { if (i - 1 >= 0) A.a[i][i - 1] = 1ll * (m - i + 1) * (m - i + 1) % mod * inv % mod; A.a[i][i] = 1ll * (calc(n) - i * (n - 2 * m + i) - (m - i) * (m - i)) % mod * inv % mod; if (i + 1 <= m) A.a[i][i + 1] = 1ll * (i + 1) * (n - 2 * m + i + 1) % mod * inv % mod; } A = power(A, k); printf("%lld\n", A.a[m][now] % 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> const int N = 210, MO = 1e9 + 7; int n, K, A[N], fac[N], inv[N], invn[N], a[N][N], ans[N][N], c[N][N], lm; inline void Add(int &a, const int &b) { a += b; if (a >= MO) a -= MO; else if (a < 0) a += MO; return; } inline int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) { ans = 1ll * ans * a % MO; } a = 1ll * a * a % MO; b = b >> 1; } return ans; } inline int Inv(int x) { return qpow(x, MO - 2); } inline int iC(int n, int m) { if (n < 0 || m < 0 || n < m) return 0; return 1ll * invn[n] * fac[m] % MO * fac[n - m] % MO; } inline void mulself() { memset(c, 0, sizeof(c)); for (int k = 0; k <= lm; k++) { for (int i = 0; i <= lm; i++) { if (!a[i][k]) continue; for (int j = 0; j <= lm; j++) { Add(c[i][j], 1ll * a[i][k] * a[k][j] % MO); } } } memcpy(a, c, sizeof(a)); return; } inline void mul() { memset(c, 0, sizeof(c)); for (int k = 0; k <= lm; k++) { for (int i = 0; i <= lm; i++) { if (!a[i][k]) continue; for (int j = 0; j <= lm; j++) { Add(c[i][j], 1ll * a[i][k] * ans[k][j] % MO); } } } memcpy(ans, c, sizeof(c)); return; } int main() { scanf("%d%d", &n, &K); int cnt = 0, aim = 0; for (int i = 1; i <= n; i++) { scanf("%d", &A[i]); cnt += A[i]; } for (int i = 1; i + cnt <= n; i++) { aim += A[i]; } int P = Inv(n * (n - 1) / 2); fac[0] = inv[0] = invn[0] = 1; fac[1] = inv[1] = invn[1] = 1; for (int i = 2; i <= n; i++) { fac[i] = 1ll * i * fac[i - 1] % MO; inv[i] = 1ll * inv[MO % i] * (MO - MO / i) % MO; invn[i] = 1ll * invn[i - 1] * inv[i] % MO; } lm = n - cnt; for (int j = 0; j <= n - cnt; j++) { int t1 = 1ll * (cnt - j) * (n - cnt - j) % MO * P % MO; int t2 = 1ll * j * j * P % MO; Add(a[j][j], (1 - t1 + MO - t2 + MO) % MO); Add(a[j][j + 1], t1); Add(a[j][j - 1], t2); } for (int i = 0; i <= lm; i++) { ans[i][i] = 1; } while (K) { if (K & 1) { mul(); } mulself(); K >>= 1; } int Ans = 1ll * ans[0][aim] * iC(n - cnt, aim) % MO * iC(cnt, aim) % MO; printf("%d\n", (Ans + MO) % MO); 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; long long fm(long long a, long long 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> namespace IO { int double_len(6); bool spacebar_can_see(1); template <typename T> class Number { public: unsigned int len; int hex; T number; inline Number(T number = 0, int hex = 10, int len = 0) : number(number), hex(hex), len(len) {} inline operator T() { return number; } }; inline bool CanSee(char ch) { return spacebar_can_see ? (31 < ch && ch < 127) : (32 < ch && ch < 127); } inline bool IsNumber(char ch) { return (47 < ch && ch < 58); } static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; inline char GetChar() { static char buf[IN_BUF], *ps = buf, *pt = buf; if (ps == pt) { ps = buf; pt = buf + fread(buf, 1, IN_BUF, stdin); } return ps == pt ? EOF : *ps++; } inline void PutChar(char ch) { static char pbuf[OUT_BUF], *pp = pbuf; struct Flusher { ~Flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } }; static Flusher out_put_flusher; if (pp == pbuf + OUT_BUF) { fwrite(pbuf, 1, OUT_BUF, stdout); pp = pbuf; } *pp++ = ch; } char read_ch = '\n'; template <class T> inline T ReadInt() { register T x(0); register bool f(1); while (!IsNumber(read_ch)) { if (read_ch == 45) { f = 0; } read_ch = GetChar(); } while (IsNumber(read_ch)) { x = (x << 1) + (x << 3) + (read_ch ^ 48); read_ch = GetChar(); } return f ? x : -x; } template <class T> inline T ReadUInt() { register T x(0); while (!IsNumber(read_ch)) { read_ch = GetChar(); } while (IsNumber(read_ch)) { x = (x << 1) + (x << 3) + (read_ch ^ 48); read_ch = GetChar(); } return x; } inline double ReadDouble() { register long long int_num(ReadInt<long long>()); if (read_ch == 46) { register double pow10(1.0), result(0); read_ch = GetChar(); while (IsNumber(read_ch)) { pow10 /= 10.0; result += pow10 * (read_ch ^ 48); read_ch = GetChar(); } return int_num + (int_num < 0 ? -result : result); } else { return (double)int_num; } } inline void ReadT(int &x) { x = ReadInt<int>(); } inline void ReadT(long long &x) { x = ReadInt<long long>(); } inline void ReadT(bool &x) { x = ReadUInt<bool>(); } inline void ReadT(unsigned int &x) { x = ReadUInt<unsigned int>(); } inline void ReadT(unsigned long long &x) { x = ReadUInt<unsigned long long>(); } inline void ReadT(double &x) { x = ReadDouble(); } inline void ReadT(char &s) { for (; !CanSee(read_ch) && (~read_ch); read_ch = GetChar()) ; s = read_ch; read_ch = 10; } inline void ReadT(char *s) { register int len(0); for (; !CanSee(read_ch) && (~read_ch); read_ch = GetChar()) ; for (; CanSee(read_ch) && (~read_ch); read_ch = GetChar()) { s[len++] = read_ch; } s[len] = 0; read_ch = 10; } template <typename T> inline void ReadT(Number<T> &x) { ReadT(x.number); } template <typename T> inline void Read(T &t) { ReadT(t); } template <typename T, typename... Args> void Read(T &t, Args &...args) { ReadT(t); Read(args...); } const char NUMBER[36] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}; template <typename T> void WriteInt(T x) { if (x < 0) { PutChar(45); x = -x; } if (9 < x) { WriteInt(x / 10); } PutChar(x % 10 ^ 48); } template <typename T> void WriteUInt(T x) { if (9 < x) { WriteInt(x / 10); } PutChar(x % 10 ^ 48); } template <typename T> void WriteKInt(T x, int hex, int len) { if (x < 0) { PutChar(45); --len; x = -x; } if (hex <= x || 1 < len) { WriteKInt(x / hex, hex, len - 1); } PutChar(NUMBER[x % hex]); } template <typename T> void WriteUKInt(T x, int hex, int len) { if (hex <= x || 1 < len) { WriteKInt(x / hex, hex, len - 1); } PutChar(NUMBER[x % hex]); } inline void WriteDouble(double x, int len) { if (x < 0) { x = -x; PutChar(45); } WriteUInt((unsigned long long)x); if (len) { x -= (int)x; PutChar(46); while (len--) { x *= 10; if (!(unsigned long long)x) { PutChar(48); } } WriteUInt((unsigned long long)x); } } inline void WriteT(int x) { WriteInt<int>(x); } inline void WriteT(long long x) { WriteInt<long long>(x); } inline void WriteT(bool x) { WriteUInt<bool>(x); } inline void WriteT(unsigned int x) { WriteUInt<unsigned int>(x); } inline void WriteT(unsigned long long x) { WriteUInt<unsigned long long>(x); } inline void WriteT(double x) { WriteDouble(x, double_len); } inline void WriteT(char *s) { int len(strlen(s)); while (len--) { PutChar(*s++); } } inline void WriteT(const char *s) { while (*s) { PutChar(*s++); } } inline void WriteT(char x) { PutChar(x); } template <typename T> inline void WriteT(Number<T> x) { if (typeid(T) == typeid(int) || typeid(T) == typeid(long long)) { WriteKInt(x.number, x.hex, x.len); return; } if (typeid(T) == typeid(unsigned int) || typeid(T) == typeid(unsigned long long)) { WriteKInt(x.number, x.hex, x.len); return; } WriteT("Can't write this type."); } template <typename T> inline void Write(T t) { WriteT(t); } template <typename T, typename... Args> void Write(T t, Args... args) { WriteT(t); Write(args...); } inline void Writeln() { PutChar(10); } template <typename T> inline void Writeln(T t) { WriteT(t); PutChar(10); } template <typename... Args> inline void Writeln(Args... args) { Write(args...); PutChar(10); } template <typename... Args> inline void WriteExit(Args... args) { Write(args...); PutChar(10); exit(0); } } // namespace IO using namespace IO; using namespace std; template <typename T> inline T Max(T a, T b) { return a < b ? b : a; } template <typename T, typename... Args> T Max(T a, Args... args) { return Max(a, Max(args...)); } template <typename T> inline T Min(T a, T b) { return a < b ? a : b; } template <typename T, typename... Args> T Min(T a, Args... args) { return Min(a, Min(args...)); } template <typename T> inline void Swap(T &a, T &b) { register T temp = a; a = b; b = temp; } const long long MOD = 1e9 + 7; inline long long Pow(long long a, long long b, long long mod = MOD) { register long long result = 1; while (b) { if (b & 1) { (result *= a) %= mod; } (a *= a) %= mod; b >>= 1; } return result % mod; } const int MAXN = 105; int n, k; int s0; int arr[MAXN]; class Matrix { private: int len; long long m[MAXN][MAXN]; public: inline Matrix(const int len) : len(len) { memset(m, 0, sizeof(m)); } inline long long *operator[](const int &place) { return m[place]; } inline Matrix operator*(Matrix a) const { Matrix result(len); for (int i = 0; i <= len; ++i) { for (int j = 0; j <= len; ++j) { for (int k = 0; k <= len; ++k) { result[i][k] += m[i][j] * a[j][k]; result[i][k] %= MOD; } } } return result; } }; inline Matrix Pow(Matrix a, long long b) { Matrix result(s0); for (int i = 0; i <= s0; ++i) { result[i][i] = 1; } while (b) { if (b & 1) { result = result * a; } a = a * a; b >>= 1; } return result; } int main() { Read(n, k); for (int i = 1; i <= n; ++i) { Read(arr[i]); s0 += arr[i] == 0; } int t0 = 0; for (int i = 1; i <= s0; ++i) { t0 += arr[i] == 0; } Matrix answer(s0); Matrix f(s0); int low = Max(0, s0 * 2 - n); long long q = Pow(1ll * n * (n - 1) / 2, MOD - 2); for (int i = low; i <= s0; ++i) { if (i ^ low) { f[i - 1][i] = i * (n - s0 * 2 + i) * q % MOD; } if (i ^ s0) { f[i + 1][i] = (s0 - i) * (s0 - i) * q % MOD; } f[i][i] = (1 - i * (n - s0 * 2 + i) * q % MOD - (s0 - i) * (s0 - i) * q % MOD + MOD * 2) % MOD; } answer[t0][0] = 1; answer = Pow(f, k) * answer; Writeln(answer[s0][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 MOD = 1000000007; int mul(long long a, int b) { return (a * b) % MOD; } int sub(int a, int b) { int c = a - b; if (c < 0) { return c + MOD; } return c; } int add(int a, int b) { int c = a + b; if (c >= MOD) { return c - MOD; } return c; } vector<vector<int> > mul(const vector<vector<int> >& A, const vector<vector<int> >& B) { int c = A.size() - 1; vector<vector<int> > Ans(c + 1, vector<int>(c + 1, 0)); for (int i = 0; i < c + 1; i++) { for (int j = 0; j < c + 1; j++) { for (int k = 0; k < c + 1; k++) { Ans[i][j] = add(Ans[i][j], mul(A[i][k], B[k][j])); } } } return Ans; } vector<vector<int> > mat_pow(const vector<vector<int> >& A, int k) { if (k == 1) { return A; } if (k % 2 == 0) { return mat_pow(mul(A, A), k / 2); } else { return mul(mat_pow(A, k - 1), A); } } void print_mat(const vector<vector<int> >& A) { for (int i = 0; i < A.size(); i++) { for (int j = 0; j < A.size(); j++) { cout << A[i][j] << " "; } cout << endl; } } int pow(int a, int k) { if (k == 0) { return 1; } if (k % 2 == 0) { return pow(mul(a, a), k / 2); } return mul(pow(a, k - 1), a); } int main() { int n, k; cin >> n >> k; vector<int> A(n); int c = n; for (int i = 0; i < n; i++) { cin >> A[i]; c -= A[i]; } if (c == 0 or c == n) { cout << 1 << endl; return 0; } int c0 = c; for (int i = 0; i < c; i++) { c0 -= A[i]; } vector<vector<int> > Mat(c + 1, vector<int>(c + 1, 0)); int q = pow(n * (n - 1), MOD - 2); for (int j = 0; j < c + 1; j++) { int p1 = mul(2 * j, mul((sub(n, 2 * c) + j), q)); int p2 = mul(2 * (c - j), mul((c - j), q)); for (int i = 0; i < c + 1; i++) { if (i == j + 1) { Mat[i][j] = p2; } if (i == j - 1) { Mat[i][j] = p1; } if (i == j) { Mat[i][j] = sub(sub(1, p1), p2); } } } vector<vector<int> > end_A = mat_pow(Mat, k); cout << end_A[c][c0] << 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; namespace IO { const unsigned int Buffsize = 1 << 15, Output = 1 << 24; static char Ch[Buffsize], *S = Ch, *T = Ch; inline char getc() { return ((S == T) && (T = (S = Ch) + fread(Ch, 1, Buffsize, stdin), S == T) ? 0 : *S++); } static char Out[Output], *nowps = Out; inline void flush() { fwrite(Out, 1, nowps - Out, stdout); nowps = Out; } template <typename T> inline void read(T& x) { x = 0; static char ch; T f = 1; for (ch = getc(); !isdigit(ch); ch = getc()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getc()) x = x * 10 + (ch ^ 48); x *= f; } template <typename T> inline void write(T x, char ch = '\n') { if (!x) *nowps++ = '0'; if (x < 0) *nowps++ = '-', x = -x; static unsigned int sta[111], tp; for (tp = 0; x; x /= 10) sta[++tp] = x % 10; for (; tp; *nowps++ = sta[tp--] ^ 48) ; *nowps++ = ch; if (nowps - Out >= 1 << 23) flush(); } inline void getstr(char* q) { register char ch; for (ch = getc(); !isupper(ch); ch = getc()) ; for (; isupper(ch); ch = getc()) *q++ = ch; *q = '\0'; } inline void getwd(char& x) { for (x = getc(); !isupper(x); x = getc()) ; } } // namespace IO using namespace IO; void file() {} inline void Chkmin(int& u, int v) { u > v ? u = v : 0; } inline void Chkmax(int& u, int v) { u < v ? u = v : 0; } inline void Chkmax(long long& u, long long v) { u < v ? u = v : 0; } inline void Chkmin(long long& u, long long v) { u > v ? u = v : 0; } const int MAXN = 101, mod = 1e9 + 7; static int n, k, a[MAXN], cnt, ps; inline void init() { read(n), read(k); for (register int i = (1); i <= (n); ++i) read(a[i]), cnt += a[i]; for (register int i = (1); i <= (n - cnt); ++i) ps += a[i]; } static int A[MAXN][MAXN], iv, ans[MAXN][MAXN]; inline int power(int u, int v) { register int sm; for (sm = 1; v; v >>= 1, u = (long long)u * u % mod) if (v & 1) sm = (long long)sm * u % mod; return sm; } static int C[MAXN][MAXN], bd; inline void mul(int A[MAXN][MAXN], int B[MAXN][MAXN]) { for (register int i = (0); i <= (bd); ++i) for (register int j = (0); j <= (bd); ++j) { C[i][j] = 0; for (register int k = (0); k <= (bd); ++k) C[i][j] = (C[i][j] + (long long)A[i][k] * B[k][j]) % mod; } for (register int i = (0); i <= (bd); ++i) for (register int j = (0); j <= (bd); ++j) A[i][j] = C[i][j]; } inline void solve() { bd = min(cnt, n - cnt); iv = power(n * (n - 1), mod - 2); for (register int i = (0); i <= (bd); ++i) { int vt = n * (n - 1); if (i) A[i][i - 1] = 2ll * i * i * iv % mod, vt -= 2 * i * i; if (i < bd) A[i][i + 1] = 2ll * (cnt - i) * (n - cnt - i) * iv % mod, vt -= 2 * (cnt - i) * (n - cnt - i); A[i][i] = (long long)vt * iv % mod, ans[i][i] = 1; } for (; k; k >>= 1, mul(A, A)) if (k & 1) mul(ans, A); cout << ans[ps][0] << endl; } int main() { file(); 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> using namespace std; const int INF = 0x3f3f3f3f; const long long inf = 0x3f3f3f3f3f3f3f3f; const long long mod = 1e9 + 7; const int N = 110; struct Mat { int a[N][N]; int r, c; Mat(int _r, int _c) { r = _r, c = _c, memset(a, 0, sizeof(a)); } }; Mat operator*(Mat X, Mat Y) { Mat Z(X.r, Y.c); for (int i = 0; i < X.r; ++i) { for (int j = 0; j < Y.c; ++j) { for (int k = 0; k < X.c; ++k) { Z.a[i][j] = (Z.a[i][j] + 1ll * X.a[i][k] * Y.a[k][j] % mod) % mod; } } } return Z; } Mat pow(Mat a, long long n) { Mat res(a.r, a.c); for (int i = 0; i < a.r; i++) res.a[i][i] = 1; while (n) { if (n & 1) res = res * a; ; a = a * a; n >>= 1; } return res; } long long Pow(long long a, long long b) { long long res = 1ll; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } int n, k; int a[N], c, x; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); c += (a[i] == 0); } for (int i = 1; i <= c; i++) x += (a[i] == 0); Mat dp(c + 1, 1); Mat A(c + 1, c + 1); dp.a[x][0] = 1; for (int i = 0; i <= c; i++) { if (i > 0) A.a[i][i - 1] = (c - i + 1) * (c - i + 1); if (i < c) A.a[i][i + 1] = (i + 1) * (n - 2 * c + i + 1); A.a[i][i] = c * (c - 1) / 2 + (n - c) * (n - c - 1) / 2 + i * (c - i) + (c - i) * (n - 2 * c + i); } A = pow(A, k); dp = A * dp; long long tmp = 0ll; for (int i = 0; i <= c; i++) tmp = (tmp + dp.a[i][0]) % mod; printf("%lld\n", dp.a[c][0] * Pow(tmp, 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; 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]); } 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; template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, pair<T1, T2> a) { out << a.first << " " << a.second; return out; } template <typename T, typename T1> T maxs(T &a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T mins(T &a, T1 b) { if (b < a) a = b; return a; } template <typename T> class matrix { public: vector<vector<T>> val; long long n; matrix<T>(long long size) { n = size; val.resize(n); for (long long i = 0; i < n; i++) val[i].resize(n); } matrix<T> operator+(const matrix<T> &) const; matrix<T> operator-(const matrix<T> &) const; matrix<T> &operator=(const matrix<T> &b) { val = b.val; n = b.n; return *this; } void set(long long first) { for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { val[i][j] = first; } } } void print() { for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { cout << val[i][j] << " "; } cout << "\n"; } } matrix<T> mul(T a) { matrix<T> ans(this->n); for (long long i = 0; i < this->n; i++) { for (long long j = 0; j < this->n; j++) { ans.val[i][j] = a * this->val[i][j]; } } return ans; } matrix<T> operator*(const matrix<T> &b) const { matrix<T> ans(b.n); for (long long i = 0; i < b.n; i++) { for (long long j = 0; j < b.n; j++) { for (long long k = 0; k < b.n; k++) { ans.val[i][j] += this->val[i][k] * b.val[k][j]; } } } return ans; } matrix<T> identity(long long N) { matrix<T> ans(N); for (long long i = 0; i < N; i++) { ans.val[i][i] = 1; } return ans; } matrix<T> matexpo(matrix<T> a, long long n) { if (n == 0) { return identity(a.n); } if (n == 1) return a; matrix<T> first = matexpo(a, n / 2); matrix<T> r = first * first; if (n & 1) r = r * a; return r; } }; const long long MOD = 1000000007; struct mod_int { long long val; mod_int(long long v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static long long mod_inv(long long a, long long m = MOD) { long long g = m, r = a, first = 0, second = 1; while (r != 0) { long long q = g / r; g %= r; swap(g, r); first -= q * second; swap(first, second); } return first < 0 ? first + m : first; } explicit operator long long() const { return val; } mod_int &operator+=(const mod_int &other) { val += other.val; if (val >= MOD) val -= MOD; return *this; } mod_int &operator-=(const mod_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t first, unsigned m = MOD) { return first % m; unsigned x_high = first >> 32, x_low = (unsigned)first; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } mod_int &operator*=(const mod_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); } friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; } friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; } friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; } friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; } mod_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } mod_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } mod_int operator++(int32_t) { mod_int before = *this; ++*this; return before; } mod_int operator--(int32_t) { mod_int before = *this; --*this; return before; } mod_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const mod_int &other) const { return val == other.val; } bool operator!=(const mod_int &other) const { return val != other.val; } mod_int inv() const { return mod_inv(val); } mod_int pow(long long p) const { assert(p >= 0); mod_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &stream, const mod_int &m) { return stream << m.val; } friend istream &operator>>(istream &stream, mod_int &m) { return stream >> m.val; } }; long long solve() { long long n, k; cin >> n >> k; vector<long long> a(n + 1); long long z = 0; for (long long i = 1; i < n + 1; i++) { cin >> a[i]; z += (!a[i]); } long long c = 0; for (long long i = 1; i < z + 1; i++) { c += (!a[i]); } matrix<mod_int> m(z + 1); for (long long j = 0; j < z + 1; j++) { m.val[j][j] = n * (n - 1) / 2 - j * (n - 2 * z + j) - (z - j) * (z - j); if (j > 0) m.val[j][j - 1] = j * (n - 2 * z + j); if (j < z) m.val[j][j + 1] = (z - j) * (z - j); } mod_int t = n * (n - 1) / 2; t = t.pow(k); m = m.matexpo(m, k); cout << m.val[c][z] / t << "\n"; return 0; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long 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; template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator<<(std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator>>(std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator!=(const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } modnum operator-() const { return neg(); } modnum operator+() const { return modnum(*this); } modnum& operator++() { v++; if (v == MOD) v = 0; return *this; } modnum& operator--() { if (v == 0) v = MOD; v--; return *this; } modnum& operator+=(const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator-=(const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator*=(const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum& operator/=(const modnum& o) { return *this *= o.inv(); } friend modnum operator++(modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator-(const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator*(const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator/(const modnum& a, const modnum& b) { return modnum(a) /= b; } }; using num = modnum<int(1e9) + 7>; const int MAXN = 110; int N; int A[MAXN]; int cnt[2]; const int MAXS = 60; int S; void mul(num a[MAXS][MAXS], const num b[MAXS][MAXS]) { num tmp[MAXS][MAXS]; for (int i = 0; i <= S; i++) { for (int j = 0; j <= S; j++) { tmp[i][j] = 0; for (int k = 0; k <= S; k++) { tmp[i][j] += a[i][k] * b[k][j]; } } } for (int i = 0; i <= S; i++) { for (int j = 0; j <= S; j++) { a[i][j] = tmp[i][j]; } } } num trans[MAXS][MAXS]; num prod[MAXS][MAXS]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int K; cin >> N >> K; for (int i = 0; i < N; i++) { cin >> A[i]; cnt[A[i]]++; } S = min(cnt[0], cnt[1]); int S0 = 0; for (int i = 0; i < cnt[0]; i++) { if (A[i] == 1) S0++; } memset(trans, 0, sizeof(trans)); for (int i = 0; i <= S; i++) { num poss = num(N) * num(N - 1) / num(2); num pminus = num(i) * num(i) / poss; num pplus = num(cnt[0] - i) * num(cnt[1] - i) / poss; num psame = num(1) - pplus - pminus; if (i > 0) trans[i][i - 1] = pminus; trans[i][i] = psame; if (i < S) trans[i][i + 1] = pplus; } for (int i = 0; i <= S; i++) { for (int j = 0; j <= S; j++) { prod[i][j] = num(i == j); } } while (K) { if (K & 1) mul(prod, trans); mul(trans, trans); K >>= 1; } cout << prod[S0][0] << '\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 N = 110; const int MOD = 1000000007; inline int add(int u, int v) { u += v; if (u >= MOD) u -= MOD; return u; } inline int sub(int u, int v) { u -= v; if (u < 0) u += MOD; return u; } inline int mul(int u, int v) { return (long long)u * v % MOD; } inline int power(int u, int v) { int res = 1; while (v) { if (v & 1) res = mul(res, u); u = mul(u, u); v >>= 1; } return res; } inline int inv(int u) { return power(u, MOD - 2); } int n, k; int a[N]; struct Matrix { int a[N / 2][N / 2]; Matrix(int dia = 0) { memset(a, 0, sizeof a); for (int i = 0; i <= n / 2; i++) a[i][i] = dia; } Matrix operator*(const Matrix &u) { Matrix res; for (int k = 0; k <= n / 2; k++) { for (int i = 0; i <= n / 2; i++) { for (int j = 0; j <= n / 2; j++) { res.a[i][j] = add(res.a[i][j], mul(a[i][k], u.a[k][j])); } } } return res; } Matrix operator^(int v) { Matrix res(1); Matrix u = *this; while (v) { if (v & 1) res = res * u; u = u * u; v >>= 1; } return res; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; int cntZero = 0; int cntOne = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; cntZero += (a[i] == 0); cntOne += (a[i] == 1); } int cnt = 0; for (int i = 1; i <= cntZero; i++) { cnt += (a[i] == 1); } Matrix base(0); for (int i = 0; i <= n / 2; i++) { int tot = n * (n - 1) / 2; int a = sub(cntZero, i); int b = sub(cntOne, i); int foo = mul(a, b); tot = sub(tot, foo); if (i + 1 <= n / 2) base.a[i][i + 1] = foo; foo = mul(i, i); tot = sub(tot, foo); if (i - 1 >= 0) base.a[i][i - 1] = foo; base.a[i][i] = tot; } Matrix a; a.a[0][cnt] = 1; base = base ^ k; a = a * base; int res = a.a[0][0]; int bar = n * (n - 1) / 2; bar = power(bar, k); res = mul(res, inv(bar)); cout << res << 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.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Random; public class CFContest { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io, new Debug(local)); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; final Debug debug; int inf = (int) 1e8; int mod = (int) 1e9 + 7; public int mod(int val) { val %= mod; if (val < 0) { val += mod; } return val; } public int mod(long val) { val %= mod; if (val < 0) { val += mod; } return (int) val; } int bitAt(int x, int i) { return (x >> i) & 1; } int bitAt(long x, int i) { return (int) ((x >> i) & 1); } public Task(FastIO io, Debug debug) { this.io = io; this.debug = debug; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); int k = io.readInt(); int[] a = new int[n]; int[] numCounts = new int[2]; for (int i = 0; i < n; i++) { a[i] = io.readInt(); numCounts[a[i]]++; } Matrix t = new Matrix(numCounts[0] + 1, numCounts[0] + 1); for (int j = 0; j <= numCounts[0]; j++) { t.mat[j][j] += numCounts[0] * (numCounts[0] - 1) / 2; t.mat[j][j] += numCounts[1] * (numCounts[1] - 1) / 2; t.mat[j][j] += (numCounts[0] - j) * (numCounts[1] - numCounts[0] + j); t.mat[j][j] += j * (numCounts[0] - j); if (j + 1 <= numCounts[0]) { t.mat[j][j + 1] = (j + 1) * (numCounts[1] - (numCounts[0] - j - 1)); } if (j - 1 >= 0) { t.mat[j][j - 1] = (numCounts[0] - j + 1) * (numCounts[0] - j + 1); } } Matrix state = new Matrix(numCounts[0] + 1, 1); int prev = 0; for (int i = 0; i < numCounts[0]; i++) { if (a[i] == 0) { prev++; } } state.mat[prev][0] = 1; Matrix finalState = Matrix.mul(Matrix.pow(t, k), state); int validCount = finalState.mat[numCounts[0]][0]; int total = Mathematics.pow(n * (n - 1) / 2, k, mod); int possibilities = mod((long)validCount * Mathematics.inverse(total, mod)); io.cache.append(possibilities); } } public static class Matrix { int[][] mat; int n; int m; static int mod = (int) 1e9 + 7; public Matrix(int n, int m) { this.n = n; this.m = m; mat = new int[n][m]; } public void fill(int v) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { mat[i][j] = v; } } } public void asStandard() { fill(0); for (int i = 0; i < n && i < m; i++) { mat[i][i] = 1; } } public static Matrix mul(Matrix a, Matrix b) { Matrix c = new Matrix(a.n, b.m); for (int i = 0; i < c.n; i++) { for (int j = 0; j < c.m; j++) { for (int k = 0; k < a.m; k++) { c.mat[i][j] = (int) ((c.mat[i][j] + (long) a.mat[i][k] * b.mat[k][j]) % mod); } } } return c; } public static Matrix pow(Matrix x, int n) { if (n == 0) { Matrix r = new Matrix(x.n, x.m); r.asStandard(); return r; } Matrix r = pow(x, n >> 1); r = Matrix.mul(r, r); if (n % 2 == 1) { r = Matrix.mul(r, x); } return r; } } public static class Segment implements Cloneable { Segment left; Segment right; int cnt; public static Segment build(int l, int r) { Segment segment = new Segment(); segment.left = segment.right = segment; return segment; } public static boolean checkOutOfRange(int ll, int rr, int l, int r) { return ll > r || rr < l; } public static boolean checkCoverage(int ll, int rr, int l, int r) { return ll <= l && rr >= r; } public static void update(int ll, int rr, int l, int r, Segment segment) { if (checkOutOfRange(ll, rr, l, r)) { return; } if (checkCoverage(ll, rr, l, r)) { return; } int m = (l + r) >> 1; segment.pushDown(); update(ll, rr, l, m, segment.left); update(ll, rr, m + 1, r, segment.right); segment.pushUp(); } public static Segment updatePersistently(int ll, int rr, int l, int r, Segment segment) { if (checkOutOfRange(ll, rr, l, r)) { return segment; } segment = segment.clone(); if (checkCoverage(ll, rr, l, r)) { segment.cnt++; return segment; } int m = (l + r) >> 1; segment.pushDown(); segment.left = updatePersistently(ll, rr, l, m, segment.left); segment.right = updatePersistently(ll, rr, m + 1, r, segment.right); segment.pushUp(); return segment; } public static void query(int ll, int rr, int l, int r, Segment segment) { if (checkOutOfRange(ll, rr, l, r)) { return; } if (checkCoverage(ll, rr, l, r)) { return; } int m = (l + r) >> 1; segment.pushDown(); query(ll, rr, l, m, segment.left); query(ll, rr, m + 1, r, segment.right); } public static int floor(int l, int r, Segment old, Segment now, int v) { if (l > v || now.cnt <= old.cnt) { return -1; } if (l == r) { return l; } int m = (l + r) >> 1; int res = -1; if (now.right.cnt > old.right.cnt) { res = floor(m + 1, r, old.right, now.right, v); } if (res == -1) { res = floor(l, m, old.left, now.left, v); } return res; } public static int ceil(int l, int r, Segment old, Segment now, int v) { if (r < v || now.cnt <= old.cnt) { return -1; } if (l == r) { return l; } int m = (l + r) >> 1; int res = -1; if (now.left.cnt > old.left.cnt) { res = ceil(l, m, old.left, now.left, v); } if (res == -1) { res = ceil(m + 1, r, old.right, now.right, v); } return res; } public void pushDown() { } public void pushUp() { cnt = left.cnt + right.cnt; } @Override public Segment clone() { try { return (Segment) super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } } } public static class FastIO { public final StringBuilder cache = new StringBuilder(); private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } public FastIO(InputStream is, OutputStream os) { this(is, os, Charset.forName("ascii")); } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { boolean sign = true; skipBlank(); if (next == '+' || next == '-') { sign = next == '+'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } if (next != '.') { return sign ? val : -val; } next = read(); long radix = 1; long point = 0; while (next >= '0' && next <= '9') { point = point * 10 + next - '0'; radix = radix * 10; next = read(); } double result = val + (double) point / radix; return sign ? result : -result; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readLine(char[] data, int offset) { int originalOffset = offset; while (next != -1 && next != '\n') { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Debug { private boolean allowDebug; public Debug(boolean allowDebug) { this.allowDebug = allowDebug; } public void assertTrue(boolean flag) { if (!allowDebug) { return; } if (!flag) { fail(); } } public void fail() { throw new RuntimeException(); } public void assertFalse(boolean flag) { if (!allowDebug) { return; } if (flag) { fail(); } } private void outputName(String name) { System.out.print(name + " = "); } public void debug(String name, int x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, long x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, double x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, int[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, long[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, double[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, Object x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, Object... x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.deepToString(x)); } } public static class Randomized { static Random random = new Random(12345678); public static double nextDouble(double min, double max) { return random.nextDouble() * (max - min) + min; } public static void randomizedArray(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(long[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); long tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(double[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); double tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(float[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); float tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static <T> void randomizedArray(T[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); T tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } public static class Mathematics { public static int ceilPowerOf2(int x) { return 32 - Integer.numberOfLeadingZeros(x - 1); } public static int floorPowerOf2(int x) { return 31 - Integer.numberOfLeadingZeros(x); } public static long modmul(long a, long b, long mod) { return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod; } /** * Get the greatest common divisor of a and b */ public static int gcd(int a, int b) { return a >= b ? gcd0(a, b) : gcd0(b, a); } private static int gcd0(int a, int b) { return b == 0 ? a : gcd0(b, a % b); } public static int extgcd(int a, int b, int[] coe) { if (a >= b) { return extgcd0(a, b, coe); } else { int g = extgcd0(b, a, coe); int tmp = coe[0]; coe[0] = coe[1]; coe[1] = tmp; return g; } } private static int extgcd0(int a, int b, int[] coe) { if (b == 0) { coe[0] = 1; coe[1] = 0; return a; } int g = extgcd0(b, a % b, coe); int n = coe[0]; int m = coe[1]; coe[0] = m; coe[1] = n - m * (a / b); return g; } /** * Get the greatest common divisor of a and b */ public static long gcd(long a, long b) { return a >= b ? gcd0(a, b) : gcd0(b, a); } private static long gcd0(long a, long b) { return b == 0 ? a : gcd0(b, a % b); } public static long extgcd(long a, long b, long[] coe) { if (a >= b) { return extgcd0(a, b, coe); } else { long g = extgcd0(b, a, coe); long tmp = coe[0]; coe[0] = coe[1]; coe[1] = tmp; return g; } } private static long extgcd0(long a, long b, long[] coe) { if (b == 0) { coe[0] = 1; coe[1] = 0; return a; } long g = extgcd0(b, a % b, coe); long n = coe[0]; long m = coe[1]; coe[0] = m; coe[1] = n - m * (a / b); return g; } /** * Get y where x * y = 1 (% mod) */ public static int inverse(int x, int mod) { return pow(x, mod - 2, mod); } /** * Get x^n(% mod) */ public static int pow(int x, int n, int mod) { int bit = 31 - Integer.numberOfLeadingZeros(n); long product = 1; for (; bit >= 0; bit--) { product = product * product % mod; if (((1 << bit) & n) != 0) { product = product * x % mod; } } return (int) product; } public static long longpow(long x, long n, long mod) { if (n == 0) { return 1; } long prod = longpow(x, n >> 1, mod); prod = modmul(prod, prod, mod); if ((n & 1) == 1) { prod = modmul(prod, x, mod); } return prod; } /** * Get x % mod */ public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } public static int mod(long x, int mod) { x %= mod; if (x < 0) { x += mod; } return (int) x; } /** * Get n!/(n-m)! */ public static long permute(int n, int m) { return m == 0 ? 1 : n * permute(n - 1, m - 1); } /** * Put all primes less or equal to limit into primes after offset */ public static int eulerSieve(int limit, int[] primes, int offset) { boolean[] isComp = new boolean[limit + 1]; int wpos = offset; for (int i = 2; i <= limit; i++) { if (!isComp[i]) { primes[wpos++] = i; } for (int j = offset, until = limit / i; j < wpos && primes[j] <= until; j++) { int pi = primes[j] * i; isComp[pi] = true; if (i % primes[j] == 0) { break; } } } return wpos - offset; } /** * Round x into integer */ public static int intRound(double x) { if (x < 0) { return -(int) (-x + 0.5); } return (int) (x + 0.5); } /** * Round x into long */ public static long longRound(double x) { if (x < 0) { return -(long) (-x + 0.5); } return (long) (x + 0.5); } } }
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
//package com.company; // Always comment out package when submitting. import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static class Task { int mod = 1000000007; public class Frac { long a, b; public Frac(long a, long b) { this.a = a % mod; this.b = b % mod; } public Frac add(Frac other) { return new Frac(a * other.b + b * other.a, b * other.b); } public Frac mult(Frac other) { return new Frac(a * other.a, b * other.b); } public long toVal() { return a * BigInteger.valueOf(b).modInverse(BigInteger.valueOf(mod)).longValue() % mod; } @Override public String toString() { return a + "/" + b; } } int n; int minP0; int p0, p1; public Frac[][] matMul(Frac[][] m1, Frac[][] m2) { Frac[][] f = new Frac[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { f[i][j] = new Frac(0, 1); } } for (int i = 0; i < n; i++) { for (int k = 0; k < n; k++) { if (m1[i][k].a == 0) continue; for (int j = 0; j < n; j++) { f[i][j] = f[i][j].add(m1[i][k].mult(m2[k][j])); } } } return f; } public Frac[][] pow(Frac[][] base, int cnt) { Frac[][] ret = new Frac[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ret[i][j] = new Frac(0, 1); } ret[i][i].a = 1; } while (cnt != 0) { if (cnt % 2 != 0) ret = matMul(ret, base); base = matMul(base, base); cnt >>= 1; } return ret; } public Frac[][] construct() { Frac[][] ret = new Frac[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ret[i][j] = new Frac(0, 1); } } long base = (long) (p0 + p1) * (p0 + p1 - 1) / 2; for (int i = minP0; i <= p0; i++) { int sum = 0; if (i > minP0) { sum += i * (p1 - p0 + i); ret[i - 1 - minP0][i - minP0] = new Frac(i * (p1 - p0 + i), base); } if (i + 1 - minP0 < n) { sum += (p0 - i) * (p0 - i); ret[i + 1 - minP0][i - minP0] = new Frac((p0 - i) * (p0 - i), base); } ret[i - minP0][i - minP0] = new Frac(base - sum, base); } return ret; } public void solve(Scanner sc, PrintWriter pw) throws IOException { n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); if (arr[i] == 0) { p0++; } else { p1++; } } if (p1 >= p0) minP0 = 0; else minP0 = p0 - p1; n = p0 - minP0 + 1; int cnt0 = 0; for (int i = 0; i < p0; i++) { if (arr[i] == 0) cnt0++; } Frac[][] base = construct(); // for (int i = 0; i < n; i++) { // for (int j = 0; j < n; j++) { // System.err.print(base[i][j].toString() + " "); // } // System.err.println(); // } Frac[][] powd = pow(base, k); // for (int i = 0; i < n; i++) { // for (int j = 0; j < n; j++) { // System.err.print(powd[i][j].toString() + " "); // } // System.err.println(); // } pw.println(powd[p0 - minP0][cnt0 - minP0].toVal()); } } // template, actual code is in class Task. static long TIME_START, TIME_END; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("Test.in")); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); // PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out")); Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); pw.close(); // System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000); // System.err.println("Time used: " + (TIME_END - TIME_START) + "."); } // Faster IO with BufferedReader wrapped with Scanner // static class Scanner { // StringTokenizer st; // BufferedReader br; // // public Scanner(InputStream s) { // br = new BufferedReader(new InputStreamReader(s)); // } // // public Scanner(FileReader s) throws FileNotFoundException { // br = new BufferedReader(s); // } // // public String next() throws IOException { // while (st == null || !st.hasMoreTokens()) // st = new StringTokenizer(br.readLine()); // return st.nextToken(); // } // // public int nextInt() throws IOException { // return Integer.parseInt(next()); // } // // public long nextLong() throws IOException { // return Long.parseLong(next()); // } // // public String nextLine() throws IOException { // return br.readLine(); // } // // public double nextDouble() throws IOException { // return Double.parseDouble(next()); // } // // public boolean ready() throws IOException { // return br.ready(); // } // } }
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; 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]); } 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 nmax = 2000 * 1000; const int inf = 2000 * 1000 * 1000; const long long infl = 1000ll * 1000ll * 1000ll * 1000ll * 1000ll * 1000ll; const int mod = 1000 * 1000 * 1000 + 7; const long double pi = acos(-1.0); template <typename T1, typename T2> ostream& operator<<(ostream& cout, const pair<T1, T2>& a) { return (cout << "(" << a.first << "; " << a.second << ")"); } template <typename T> ostream& operator<<(ostream& cout, const vector<T>& a) { if (!a.size()) { return (cout << "()"); } cout << "(" << a[0]; for (int i = 1; i < (int)a.size(); i++) { cout << "; " << a[i]; } return (cout << ")"); } template <typename T> ostream& operator<<(ostream& cout, const set<T>& a) { if (!a.size()) { return (cout << "{}"); } auto it = a.begin(); cout << "{" << *it; ++it; for (; it != a.end(); ++it) { cout << "; " << *it; } return (cout << "}"); } vector<vector<long long> > mul(const vector<vector<long long> >& a, const vector<vector<long long> >& b) { int n = a.size(); vector<vector<long long> > ans; ans.resize(n); for (int i = 0; i < n; i++) { ans[i].assign(n, 0); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % mod; } } } return ans; } vector<vector<long long> > answer, a; void powa(long long k) { int n = a.size(); for (int i = 0; i < n; i++) { answer.push_back(vector<long long>(n)); } for (int i = 0; i < n; i++) { answer[i][i] = 1; } while (k) { if (k & 1) { answer = mul(answer, a); k--; } else { a = mul(a, a); k /= 2; } } } long long poww(long long a, long long b) { long long answer = 1; while (b) { if (b & 1) { answer = (answer * a) % mod; b--; } else { a = (a * a) % mod; b /= 2; } } return answer; } long long n, k, c[nmax]; int main() { cin >> n >> k; int cnt0 = 0; for (int i = 0; i < n; i++) { cin >> c[i]; cnt0 += (c[i] == 0); } int st = 0; for (int i = 0; i < cnt0; i++) { st += (c[i] == 0); } a.resize(cnt0 + 1); for (int i = 0; i <= cnt0; i++) { a[i].assign(cnt0 + 1, 0); } int mind = max(0ll, cnt0 - (n - cnt0)); for (int i = mind; i <= cnt0; i++) { if (i - 1 >= mind) { a[i][i - 1] = i * (n - 2 * cnt0 + i); } if (i + 1 <= cnt0) { a[i][i + 1] = (cnt0 - i) * (cnt0 - i); } a[i][i] = cnt0 * (cnt0 - 1) / 2 + (n - cnt0) * (n - cnt0 - 1) / 2 + i * (cnt0 - i) + (cnt0 - i) * (n - 2 * cnt0 + i); } powa(k); cerr << answer[st][cnt0] << endl; cout << (answer[st][cnt0] * poww(poww(n * (n - 1) / 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> using namespace std; using ll = long long; struct Matrix { int n, m; vector<vector<ll>> mat; Matrix(int n, int m) : n(n), m(m), mat(n, vector<ll>(m)) {} vector<ll> &operator[](int i) { return mat[i]; } const vector<ll> &operator[](int i) const { return mat[i]; } static Matrix identity(int n) { Matrix res(n, n); for (int i = 0; i < n; i++) res[i][i] = 1; return res; } Matrix operator*(const Matrix &rhs) const { assert(m == rhs.n); Matrix res(n, rhs.m); for (int i = 0; i < n; i++) { for (int j = 0; j < rhs.m; j++) { for (int k = 0; k < m; k++) { res[i][j] += mat[i][k] * rhs[k][j] % 1000000007; res[i][j] %= 1000000007; } } } return res; } Matrix fexp(ll x) const { assert(n == m && x >= 0); Matrix res = identity(n), cur = *this; while (x) { if (x % 2) res = res * cur; cur = cur * cur; x /= 2; } return res; } }; ll fexp(ll a, ll b) { ll res = 1; while (b) { if (b % 2) res = res * a % 1000000007; a = a * a % 1000000007; b /= 2; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k, z = 0, s = 0; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; z += !a[i]; } for (int i = 0; i < z; i++) s += !a[i]; Matrix dp(z + 1, z + 1); for (int i = 0; i <= z; i++) { if (i < z) dp[i][i + 1] = (ll)(z - i) * (z - i); if (i > 0) dp[i][i - 1] = (ll)i * (n - (2 * z - i)); dp[i][i] = (ll)n * (n - 1) / 2 - (z - i) * (z - i) - i * (n - (2 * z - i)); } dp = dp.fexp(k); ll tot = 0; for (int j = 0; j <= z; j++) tot = (tot + dp[s][j]) % 1000000007; cout << dp[s][z] * fexp(tot, 1000000007 - 2) % 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
import java.io.*; import java.util.*; public class Codeforces1151F { public static void main(String[] args) throws IOException { int P = 1000000007; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } //count number of zeroes in array int r = 0; for (int i = 0; i < n; i++) { r += (1-a[i]); } //count number of ones in first r things int s = 0; for (int i = 0; i < r; i++) { s += a[i]; } if (r > (n/2)) { r = n-r; } int nC2Inv = (n*(n-1))/2; nC2Inv = inverse(nC2Inv, P); //create matrix int[][] matrix = new int[r+1][r+1]; for (int i = 0; i <= r; i++) { matrix[i][i] = 1; if (i > 0) { matrix[i][i-1] = multiply(i*i, nC2Inv, P); matrix[i][i] = (matrix[i][i] + P - matrix[i][i-1])%P; } if (i < r) { matrix[i][i+1] = multiply(((r-i)*(n-r-i)), nC2Inv, P); matrix[i][i] = (matrix[i][i] + P - matrix[i][i+1])%P; } } //matrix power k int[][] ans = power(matrix, k, P); System.out.println(ans[s][0]); } public static int[][] power(int[][] a, int k, int N) { if (k == 1) { return a; } else { int[][] prod = multiply(a, a, N); int[][] ans = power(prod, k/2, N); if (k%2 == 1) { ans = multiply(ans, a, N); } return ans; } } public static int[][] multiply (int[][] a, int[][] b, int N) { int n = a.length; int m = a[0].length; int p = b[0].length; int[][] ans = new int[n][p]; for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { for (int k = 0; k < m; k++) { ans[i][j] = (ans[i][j] + multiply(a[i][k], b[k][j], N))%N; } } } return ans; } public static int multiply (int a, int b, int n) { long ab = (long) a * (long) b; return ((int) (ab%n)); } public static int inverse (int a, int n) { int m = n; int r1 = 1; int r2 = 0; int r3 = 0; int r4 = 1; while ((a > 0) && (n > 0)) { if (n >= a) { r3 -= r1*(n/a); r4 -= r2*(n/a); n = n%a; } else { int tmp = a; a = n; n = tmp; tmp = r1; r1 = r3; r3 = tmp; tmp = r2; r2 = r4; r4 = tmp; } } if (a == 0) { if (r3 >= 0) return (r3%m); else return (m+(r3%m)); } else { if (r1 >= 0) return (r1%m); else return (m+(r1%m)); } } }
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 = 1e9 + 7; const int N = 105; int tot = 0; struct Mat { long long a[N][N]; Mat() { memset(a, 0, sizeof(a)); } Mat operator*(const Mat& tmp) const { Mat ans; for (int i = 0; i < tot; i++) { for (int j = 0; j < tot; j++) { for (int k = 0; k < tot; k++) { ans.a[i][j] += a[i][k] * tmp.a[k][j]; ans.a[i][j] %= MOD; } } } return ans; } }; Mat pw(Mat base, long long n) { Mat ans; for (int i = 0; i < tot; i++) ans.a[i][i] = 1; while (n) { if (n & 1) ans = ans * base; base = base * base; n >>= 1; } return ans; } long long pw(long long a, long long n) { long long ans = 1; while (n) { if (n & 1) ans = ans * a % MOD; a = a * a % MOD; n >>= 1; } return ans; } int x[N]; void solve() { int n, k; scanf("%d%d", &n, &k); int a = 0, b = 0; for (int i = 1; i <= n; i++) { scanf("%d", x + i); if (x[i]) a++; else b++; } long long inv = pw(n * (n - 1) / 2, MOD - 2); Mat base; tot = a + 1; for (int i = max(0, a - b); i <= a; i++) { long long p = a * (a - 1) / 2 + b * (b - 1) / 2 + i * (a - i) + (a - i) * (b - a + i); base.a[i][i] = p * inv % MOD; if (i > 0) { p = i * (b - a + i); base.a[i][i - 1] = p * inv % MOD; } if (i < a) { p = (a - i) * (a - i); base.a[i][i + 1] = p * inv % MOD; } } long long f[N] = {}; int cnt = 0; for (int i = n; i >= n - a + 1; i--) if (x[i]) cnt++; f[cnt] = 1; base = pw(base, k); long long ans = 0; for (int i = 0; i < tot; i++) { ans += base.a[i][a] * f[i]; ans %= MOD; } if (ans < 0) ans += MOD; cout << ans << endl; } int main() { 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 long long M = 1000000007; long long mul(long long a, long long b) { return a * b % M; } long long Pow(long long a, long long b) { long long x = 1; for (; b > 0; b >>= 1) { if (b & 1) x = mul(x, a); a = mul(a, a); } return x; } vector<vector<long long>> eye(int n) { vector<vector<long long>> I(n, vector<long long>(n)); for (int i = 0; i < n; ++i) I[i][i] = 1; return I; } long long add(long long a, long long b) { return (a + b) % M; } vector<vector<long long>> muli(vector<vector<long long>> &A, vector<vector<long long>> &B) { int l = A.size(), m = B.size(), n = B[0].size(); vector<vector<long long>> C(l, vector<long long>(n)); for (int i = 0; i < l; ++i) for (int k = 0; k < m; ++k) for (int j = 0; j < n; ++j) C[i][j] = add(C[i][j], mul(A[i][k], B[k][j])); return C; } vector<vector<long long>> pow(vector<vector<long long>> A, long long b) { vector<vector<long long>> X = eye(A.size()); for (; b > 0; b >>= 1) { if (b & 1) X = muli(X, A); A = muli(A, A); } return X; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n); int t = 0; for (auto &i : a) cin >> i, t += i == 0; int cur = 0; for (int i = 0; i < t; ++i) cur += a[i] > 0; vector<vector<long long>> m(t + 1, vector<long long>(t + 1)); long long tot = (n - 1) * n / 2; for (int i = 0; i <= t; ++i) for (int j = 0; j <= t; ++j) { if (j - 1 == i) m[i][j] = j * j % M * Pow(tot, M - 2) % M; if (j + 1 == i) m[i][j] = (t - j) * (n - t - j) % M * Pow(tot, M - 2) % M; if (j == i) m[i][j] = (tot - (j * j % M + (t - j) * (n - t - j) % M) % M + M) % M * Pow(tot, M - 2) % M; } m = pow(m, k); cout << m[0][cur] << "\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> struct MI { private: char bb[4096]; FILE *f; char *bs, *be; char e; bool o, l; public: MI() : MI(stdin) {} MI(FILE *f) : f(f), bs(0), be(0), o(0), l(0) {} inline char get() { if (o) { o = 0; return e; } if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f); if (bs == be) { l = 1; return -1; }; return *bs++; } inline void unget(char c) { o = 1; e = c; } template <class T> inline T read() { T r; *this > r; return r; } template <class T> inline MI &operator>(T &); }; template <class T> struct Q { const static bool U = T(-1) >= T(0); inline void operator()(MI &t, T &r) const { r = 0; char c; bool y = 0; if (U) for (;;) { c = t.get(); if (c == -1) goto E; if (isdigit(c)) break; } else for (;;) { c = t.get(); if (c == -1) goto E; if (c == '-') { c = t.get(); if (isdigit(c)) { y = 1; break; }; } else if (isdigit(c)) break; ; }; for (;;) { if (c == -1) goto E; if (isdigit(c)) r = r * 10 + (c ^ 48); else break; c = t.get(); } t.unget(c); E:; if (y) r = -r; } }; template <> struct Q<char> {}; template <class T> inline MI &MI::operator>(T &t) { Q<T>()(*this, t); return *this; } template <class T> inline std::ostream &operator<(std::ostream &out, const T &t) { return out << t; } using std::cout; MI cin; const int N = 101; const int mod = 1000000007; int n, cnt, K; struct mat { int v[N][N]; } tr, ret, tmp; inline int pro(int x) { return x >= mod ? x - mod : x; } inline int per(int x) { return x < 0 ? x + mod : x; } inline long long ksm(long long x, int p = mod - 2) { long long ret = 1; for (; p; p >>= 1, (x *= x) %= mod) if (p & 1) (ret *= x) %= mod; return ret; } inline void mult(const mat &a, const mat &b, mat &c) { const size_t clear_size(sizeof(int) * (cnt + 1)); for (int i = 0; i <= cnt; ++i) memset(c.v[i], 0, clear_size); for (int i = 0; i <= cnt; ++i) for (int k = 0; k <= cnt; ++k) for (int j = 0; j <= cnt; ++j) c.v[i][j] = pro(c.v[i][j] + (long long)a.v[i][k] * b.v[k][j] % mod); } bool v[N]; int main() { cin > n > K; int pre = 0; for (int i = 1; i <= n; ++i) if (!(v[i] = (cin.read<int>()))) pre += !v[++cnt]; if (cnt == n) return cout < "1\n", 0; const int mi(std::max(cnt * 2 - n, 0)), si(pro(ksm(((long long)n * (n - 1) % mod)) << 1)); for (int i = mi; i <= cnt; ++i) { int tmp = 1; if (i != mi) { tr.v[i][i - 1] = (long long)i * (n - cnt * 2 + i) % mod * si % mod; tmp = per(tmp - tr.v[i][i - 1]); } if (i != cnt) { tr.v[i][i + 1] = (long long)(cnt - i) * (cnt - i) % mod * si % mod; tmp = per(tmp - tr.v[i][i + 1]); } tr.v[i][i] = tmp; } for (int i = 0; i <= cnt; ++i) ret.v[i][i] = 1; while (K) { if (K & 1) { mult(ret, tr, tmp); ret = tmp; } mult(tr, tr, tmp); tr = tmp; K >>= 1; } cout < ret.v[pre][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; void read(long long &x) { x = 0; long long f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -f; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; x *= f; } void print(long long x) { if (x < 0) putchar('-'), x = -x; if (!x) return; print(x / 10), putchar(x % 10 + 48); } void write(long long x) { if (!x) putchar('0'); else print(x); putchar('\n'); } const long long maxn = 2e5 + 10; const long long inf = 1e9; const long double eps = 1e-12; const long long mod = 1e9 + 7; long long add(long long x, long long y) { return x + y >= mod ? x + y - mod : x + y; } long long del(long long x, long long y) { return x - y < 0 ? x - y + mod : x - y; } long long mul(long long x, long long y) { return 1ll * x * y - 1ll * x * y / mod * mod; } void inc(long long &x, long long y) { x += y; x %= mod; } long long n, k, a[maxn], res, c; long long qpow(long long a, long long x) { long long res = 1; for (; x; x >>= 1, a = 1ll * a * a % mod) if (x & 1) res = 1ll * res * a % mod; return res; } struct Matrix { long long a, b, r[110][110]; Matrix() { a = b = 0, memset(r, 0, sizeof r); } Matrix operator*(const Matrix &t) const { Matrix res; res.a = a, res.b = t.b; for (long long i = 0; i <= a; i++) for (long long j = 0; j <= t.b; j++) for (long long k = 0; k <= b; k++) res.r[i][j] = add(res.r[i][j], mul(r[i][k], t.r[k][j])); return res; } } tr, ans; signed main() { read(n), read(k); for (long long i = 1; i <= n; i++) read(a[i]), res += !(a[i] & 1); for (long long i = 1; i <= res; i++) c += !a[i]; ans.a = 0, ans.b = res; ans.r[0][c] = 1; tr.a = tr.b = res; for (long long i = 0; i <= res; i++) { long long lw = i, lb = res - i, rw = res - lw, rb = n - res - lb; if (i != res) inc(tr.r[i][i + 1], lb * rw % mod); if (i != 0) inc(tr.r[i][i - 1], lw * rb % mod); inc(tr.r[i][i], (res * (res - 1) + (n - res) * (n - res - 1)) % mod * qpow(2, mod - 2) % mod); inc(tr.r[i][i], (lw * lb + rw * rb) % mod); } long long x = k, f = 0; for (; x; x >>= 1, tr = tr * tr) if (x & 1) ans = ans * tr; for (long long i = 0, i_r = res; i <= i_r; i++) f = (f + ans.r[0][i]) % mod; write(ans.r[0][res] * qpow(f, 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
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/2+1][n/2+1]; for(int i=0;i<=n/2;i++){ if(i!=0){ A[i][i-1] = i * i * mot%mod; } if(i!=n/2){ 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); 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/2+1][n/2+1]; for(int i=0;i<n/2+1;i++){ for(int j=0;j<n/2+1;j++){ for(int k=0;k<n/2+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> using namespace std; template <typename Tp> void read(Tp &x) { x = 0; long long f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') { f = -1; } c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } x *= f; } struct Mat { long long N; long long mat[105][105]; Mat() { memset(mat, 0, sizeof(mat)); } void reset() { for (long long i = 0; i <= N; i++) mat[i][i] = 1; } }; Mat mul(Mat a, Mat b) { Mat ret; ret.N = a.N; long long N = a.N; for (long long i = 0; i <= N; i++) { for (long long j = 0; j <= N; j++) { for (long long k = 0; k <= N; k++) { ret.mat[i][j] = (ret.mat[i][j] + a.mat[i][k] * b.mat[k][j] % 1000000007) % 1000000007; } } } return ret; } Mat Ksm(Mat b, long long p) { Mat ret; ret.N = b.N; ret.reset(); while (p) { if (p & 1) ret = mul(ret, b); b = mul(b, b); p >>= 1; } return ret; } long long n, m; long long a[105], s[105]; long long fm; long long ksm(long long b, long long p) { long long ret = 1; while (p) { if (p & 1) ret = ret * b % 1000000007; b = b * b % 1000000007; p >>= 1; } return ret; } long long calc() { long long tep = n - s[n]; Mat Base; Mat ans; Base.N = max(tep, 1ll); ans.N = max(tep, 1ll); ans.mat[1][tep - s[tep]] = 1; for (long long i = 0; i <= tep; i++) { if (i) Base.mat[i - 1][i] = (tep - i + 1) % 1000000007 * (tep - i + 1) % 1000000007; if (i < tep) Base.mat[i + 1][i] = (i + 1) % 1000000007 * (n - 2 * tep + i + 1); Base.mat[i][i] += (tep - i) % 1000000007 * i % 1000000007; Base.mat[i][i] += (tep - i) % 1000000007 * (n - 2 * tep + i) % 1000000007; Base.mat[i][i] += (tep * (tep - 1) / 2) % 1000000007; Base.mat[i][i] += ((n - tep) * (n - tep - 1) / 2) % 1000000007; Base.mat[i][i] %= 1000000007; } ans = mul(ans, Ksm(Base, m)); return ans.mat[1][tep]; } signed main() { read(n); read(m); for (long long i = 1; i <= n; i++) read(a[i]), s[i] = s[i - 1] + a[i]; fm = n * (n - 1) / 2; fm = ksm(fm, m); fm = ksm(fm, 1000000007 - 2); cout << calc() * fm % 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
import java.io.*; import java.util.*; public class Main implements Runnable{ public static void main(String[] args){ //new Thread(null,new Main(),"Thread-1",1024*1024*10).start(); new Main().run(); } int[]a; int tot=0; long[]inv; int mod=1000000007; private void solve() throws Exception { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n=in.nextInt(),k=in.nextInt(); a=new int[n]; inv=getInvs(10000,mod); for(int i=0;i<n;i++){ a[i]=in.nextInt(); if(a[i]==1)tot++; } int where=0; for(int i=0;i<n-tot;i++){ if(a[i]==1)where++; } int len=Math.min(n-tot,tot); long[][]matrix=new long[len+1][len+1]; long all=(long)n*(n-1)*inv[2]%mod; long invall=inv[(int)all]; for(int i=0;i<=len;i++){ long cnt=0; if(i>0){ matrix[i-1][i]=i*i*invall%mod; cnt+=i*i; } if(i<len){ matrix[i+1][i]=(tot-i)*(n-tot-i)*invall%mod; cnt+=(tot-i)*(n-tot-i); } matrix[i][i]=((all-cnt)%mod+mod)%mod*invall%mod; } long[][]ans=pow(matrix,k); out.println(ans[0][where]); out.flush(); } public long[][]pow(long[][]a,int b){ long[][]ans=new long[a.length][a.length]; for(int i=0;i<a.length;i++)ans[i][i]=1; while(b>0){ if((b&1)==1)ans=modMul(ans,a); a=modMul(a,a); b>>=1; } return ans; } public long[][] modMul(long[][]a,long[][]b){ int row=a.length,column=b[0].length; int len=b.length; long[][] ans=new long[row][column]; for(int i=0;i<row;i++){ for(int j=0;j<len;j++){ //if(a[i][j]==0)continue;//稀疏矩阵优化 for(int k=0;k<column;k++){ ans[i][k]+=a[i][j]*b[j][k]%mod; ans[i][k]%=mod; } } } return ans; } public static long[] getInvs(int n,int mod){ long[]inv=new long[n+1]; inv[1]=1; for(int i=2;i<inv.length;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; return inv; } @Override public void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); } } class InputReader{ StreamTokenizer tokenizer; public InputReader(InputStream stream){ tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream))); tokenizer.ordinaryChars(33,126); tokenizer.wordChars(33,126); } public String next() throws IOException { tokenizer.nextToken(); return tokenizer.sval; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean hasNext() throws IOException { int res=tokenizer.nextToken(); tokenizer.pushBack(); return res!=tokenizer.TT_EOF; } } }
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 int inf = 0x3f3f3f3f; const double PI = acos(-1.0); int m, n, x, t, y, a[105], k, l, r, z, all, b[105]; long long en; char c; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n, long long p = (long long)1000000007) { long long r = 1 % p; for (a %= p; n; a = a * a % p, n >>= 1) if (n & 1) r = r * a % p; return r; } long long inv(long long x, long long p = (long long)1000000007) { return x <= 1 ? 1 : inv(p % x, p) * (p - p / x) % p; } void init() {} struct Matix { long long a[105][105]; } q, ans; Matix operator*(Matix aa, Matix bb) { Matix res = {}; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) res.a[i][j] = 0; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) (res.a[i][j] += 1ll * aa.a[i][k] * bb.a[k][j] % (long long)1000000007) %= (long long)1000000007; return res; } Matix operator^(Matix aa, int bb) { Matix res = {}; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) res.a[i][j] = (i == j); for (; bb; aa = aa * aa, bb >>= 1) if (bb & 1) res = res * aa; return res; } void solve() { scanf("%d%d", &n, &k); int m1 = 0, m0 = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i]) { m1++; } else m0++; } m = m0; Matix res = {}; for (int i = 0; i <= m0; i++) for (int j = 0; j <= m0; j++) res.a[i][j] = 0; for (int i = 0; i <= m0; i++) { if (i != 0) { res.a[i - 1][i] = 1LL * (m0 - i + 1) * (m0 - i + 1) % (long long)1000000007; } res.a[i][i] = inv(2) * m1 * (m1 - 1) % (long long)1000000007 + inv(2) * m0 * (m0 - 1) % (long long)1000000007 + i * (m0 - i) % (long long)1000000007 + (m0 - i) * (m1 - m0 + i) % (long long)1000000007; res.a[i][i] %= (long long)1000000007; if (i != m0) { res.a[i + 1][i] = 1LL * (i + 1) * (m1 - m0 + i + 1) % (long long)1000000007; } } x = 0; for (int i = 1; i <= m0; i++) { if (!a[i]) x++; } ans = res ^ k; en = inv(qpow(n * (n - 1) / 2, k)) * ans.a[x][m0] % (long long)1000000007; printf("%lld\n", en); } int main() { int T; T = 1; for (int i = 1; i <= T; i++) { 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> using namespace std; const int M = 1000000007; const int MM = 998244353; const long double PI = acos(-1); template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << '(' << p.first << "," << p.second << ')'; } const int MOD = 1000000007; struct Mint { int val; Mint(long long v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int mod_inv(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } Mint &operator+=(const Mint &other) { val += other.val; if (val >= MOD) val -= MOD; return *this; } Mint &operator-=(const Mint &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } Mint &operator*=(const Mint &other) { val = fast_mod((uint64_t)val * other.val); return *this; } Mint &operator/=(const Mint &other) { return *this *= other.inv(); } friend Mint operator+(const Mint &a, const Mint &b) { return Mint(a) += b; } friend Mint operator-(const Mint &a, const Mint &b) { return Mint(a) -= b; } friend Mint operator*(const Mint &a, const Mint &b) { return Mint(a) *= b; } friend Mint operator/(const Mint &a, const Mint &b) { return Mint(a) /= b; } Mint &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } Mint &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } Mint operator++(int32_t) { Mint before = *this; ++*this; return before; } Mint operator--(int32_t) { Mint before = *this; --*this; return before; } Mint operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const Mint &other) const { return val == other.val; } bool operator!=(const Mint &other) const { return val != other.val; } Mint inv() const { return mod_inv(val); } Mint power(long long p) const { assert(p >= 0); Mint a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &stream, const Mint &m) { return stream << m.val; } friend istream &operator>>(istream &stream, Mint &m) { return stream >> m.val; } }; struct Matrix { vector<vector<Mint>> val; long long n_; Matrix(long long n) : n_(n) { val.resize(n_); for (long long i = 0; i < n_; ++i) val[i].resize(n_); } void print() { for (long long i = 0; i < n_; ++i) { for (long long j = 0; j < n_; ++j) cout << val[i][j] << " "; cout << "\n"; } } void set(long long x) { for (long long i = 0; i < n_; ++i) for (long long j = 0; j < n_; ++j) val[i][j] = x; } Matrix operator*(const Matrix &b) const { long long n = n_; Matrix ans(n); ans.set(0); for (long long i = 0; i < n_; ++i) { for (long long j = 0; j < n_; ++j) { ans.val[i][j] = 0; for (long long k = 0; k < n_; ++k) { ans.val[i][j] += val[i][k] * b.val[k][j]; } } } return ans; } }; Matrix I(long long n) { Matrix Iden(n); Iden.set(0); for (long long i = 0; i < n; ++i) Iden.val[i][i] = 1; return Iden; } Matrix power(Matrix m, long long pw) { if (pw == 0) return I(m.n_); if (pw == 1) return m; Matrix t = power(m, pw / 2); t = t * t; if (pw & 1) t = t * m; return t; } int _runtimeTerror_() { int n, k; cin >> n >> k; int zero = 0; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; zero += a[i] == 0; } int cur = 0; for (int i = 0; i < zero; ++i) cur += a[i] == 1; vector<Mint> ans(zero + 1, 0); ans[cur] = 1; Matrix dp(zero + 1); dp.set(0); Mint x = n * (n - 1) / 2; for (int i = 0; i <= zero; ++i) { dp.val[i][i] = i * i + (zero - i) * (n - zero - i); dp.val[i][i] = 1 - dp.val[i][i] / x; if (i) dp.val[i][i - 1] = (zero - i + 1) * (n - zero - i + 1) / x; if (i < zero) dp.val[i][i + 1] = (i + 1) * (i + 1) / x; } dp = power(dp, k); Mint A = 0; for (int i = 0; i <= zero; ++i) A += dp.val[0][i] * ans[i]; cout << A << "\n"; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) _runtimeTerror_(); 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 x = 0; char ch = getchar(); bool d = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') d = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return d ? x : -x; } inline unsigned long long rnd() { return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; } const int N = 105, mo = 1e9 + 7, inv2 = (mo + 1) / 2; int ksm(int x, int p) { int res = 1; for (; p; p >>= 1, x = (long long)x * x % mo) { if (p & 1) res = (long long)res * x % mo; } return res; } struct mat { int n, m, a[N][N]; mat() {} mat(int _n, int _m) { n = _n; m = _m; memset(a, 0, sizeof(a)); } int *operator[](int &x) { return a[x]; } int *operator[](const int &x) { return a[x]; } } F, ssw; mat operator*(mat A, mat B) { mat C(A.n, B.m); for (int i = (int)(0); i <= (int)(A.n); i++) for (int k = (int)(0); k <= (int)(A.m); k++) { for (int j = (int)(0); j <= (int)(B.m); j++) C[i][j] = (C[i][j] + (long long)A[i][k] * B[k][j]) % mo; } return C; } int n, a[N]; mat power(mat x, int p) { mat res = x; p--; if (p == 0) return res; for (; p; p >>= 1, x = x * x) { if (p & 1) res = res * x; } return res; } int f[N][N]; int main() { n = read(); int k = read(), m = 0, tot = 0; for (int i = (int)(1); i <= (int)(n); i++) { a[i] = read(); if (a[i] == 0) tot++; else m++; } F.n = F.m = m; for (int i = (int)(0); i <= (int)(m); i++) { int zl = tot - m + i, zy = n - tot - i, yl = m - i, yy = i; F[i][i] = (long long)m * (m - 1) % mo * inv2 % mo; F[i][i] = (F[i][i] + (long long)(n - m) * (n - m - 1) % mo * inv2) % mo; F[i][i] = (F[i][i] + (long long)zl * yl) % mo; F[i][i] = (F[i][i] + (long long)zy * yy) % mo; if (i) F[i][i - 1] = (long long)zl * yy % mo; if (i <= m) F[i][i + 1] = (long long)zy * yl % mo; } ssw.n = 1; ssw.m = m; int cnt = 0; for (int i = (int)(n); i >= (int)(n - m + 1); i--) if (a[i] == 1) cnt++; ssw[1][cnt] = 1; if (k != 0) ssw = ssw * power(F, k); int p = ksm(n * (n - 1) / 2, mo - 2); cout << (long long)ssw[1][m] * ksm(p, k) % mo; }
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
// Don't place your source in a package import javax.swing.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; import java.math.*; import java.util.stream.Stream; // Please name your class Main public class Main { static FastScanner fs=new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int Int() { return Integer.parseInt(next()); } long Long() { return Long.parseLong(next()); } String Str(){ return next(); } } public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //reading /writing file //Scanner sc=new Scanner(new File("input.txt")); //PrintWriter pr=new PrintWriter("output.txt"); int T=1; for(int t=0;t<T;t++){ int n=Int();int k=Int(); int A[]=Arr(n); Solution sol=new Solution(out); sol.solution(A,k); } out.close(); } public static int[] Arr(int n){ int A[]=new int[n]; for(int i=0;i<n;i++)A[i]=Int(); return A; } public static int Int(){ return fs.Int(); } public static long Long(){ return fs.Long(); } public static String Str(){ return fs.Str(); } } class Solution{ PrintWriter out; int INF=Integer.MAX_VALUE; int MOD=1000000007; public Solution(PrintWriter out){ this.out=out; } public void solution(int A[],int k) { //non-decreasing order => 0000 11111111 int N=A.length; int zero=0; for(int i:A){ if(i==0){ zero++; } } int one=N-zero; if(zero==0||zero==A.length){ out.println(1); return; } //init NCR int ncr[][]=new int[N+5][N+5]; ncr[0][0]=1; for (int i=1;i<ncr.length;i++) { ncr[i][0]=1; for (int j=1;j<ncr[0].length;j++) { ncr[i][j]=(ncr[i-1][j]+ncr[i-1][j-1])%MOD; } } long M[][]=new long[zero+1][zero+1]; for(int i=0;i<M.length;i++){ int lzero=i; int lone=zero-lzero; int rzero=zero-i; int rone=N-zero-lone; if(lone<0||lone>one||rone<0||rone>one||rzero>(N-zero))continue; // System.out.println(lzero+" "+rzero+" "+lone+" "+rone); M[i][i]+=(ncr[zero][2]+ncr[N-zero][2]);//not cross M[i][i]%=MOD; //0->0 M[i][i]+=(lzero*rzero); M[i][i]%=MOD; //1->1 M[i][i]+=(lone*rone); M[i][i]%=MOD; //0<-1 last:decrease zero if(i+1<M.length&&lzero+1<=zero&&rzero-1>=0&&lone-1>=0&&rone+1<=one){//reverse back to last state M[i][i+1]+=((lzero+1)*(rone+1)); M[i][i+1]%=MOD; } //1<-0 if(i-1>=0&&lzero-1>=0&&rzero+1<=one&&lone+1<=zero&&rone-1>=0){//reverse back to last state M[i][i-1]+=((lone+1)*(rzero+1)); M[i][i-1]%=MOD; } } M=Matrix.matrixPower(M,k); /*System.out.println(); for(long p[]:M){ System.out.println(Arrays.toString(p)); }*/ int cnt=0; for(int i=0;i<zero;i++){ if(A[i]==0)cnt++; } long f[]=new long[zero+1]; f[cnt]=1; long ff[]=new long[zero+1]; for(int i=0;i<M.length;i++){ long sum=0; for(int j=0;j<M[0].length;j++){ sum+=M[i][j]*f[j]; sum%=MOD; } ff[i]=sum; } //System.out.println(); //System.out.println(Arrays.toString(ff)); long a=ff[zero]; long b=0; for(long i:ff){ b+=i; b%=MOD; } long bin=inverse(b); a*=bin; a%=MOD; out.println(a); } public long inverse(long val){ BigInteger B= BigInteger.valueOf(val); long binverse=B.modInverse(BigInteger.valueOf(MOD)).longValue(); return binverse; } } class Matrix { static int mod=1000000007; /** * compute pow(base, pow) * O(N^3) * logN **/ public static long[][] matrixPower(long [][] base, long pow) { int N = base.length; long [][] ans = new long [N][N]; // generate identity matrix for (int i = 0; i < N; i++) ans[i][i] = 1; // binary exponentiation while ( pow != 0 ) { if ( (pow&1) != 0 ) ans = multiplyMatrix(ans, base); base = multiplyMatrix(base, base); pow >>= 1; } return ans; } /** * compute m * m2 * O(N^3) **/ public static long [][] multiplyMatrix(long [][] m, long [][] m2) { int N = m.length; long [][] ans = new long [N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { ans[i][j] = 0; for (int k = 0; k < N; k++) { ans[i][j] += m[i][k] * m2[k][j]; ans[i][j] %= mod; } } return ans; } }
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 = 1e9 + 7; int n, num, a[105]; long long k, num1[105], num2[105]; struct Matrix { long long a[105][105]; Matrix() { memset(a, 0, sizeof(a)); } long long* operator[](int i) { return a[i]; } Matrix operator+(Matrix b) { Matrix ans; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) ans[i][j] = a[i][j] + b[i][j]; return ans; } Matrix operator-(Matrix b) { Matrix ans; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) ans[i][j] = a[i][j] - b[i][j]; return ans; } Matrix operator*(Matrix b) { Matrix ans; for (int k = 0; k <= n; k++) for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % MOD; return ans; } } A; long long quick_pow(long long x, long long a) { long long ans = 1; while (a) { if (a & 1) ans = ans * x % MOD; x = x * x % MOD; a >>= 1; } return ans; } Matrix quick_pow(Matrix x, long long a) { Matrix ans; for (int i = 0; i <= n; i++) ans[i][i] = 1; while (a) { if (a & 1) ans = ans * x; x = x * x; a >>= 1; } return ans; } int calc() { int ans = 0; for (int i = 1; i <= num; i++) if (a[i] == 1) ans++; return ans; } int main() { scanf("%d%I64d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] == 0) num++; } if (num > n - num) { for (int i = 1; i <= n; i++) a[i] ^= 1; for (int i = 1; i <= n / 2; i++) swap(a[i], a[n - i + 1]); } num = min(num, n - num); long long inv = quick_pow(n * (n - 1) / 2, MOD - 2); for (int i = 0; i <= num; i++) { num1[i] = i * i; num2[i] = (n - num - i) * (num - i); if (i) A[i][i - 1] = num1[i] * inv % MOD; if (i != num) A[i][i + 1] = num2[i] * inv % MOD; A[i][i] = (n * (n - 1) / 2 - num1[i] - num2[i]) * inv % MOD; } A = quick_pow(A, k); printf("%I64d\n", A[calc()][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 = 1e9 + 7; int N, K; struct Matrix { long long a[103][103]; Matrix operator*(Matrix other) const { Matrix ret; int i, j, k; for (i = 0; i <= N; ++i) { for (j = 0; j <= N; ++j) { ret.a[i][j] = 0; for (k = 0; k <= N; ++k) { ret.a[i][j] += a[i][k] * other.a[k][j]; ret.a[i][j] %= mod; } } } return ret; } }; Matrix I; Matrix fastpower(Matrix mt, long long p) { Matrix ret = I; while (p > 0) { if (p % 2 == 1) { ret = ret * mt; p--; } else { mt = mt * mt; p /= 2; } } return ret; } long long calc(long long v) { long long ret = 1, p = mod - 2; while (p > 0) { if (p % 2 == 1) { ret = ret * v; ret %= mod; p--; } else { v = v * v; v %= mod; p /= 2; } } return ret; } int inv[103 * 103]; int a[103]; Matrix mt; int main() { int i, cnt = 0; for (i = 1; i < (103 * 103); ++i) { inv[i] = calc(i); } scanf("%d%d", &N, &K); for (i = 1; i <= N; ++i) { scanf("%d", &a[i]); if (a[i] == 0) { cnt++; } I.a[i][i] = 1; } I.a[0][0] = 1; if (cnt == 0 || cnt == N) { printf("1\n"); return 0; } for (i = 0; i <= N; ++i) { long long iv = inv[N * (N - 1) / 2]; if (i > 0) { if (cnt - (i - 1) >= 0 && N - cnt - (i - 1) >= 0) { mt.a[i][i - 1] += 1ll * (cnt - (i - 1)) * (N - cnt - (i - 1)) * iv; mt.a[i][i - 1] %= mod; } } if (cnt - i >= 0 && N - cnt - i >= 0) { long long tol = N * (N - 1) / 2; tol -= 1ll * (cnt - i) * (N - cnt - i); tol -= 1ll * i * i; tol *= iv; tol %= mod; mt.a[i][i] += tol; mt.a[i][i] %= mod; } if (i < N) { if ((i + 1) <= cnt && N - cnt - (i + 1) >= 0) { mt.a[i][i + 1] += 1ll * (i + 1) * (i + 1) * iv; mt.a[i][i + 1] %= mod; } } } N = N - cnt; mt = fastpower(mt, K); int tol = 0; for (i = 1; i <= cnt; ++i) { tol += a[i]; } printf("%I64d\n", (mt.a[0][tol] + 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; long long base[101][101], ans[101][101]; long long n, k, maxmisp; long long zc = 0, oc = 0; void mulans() { long long fin[101][101]; memset(fin, 0, sizeof(fin)); for (long long i = 0; i <= 100; i++) for (long long j = 0; j <= 100; j++) for (long long k = 0; k <= 100; k++) { fin[i][j] = (ans[i][k] * base[k][j] + fin[i][j]) % mod; } memcpy(ans, fin, sizeof(fin)); } void mulbase() { long long fin[101][101]; memset(fin, 0, sizeof(fin)); for (long long i = 0; i <= 100; i++) for (long long j = 0; j <= 100; j++) for (long long k = 0; k <= 100; k++) { fin[i][j] = (base[i][k] * base[k][j] + fin[i][j]) % mod; } memcpy(base, fin, sizeof(fin)); } long long getinv(long long base) { long long expo = mod - 2; long long ans = 1; while (expo) { if (expo & 1) ans = ans * base % mod; base = base * base % mod; expo >>= 1; } return ans; } void calcbase() { long long den = 2 * getinv(n * (n - 1) % mod) % mod; for (long long i = 0; i <= maxmisp; i++) { long long properz = zc - i, propero = oc - i, wrongz, wrongo; wrongz = wrongo = i; long long a = wrongo * wrongz % mod; a = (a * den) % mod; long long b = (zc * (zc - 1)) / 2 + (oc * (oc - 1)) / 2 + (wrongz * propero) + (wrongo * properz); b %= mod; b = (b * den) % mod; long long c = properz * propero % mod; c = (c * den) % mod; long long sum = (a + b + c) % mod; if (i > 0) { base[i - 1][i] = a; } base[i][i] = b; if (i < maxmisp) base[i + 1][i] = c; } } void power() { for (long long i = 0; i <= 100; i++) ans[i][i] = 1; while (k) { if (k & 1) mulans(); mulbase(); k >>= 1; } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; if (v[i]) oc++; else zc++; } maxmisp = min(zc, oc); long long currmisp = 0; for (long long i = 0; i < n; i++) { if (v[i] == 0 && i >= zc) { currmisp++; } } calcbase(); power(); cout << ans[0][currmisp]; 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; struct Matrix { int n, m, a[146][146]; Matrix(int _n = 0, int _m = 0, int x = 0) { n = _n, m = _m; memset(a, 0, sizeof a); for (int i = 0; i < min(n, m); ++i) a[i][i] = x; } Matrix operator*(const Matrix& b) { Matrix res = Matrix(n, b.m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (a[i][j]) for (int k = 0; k < b.m; ++k) (res.a[i][k] += 1ll * a[i][j] * b.a[j][k] % Mod) %= Mod; return res; } Matrix operator^(int p) { Matrix res = Matrix(n, n, 1), x = *this; while (p) { if (p & 1) res = res * x; x = x * x; p >>= 1; } return res; } }; inline int fsp(int x, int p = Mod - 2) { int res = 1; while (p) { if (p & 1) res = 1ll * res * x % Mod; x = 1ll * x * x % Mod; p >>= 1; } return res; } int n, m, cnt, tmp, a[146]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { scanf("%d", a + i); if (!a[i]) ++cnt; } for (int i = 1; i <= cnt; ++i) tmp += a[i] ^ 1; Matrix base = Matrix(cnt + 1, cnt + 1), res; for (int i = 0; i <= cnt; ++i) { if (i < cnt) base.a[i + 1][i] += (cnt - i) * (cnt - i); base.a[i][i] += cnt * (cnt - 1) / 2 + (n - cnt) * (n - cnt - 1) / 2 + i * (cnt - i) + (cnt - i) * (n + i - 2 * cnt); if (i) base.a[i - 1][i] += i * (n + i - 2 * cnt); } res = base ^ m; long long ans = 0; for (int i = 0; i <= cnt; ++i) (ans += res.a[i][tmp]) %= Mod; cout << 1ll * res.a[cnt][tmp] * fsp(ans) % Mod << endl; return 0; }
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, raw_input().split()) A = [int(a) for a in raw_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])
PYTHON
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 x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } const long long N = 107, MOD = 1e9 + 7; long long n, K, c; bool s[N]; struct Mat { long long val[N][N]; long long Max_n, Max_m; Mat() { memset(val, 0, sizeof(val)); Max_n = Max_m = 0; } Mat operator*(const Mat el) const { Mat c; for (long long i = 0; i <= Max_n; ++i) for (long long j = 0; j <= el.Max_m; ++j) for (long long k = 0; k <= Max_m; ++k) c.val[i][j] = (c.val[i][j] + ((val[i][k] * el.val[k][j]) % MOD)) % MOD; c.Max_n = Max_n, c.Max_m = el.Max_m; return c; } inline void I(long long n) { Max_n = Max_m = n; for (long long i = 0; i <= n; ++i) val[i][i] = 1; } }; inline long long power(long long x, long long y) { long long res = 1, base = x; while (y) { if (y & 1) res = (res * base) % MOD; base = (base * base) % MOD; y >>= 1; } return res; } signed main() { n = read(), K = read(); for (long long i = 1; i <= n; ++i) s[i] = read(), c += (s[i] == 0); Mat A, F; long long t = 0; for (long long i = 1; i <= c; ++i) if (s[i] == 0) ++t; F.val[0][t] = 1; F.Max_n = 1, F.Max_m = c; for (long long i = 0; i <= c; ++i) { if (i != 0) A.val[i - 1][i] = (c - i + 1) * (c - i + 1); A.val[i][i] = (c * (c - 1) / 2) + ((n - c) * (n - c - 1) / 2) + (i * (c - i)) + (c - i) * (n - c - c + i); if (i != c) A.val[i + 1][i] = (i + 1) * (n - c - c + i + 1); } A.Max_n = A.Max_m = c; long long y = K; Mat res; res.I(c); while (y) { if (y & 1) res = res * A; A = A * A; y >>= 1; } F = F * res; long long ans = F.val[0][c], sum = 0; for (long long i = 0; i <= c; ++i) sum = (sum + F.val[0][i]) % MOD; ans = (ans * power(sum, MOD - 2)) % MOD; 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; 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); for (int ok0 = 0; ok0 <= cnt0; ok0++) { mint dec = ok0 * (n + ok0 - 2 * cnt0); mint inc = (cnt0 - ok0) * (cnt0 - ok0); if (ok0 > 0) trans[ok0 - 1][ok0] = dec; trans[ok0][ok0] = n * (n - 1) * inv(2) - dec - inc; if (ok0 < cnt0) trans[ok0 + 1][ok0] = inc; } auto res = (trans ^ k) * state; auto sum = accumulate(res.begin(), res.end(), mint(0)); cout << res[cnt0] * inv(sum) << 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; using ll = long long; using Matrix = vector<vector<ll>>; const ll MOD = 1e9 + 7; Matrix prod(const Matrix &a, const Matrix &b) { int m = a.size(); Matrix ret(m, vector<ll>(m, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < m; k++) { ret[i][j] += a[i][k] * b[k][j]; ret[i][j] %= MOD; } } } return ret; } Matrix matpow(Matrix &a, ll n) { int m = a.size(); Matrix ret(m, vector<ll>(m, 0)); for (int i = 0; i < m; i++) ret[i][i] = 1; while (n) { if (n & 1) { ret = prod(ret, a); } a = prod(a, a); n /= 2; } return ret; } template <typename T> T mypow(T a, T b) { if (b == 0) return 1; T tmp = mypow(a, b / 2); if (b % 2) return (((tmp * tmp) % MOD) * a) % MOD; else return (tmp * tmp) % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; ll zero = 0, cnt = 0; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 0) zero++; } for (int i = 0; i < n; i++) { if (a[i] && i < zero) cnt++; } ll one = n - zero, mi = min(zero, one); Matrix mat(mi + 1, vector<ll>(mi + 1, 0)); ll al = (n * (n - 1) / 2) % MOD; al = mypow(al, MOD - 2); for (ll i = 0; i <= mi; i++) { ll sum = (one * (one - 1) / 2) % MOD + (zero * (zero - 1) / 2) % MOD; sum += (i * (n - 2 * i)) % MOD; sum %= MOD; sum *= al; sum %= MOD; mat[i][i] = sum; if (i) { sum = (zero - i + 1) * (one - i + 1) % MOD; sum *= al; sum %= MOD; mat[i][i - 1] = sum; } if (i + 1 <= mi) { sum = (i + 1) * (i + 1) % MOD; sum *= al; sum %= MOD; mat[i][i + 1] = sum; } } mat = matpow(mat, k); cout << mat[0][cnt] << 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; using ll = long long; const int mod = 1e9 + 7, oo = 1e9; const ll loo = 1e18; ll modpow(ll a, ll b) { ll ans = 1; for (; b; b /= 2, a = a * a % mod) if (b & 1) ans = (ans * a) % mod; return ans; } ll gcd(ll a, ll b) { while (a) b %= a, swap(a, b); return b; } const bool DEBUG = 1; const int mxn = 100; int n, k, y; int s[mxn]; void add(ll &a, ll b) { if (b >= mod) b %= mod; a += b; if (a >= mod) a -= mod; } struct Matrx { ll a[mxn][mxn]; Matrx() { for (int i = 0; i < mxn; i++) for (int j = 0; j < mxn; j++) a[i][j] = 0; } Matrx mul(Matrx b) { Matrx c; for (int i = 0; i < mxn; i++) for (int j = 0; j < mxn; j++) for (int k = 0; k < mxn; k++) add(c.a[i][k], a[i][j] * b.a[j][k]); return c; } void display() { cout << "ARR: " << endl; for (int i = 0; i < y + 1; i++) { for (int j = 0; j < y + 1; j++) cout << a[i][j] << " "; cout << endl; } cout << " ======================== " << endl; } } id; Matrx expo(Matrx a, int b) { return b ? (b & 1 ? a : id).mul(expo(a.mul(a), b / 2)) : id; } ll inv(ll a) { return modpow(a, mod - 2); } void ready(Matrx &a) { ll N = (n * (n - 1) / 2) % mod; ll invN = inv(N); for (int c = 0; c < y + 1; c++) { int d = (n - y) - (y - c); if (d < 0) continue; ll M = 0; if (c) { add(a.a[c - 1][c], c * d * invN); M += c * d; } if (c < y) { add(a.a[c + 1][c], (y - c) * (y - c) * invN); M += (y - c) * (y - c); } add(a.a[c][c], (N - M) * invN); } } void solve() { y = 0; int cur = 0; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) y += !s[i]; if (y == 0 || y == n) { cout << 1 << endl; return; } for (int i = 0; i < y; i++) cur += !s[i]; for (int i = 0; i < mxn; i++) id.a[i][i] = 1; Matrx func; ready(func); Matrx coef; coef.a[cur][0] = 1; Matrx ans = expo(func, k).mul(coef); ll sum = 0; for (int i = 0; i < y + 1; i++) sum = (sum + ans.a[i][0]) % mod; cout << ans.a[y][0] * inv(sum) % mod << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); while (cin >> n >> k) 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; int n, a[110]; long long p, q, k, ansx, ansy; struct mat { long long m[110][110]; mat() { memset(m, 0, sizeof(m)); }; } ans, tmp; inline void add(long long &x, long long y) { if (y >= 1000000007) y -= 1000000007; x += y; if (x >= 1000000007) x -= 1000000007; } inline mat mul(mat a, mat b) { mat res; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) add(res.m[i][j], a.m[i][k] * b.m[k][j] % 1000000007); return res; } inline mat mpower(mat a, long long b) { mat res; for (int i = 0; i <= n; i++) res.m[i][i] = 1ll; while (b) { if (b & 1ll) res = mul(res, a); a = mul(a, a); b >>= 1ll; } return res; } inline long long power(long long a, long long b) { long long res = 1ll; while (b) { if (b & 1ll) res = res * a % 1000000007; a = a * a % 1000000007; b >>= 1ll; } return res; } int main() { scanf("%d%lld", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] == 0) p++; } for (int i = 1; i <= p; i++) if (a[i] == 0) q++; ans.m[0][q] = 1ll; for (int i = 0; i <= p; i++) { if (i) tmp.m[i - 1][i] = (long long)(p - i + 1) * (p - i + 1) % 1000000007; add(tmp.m[i][i], (long long)i * (p - i) % 1000000007 + (long long)(p - i) * (n - p - p + i) % 1000000007); add(tmp.m[i][i], (long long)p * (p - 1) / 2 % 1000000007 + (long long)(n - p) * (n - p - 1) / 2 % 1000000007); if (i != p) tmp.m[i + 1][i] = (long long)(i + 1) * (n - p - p + i + 1) % 1000000007; } tmp = mpower(tmp, k); ans = mul(ans, tmp); ansx = ans.m[0][p]; for (int i = 0; i <= p; i++) add(ansy, ans.m[0][i]); printf("%lld\n", ansx * power(ansy, 1000000007 - 2) % 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 N = 1e2 + 5; const long long p = 1e9 + 7; long long read() { long long s = 0; char c = getchar(), lc = '+'; while (c < '0' || '9' < c) lc = c, c = getchar(); while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar(); return lc == '-' ? -s : s; } void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x < 10) putchar(x + '0'); else { write(x / 10); putchar(x % 10 + '0'); } } void print(long long x, char c = '\n') { write(x); putchar(c); } long long power(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % p; a = a * a % p; b /= 2; } return ret; } inline void Mod(long long &x) { if (x >= p) x -= p; } struct Matrix { long long a[N][N]; Matrix() { memset(a, 0, sizeof(a)); } } t; struct Vector { long long a[N]; Vector() { memset(a, 0, sizeof(a)); } } ans; Matrix operator*(const Matrix &a, const Matrix &b) { Matrix ret; for (register long long i = 0; i <= 100; i++) for (register long long k = 0; k <= 100; k++) for (register long long j = 0; j <= 100; j++) Mod(ret.a[i][j] += a.a[i][k] * b.a[k][j] % p); return ret; } Vector operator*(const Matrix &a, const Vector &b) { Vector ret; for (register long long i = 0; i <= 100; i++) for (register long long k = 0; k <= 100; k++) Mod(ret.a[k] += b.a[i] * a.a[i][k] % p); return ret; } long long a[N]; signed main() { long long n = read(), k = read(), x = 0, y = 0, sx = 0, inv = power(n * (n - 1) / 2, p - 2); for (long long i = 1; i <= n; i++) a[i] = read(); for (long long i = 1; i <= n; i++) x += a[i] == 0; for (long long i = 1; i <= n; i++) y += a[i] == 1; for (long long i = 1; i <= x; i++) sx += a[i] == 1; ans.a[sx] = 1; for (long long i = 0; i <= x; i++) { if (i > 0) t.a[i][i - 1] = i * i * inv % p; if (i < x && i < y) t.a[i][i + 1] = (x - i) * (y - i) * inv % p; t.a[i][i] = (n * (n - 1) / 2 - i * i - (i < x && i < y) * (x - i) * (y - i)) * inv % p; } while (k) { if (k & 1) ans = t * ans; t = t * t; k /= 2; } print(ans.a[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 = 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; long long fastmod(long long a, long long m = M) { return (-m < a and a < m) ? a : a % m; } long long fastmul(long long a, long long b, long long m = M) { if (b == 0) return 0; if (a * b / b == a) return fastmod(a * b, m); long long r = 0; while (b) { if (b & 1) r = fastmod(r + a, m); a = fastmod(a + a, m); b /= 2; } return r; } template <int m = M> struct modArit { int v; modArit(int v = 0) : v(fastmod(v, m)) {} 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>(fastmul(v, r.v, m)); } modArit<m> operator/(modArit<m> r) const { return *this * inv(r); } modArit<m> operator/(int r) const { return modArit<m>(v / r); } }; template <int n, int m, class T = modArit<>> struct Matrix { T a[N][N] = {}; Matrix(int d = 0) { for (int i = 0; i < n; i++) a[i][i] = d; } template <int mm> Matrix<n, mm, T> operator*(const Matrix<m, mm, T> r) { 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.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<N, N> 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<N, N> 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; void add(long long &a, long long b) { a += b; if (a >= ((long long)1e9 + 7)) a -= ((long long)1e9 + 7); if (a < 0) a += ((long long)1e9 + 7); } struct Matrix { int n, m; long long a[105][105]; Matrix() { memset(a, 0, sizeof(a)); } Matrix(int x, int y) : n(x), m(y) { memset(a, 0, sizeof(a)); } void init() { for (int i = 0; i <= m; i++) { a[i][i] = 1; } } friend Matrix operator*(Matrix a, Matrix b) { Matrix c(a.n, b.m); for (int i = 0; i <= a.n; i++) { for (int j = 0; j <= b.m; j++) { for (int k = 0; k <= a.m; k++) { add(c.a[i][j], a.a[i][k] * b.a[k][j] % ((long long)1e9 + 7)); } } } return c; } } mat, f; const long long inv2 = (((long long)1e9 + 7) + 1) >> 1; int n, m, lim; int p[105]; long long a, b; Matrix qpow(Matrix a, long long n) { Matrix res(lim, lim); res.init(); while (n) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } long long qpow(long long a, int n) { long long res = 1; while (n) { if (n & 1) res = res * a % ((long long)1e9 + 7); a = a * a % ((long long)1e9 + 7); n >>= 1; } return res; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d", &p[i]), b += p[i]; a = n - b; lim = min(a, b); f.n = 0, f.m = lim; mat.n = mat.m = lim; int tmp = 0; for (int i = 1; i <= a; i++) tmp += p[i]; f.a[0][tmp] = 1; for (int i = 0; i <= lim; i++) { mat.a[i][i] = (a * (a - 1) % ((long long)1e9 + 7) * inv2 % ((long long)1e9 + 7) + b * (b - 1) % ((long long)1e9 + 7) * inv2 % ((long long)1e9 + 7) + (a - i) * i % ((long long)1e9 + 7) + (b - i) * i % ((long long)1e9 + 7)) % ((long long)1e9 + 7); if (i > 0) mat.a[i - 1][i] = (b - i + 1) % ((long long)1e9 + 7) * (a - i + 1) % ((long long)1e9 + 7); if (i < lim) mat.a[i + 1][i] = (i + 1) % ((long long)1e9 + 7) * (i + 1) % ((long long)1e9 + 7); } Matrix res = qpow(mat, m); res = f * res; long long t = (long long)n * (n - 1) % ((long long)1e9 + 7) * inv2 % ((long long)1e9 + 7); t = qpow(t, m); long long ans = res.a[0][0] * qpow(t, ((long long)1e9 + 7) - 2) % ((long long)1e9 + 7); 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; using ll = long long; int const nmax = 100; int const modulo = 1000000007; struct Matrix { int n; int m; vector<vector<int>> mat; Matrix(int n_, int m_) { n = n_; m = m_; mat.resize(n); for (int i = 0; i < n; i++) mat[i].resize(m); } Matrix operator*(Matrix const &a) const { assert(m == a.n); Matrix result(n, a.m); for (int i = 0; i < n; i++) for (int j = 0; j < a.m; j++) for (int h = 0; h < m; h++) { result.mat[i][j] += 1LL * mat[i][h] * a.mat[h][j] % modulo; if (modulo <= result.mat[i][j]) result.mat[i][j] -= modulo; } return result; } }; Matrix lgpow(Matrix a, int b) { if (b == 0) { Matrix result(a.n, a.m); for (int i = 0; i < a.n; i++) result.mat[i][i] = 1; return result; } else if (b == 1) return a; else { Matrix result = lgpow(a, b / 2); if (b % 2 == 0) return result * result; else return result * result * a; } } int v[1 + nmax]; void gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; } else { gcd(b, a % b, x, y); int aux = x; x = y; y = aux - a / b * y; } } int main() { int n, k; cin >> n >> k; Matrix init(1 + n, 1 + n), coef(1 + n, 1 + n); int ones = 0, zeros = 0; for (int i = 1; i <= n; i++) { cin >> v[i]; v[i] = !v[i]; if (v[i] == 1) ones++; else zeros++; } int name = 0; for (int i = 1; i <= ones; i++) name += v[i]; init.mat[0][name] = 1; int x, y; gcd(1LL * n * (n - 1) / 2 % modulo, modulo, x, y); x %= modulo; if (x < 0) x += modulo; for (int i = 0; i <= ones; i++) { ll smash = 1LL * i * (zeros - (ones - i)); ll repair = 1LL * (ones - i) * (ones - i); ll total = 1LL * n * (n - 1) / 2; coef.mat[i][i] = 1LL * (total - smash - repair) % modulo * x % modulo; if (1 <= i) coef.mat[i][i - 1] = 1LL * smash % modulo * x % modulo; if (i + 1 <= ones) coef.mat[i][i + 1] = 1LL * repair % modulo * x % modulo; } cout << (init * lgpow(coef, k)).mat[0][ones] << '\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> #pragma GCC optimize("-O2") #pragma GCC optimization("unroll-loops") using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\t"; err(++it, args...); } template <typename T1, typename T2> ostream& operator<<(ostream& c, pair<T1, T2>& v) { c << "(" << v.first << "," << v.second << ")"; return c; } template <template <class...> class TT, class... T> ostream& operator<<(ostream& out, TT<T...>& c) { out << "{ "; for (auto& x : c) out << x << " "; out << "}"; return out; } const int LIM = 1e5 + 5, MOD = 1e9 + 7; const long double EPS = 1e-9; const long long MAX_N = 105; struct Matrix { long long mat[MAX_N][MAX_N]; }; Matrix matMul(Matrix a, Matrix b) { Matrix ans; int i, j, k; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) for (ans.mat[i][j] = k = 0; k < MAX_N; k++) { ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD; ans.mat[i][j] %= MOD; } return ans; } Matrix matPow(Matrix base, long long p) { p %= MOD; Matrix ans; long long i, j; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j); while (p) { if (p & 1) ans = matMul(ans, base); base = matMul(base, base); p >>= 1; } return ans; } int fpow(int a, int p) { if (p == 0) return 1; long long z = fpow(a, p / 2); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> v; for (int i = 0; i < n; ++i) { int x; cin >> x; v.push_back(x); } reverse(v.begin(), v.end()); long long tot = 0; for (int i = 0; i < n; ++i) { if (v[i]) tot++; } long long cnt = 0; for (int i = 0; i < tot; ++i) { if (v[i]) cnt++; } Matrix m; for (int i = 0; i < tot + 1; ++i) { for (int j = 0; j < tot + 1; ++j) { if (i + n - tot < tot) { m.mat[i][j] = 0; continue; } if (abs(i - j) > 1) m.mat[i][j] = 0; else if (i == j) { m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 + i * (tot - i) + (tot - i) * (n - 2 * tot + i); } else if (i - j == 1) { m.mat[i][j] = (i) * (n - 2 * tot + i); } else if (j - i == 1) { m.mat[i][j] = (tot - i) * (tot - i); } m.mat[i][j] %= MOD; } } Matrix ans = matPow(m, k); long long a1 = 0, a2 = 0; a1 = ans.mat[cnt][tot]; for (int i = 0; i < tot + 1; ++i) { a2 += ans.mat[cnt][i]; a2 %= MOD; } long long ansf = 0; ansf = a1; ansf *= fpow(a2, MOD - 2); cout << (ansf) % MOD << '\n'; return 0; }
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, inv2 = (mod + 1) / 2; long long n; struct mat { long long a[105][105]; mat() { memset(a, 0, sizeof(a)); } mat operator*(const mat& b) const { mat res; for (long long i = 0; i <= n; ++i) for (long long j = 0; j <= n; ++j) for (long long k = 0; k <= n; ++k) res.a[i][j] += a[i][k] * b.a[k][j] % mod, res.a[i][j] %= mod; return res; } } F, a; mat power(mat b, long long N) { mat res; for (long long i = 0; i <= n; ++i) res.a[i][i] = 1; while (N) { if (N & 1) res = res * b; b = b * b; N >>= 1; } return res; } long long ksm(long long b, long long n) { long long res = 1; while (n) { if (n & 1) res = res * b % mod; b = b * b % mod; n >>= 1; } return res; } char ch[1005]; signed main() { long long K, s = 0; cin >> n >> K; for (long long i = 1; i <= n; ++i) { cin >> ch[i]; if (ch[i] == '0') ++s; } long long cnt = 0, res = ksm(n * (n - 1) % mod * inv2 % mod, mod - 2); res = ksm(res, K); for (long long i = 1; i <= s; ++i) if (ch[i] == '0') ++cnt; mat ans, F; ans.a[cnt][1] = 1; for (long long j = 0; j <= s; ++j) { if (j) F.a[j][j - 1] = (s - j + 1) * (s - j + 1) % mod; F.a[j][j] = (s * (s - 1) % mod * inv2 % mod + (n - s) * (n - s - 1) % mod * inv2 % mod + j * (s - j) % mod + (s - j) * (n - s - s + j) % mod) % mod; if (j + 1 <= s) F.a[j][j + 1] = (j + 1) * (n - s - s + j + 1) % mod; } F = power(F, K); ans = F * ans; cout << ans.a[s][1] * res % 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; using ull = unsigned long long; constexpr long long int mod = 1e9 + 7; template <class T> struct Mat { vector<vector<T>> A; Mat() {} Mat(size_t n, size_t m) : A(n, vector<T>(m, 0)) {} Mat(size_t n) : A(n, vector<T>(n, 0)){}; size_t height() const { return A.size(); } size_t width() const { return A[0].size(); } inline const vector<T> &operator[](int k) const { return A.at(k); } inline vector<T> &operator[](int k) { return A.at(k); } static Mat I(size_t n) { Mat mat(n); for (int i = 0; i < n; i++) { mat[i][i] = 1; } return mat; } Mat &operator+=(const Mat &B) { size_t n = height(), m = width(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] = ((*this)[i][j] + B[i][j]) % mod; } } return (*this); } Mat &operator-=(const Mat &B) { size_t n = height(), m = width(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] = ((*this)[i][j] - B[i][j]) % mod; if ((*this)[i][j] < 0) (*this)[i][j] += mod; } } return (*this); } Mat &operator*=(const Mat &B) { int n = height(), m = B.width(), p = width(); assert(p == B.height()); vector<vector<T>> C(n, vector<T>(m, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < p; k++) { C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]) % mod; } } } A.swap(C); return (*this); } Mat &operator^=(long long int k) { Mat B = Mat::I(height()); while (k) { if (k % 2) B *= (*this); (*this) *= (*this); k >>= 1LL; } A.swap(B.A); return (*this); } Mat operator+(const Mat &B) const { return (Mat(*this) += B); } Mat operator-(const Mat &B) const { return (Mat(*this) -= B); } Mat operator*(const Mat &B) const { return (Mat(*this) *= B); } Mat operator^(const Mat &B) const { return (Mat(*this) ^= B); } }; long long int mod_pow(long long int a, long long int b) { a %= mod; if (b == 0) return 1; if (b == 1) return a; long long int res = mod_pow(a, b / 2) % mod; res *= res; res %= mod; if (b % 2) res *= a; return res % mod; } struct perm { private: int sz; vector<long long int> p, invp; public: perm(int n) { sz = n + 1; p.resize(sz), invp.resize(sz); p[0] = 1; for (int i = 1; i <= sz - 1; i++) { p[i] = p[i - 1] * i % mod; } invp[sz - 1] = mod_pow(p[sz - 1], mod - 2); for (int i = sz - 2; i >= 0; i--) { invp[i] = invp[i + 1] * (i + 1) % mod; } } long long int comb(long long int x, long long int y) { if (x < y || y < 0) return 0; return (p[x] * invp[x - y] % mod) * invp[y] % mod; } }; perm p(1 << 20); int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> a(n), b(n); int one = 0; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; if (a[i]) one++; } Mat<long long int> mat(one + 1), res(one + 1); sort(b.begin(), b.end()); int cnt = 0; for (int i = 0; i < n; i++) { if (a[i] && a[i] == b[i]) cnt++; } res[cnt][0] = 1; for (int i = 0; i <= one; i++) { int t1 = i, f1 = one - i, t0 = n - 2 * f1 - t1, f0 = f1; if (t0 < 0) continue; mat[i][i] = (n * (n - 1) / 2 - t1 * t0 - f1 * f0 + mod + mod) % mod; if (i < one) mat[i + 1][i] = f1 * f0; if (i) mat[i - 1][i] = t1 * t0; } res = (mat ^= k) * res; printf("%lld\n", res[one][0] * mod_pow(mod_pow(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; 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 Mod = 1000000007; struct Matrix { long long n, m, a[146][146]; Matrix(long long _n = 0, long long _m = 0, long long x = 0) { n = _n, m = _m; memset(a, 0, sizeof a); for (long long i = 0; i < min(n, m); ++i) a[i][i] = x; } Matrix operator*(const Matrix& b) { Matrix res = Matrix(n, b.m); for (long long i = 0; i < n; ++i) for (long long j = 0; j < m; ++j) if (a[i][j]) for (long long k = 0; k < b.m; ++k) (res.a[i][k] += 1ll * a[i][j] * b.a[j][k] % Mod) %= Mod; return res; } Matrix operator^(long long p) { Matrix res = Matrix(n, n, 1), x = *this; while (p) { if (p & 1) res = res * x; x = x * x; p >>= 1; } return res; } }; inline long long fsp(long long x, long long p = Mod - 2) { long long res = 1; while (p) { if (p & 1) res = 1ll * res * x % Mod; x = 1ll * x * x % Mod; p >>= 1; } return res; } long long n, m, cnt, tmp, a[146]; int main() { cin >> n >> m; for (long long i = 1; i <= n; ++i) { scanf("%d", a + i); if (!a[i]) ++cnt; } for (long long i = 1; i <= cnt; ++i) tmp += a[i] ^ 1; Matrix base = Matrix(cnt + 1, cnt + 1), res; for (long long i = 0; i <= cnt; ++i) { if (i < cnt) base.a[i + 1][i] += (cnt - i) * (cnt - i); base.a[i][i] += cnt * (cnt - 1) / 2 + (n - cnt) * (n - cnt - 1) / 2 + i * (cnt - i) + (cnt - i) * (n + i - 2 * cnt); if (i) base.a[i - 1][i] += i * (n + i - 2 * cnt); } res = base ^ m; long long ans = 0; for (long long i = 0; i <= cnt; ++i) (ans += res.a[i][tmp]) %= Mod; cout << 1ll * res.a[cnt][tmp] * fsp(ans) % 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; const int md = 1000000007; int n, m, K, i, j, k, a[105], inv[10005], g[105][105], h[105][105], c[105][105]; void Mult(int a[105][105], int b[105][105]) { int i, j, k; for (i = 0; i <= m; ++i) for (j = 0; j <= m; ++j) c[i][j] = 0; for (i = 0; i <= m; ++i) for (j = 0; j <= m; ++j) for (k = 0; k <= m; ++k) c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % md; for (i = 0; i <= m; ++i) for (j = 0; j <= m; ++j) a[i][j] = c[i][j]; } int main() { scanf("%d%d", &n, &K); inv[1] = 1; for (i = 2; i <= n * n; ++i) inv[i] = 1ll * inv[md % i] * (md - md / i) % md; for (i = 1; i <= n; ++i) { scanf("%d", a + i); if (a[i] == 0) ++m; } for (i = 1, j = 0; i <= m; ++i) if (a[i]) ++j; h[0][j] = 1; for (j = 0; j <= m; ++j) { if (j) g[j][j - 1] = (g[j][j - 1] + 1ll * j * j % md * inv[n * (n - 1) / 2] % md) % md; if (j < m) g[j][j + 1] = (g[j][j + 1] + 1ll * (m - j) * (n - m - j) % md * inv[n * (n - 1) / 2] % md) % md; g[j][j] = (g[j][j] + (1 - (1ll * j * j + 1ll * (m - j) * (n - m - j)) % md * inv[n * (n - 1) / 2] % md + md) % md) % md; } while (K) { if (K & 1) Mult(h, g); Mult(g, g); K >>= 1; } printf("%d", h[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
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Random; import java.util.TreeSet; public final class CF_553_D2_F { static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } static long inv(long x,long mod){ return powerMod(x,mod-2,mod); } static long[][] mult(long[][] A,long[][] B,long mod){ int M=A.length; long[][] res=new long[M][M]; for (int i=0;i<M;i++) for (int j=0;j<M;j++) { res[i][j]=0; for (int k=0;k<M;k++) { res[i][j]+=A[i][k]*B[k][j]; if (res[i][j]>=mod) res[i][j]%=mod; } } return res; } static long[][] powerMat(long[][] A,long e,long mod){ long[][] X=null; long[][] B=A; while (e>0) { if (e%2==1) { if (X==null) X=B; else X=mult(B,X,mod); } B=mult(B,B,mod); e=e/2; } return X; } static long mult(long a,long b,long mod){ long c=a*b; if (c>=mod) c%=mod; return c; } static long mod=1000000007; // Global vars static BufferedWriter out; static InputReader reader; static int[] anc,rank,max,list; static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader = new InputReader(System.in); int n=reader.readInt(); int[] a=new int[n]; long k=reader.readInt(); log(inv(2,mod)); int zer=0; int one=0; for (int i=0;i<n;i++){ a[i]=reader.readInt(); if (a[i]==0) zer++; else one++; } int MX=zer+1; long[][] mat=new long[MX][MX]; // lets prob[x] = probability that we got x "zeroes" in the first zer position // how to move from prob[x] to prob[y] // too interesting case: a zero below zer pos is changed with a one beyond // or same with one and zero long nn=(n*(n-1))/2; long invnn=inv(nn,mod); for (int x=0;x<MX;x++){ long A=0; long B=0; if (x>0){ // take a zero below zer long prob1=x; //log("prob1:"+prob1); // take a one beyond zer // how many ones beyond zer long prob2=one-(zer-x); if (prob2>=0){ //log("prob2:"+prob2); A=mult(prob1,prob2,mod); A=mult(A,invnn,mod); mat[x][x-1]=A; } } if (x<zer){ // take a one below zer long prob1=zer-x; // take a zero beyond zer long prob2=zer-x; if (prob2>=0){ B=mult(prob1,prob2,mod); B=mult(B,invnn,mod); mat[x][x+1]=B; } } long C=(mod-A); if (C>=mod) C-=mod; C+=(mod-B); if (C>=mod) C-=mod; C++; if (C>=mod) C-=mod; mat[x][x]=C; } /* log("----------"); for (int i=0;i<MX;i++){ log(mat[i]); } */ long[][] matk=powerMat(mat,k,mod); int bob=0; for (int i=0;i<zer;i++){ if (a[i]==0) bob++; } //log("bob:"+bob); output(matk[bob][zer]); /* for (int i=0;i<MX;i++){ log(matk[i]); } */ try { out.close(); } catch (Exception e) { } } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
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; inline int In() { char c = getchar(); int x = 0, ft = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') ft = -1; for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x * ft; } inline int min(int a, int b) { return a < b ? a : b; } const int P = 1e9 + 7; int n, lim, K, All, inv_All, A[105], cnt[2], tmp; struct Mat { int m[55][55]; } f; Mat operator*(Mat a, Mat b) { Mat c; for (int i = 0; i <= lim; ++i) for (int j = 0; j <= lim; ++j) c.m[i][j] = 0; for (int i = 0; i <= lim; ++i) for (int j = 0; j <= lim; ++j) for (int k = 0; k <= lim; ++k) c.m[i][j] = (1ll * a.m[i][k] * b.m[k][j] % P + c.m[i][j]) % P; return c; } inline Mat Mat_fpow(Mat x, int k) { Mat s; for (int i = 0; i <= lim; ++i) for (int j = 0; j <= lim; ++j) s.m[i][j] = (i == j); for (; k; k >>= 1, x = x * x) if (k & 1) s = x * s; return s; } inline int fpow(int x, int k) { int s = 1, t = x; for (; k; k >>= 1, t = 1ll * t * t % P) { if (k & 1) s = 1ll * s * t % P; } return s; } int main() { n = In(); K = In(); All = n * (n - 1) / 2; inv_All = fpow(All, P - 2); for (int i = 1; i <= n; ++i) A[i] = In(), ++cnt[A[i]]; for (int i = 1; i <= cnt[0]; ++i) if (A[i]) ++tmp; lim = min(cnt[0], cnt[1]); for (int j = 0, p1, p2; j <= lim; ++j) { p1 = j * j; p2 = (cnt[0] - j) * (cnt[1] - j); if (j >= 1) f.m[j - 1][j] = 1ll * p1 * inv_All % P; f.m[j][j] = 1ll * (All - p1 - p2) * inv_All % P; if (j < lim) f.m[j + 1][j] = 1ll * p2 * inv_All % P; } f = Mat_fpow(f, K); printf("%d\n", f.m[0][tmp]); 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 uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &item) { out << '(' << item.first << ", " << item.second << ')'; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { for (const auto &item : v) out << item << ' '; return out; } const int NMAX = 110; const int MOD = 1000000007; struct matrix { int n; int v[NMAX][NMAX]; matrix() { memset(v, 0, sizeof v); } friend matrix operator*(const matrix &a, const matrix &b) { matrix c; c.n = a.n; for (int i = 0; i < a.n; ++i) for (int j = 0; j < a.n; ++j) { c.v[i][j] = 0; for (int k = 0; k < a.n; ++k) { c.v[i][j] = (c.v[i][j] + 1LL * a.v[i][k] * b.v[k][j]) % MOD; } } return c; } friend ostream &operator<<(ostream &out, const matrix &m) { for (int i = 0; i < m.n; ++i) { for (int j = 0; j < m.n; ++j) out << m.v[i][j] << ' '; if (i + 1 < m.n) out << '\n'; } return out; } }; matrix pw(matrix base, int exp) { matrix res; res.n = base.n; for (int i = 0; i < res.n; ++i) res.v[i][i] = 1; for (; exp; exp >>= 1) { if (exp & 1) res = res * base; base = base * base; } return res; } int pw(int base, int exp) { int res; for (res = 1; exp; exp >>= 1) { if (exp & 1) res = 1LL * res * base % MOD; base = 1LL * base * base % MOD; } return res; } int main() { ios_base::sync_with_stdio(false); int i, n, k, n0, n1, x; string s; cin >> n >> k; for (n0 = n1 = 0, i = 0; i < n; ++i) { cin >> x; if (x) ++n1; else ++n0; s += '0' + x; } int low = max(n0 - n1, 0); int down = pw((1LL * n * (n - 1) / 2) % MOD, MOD - 2); matrix m; m.n = n0 + 1; for (x = low; x <= n0; ++x) { int p1 = (1LL * (n0 - x + 1) * (n0 - x + 1)) % MOD; p1 = (1LL * p1 * down) % MOD; if (x - 1 < low) p1 = 0; int p2 = (1LL * (x + 1) * (n1 - n0 + x + 1)) % MOD; p2 = (1LL * p2 * down) % MOD; if (x + 1 > n0) p2 = 0; int p3 = (1LL * n0 * (n0 - 1) / 2 + 1LL * n1 * (n1 - 1) / 2 + 1LL * x * (n0 - x) + 1LL * (n0 - x) * (n1 - n0 + x)) % MOD; p3 = (1LL * p3 * down) % MOD; if (x - 1 >= low) m.v[x - 1][x] = p1; m.v[x][x] = p3; if (x + 1 <= n0) m.v[x + 1][x] = p2; } m = pw(m, k); for (x = 0, i = 0; i < n0; ++i) x += (s[i] == '0'); cout << m.v[x][n0] << '\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 = 1000 * 1000 * 1000 + 7; 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; struct Matrix { int h, w; vector<long long> mat; Matrix() { h = w = 0; } Matrix(int h, int w) { this->h = h, this->w = w; mat.resize(h * w); } long long& at(int i, int j) { return mat[w * (i - 1) + (j - 1)]; } static Matrix ident(int size) { Matrix ret(size, size); for (int i = 1; i <= size; i++) ret.at(i, i) = 1; return ret; } Matrix operator*(Matrix& ope) { Matrix ret(h, ope.w); for (int i = 1; i <= h; i++) { for (int j = 1; j <= ope.w; j++) { for (int k = 1; k <= w; k++) { ret.at(i, j) += at(i, k) * ope.at(k, j) % 1000000007; ret.at(i, j) %= 1000000007; } } } return ret; } Matrix pow(long long n) { if (n == 0) return ident(h); if (n % 2) { return pow(n - 1) * (*this); } else { Matrix tmp = pow(n / 2); return tmp * tmp; } } void print() { for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cout << at(i, j) << " "; } cout << endl; } } }; long long modpow(long long a, long long n) { if (n == 0) return 1; if (n % 2) { return ((a % 1000000007) * (modpow(a, n - 1) % 1000000007)) % 1000000007; } else { return modpow((a * a) % 1000000007, n / 2) % 1000000007; } } long long n, k; long long a[105]; int main(void) { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; int cnt = 0, cor = 0; for (int i = 1; i <= n; i++) if (a[i]) cnt++; for (int i = n - cnt + 1; i <= n; i++) if (a[i]) cor++; Matrix mat(cnt + 1, cnt + 1); long long bunbo = modpow(n * (n - 1) / 2, 1000000007 - 2); for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= cnt; j++) { mat.at(i + 1, j + 1) = 0; } } for (int i = 0; i <= cnt; i++) { long long a = (cnt - i) * (cnt - i) % 1000000007, b = i * (n + i - 2 * cnt) % 1000000007; if (i - 1 >= 0) mat.at(i, i + 1) = b * bunbo % 1000000007; if (i + 1 <= cnt) mat.at(i + 2, i + 1) = a * bunbo % 1000000007; mat.at(i + 1, i + 1) = (n * (n - 1) / 2 - (a + b) + 2 * 1000000007) % 1000000007 * bunbo % 1000000007; } cout << mat.pow(k).at(cnt + 1, cor + 1) << 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 maxn = 100 + 10; int n, k; int s[maxn], cc, cnt; int c[maxn][maxn]; const long long mod = 1e9 + 7; long long ans[maxn][maxn], b[maxn][maxn], a[maxn][maxn], tmp[maxn][maxn]; void init() { c[1][0] = c[1][1] = 1; for (int i = 2; i <= 100; ++i) { c[i][0] = 1; c[i][1] = i; c[i][2] = i * (i - 1) / 2; } return; } long long mypow(long long x, long long p) { long long ret = 1; while (p) { if (p & 1) { ret *= x; ret %= mod; } x *= x; x %= mod; p >>= 1; } return ret; } void multi(long long x[][maxn], long long y[][maxn]) { memset(tmp, 0, sizeof(tmp)); for (int i = 0; i <= cnt; ++i) { for (int j = 0; j <= cnt; ++j) { for (int k = 0; k <= cnt; ++k) { tmp[i][j] += (x[i][k] * y[k][j]) % mod; tmp[i][j] %= mod; } } } for (int i = 0; i <= cnt; ++i) { for (int j = 0; j <= cnt; ++j) { x[i][j] = tmp[i][j]; } } } int main() { init(); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> s[i]; if (!s[i]) { cnt++; } } for (int i = 1; i <= cnt; ++i) { if (!s[i]) { cc++; } } a[cc][0] = 1; for (int i = 0; i <= cnt; ++i) { int last = cnt - i; if (last < n - cnt) { b[i][i - 1] = c[last + 1][1] * c[cnt - i + 1][1]; } if (last <= n - cnt) { b[i][i] = c[cnt][2] + c[n - cnt][2] + c[i][1] * c[cnt - i][1] + c[cnt - i][1] * c[n - 2 * cnt + i][1]; if (i != cnt) { b[i][i + 1] = c[i + 1][1] * c[n - 2 * cnt + i + 1][1]; } } } for (int i = 0; i <= cnt; ++i) { ans[i][i] = 1; } while (k) { if (k & 1) { multi(ans, b); } multi(b, b); k >>= 1; } multi(ans, a); long long ret = 0; for (int i = 0; i <= cnt; ++i) { ret += ans[i][0]; ret %= mod; } ret = mypow(ret, mod - 2); ret *= ans[cnt][0]; ret %= mod; cout << ret << 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; 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; } } 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() { 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 int N = 1e2 + 5; const int MOD = 1e9 + 7; long long C[N][N]; void multiply(long long A[N][N], long long B[N][N], int 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] = (C[i][j] + A[i][k] * B[k][j]) % MOD; } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { A[i][j] = C[i][j]; C[i][j] = 0; } } } long long AM[N][N], B[N][N]; void matPower(int n, int sz) { if (n == 0) return; matPower(n >> 1, sz); multiply(AM, AM, sz); if (n & 1) multiply(AM, B, sz); } long long power(long long a, long long n, long long m = MOD) { if (n == 0) return 1; long long p = power(a, n >> 1, m); p = (p * p) % m; if (n & 1) p = (p * a) % m; return p; } int A[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; int c = 0; for (int i = 1; i <= n; i++) { int a; cin >> a; A[i] = a; if (a == 0) c++; } int ip = 0; for (int i = 1; i <= c; i++) { if (A[i] == 0) ip++; } long long tot = (n * (n - 1)) >> 1; long long totInv = power(tot, MOD - 2); for (int i = 0; i <= c; i++) { AM[i][i] = 1; long long ip0 = i, ip1 = c - i; long long op0 = c - i, op1 = n - 2 * c + i; if (ip0 < 0 or ip1 < 0 or op0 < 0 or op1 < 0) continue; if (ip0 > c or ip1 > n - c or op0 > c or op1 > n - c) continue; long long cnt = 1; if (i) { B[i][i - 1] = (ip0 * op1 * totInv) % MOD; cnt -= B[i][i - 1]; } if (i < c) { B[i][i + 1] = (ip1 * op0 * totInv) % MOD; cnt -= B[i][i + 1]; } cnt %= MOD; if (cnt < 0) cnt += MOD; B[i][i] = cnt; } matPower(k, c); long long ans = AM[ip][c]; 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 double PI = 3.14159265359; const long long MOD = (long long)1e9 + 7; const long long MAXN = (long long)100 + 10; const long long INF = (long long)2242545357980376863; const long double EPS = (long double)1e-8; long long pw(long long b, long long p) { if (p == 0) return 1; b %= MOD; if (p % 2 == 1) return (b * pw(b, p - 1)) % MOD; return pw(b * b, p / 2ll); } long long inv(long long x) { return pw(x, MOD - 2); } const long long LOG = 150; long long dp[LOG][MAXN][MAXN]; long long G[MAXN][MAXN]; vector<long long> bt; long long a[MAXN]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long kp = k; for (int i = 0; i < n; i++) cin >> a[i]; long long c0 = 0; for (int i = 0; i < n; i++) if (a[i] == 0) c0++; long long c00 = 0; for (int i = 0; i < n; i++) if (a[i] == 0 and i < c0) c00++; long long c1 = n - c0; long long m = n; n = c0 + 1; long long sm = 0; ; ; for (int i = 0; i < n; i++) { sm = 0; for (int j = 0; j < n; j++) { if (i == j) { dp[0][i][j] = (i * (c0 - i)) + ((c0 - i) * (c1 - (c0 - i))) + (c0 * (c0 - 1) / 2ll) + ((c1 * (c1 - 1)) / 2ll); } if (i - 1 == j) { dp[0][i][j] = (i * (c1 - (c0 - i))); } if (i + 1 == j) { dp[0][i][j] = ((c0 - i) * (c0 - i)); } sm += dp[0][i][j]; G[i][j] = dp[0][i][j]; } } while (k > 0) { bt.push_back(k % 2ll); k /= 2ll; } bt.pop_back(); long long lg = 0; while (bt.size()) { lg++; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int mid = 0; mid < n; mid++) { dp[lg][i][j] += (dp[lg - 1][i][mid] * dp[lg - 1][mid][j]); dp[lg][i][j] %= MOD; } } } if (bt.back() == 1) { lg++; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int mid = 0; mid < n; mid++) { dp[lg][i][j] += (dp[lg - 1][i][mid] * G[mid][j]); dp[lg][i][j] %= MOD; } } } } bt.pop_back(); } long long ans = dp[lg][c00][c0] % MOD; cout << (ans * inv(pw(m * (m - 1) / 2, kp))) % MOD; return 0; }
CPP