Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; template <std::uint32_t mod> class modint { private: std::uint32_t n; public: modint() : n(0){}; modint(std::uint64_t n_) : n(n_ % mod){}; bool operator==(const modint& m) const { return n == m.n; } bool operator!=(const modint& m) const { return n != m.n; } std::uint32_t get() const { return n; } modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; } modint operator+(const modint& m) const { return modint(*this) += m; } modint operator-(const modint& m) const { return modint(*this) -= m; } modint operator*(const modint& m) const { return modint(*this) *= m; } modint binpow(std::uint64_t b) const { modint ans = 1, m = modint(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } modint inv() { return (*this).binpow(mod - 2); } }; const int maxn = 110, mod = 1e9 + 7; int arr[maxn]; modint<mod> mat[maxn][maxn], dp[maxn], tmp[maxn][maxn], ans[maxn][maxn]; int main() { int n, k, a, b, s; a = b = s = 0; cin >> n >> k; for (int i = 0; i < n; ++i) { scanf("%d", &arr[i]); if (arr[i]) ++b; else ++a; } for (int i = 0; i < a; ++i) if (arr[i] == 0) ++s; for (int i = 0; i <= n; ++i) ans[i][i] = modint<mod>(1); for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { if (j >= a - b && j <= a && i >= a - b && i <= a) { int a0 = j, a1 = a - a0, b1 = b - a1, b0 = b - b1; if (j - 1 == i) mat[i][j] = modint<mod>(a0 * b1); else if (j + 1 == i) mat[i][j] = modint<mod>(a1 * b0); else if (j == i) mat[i][j] = modint<mod>(n * (n - 1) / 2 - a0 * b1 - a1 * b0); } } } int ok = k; while (k) { if (k & 1) { for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { tmp[i][j] = modint<mod>(0); for (int k = 0; k <= n; ++k) { tmp[i][j] += mat[i][k] * ans[k][j]; } } } memcpy(ans, tmp, sizeof ans); } for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { tmp[i][j] = modint<mod>(0); for (int k = 0; k <= n; ++k) { tmp[i][j] += mat[i][k] * mat[k][j]; } } } memcpy(mat, tmp, sizeof(mat)); k >>= 1; } long long num = ans[a][s].get(), deninv = modint<mod>(n * (n - 1) / 2).binpow(ok).inv().get(); num = num * deninv % mod; if (num < 0) num += mod; cout << num; return 0; }
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; #pragma GCC diagnostic ignored "-Wmissing-declarations" inline int safe_mul(const int x, const int y) __attribute__((warn_unused_result)); int const mod = 1e9 + 7; inline int safe_mul(const int x, const int y) { return x * static_cast<int64_t>(y) % mod; } inline void safe_add(int& x, const int y) { x += y; if (x >= mod) x -= mod; } inline int safe_pow(int x, int y) { int res = 1; for (; y > 0; y >>= 1, x = safe_mul(x, x)) if (y & 1) res = safe_mul(res, x); return res; } inline int safe_inv(const int x) { return safe_pow(x, mod - 2); } vector<vector<int>> mul(const vector<vector<int>>& a, const vector<vector<int>>& b) { auto n = a.size(); vector<vector<int>> res(n, vector<int>(n, 0)); for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < n; ++j) for (size_t k = 0; k < n; ++k) safe_add(res[i][j], safe_mul(a[i][k], b[k][j])); return res; } vector<vector<int>> pow(vector<vector<int>> a, int y) { auto n = a.size(); vector<vector<int>> res(n, vector<int>(n, 0)); for (size_t i = 0; i < n; ++i) res[i][i] = 1; for (; y > 0; y >>= 1, a = mul(a, a)) if (y & 1) res = mul(res, a); return res; } int const maxn = 105; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n); for (int& x : a) cin >> x; int c = count(begin(a), end(a), 0); vector<vector<int>> mat(c + 1, vector<int>(c + 1, 0)); int cnt = n * (n - 1) / 2; int d = safe_inv(cnt); for (int f = 0; f <= c; ++f) { if (c - f > n - c) continue; int l = safe_mul(f * ((n - c) - (c - f)), d); int r = safe_mul((c - f) * (c - f), d); int t = 1; safe_add(t, mod - l); safe_add(t, mod - r); if (l) mat[f - 1][f] = l; if (r) mat[f + 1][f] = r; mat[f][f] = t; } mat = pow(mat, k); int c0 = count(begin(a), begin(a) + c, 0); cout << mat[c][c0] << endl; }
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 INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; ll modpow(ll x, ll p, ll mod) { ll res = 1LL; for (; p; p >>= 1, (x *= x) %= mod) if (p & 1) (res *= x) %= mod; return res; } template <ll mod> struct Mint { ll x; Mint(ll x = 0) : x((x %= mod) < 0 ? x + mod : x) {} Mint& operator+=(const Mint& rhs) { if ((x += rhs.x) >= mod) x -= mod; return *this; } Mint& operator-=(const Mint& rhs) { return *this += mod - rhs.x; } Mint& operator*=(const Mint& rhs) { (x *= rhs.x) %= mod; return *this; } Mint& operator/=(const Mint& rhs) { return *this *= modpow(rhs.x, mod - 2, mod); } Mint power(ll p) const { return Mint(modpow(x, p, mod)); } bool operator==(const Mint& rhs) const { return x == rhs.x; } bool operator<(const Mint& rhs) const { return x < rhs.x; } friend Mint operator+(const Mint& lhs, const Mint& rhs) { return Mint(lhs) += rhs; } friend Mint operator-(const Mint& lhs, const Mint& rhs) { return Mint(lhs) -= rhs; } friend Mint operator*(const Mint& lhs, const Mint& rhs) { return Mint(lhs) *= rhs; } friend Mint operator/(const Mint& lhs, const Mint& rhs) { return Mint(lhs) /= rhs; } friend ostream& operator<<(ostream& out, const Mint& a) { return out << a.x; } friend istream& operator>>(istream& in, Mint& a) { ll x; in >> x; a = Mint(x); return in; } }; template <typename T> T templatepow(T x, ll p) { assert(p >= 0); T res(1); while (p > 0) { if (p & 1) res = res * x; x = x * x, p >>= 1; } return res; } template <typename T, int N, int M> struct Matrix { T A[N][M]; int row[N]; Matrix() { fill(&A[0][0], &A[0][0] + N * M, T(0)); iota(begin(row), end(row), 0); } Matrix(T value) : Matrix() { for (int i = 0; i < min(N, M); ++i) A[i][i] = value; } Matrix(initializer_list<initializer_list<T>> lst) : Matrix() { int i = 0, j = 0; for (const auto& v : lst) { for (const auto& x : v) A[i][j++] = x; i++, j = 0; } } T* operator[](int i) { return A[row[i]]; } const T* operator[](int i) const { return A[row[i]]; } void swap_rows(int i, int j) { swap(row[i], row[j]); } template <typename Op> Matrix& compose(const Matrix& rhs, Op&& op) { auto& lhs = *this; for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) lhs[i][j] = op(lhs[i][j], rhs[i][j]); return *this; } Matrix& operator+=(const Matrix& rhs) { return compose(rhs, std::plus<T>()); } Matrix& operator-=(const Matrix& rhs) { return compose(rhs, std::minus<T>()); } Matrix operator+(const Matrix& rhs) const { return Matrix(*this) += rhs; } Matrix operator-(const Matrix& rhs) const { return Matrix(*this) -= rhs; } template <int K> Matrix<T, N, K> operator*(const Matrix<T, M, K>& rhs) const { const auto& lhs = *this; Matrix<T, N, K> res; for (int i = 0; i < N; ++i) for (int j = 0; j < K; ++j) for (int k = 0; k < M; ++k) res[i][j] += lhs[i][k] * rhs[k][j]; return res; } friend Matrix operator*(const T& alpha, Matrix A) { for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) A[i][j] *= alpha; return A; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); constexpr ll mod = 1e9 + 7; using mint = Mint<mod>; int n; ll k; cin >> n >> k; vector<int> a(n); for (auto& x : a) cin >> x; constexpr int nmax = 100; Matrix<mint, nmax, nmax> A; mint p = 1 / mint(1LL * n * (n - 1) / 2); int ones = accumulate(begin(a), end(a), 0), zeros = n - ones, N = min(ones, zeros); for (int x = 0; x <= N; ++x) { A[x][x] = 1 - (1LL * x * x + 1LL * (ones - x) * (zeros - x)) * p; if (x + 1 <= N) A[x][x + 1] = (1LL * (ones - x) * (zeros - x)) * p; if (x > 0) A[x][x - 1] = (1LL * x * x) * p; } A = templatepow(A, k); int x = 0; for (int i = 0; i < zeros; ++i) x += a[i] == 1; cerr << "x" << " == " << (x) << '\n'; ; mint ans = A[x][0]; cout << ans << '\n'; exit(0); }
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 int modadd(long long int n, long long int m, long long int p = 1000000007) { return ((n + m) % p + p) % p; } inline long long int modsub(long long int n, long long int m, long long int p = 1000000007) { return ((n - m + p) % p + p) % p; } inline long long int modpro(long long int n, long long int m, long long int p = 1000000007) { return (((n % p) * (m % p)) % p + p) % p; } unsigned long long int powe(long long int first, long long int second) { unsigned long long int res = 1; while (second > 0) { if (second & 1) res = res * first; second = second >> 1; first = first * first; } return res; } long long int modpow(long long int first, long long int second, long long int p = 1000000007) { long long int res = 1; while (second > 0) { if (second & 1) res = modpro(res, first, p); second = second >> 1; first = modpro(first, first, p); } return res; } inline long long int modInverse(long long int n, long long int p = 1000000007) { if (n == 1) return 1; return modpow(n, p - 2, p); } inline long long int moddiv(long long int n, long long int m, long long int p = 1000000007) { return modpro(n, modInverse(m, p), p); } inline long long int modadd3(long long int first, long long int second, long long int z, long long int p = 1000000007) { return modadd(modadd(first, second, p), z, p); } inline long long int modadd4(long long int first, long long int second, long long int z, long long int w, long long int p = 1000000007) { return modadd(modadd(first, second, p), modadd(z, w, p), p); } inline long long int modnCr(long long int fac[], int n, int r, long long int p = 1000000007) { if (r == 0) return 1; return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p); } template <typename T> inline T max3(T first, T second, T z) { return max(max(first, second), z); } template <typename T> inline T max4(T first, T second, T z, T w) { return max(max3(first, second, w), z); } template <typename T> inline T min3(T first, T second, T z) { return min(min(first, second), z); } template <typename T> inline T min4(T first, T second, T z, T w) { return min(min3(first, second, w), z); } template <typename T> void printArr(T *arr, int s, int n) { for (int i = s; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } class matrix { public: int row, col; std::vector<std::vector<long long int>> num; matrix(int row, int col, int defaultValue = 0) { this->num = std::vector<std::vector<long long int>>( row, std::vector<long long int>(col, defaultValue)); this->row = row, this->col = col; } matrix(std::vector<std::vector<long long int>> num) { this->num = num; this->row = this->num.size(); this->col = this->num[0].size(); } matrix operator*(matrix &another) { if (this->col != another.row) { printf("Wrong size: %d*%d X %d*%d\n", this->row, this->col, another.row, another.col); throw "Wrong size"; } matrix newone(this->row, another.col); for (int r = 0; r < newone.row; r++) for (int c = 0; c < newone.col; c++) { for (int k = 0; k < this->col; k++) { newone.num[r][c] = (this->num[r][k] * another.num[k][c] + newone.num[r][c]) % 1000000007; } } return newone; } matrix operator^(long long int first) { if (first == 0) { printf("Not implemented yet.\n"); throw "Not implemented"; } else if (first == 1) return *this; else { matrix halfpower = (*this) ^ (first / 2); if (first % 2 == 0) return halfpower * halfpower; else return halfpower * halfpower * (*this); } } }; bool a[2005]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int erer = 1; for (int erer2 = (1); erer2 < (erer + 1); erer2++) { long long int n, k; cin >> n >> k; int one = 0; for (int i = (0); i < (n); i++) { cin >> a[i]; one += a[i]; } matrix tpm(one + 1, one + 1); long long int now = n * (n - 1) / 2; long long int inv = modInverse(now % 1000000007); for (int i = (0); i < (one + 1); i++) { int rone = i; int rzero = one - i; int lone = one - rone; int lzero = n - one - rzero; long long int sum = 0; if (i < one) { long long int temp = lone * rzero; tpm.num[i][i + 1] = (temp * inv) % 1000000007; sum += temp; } if (i) { long long int temp = rone * lzero; tpm.num[i][i - 1] = (temp * inv) % 1000000007; sum += temp; } tpm.num[i][i] = ((now - sum) * inv) % 1000000007; } tpm = tpm ^ k; int c = 0; for (int i = (n)-1; i >= (n - one); i--) { if (a[i]) c++; } cout << tpm.num[c][one]; } return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 105; int n, K, m; int a[N]; long long f[N][N]; long long Powx(long long a, int b) { long long ans = 1; while (b != 0) { if (b % 2) ans = (ans * a) % mod; a = (a * a) % mod; b /= 2; } return ans; } long long c[N][N]; void Chen(long long a[][N], long long b[][N]) { for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) c[i][j] = 0; for (int k = 0; k <= m; k++) for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) a[i][j] = c[i][j]; } long long ans[N][N]; void Pow(long long a[][N], int b) { for (int i = 0; i <= m; i++) ans[i][i] = 1; while (b != 0) { if (b % 2) Chen(ans, a); Chen(a, a); b /= 2; } } int main() { cin >> n >> K; m = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] == 0) m++; } for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) { if (m - i > n - m || m - j > n - m) continue; if (abs(i - j) > 1) continue; if (j == i - 1) f[i][j] = i * (n - m - (m - i)); if (j == i) f[i][j] = m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + i * (m - i) + (n - m - (m - i)) * (m - i); if (j == i + 1) f[i][j] = (m - i) * (m - i); } Pow(f, K); int x = 0; for (int i = 1; i <= m; i++) if (a[i] == 0) x++; long long anss[N]; for (int i = 0; i <= m; i++) anss[i] = ans[x][i]; long long sum = 0; for (int i = 0; i <= m; i++) sum = (sum + anss[i]) % mod; cout << anss[m] * Powx(sum, mod - 2) % mod; }
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 long long md = 1e9 + 7; const int Inf = 1e9; const long long Inf64 = 1e18; const long long MaxN = 200001; const long long MaxM = 1000001; const long double eps = 1e-15; const long long dx[4] = {0, 1, 0, -1}; const long long dy[4] = {1, 0, -1, 0}; using namespace std; long long gcd(long long a, long long b) { while (a) { b %= a; swap(a, b); } return b; } long long gcdex(long long a, long long b, long long& x, long long& y) { if (a == 0) { x = 0; y = 1; return b; } else { long long x1, y1; long long d = gcdex(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } } struct Mat { long long a[105][105]; long long N; Mat() { memset(a, 0, sizeof(a)); } Mat(long long n) { N = n; memset(a, 0, sizeof(a)); } Mat operator*(Mat b) { Mat c = Mat(N); for (int i = 0; i <= N; i++) { for (int j = 0; j <= N; j++) { for (int k = 0; k <= N; k++) { c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % md; } } } return c; } }; Mat e_pow(Mat a, long long s) { Mat c(a.N); for (int i = 0; i <= a.N; i++) { c.a[i][i] = 1; } while (s) { if (s & 1) c = c * a; a = a * a; s >>= 1; } return c; } long long e_pow(long long a, long long s) { long long c = 1; while (s) { if (s & 1) c = (c * a) % md; a = (a * a) % md; s >>= 1; } return c; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long N, K; cin >> N >> K; vector<long long> A(N); long long ed = 0; for (int i = 0; i < N; i++) { cin >> A[i]; ed += A[i]; } long long no = N - ed; Mat Mt(N); long long cnt = 0; for (int i = 0; i < no; i++) { cnt += A[i]; } for (int i = 0; i <= N; i++) { Mt.a[i][i] = no * (no - 1) / 2 + ed * (ed - 1) / 2; if (no > i) Mt.a[i][i] += (no - i) * i; if (ed > i) Mt.a[i][i] += (ed - i) * i; if (i < N) { if (ed > i && no > i) Mt.a[i][i + 1] = (ed - i) * (no - i); } if (i > 0) { Mt.a[i][i - 1] = i * i; } } Mt = e_pow(Mt, K); cerr << e_pow(e_pow(N * (N - 1) / 2, K), md - 2); long long Ans = Mt.a[cnt][0] * e_pow(e_pow(N * (N - 1) / 2, K), md - 2) % md; cout << Ans; cerr << '\n' << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << '\n'; return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> #pragma GCC optimize(3, "Ofast", "inline") 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
#include <bits/stdc++.h> #pragma GCC optimize("-Ofast") #pragma GCC optimization("unroll-loops") using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\t"; err(++it, args...); } template <typename T1, typename T2> ostream& operator<<(ostream& c, pair<T1, T2>& v) { c << "(" << v.first << "," << v.second << ")"; return c; } template <template <class...> class TT, class... T> ostream& operator<<(ostream& out, TT<T...>& c) { out << "{ "; for (auto& x : c) out << x << " "; out << "}"; return out; } const int LIM = 1e5 + 5, MOD = 1e9 + 7; const long double EPS = 1e-9; const long long MAX_N = 105; struct Matrix { long long mat[MAX_N][MAX_N]; }; Matrix matMul(Matrix a, Matrix b) { Matrix ans; int i, j, k; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) for (ans.mat[i][j] = k = 0; k < MAX_N; k++) { ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD; ans.mat[i][j] %= MOD; } return ans; } Matrix matPow(Matrix base, long long p) { p %= MOD; Matrix ans; long long i, j; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) ans.mat[i][j] = (i == j); while (p) { if (p & 1) ans = matMul(ans, base); base = matMul(base, base); p >>= 1; } return ans; } int fpow(int a, int p) { if (p == 0) return 1; long long z = fpow(a, p / 2); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> v; for (int i = 0; i < n; ++i) { int x; cin >> x; v.push_back(x); } reverse(v.begin(), v.end()); long long tot = 0; for (int i = 0; i < n; ++i) { if (v[i]) tot++; } long long cnt = 0; for (int i = 0; i < tot; ++i) { if (v[i]) cnt++; } Matrix m; for (int i = 0; i < tot + 1; ++i) { for (int j = 0; j < tot + 1; ++j) { if (i + n - tot < tot) { m.mat[i][j] = 0; continue; } if (abs(i - j) > 1) m.mat[i][j] = 0; else if (i == j) { m.mat[i][j] = tot * (tot - 1) / 2 + (n - tot) * (n - tot - 1) / 2 + i * (tot - i) + (tot - i) * (n - 2 * tot + i); } else if (i - j == 1) { m.mat[i][j] = (i) * (n - 2 * tot + i); } else if (j - i == 1) { m.mat[i][j] = (tot - i) * (tot - i); } m.mat[i][j] %= MOD; } } Matrix ans = matPow(m, k); long long a1 = 0, a2 = 0; a1 = ans.mat[cnt][tot]; for (int i = 0; i < tot + 1; ++i) { a2 += ans.mat[cnt][i]; a2 %= MOD; } long long ansf = 0; ansf = a1; ansf *= fpow(a2, MOD - 2); cout << (ansf) % MOD << '\n'; return 0; }
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 java.math.BigInteger; import static java.lang.Math.*; /* spar5h */ public class cf6 implements Runnable { int x; long mod = (long)1e9 + 7; long[][] mat; long[][] multiply(long[][] a, long[][] b) { long[][] ret = new long[x + 1][x + 1]; for(int i = 0; i <= x; i++) for(int j = 0; j <= x; j++) for(int k = 0; k <= x; k++) ret[i][j] = (ret[i][j] + a[i][k] * b[k][j] % mod) % mod; return ret; } long[][] matExp(int k) { if(k == 1) return mat; long[][] ret = matExp(k / 2); ret = multiply(ret, ret); if(k % 2 == 1) ret = multiply(ret, mat); return ret; } long modExp(long x, long y) { if(y == 1) return x; long ret = modExp(x, y / 2); ret = ret * ret % mod; if(y % 2 == 1) ret = ret * x % mod; return ret; } public void run() { InputReader s = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = s.nextInt(), k = s.nextInt(); int[] a = new int[n]; x = 0; for(int i = 0; i < n; i++) { a[i] = s.nextInt(); x += a[i]; } int[] l = new int[2]; int[] r = new int[2]; for(int i = 0; i < x; i++) r[a[n - 1 - i]]++; for(int i = x; i < n; i++) l[a[n - 1 - i]]++; mat = new long[x + 1][x + 1]; for(int i = 0; i <= x; i++) { mat[i][i] = n * (n - 1) / 2; if(x - i > n - x) continue; if(i - 1 >= 0) { mat[i - 1][i] = (n - x - (x - i)) * i; mat[i][i] -= mat[i - 1][i]; } if(i + 1 <= x) { mat[i + 1][i] = (x - i) * (x - i); mat[i][i] -= mat[i + 1][i]; } } w.println(matExp(k)[x][r[1]] * modExp(modExp(n * (n - 1) / 2, k), mod - 2) % mod); w.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new cf6(),"cf6",1<<26).start(); } }
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 N = 101; const int mod = 1e9 + 7; int n, m, sum_0, right_sum_0; int x[N]; long long a[N], b[N][N], c[N][N]; long long calc(long long x, long long y) { long long z = 1; while (y) { if (y & 1) (z *= x) %= mod; (x *= x) %= mod, y /= 2; } return z; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &x[i]); sum_0 += (x[i] == 0); } for (int i = 1; i <= sum_0; i++) right_sum_0 += (!x[i]); a[right_sum_0] = 1; for (int i = 0; i <= sum_0; i++) { int sum_1 = n - sum_0; int right_sum_0 = i; int n_right_sum_0 = sum_0 - right_sum_0; int n_right_sum_1 = sum_0 - right_sum_0; int right_sum_1 = n - sum_0 - n_right_sum_1; b[i][i + 1] += n_right_sum_0 * n_right_sum_1; b[i][i - 1] += right_sum_0 * right_sum_1; b[i][i] += sum_0 * max(sum_0 - 1, 0) / 2 + sum_1 * max(sum_1 - 1, 0) / 2 + right_sum_0 * n_right_sum_1 + right_sum_1 * n_right_sum_0; } int mm = m; while (m) { if (m & 1) { for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) (c[0][j] += a[i] * b[i][j]) %= mod; for (int i = 0; i <= n; i++) a[i] = c[0][i], c[0][i] = 0; } for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) (c[i][k] += b[i][j] * b[j][k]) %= mod; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) b[i][j] = c[i][j], c[i][j] = 0; m /= 2; } printf("%lld\n", a[sum_0] * calc(calc(n * (n - 1) / 2, mm), mod - 2) % mod); return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; inline int read() { int w = 0, x = 0; char c = getchar(); while (!isdigit(c)) w |= c == '-', c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); return w ? -x : x; } namespace star { const int maxn = 105, mod = 1e9 + 7; int n, m, k, a[maxn]; inline int fpow(int a, int b) { int ans = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) ans = 1ll * ans * a % mod; return ans; } struct mat { int a[maxn][maxn]; mat() { memset(a, 0, sizeof a); } inline void set() { for (int i = 0; i <= n; i++) a[i][i] = 1; } inline const int *operator[](const int &x) const { return a[x]; } inline int *operator[](const int &x) { return a[x]; } inline mat operator*(const mat &b) { mat ans; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) ans[i][j] = (ans[i][j] + 1ll * a[i][k] * b[k][j] % mod) % mod; return ans; } } now, pow; inline mat fpow(mat a, int b) { mat ans; ans.set(); for (; b; b >>= 1, a = a * a) if (b & 1) ans = ans * a; return ans; } inline void work() { m = read(); k = read(); for (int i = 1; i <= m; i++) n += (a[i] = read()) == 0; int t = 0; for (int i = 1; i <= n; i++) t += a[i] == 0; now[0][t] = 1; for (int i = 0; i <= n; i++) { if (i) pow[i - 1][i] = 1ll * (n - i + 1) * (n - i + 1) % mod; pow[i][i] = (1ll * i * (n - i) % mod + 1ll * (n - i) * (m - n - n + i) % mod) % mod; pow[i][i] = (pow[i][i] + 1ll * n * (n - 1) / 2 % mod) % mod; pow[i][i] = (pow[i][i] + 1ll * (m - n) * (m - n - 1) / 2 % mod) % mod; if (i != n) pow[i + 1][i] = 1ll * (i + 1) * (m - n - n + i + 1) % mod; } now = now * fpow(pow, k); int up = now[0][n], down = 0; for (int i = 0; i <= n; i++) down = (down + now[0][i]) % mod; printf("%lld\n", 1ll * up * fpow(down, mod - 2) % mod); } } // namespace star signed main() { star::work(); return 0; }
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 { int sz; vector<vector<long long> > arr; Matrix() {} Matrix(int is) { sz = is; arr = vector<vector<long long> >(sz + 1, vector<long long>(sz + 1, 0)); } void unitize() { for (int i = 0; i <= sz; i++) { for (int j = 0; j <= sz; j++) { arr[i][j] = (i == j); } } } Matrix operator*(const Matrix &a) { Matrix c(sz); for (int i = 0; i <= sz; i++) { for (int j = 0; j <= sz; j++) { for (int k = 0; k <= sz; k++) { (c.arr[i][j] += arr[i][k] * a.arr[k][j]) %= mod; } } } return c; } Matrix operator+(const Matrix &a) { Matrix c(sz); for (int i = 0; i <= sz; i++) { for (int j = 0; j <= sz; j++) { c.arr[i][j] = arr[i][j] + a.arr[i][j]; if (c.arr[i][j] >= mod) c.arr[i][j] -= mod; } } return c; } }; int a[105]; long long modPow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) (ans *= a) %= mod; (a *= a) %= mod; b >>= 1; } return ans; } Matrix powerup(Matrix a, long long b) { Matrix ans(a.sz); ans.unitize(); while (b) { if (b & 1) (ans = ans * a); (a = a * a); b >>= 1; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << setprecision(32); int n, k; cin >> n >> k; int m = n; for (int i = 1; i <= n; i++) { cin >> a[i]; m -= a[i]; } if (m == n || m == 0) { cout << 1 << endl; return 0; } Matrix A(m); for (int i = 0; i <= m; i++) { long long tmp = m * (m - 1) / 2 + (n - m) * (n - m - 1) / 2 + i * (m - i) + (m - i) * (n + i - 2 * m); tmp *= modPow(n * (n - 1) / 2, mod - 2); tmp %= mod; A.arr[i][i] = tmp; if (i != m) { A.arr[i][i + 1] = (m - i) * (m - i) * modPow(n * (n - 1) / 2, mod - 2) % mod; } if (i != 0) { A.arr[i][i - 1] = i * (n + i - 2 * m) * modPow(n * (n - 1) / 2, mod - 2) % mod; } } Matrix B = powerup(A, k); int p = m; for (int i = 1; i <= m; i++) { p -= a[i]; } cout << B.arr[p][m] << endl; return 0; }
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.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static long MOD = 1000000007; public static void main(String[] args) { final Scanner scanner = new Scanner(System.in); final int n = scanner.nextInt(); final int k = scanner.nextInt(); final ArrayList<Integer> values = new ArrayList<>(n); for (int i = 0; i < n; i++) { values.add(scanner.nextInt()); } final int zeroCount = values.stream().filter(integer -> integer.equals(0)).mapToInt(i -> 1).sum(); final int oneCount = n - zeroCount; final int size = Math.min(zeroCount, oneCount); long[][] table = new long[size + 1][size + 1]; int diff = 0; for (int i = 0; i < n; i++) { if (values.get(i) == 0 && i >= zeroCount) { diff++; } } final int allPosible = (n * (n - 1)) / 2; final long allPosibleInverse = binPow(allPosible, MOD - 2); for (int i = 0; i < size + 1; i++) { for (int j = 0; j < size + 1; j++) { if (i - j == 1) { table[i][j] = i * i * allPosibleInverse; } if (i - j == 0) { table[i][j] = (allPosible - i * i - (zeroCount - i) * (oneCount - i)) * allPosibleInverse; } if (i - j == -1) { table[i][j] = (zeroCount - i) * (oneCount - i) * allPosibleInverse; } table[i][j] = table[i][j] % MOD; } } table = binPow(table, k); // for (long[] longs : table) { // for (long value : longs) { // System.out.printf("%10d", value); // } // System.out.println(); // } System.out.println(table[diff][0]); } private static long binPow(long n, long count) { if (count == 0) { return 1L; } long temp = binPow(n, count / 2); temp = (temp * temp) % MOD; if (count % 2 != 0) { temp = (temp * n) % MOD; } return temp % MOD; } private static long[][] binPow(long[][] table, long count) { if (count == 0) { final long[][] ans = new long[table.length][table.length]; for (int i = 0; i < table.length; i++) { ans[i][i] = 1; } return ans; } long[][] temp = binPow(table, count / 2); temp = (mul(temp, temp)); if (count % 2 != 0) { temp = mul(temp, table); } return temp; } private static long[][] mul(long[][] a, long[][] b) { final long[][] ans = new long[a.length][a.length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { for (int k = 0; k < a.length; k++) { ans[i][j] += (a[i][k] * b[k][j]) % MOD; ans[i][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; inline long long int pow(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = res * a % 1000000007; b >>= 1; a = a * a % 1000000007; } return res; } inline long long int inv(long long int x) { return pow(x, 1000000007 - 2); } int n, k; int a[100], cnt0 = 0, p = 0; struct matrix { long long int mat[101][101]; } m; matrix operator*(matrix m1, matrix m2) { matrix res; memset(res.mat, 0, sizeof(res.mat)); for (int i = 0; i <= cnt0; i++) for (int j = 0; j <= cnt0; j++) for (int t = 0; t <= cnt0; t++) res.mat[i][j] = (res.mat[i][j] + m1.mat[i][t] * m2.mat[t][j]) % 1000000007; return res; } matrix matrix_pow(matrix x, int t) { matrix ans; for (int i = 0; i <= cnt0; i++) ans.mat[i][i] = 1; while (t > 0) { if (t & 1) ans = ans * x; x = x * x; t >>= 1; } return ans; } int main(void) { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == 0) cnt0++; } for (int i = 0; i < cnt0; i++) if (a[i] == 0) p++; long long int f = inv(n * (n - 1)) * 2 % 1000000007; for (int i = max(cnt0 * 2 - n, 0); i <= cnt0; i++) { m.mat[i][i] = 1 + 1000000007 * 2; if (i != 0) { m.mat[i][i - 1] = i * (n - cnt0 * 2 + i) * f % 1000000007; m.mat[i][i] -= m.mat[i][i - 1]; } if (i != cnt0) { m.mat[i][i + 1] = (cnt0 - i) * (cnt0 - i) * f % 1000000007; m.mat[i][i] -= m.mat[i][i + 1]; } m.mat[i][i] %= 1000000007; } m = matrix_pow(m, k); printf("%lld\n", m.mat[p][cnt0]); return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; vector<vector<long long> > mulmat(vector<vector<long long> > &mat1, vector<vector<long long> > &mat2) { vector<vector<long long> > ans(mat1.size(), vector<long long>(mat2[0].size(), 0)); for (int i = 0; i < mat1.size(); i++) { for (int j = 0; j < mat2[0].size(); j++) { for (int k = 0; k < mat2.size(); k++) { ans[i][j] = (ans[i][j] + mat1[i][k] * mat2[k][j]) % mod; } } } return ans; } vector<vector<long long> > matrixpow(vector<vector<long long> > &mat, long long pow) { int n = mat.size(); vector<vector<long long> > bas(mat.size(), vector<long long>(mat.size(), 0)); vector<vector<long long> > mul(mat); for (int i = 0; i < mat.size(); i++) { bas[i][i] = 1; } while (pow) { if (pow & 1) bas = mulmat(bas, mul); pow >>= 1; mul = mulmat(mul, mul); } return bas; } long long fastpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; b >>= 1; a = (a * a) % mod; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k, zc = 0; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 0) zc++; } vector<vector<long long> > vals(zc + 1, vector<long long>(zc + 1, 0)); for (int i = 0; i <= zc; i++) { long long zer = i; long long on = zc - zer; long long zero = zc - i; long long ono = n - zc - zero; if (on < 0 || zero < 0 || ono < 0) continue; if (i) vals[zer][zer - 1] = ono * zer; vals[zer][zer] = n * (n - 1) / 2 - ono * zer - zero * on; if (i != zc) vals[zer][zer + 1] = zero * on; } vector<vector<long long> > fmat = matrixpow(vals, k); vector<vector<long long> > start(1, vector<long long>(zc + 1, 0)); int zco = 0; for (int i = 0; i < zc; i++) if (a[i] == 0) zco++; start[0][zco] = 1; vector<vector<long long> > finb = mulmat(start, fmat); long long num = finb[0][zc]; long long den = 0; for (int i = 0; i <= zc; i++) den = (den + finb[0][i]) % mod; den = fastpow(den, mod - 2); cout << (num * den) % mod << '\n'; return 0; }
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 = 107, MOD = 1e9 + 7; int a[MAXN], o = 0, z, x, n, k, dp[MAXN][MAXN] = {}; int fir[MAXN], ans[MAXN]; void add(int &a, const int &b) { a += b; if (a >= MOD) a -= MOD; } struct matrix { int f[MAXN][MAXN] = {}; matrix operator*(matrix &b) { matrix c; for (int i = 0; i <= z; ++i) for (int j = 0; j <= z; ++j) { c.f[i][j] = 0; for (int k = 0; k <= z; ++k) add(c.f[i][j], 1LL * this->f[i][k] * b.f[k][j] % MOD); } return c; } } base; matrix pw(matrix cs, int sm) { if (sm == 1) return cs; matrix hpw = pw(cs, sm >> 1); hpw = hpw * hpw; if (sm & 1) hpw = hpw * cs; return hpw; } int pw(int cs, int sm) { if (sm == 0) return 1; int hpw = pw(cs, sm >> 1); hpw = 1LL * hpw * hpw % MOD; if (sm & 1) hpw = 1LL * hpw * cs % MOD; return hpw; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i], o += a[i]; z = n - o, x = z; for (int i = 1; i <= z; ++i) x -= a[i]; if (z == 0) return cout << 1, 0; fir[x] = 1; for (int i = 0; i <= z; ++i) { if (i > 0) { base.f[i - 1][i] = (z - i + 1) * (z - i + 1); } if (i < z) { base.f[i + 1][i] = (o - z + i + 1) * (i + 1); } base.f[i][i] += z * (z - 1) / 2; base.f[i][i] += (n - z) * (n - z - 1) / 2; base.f[i][i] += i * (z - i); base.f[i][i] += (z - i) * (o - z + i); } base = pw(base, k); for (int j = 0; j <= z; ++j) { ans[j] = 0; for (int k = 0; k <= z; ++k) add(ans[j], 1LL * fir[k] * base.f[k][j] % MOD); } int mau = pw(n * (n - 1) / 2, MOD - 2); cout << 1LL * ans[z] * pw(mau, k) % MOD; return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; long long int a[101][101]; long long int res[101][101], tmp2[101][101]; const int mod = 1e9 + 7; long long int mypow(long long int x, long long int b) { long long int res = 1; while (b) { if (b & 1) res = res * x % mod; x = x * x % mod; b >>= 1; } return res; } void ex(int n, long long int b) { for (int i = 0; i < n; i++) res[i][i] = 1; while (b) { if (b & 1) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) tmp2[i][j] = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) tmp2[i][j] = (tmp2[i][j] + res[i][k] * a[k][j]) % mod; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) res[i][j] = tmp2[i][j]; } b >>= 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) tmp2[i][j] = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) tmp2[i][j] = (tmp2[i][j] + a[i][k] * a[k][j]) % mod; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = tmp2[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = res[i][j]; } int v[101]; int c[101]; long long int cnt; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N, k; cin >> N >> k; long long int tmp = N * (N - 1) / 2; for (int i = 0; i < N; i++) cin >> v[i]; for (int i = 0; i < N; i++) if (v[i] == 0) cnt++; int tttmp = 0; for (int i = cnt; i < N; i++) if (v[i] == 1) tttmp++; for (int j = 0; j <= N; j++) { if (j > N - cnt || N - j - cnt > cnt) continue; long long int now = tmp; for (int i = 0; i <= N; i++) { if (i == j - 1) { now = ((now - (2 * cnt + j - N) * j) % mod + mod) % mod; a[i][j] = ((2 * cnt + j - N) * j) % mod * mypow(tmp, mod - 2) % mod; } else if (i == j + 1) { now = ((now - (N - cnt - j) * (N - cnt - j)) % mod + mod) % mod; a[i][j] = ((N - cnt - j) * (N - cnt - j) % mod * mypow(tmp, mod - 2)) % mod; } } a[j][j] = now * mypow(tmp, mod - 2) % mod; } ex(N + 1, k); cout << a[N - cnt][tttmp] << endl; }
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 int64_t MOD = 1e9 + 7; void add(int64_t& a, int64_t b) { a = (a + b) % MOD; } void mul(int64_t& a, int64_t b) { a = (a * b) % MOD; } int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y) { int64_t d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } int64_t inv_mod(int64_t a) { int64_t x, y; extgcd(a, MOD, x, y); return (MOD + x % MOD) % MOD; } const int SZ = 100; void matmul(int64_t A[SZ][SZ], int64_t B[SZ][SZ]) { int64_t ret[SZ][SZ] = {0}; for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++) for (int k = 0; k < SZ; k++) add(ret[i][j], A[i][k] * B[k][j]); for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++) A[i][j] = ret[i][j]; } int main() { int N, K, A[100]; cin >> N >> K; for (int i = 0; i < N; i++) cin >> A[i]; int one = accumulate(A, A + N, 0); int zero = N - one; int d = 0; for (int i = 0; i < zero; i++) d += A[i]; int64_t M[100][100] = {0}, S[100][100] = {0}; for (int i = 0; i <= min(one, zero); i++) { S[i][i] = 1; M[i][i] = one * (one - 1) / 2 + zero * (zero - 1) / 2 + i * (N - 2 * i); if (i < min(one, zero)) M[i + 1][i] = (zero - i) * (one - i); if (i > 0) M[i - 1][i] = i * i; } int64_t c = 1, base = N * (N - 1) / 2; while (K > 0) { if (K & 1) { mul(c, base); matmul(S, M); } matmul(M, M); mul(base, base); K /= 2; } int64_t ans = S[0][d]; mul(ans, inv_mod(c)); cout << ans << endl; return 0; }
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 long long P = 1e9 + 7; inline long long add(long long x, long long y) { x += y; return x >= P ? x - P : x; } inline long long sub(long long x, long long y) { x -= y; return x < 0 ? x + P : x; } map<long long, long long> INV; inline long long inv(long long x) { if (!INV[x]) { int y = P - 2; long long res = 1; while (y) { if (y & 1) res = res * x % P; x = x * x % P; y >>= 1; } INV[x] = res; } return INV[x]; } struct Matrix { long long w[N][N]; int lim; void clear(int x) { lim = x; memset(w, 0, sizeof(w)); } void init(int x) { clear(x); for (int i = 0; i <= lim; ++i) w[i][i] = 1; } friend Matrix operator*(Matrix x, Matrix y) { Matrix z; z.clear(x.lim); for (int i = 0; i <= z.lim; ++i) for (int j = 0; j <= z.lim; ++j) for (int k = 0; k <= z.lim; ++k) z.w[i][j] = add(z.w[i][j], x.w[i][k] * y.w[k][j] % P); return z; } void print() { for (int i = 0; i <= lim; ++i) { for (int j = 0; j <= lim; ++j) printf("%d ", w[i][j]); printf("\n"); } } } beg, chg; int n, m, a[N], cnt; inline Matrix mpow(Matrix x, int y) { Matrix res; res.init(cnt); while (y) { if (y & 1) res = res * x; x = x * x; y >>= 1; } return res; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), cnt += (a[i] ^ 1); beg.clear(cnt); chg.clear(cnt); int tt = 0; for (int i = 1; i <= cnt; ++i) tt += (a[i] ^ 1); beg.w[tt][0] = 1; for (int i = 0; i <= cnt; ++i) if (n + i >= (cnt << 1)) { long long c00 = i, c01 = cnt - i, c10 = cnt - i, c11 = 1ll * n - c00 - c01 - c10; if (i < cnt) chg.w[i + 1][i] = 1ll * (1ll * c01 * c10 % P) * inv(1ll * n * (n - 1) % P * inv(2) % P) % P; if (i) chg.w[i - 1][i] = 1ll * (1ll * c00 * c11 % P) * inv(1ll * n * (n - 1) % P * inv(2) % P) % P; chg.w[i][i] = sub(1, add(chg.w[i + 1][i], chg.w[i - 1][i])); } Matrix res = mpow(chg, m); res = res * beg; printf("%lld\n", res.w[cnt][0]); return 0; }
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 a[105], n, k, s, t, Lucina; inline int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b) { return (int)((long long)a * b % mod); } inline int power(int a, int b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } struct matrix { int m[102][102]; matrix() { for (int i = 0; i <= Lucina + 1; i++) for (int j = 0; j <= Lucina + 1; j++) m[i][j] = 0; } matrix I() { matrix tmp; for (int i = 0; i <= Lucina + 1; i++) tmp.m[i][i] = 1; return tmp; } matrix operator*(const matrix x) const { matrix tmp; for (int i = 0; i <= Lucina; i++) for (int j = 0; j <= Lucina; j++) { for (int r = 0; r <= Lucina; r++) { tmp.m[i][j] = add(tmp.m[i][j], mul(m[i][r], x.m[r][j])); } } return tmp; } matrix power(matrix x, int b) { matrix tmp = I(); while (b > 0) { if (b & 1) { tmp = (tmp * x); } x = (x * x); b >>= 1; } return tmp; } void print() { for (int i = 0; i <= Lucina; i++) for (int j = 0; j <= Lucina; j++) printf("%d%c", m[i][j], j == Lucina ? '\n' : ' '); } }; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); s += (a[i] == 0); } if (s == 0 || s == n) return printf("1"), 0; Lucina = s; for (int i = 1; i <= Lucina; i++) { t += (a[i] == 0); } matrix x; x.m[t][0] = 1; int d = mul(n, n - 1) / 2; int crazy = n * (n - 1) / 2; d = power(d, mod - 2); matrix l; int peace = ((Lucina * (Lucina - 1)) + ((n - Lucina) * (n - Lucina - 1))) / 2; for (int i = 0; i <= Lucina; i++) { l.m[i][i] = add(l.m[i][i], peace); l.m[i][i] = add(l.m[i][i], mul(n - Lucina * 2 + i * 2, Lucina - i)); if (i - 1 >= 0) l.m[i][i - 1] = add(l.m[i][i - 1], mul(Lucina - i + 1, Lucina - i + 1)); if (i + 1 <= Lucina) l.m[i][i + 1] = add(l.m[i][i + 1], mul(i + 1, n - Lucina - Lucina + i + 1)); l.m[i][i - 1] = mul(l.m[i][i - 1], d); l.m[i][i + 1] = mul(l.m[i][i + 1], d); l.m[i][i] = mul(l.m[i][i], d); } matrix r = l.power(l, k); matrix ans = r * x; printf("%d\n", ans.m[Lucina][0]); }
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> void err() { std::cout << std::endl; } template <typename T, typename... Args> void err(T a, Args... args) { std::cout << a << ' '; err(args...); } using namespace std; const int mod = 1e9 + 7; const int inf = 1 << 30; const int maxn = 100 + 5; long long qpow(long long x, long long n) { long long r = 1; while (n > 0) { if (n & 1) r = r * x % mod; n >>= 1; x = x * x % mod; } return r; } long long inv(long long x) { return qpow(x, mod - 2); } void add(long long& x, long long y) { x += y; if (x >= mod) x -= mod; } struct Mat { static const int M = 100 + 5; long long a[M][M]; Mat() { memset(a, 0, sizeof(a)); } void clear() { memset(a, 0, sizeof(a)); } void eye() { for (int i = 0; i < M; i++) a[i][i] = 1; } long long* operator[](long long x) { return a[x]; } const long long* operator[](long long x) const { return a[x]; } Mat operator*(const Mat& b) { const Mat& a = *this; Mat r; for (int i = 0; i < M; i++) for (int j = 0; j < M; j++) for (int k = 0; k < M; k++) r[i][j] = (r[i][j] + a[i][k] * b[k][j]) % mod; return r; } Mat pow(long long n) const { Mat a = *this, r; r.eye(); while (n > 0) { if (n & 1) r = r * a; n >>= 1; a = a * a; } return r; } Mat operator+(const Mat& b) { const Mat& a = *this; Mat r; for (int i = 0; i < M; i++) for (int j = 0; j < M; j++) r[i][j] = (a[i][j] + b[i][j]) % mod; return r; } void print() const { for (int i = 0; i < M; i++) for (int j = 0; j < M; j++) printf("%lld%c", (*this)[i][j], j == M - 1 ? '\n' : ' '); } } F, T; int n, k, m, a[maxn]; long long dp[2][maxn]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", a + i); if (a[i] == 0) m++; } int c = 0; for (int i = 1; i <= m; i++) if (a[i] == 0) c++; F[c][0] = 1; long long iv = inv(n * (n - 1) / 2), c1 = m * (m - 1) / 2, c2 = (n - m) * (n - m - 1) / 2; for (int i = 0; i <= m; i++) { T[i][i] = 1ll * (c1 + c2 + i * (m - i) + (m - i) * (n - 2 * m + i)) * iv % mod; if (i > 0) { T[i][i - 1] = 1ll * (m - i + 1) * (m - i + 1) * iv % mod; } if (i < m) { T[i][i + 1] = 1ll * (i + 1) * (n - 2 * m + i + 1) * iv % mod; } } cout << (T.pow(k) * F)[m][0] << endl; return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; const long long N = 105, mod = 1e9 + 7; struct ab { long long a[N][N]; ab() { memset(a, 0, sizeof a); } } ans, xx; long long a[N], n; long long p2(long long x) { return x * (x - 1) / 2; } long long POW(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } ab mul(ab x, ab y) { ab z; for (long long i = 0; i <= n; i++) { for (long long j = 0; j <= n; j++) { for (long long k = 0; k <= n; k++) z.a[i][j] = (z.a[i][j] + x.a[i][k] * y.a[k][j]) % mod; } } return z; } signed main() { long long k, sum = 0, now = 0; scanf("%lld%lld", &n, &k); long long inv = POW(p2(n), mod - 2); for (long long i = 1; i <= n; i++) { scanf("%lld", a + i); sum += a[i]; } sum = n - sum; for (long long i = 1; i <= sum; i++) now += a[i]; now = sum - now; for (long long i = 0; i <= n; i++) ans.a[i][i] = 1; for (long long i = max(0ll, 2 * sum - n); i <= sum; i++) { if (i > 0) xx.a[i][i - 1] = (sum - i + 1) * (sum - i + 1) * inv % mod; xx.a[i][i] = (p2(sum) + p2(n - sum) + i * (sum - i) + (sum - i) * (n + i - 2 * sum)) * inv % mod; xx.a[i][i + 1] = (i + 1) * (n + i + 1 - 2 * sum) * inv % mod; } while (k) { if (k & 1) ans = mul(ans, xx); xx = mul(xx, xx); k >>= 1; } printf("%lld\n", ans.a[sum][now]); return 0; }
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 pow_mod(long long a, long long p) { if (p == 0) return 1; long long ret = pow_mod(a, p / 2); ret = ret * ret % mod; if (p % 2 == 1) ret = ret * a % mod; return ret; } long long inv(long long a) { return pow_mod(a, mod - 2); } struct matrix { long long a[105][105]; int row, col; matrix() : row(105), col(105) { memset(a, 0, sizeof(a)); } matrix(int x, int y) : row(x), col(y) { memset(a, 0, sizeof(a)); } long long* operator[](int x) { return a[x]; } matrix operator*(matrix x) { matrix tmp(col, x.row); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) if (a[i][j]) for (int k = 0; k < x.col; k++) if (x[j][k]) { tmp[i][k] += a[i][j] * x[j][k]; tmp[i][k] %= mod; } return tmp; } void operator*=(matrix x) { *this = *this * x; } matrix operator^(int x) { matrix ret(row, col); for (int i = 0; i < col; i++) ret[i][i] = 1; matrix tmp = *this; for (; x > 0; x >>= 1, tmp *= tmp) if (x & 1) ret *= tmp; return ret; } }; int c2(int x) { return x * (x - 1) % mod * inv(2) % mod; } int a[105]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int m = 0; for (int i = 0; i < n; i++) if (a[i] == 0) m++; int t = 0; n = n - m; for (int i = 0; i < m; i++) if (a[i] == 1) t++; int p = min(m, n); matrix mat(p + 1, p + 1); for (int j = 0; j <= p; j++) { mat.a[j][j] = (c2(m - j) + c2(n - j) + 2 * (m - j) * j + 2 * (n - j) * j + 2 * c2(j)) % mod * inv(c2(m + n)) % mod; if (j + 1 <= p) mat.a[j][j + 1] = ((j + 1) * (j + 1)) % mod * inv(c2(m + n)) % mod; if (j - 1 >= 0) mat.a[j][j - 1] = ((m - j + 1) * (n - j + 1)) % mod * inv(c2(m + n)) % mod; } mat = mat ^ k; printf("%lld\n", mat.a[0][t]); return 0; }
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 arr[110]; int src[30][110][110]; int temp[110][110]; int dst[110][110]; int add(int a, int b) { return (a + b) % 1000000007; } int sub(int a, int b) { return (a - b + 1000000007) % 1000000007; } int prod(int a, int b) { return 1LL * a * b % 1000000007; } int power(int a, int n) { if (!n) return 1; int mid = power(a, n / 2); mid = prod(mid, mid); if (n % 2) mid = prod(mid, a); return mid; } int inv(int a) { return power(a, 1000000007 - 2); } void matmul(int a[110][110], int b[110][110], int c[110][110], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp[i][j] = 0; for (int k = 0; k < n; k++) temp[i][j] = add(temp[i][j], prod(a[i][k], b[k][j])); } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) c[i][j] = temp[i][j]; } } void matPower(int k, int n) { for (int i = 1; i < 30; i++) matmul(src[i - 1], src[i - 1], src[i], n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) dst[i][j] = (i == j); } for (int i = 0; i < 30; i++) { if (k & (1 << i)) matmul(dst, src[i], dst, n); } } int main(void) { int N, K, cnt, C; cin >> N >> K; C = 0; cnt = 0; for (int i = 0; i < N; i++) { cin >> arr[i]; if (!arr[i]) cnt++; } for (int i = 0; i < cnt; i++) C += !arr[i]; for (int i = max(0, 2 * cnt - N); i <= cnt; i++) { src[0][i][i] = 1; if (i > max(0, 2 * cnt - N)) { src[0][i][i - 1] = prod(inv(N * (N - 1) / 2), i * (N + i - cnt - cnt)); src[0][i][i] = sub(src[0][i][i], src[0][i][i - 1]); } if (i < cnt) { src[0][i][i + 1] = prod(inv(N * (N - 1) / 2), (cnt - i) * (cnt - i)); src[0][i][i] = sub(src[0][i][i], src[0][i][i + 1]); } } for (int i = max(0, 2 * cnt - N); i <= cnt; i++) { for (int j = max(0, 2 * cnt - N); j <= cnt; j++) src[0][i - max(0, 2 * cnt - N)][j - max(0, 2 * cnt - N)] = src[0][i][j]; } C -= max(0, 2 * cnt - N); cnt -= max(0, 2 * cnt - N); matPower(K, cnt + 1); cout << dst[C][cnt] << endl; return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int matrix[102][102]; long long int mod = 1000000007; long long int res[102][102]; long long int org[102][102]; long long int d; void mult1() { for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { res[i][j] = 0; } } for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { for (long long int k = 0; k <= d; k++) { res[i][j] += matrix[i][k] * matrix[k][j]; if (res[i][j] >= mod) { res[i][j] %= mod; } } } } for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { matrix[i][j] = res[i][j]; } } return; } void mult2() { for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { res[i][j] = 0; } } for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { for (long long int k = 0; k <= d; k++) { res[i][j] += matrix[i][k] * org[k][j]; if (res[i][j] >= mod) { res[i][j] %= mod; } } } } for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { matrix[i][j] = res[i][j]; } } return; } void mexp(long long int n) { if (n == 1) { return; } if (n % 2 == 1) { mexp((n - 1) / 2); mult1(); mult2(); return; } else { mexp(n / 2); mult1(); return; } } long long int arr[105]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, k; cin >> n >> k; long long int z = 0; long long int o = 0; for (long long int i = 1; i <= n; i++) { cin >> arr[i]; if (arr[i]) { o++; } else { z++; } } long long int c = 0; for (long long int i = 1; i <= z; i++) { if (arr[i] == 0) { c++; } } d = z; long long int tot = (n * (n - 1)) / 2; for (long long int i = 0; i <= d; i++) { if (o - z + i >= 0) { org[i][i] = (((z * (z - 1)) / 2 + (o * (o - 1)) / 2 + i * (z - i) + ((z - i) * (o - z + i))) * (power(tot, mod - 2, mod))) % mod; if (i > 0) { org[i][i - 1] = ((((z - i + 1) * (z - i + 1))) * (power(tot, mod - 2, mod))) % mod; } if (i < d && (o - (z - i - 1) >= 0)) { org[i][i + 1] = ((((i + 1) * (o - (z - i - 1)))) * (power(tot, mod - 2, mod))) % mod; } } } for (long long int i = 0; i <= d; i++) { for (long long int j = 0; j <= d; j++) { matrix[i][j] = org[i][j]; } } mexp(k); cout << matrix[d][c]; }
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(int &x, int y) { x += y; if (x >= 1000000007) x -= 1000000007; } inline int read() { int x = 0; bool t = false; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') t = true, ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return t ? -x : x; } int n, N, K, z, C[105][105], a[105]; int fpow(int a, int b) { int s = 1; while (b) { if (b & 1) s = 1ll * s * a % 1000000007; a = 1ll * a * a % 1000000007; b >>= 1; } return s; } struct Matrix { int s[105][105]; void clear() { memset(s, 0, sizeof(s)); } void init() { clear(); for (int i = 0; i <= N; ++i) s[i][i] = 1; } int *operator[](int x) { return s[x]; } } A, B; Matrix operator*(Matrix a, Matrix b) { Matrix c; c.clear(); for (int i = 0; i <= N; ++i) for (int j = 0; j <= N; ++j) for (int k = 0; k <= N; ++k) c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % 1000000007; return c; } Matrix fpow(Matrix a, int b) { Matrix s; s.init(); while (b) { if (b & 1) s = s * a; a = a * a; b >>= 1; } return s; } int main() { n = read(); K = read(); for (int i = 1; i <= n; ++i) a[i] = read(), z += a[i] ^ 1; for (int i = 0; i <= n; ++i) C[i][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % 1000000007; int s = 0; for (int i = 1; i <= n; ++i) if (i <= z && a[i]) ++s; N = min(n - z, z); B[0][s] = 1; for (int i = 0; i <= N; ++i) { add(A[i][i], (C[z][2] + C[n - z][2]) % 1000000007); add(A[i][i], 1ll * i * (n - z - i) % 1000000007); add(A[i][i], 1ll * (z - i) * i % 1000000007); if (i) add(A[i][i - 1], 1ll * i * i % 1000000007); if (i < N) add(A[i][i + 1], 1ll * (z - i) * (n - z - i) % 1000000007); } B = B * fpow(A, K); int ans = 1ll * B[0][0] * fpow(fpow(n * (n - 1) / 2, K), 1000000007 - 2) % 1000000007; printf("%d\n", ans); return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> inline int read() { int res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); return bo ? ~res + 1 : res; } const int N = 105, ZZQ = 1e9 + 7; int n, k, a[N], cnt, c0, c1, st, I; int qpow(int a, int b) { int res = 1; while (b) { if (b & 1) res = 1ll * res * a % ZZQ; a = 1ll * a * a % ZZQ; b >>= 1; } return res; } struct matrix { int n, m, a[N][N]; matrix() {} matrix(int _n, int _m) : n(_n), m(_m) { memset(a, 0, sizeof(a)); } friend inline matrix operator*(matrix a, matrix b) { matrix res = matrix(a.n, b.m); for (int i = 1; i <= res.n; i++) for (int j = 1; j <= res.m; j++) for (int k = 1; k <= a.m; k++) res.a[i][j] = (1ll * a.a[i][k] * b.a[k][j] + res.a[i][j]) % ZZQ; return res; } friend inline matrix operator^(matrix a, int b) { matrix res = matrix(a.n, a.m); for (int i = 1; i <= res.n; i++) res.a[i][i] = 1; while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } } A, St; int main() { n = read(); k = read(); for (int i = 1; i <= n; i++) a[i] = read(), cnt += !a[i]; I = qpow(n * (n - 1) >> 1, ZZQ - 2); c0 = cnt; c1 = n - cnt; if (n - cnt < cnt) cnt = n - cnt; A = matrix(cnt + 1, cnt + 1); St = matrix(cnt + 1, 1); for (int i = 1; i <= c0; i++) if (a[i]) st++; St.a[st + 1][1] = 1; for (int i = 0; i <= cnt; i++) { int cur = n * (n - 1) >> 1; if (i) A.a[i][i + 1] = 1ll * i * i * I % ZZQ, cur -= i * i; if (i < cnt) A.a[i + 2][i + 1] = 1ll * (c0 - i) * (c1 - i) * I % ZZQ, cur -= (c0 - i) * (c1 - i); A.a[i + 1][i + 1] = 1ll * cur * I % ZZQ; } std::cout << ((A ^ k) * St).a[1][1] << std::endl; return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; long long dp[10002][103]; long long n; long long power(long long x, long long k) { long long result = 1; while (k) { if (k & 1) result = (result * x) % 1000000007; x = (x * x) % 1000000007; k = k >> 1; } return result; } class matrix { public: long long X[105][105] = {}; matrix operator*(matrix& P) { matrix R; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= n; k++) { R.X[i][k] = (R.X[i][k] + 1ll * X[i][j] * P.X[j][k]) % 1000000007; } } } return R; } matrix operator^(long long e) { if (e == 1) { return *this; } matrix T = *this ^ (e / 2); T = T * T; return e % 2 ? T * *this : T; } } T; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); long long i, j, k, c = 0; cin >> n >> k; long long a[n + 5], p; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 0) c++; } long long s = 0; for (i = 0; i < c; i++) if (a[i] == 0) s++; for (i = 0; i < c + 1; i++) for (j = 0; j < c + 1; j++) { T.X[i][j] = 0; } for (j = 0; j < c + 1; j++) { T.X[j][j] = ((n - c) * (n - c - 1) / 2 + c * (c - 1) / 2 + (c - j) * (n - 2 * c + j) + j * (c - j)) % 1000000007; if (j != 0) T.X[j][j - 1] = ((c - j + 1) * (c - j + 1)) % 1000000007; if (j != c) T.X[j][j + 1] = ((j + 1) * (n - 2 * c + j + 1)) % 1000000007; } matrix A = T ^ k; p = A.X[c][s]; j = n * (n - 1) / 2; long long q = power(j, k); q = power(q, 1000000007 - 2); long long ans = p * q; ans = ans % 1000000007; cout << ans; }
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); F solver = new F(); solver.solve(1, in, out); out.close(); } static class F { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.ni(), k = in.ni(); int[] p = in.na(n); long mod = (int) 1e9 + 7; long cnt0 = 0, cnt1 = 0; for (int i = 0; i < n; i++) { if (p[i] == 1) cnt1++; else cnt0++; } if (cnt0 == 0 || cnt1 == 0) { out.println(1); return; } long N = (cnt0 + cnt1) * (cnt0 + cnt1 - 1) / 2 % mod; long[][] matrix = new long[(int) cnt0 + 1][(int) cnt0 + 1]; // m[i][j] - moving from state j to state i long pSame = (cnt0 * (cnt0 - 1) / 2 + cnt1 * (cnt1 - 1) / 2) % mod; // exchange withing groups int from = Math.max(0, (int) (cnt0 - cnt1)); for (int i = from; i <= cnt0; i++) { // i - amount of 0 in first group matrix[i][i] = ((cnt0 - i) * (cnt1 - cnt0 + 2 * i) % mod + pSame) % mod;//between groups but same numbers if (i < cnt0) { matrix[i + 1][i] = (cnt0 - i) * (cnt0 - i) % mod; // increasing amount of 0s, cnt0-i number of 0s in second group and 1s in first } if (i > from) { matrix[i - 1][i] = i * (cnt1 - cnt0 + i) % mod; // decreasing amount of 0s } } // for (int i = 0; i <= cnt0; i++) { // System.out.println(Arrays.toString(matrix[i])); // } int a0 = 0; for (int i = 0; i < cnt0; i++) { if (p[i] == 0) a0++; } long[] start = new long[(int) cnt0 + 1]; start[a0] = 1; // initial state long[][] mPow = MatrixUtils.binPow(matrix, k, mod); long[] res = new long[(int) cnt0 + 1]; MatrixUtils.multiply(mPow, start, res, mod); long invNK = IntegerUtils.pow(N, mod - 1 - k, mod); out.println(res[(int) cnt0] * invNK % mod); } } static class MatrixUtils { public static long[][] binPow(long[][] a, long k, long mod) { int n = a.length; long[][] res = new long[n][n]; long[][] aa = new long[n][n]; for (int i = 0; i < n; i++) { res[i][i] = 1; for (int j = 0; j < n; j++) { aa[i][j] = a[i][j]; } } long[][] nxt = new long[n][n]; long[][] tmp; while (k > 0) { if (k % 2 == 0) { multiply(aa, aa, nxt, mod); tmp = aa; aa = nxt; nxt = tmp; k /= 2; } else { multiply(aa, res, nxt, mod); tmp = nxt; nxt = res; res = tmp; k--; } } return res; } public static void multiply(long[][] a, long[][] b, long[][] res, long mod) { for (int i = 0; i < res.length; i++) { for (int j = 0; j < res[i].length; j++) { res[i][j] = 0; for (int k = 0; k < b.length; k++) { res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % mod; } } } } public static void multiply(long[][] a, long[] b, long[] res, long mod) { for (int i = 0; i < res.length; i++) { res[i] = 0; for (int k = 0; k < b.length; k++) { res[i] = (res[i] + a[i][k] * b[k]) % mod; } } } } static class IntegerUtils { public static long pow(long a, long p, long mod) { if (p == 0) { return 1; } long t = pow(a, p / 2, mod); if (p % 2 != 0) { return (((t * t) % mod) * a) % mod; } else { return (t * t) % mod; } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String ns() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int ni() { return Integer.parseInt(ns()); } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } } }
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("O3") #pragma GCC target("avx,avx2,fma") using namespace std; template <class T> using v2d = vector<vector<T>>; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 1e2 + 10; const long long mod = 1e9 + 7; struct Matrix { int n, m; vector<vector<long long>> s; Matrix() : n(0), m(0) {} Matrix(vector<vector<long long>> &a) { s = a; n = s.size(); if (n > 0) { m = s[0].size(); } } void assign(vector<vector<long long>> &a) { s = a; n = s.size(); if (n > 0) { m = s[0].size(); } } Matrix(int n, int m) : n(n), m(m) { s = vector<vector<long long>>(n, vector<long long>(m)); } vector<long long> &operator[](int i) { return s[i]; } }; void mul(Matrix &a, Matrix b) { assert(a.m == b.n); int n = a.n, m = a.m, p = b.m; vector<vector<long long>> &s = a.s, &t = b.s, r(n, vector<long long>(p)); for (int i = 0; i < (int)(n); ++i) { for (int k = 0; k < (int)(p); ++k) { for (int j = 0; j < (int)(m); ++j) { r[i][j] += s[i][k] * t[k][j] % mod; if (r[i][j] >= mod) { r[i][j] -= mod; } } } } a.assign(r); } Matrix power(Matrix a, long long b) { assert(a.n == a.m); int n = a.n; Matrix r(n, n); for (int i = 0; i < (int)(n); ++i) { r[i][i] = 1; } while (b) { if (b & 1) { mul(r, a); } b >>= 1; mul(a, a); } return r; } long long power(long long a, long long b) { long long r = 1; while (b) { if (b & 1) { r = r * a % mod; } b >>= 1; a = a * a % mod; } return r; } long long nC2(int n) { return n * (n - 1) / 2; } long long inv(long long a) { return power(a, mod - 2); } int n, p, a[maxN]; long long k; void solve() { cin >> n >> k; for (int i = 1; i <= (int)(n); ++i) { cin >> a[i]; p += a[i] == 0; } int tmp = 0; for (int i = 1; i <= (int)(p); ++i) { tmp += a[i] == 0; } Matrix b(p + 1, p + 1); long long iv = inv(nC2(n)); for (int i = 0; i < (int)(p + 1); ++i) { if (i >= 1) { b[i - 1][i] = (p - i + 1) * (p - i + 1) * iv % mod; } if (i < p) { b[i + 1][i] = (n - p * 2 + i + 1) % mod * (i + 1) % mod * iv % mod; } b[i][i] = (nC2(p) + nC2(n - p) + i * (p - i) + (p - i) * (n - p * 2 + i)) % mod * iv % mod; } Matrix ans(1, p + 1); ans[0][tmp] = 1; mul(ans, power(b, k)); cout << ans[0][p]; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T = 1; while (T--) { solve(); } return 0; }
CPP
1151_F. Sonya and Informatics
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens: * Two numbers i and j are chosen equiprobable such that (1 ≀ i < j ≀ n). * The numbers in the i and j positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q not≑ 0~\pmod {10^9+7}. Input The first line contains two integers n and k (2 ≀ n ≀ 100, 1 ≀ k ≀ 10^9) β€” the length of the array a and the number of operations. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 1) β€” the description of the array a. Output If the desired probability is 0, print 0, otherwise print the value P β‹… Q^{-1} \pmod {10^9+7}, where P and Q are defined above. Examples Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 Note In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
2
12
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; inline int add(int x, int y) { return (x += y) >= mod ? x - mod : x; } inline void inc(int &x, int y) { (x += y) >= mod && (x -= mod); } inline int mul(int x, int y) { return 1LL * x * y - 1LL * x * y / mod * mod; } inline int modpower(int x, long long y) { int res = 1; while (y) { if (y & 1) res = mul(res, x); y >>= 1, x = mul(x, x); } return res; } int n, c, k, tot; int a[105]; struct matrix { int mat[105][105]; void clear() { memset(mat, 0, sizeof(mat)); } matrix(int x = 0) { clear(); for (int i = 0; i <= c; ++i) mat[i][i] = x; } int *operator[](const int &x) { return mat[x]; } friend matrix operator*(matrix &a, matrix &b) { matrix res; for (int i = 0; i <= c; ++i) for (int j = 0; j <= c; ++j) for (int k = 0; k <= c; ++k) inc(res[i][j], mul(a[i][k], b[k][j])); return res; } friend matrix operator^(matrix a, long long b) { matrix res(1); while (b) { if (b & 1) res *= a; b >>= 1, a *= a; } return res; } matrix &operator*=(matrix &x) { return *this = *this * x; } matrix &operator^=(long long x) { return *this = *this ^ x; } } dp, trans; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", a + i), c += (a[i] == 0); dp[0][count(a + 1, a + c + 1, 0)] = 1; for (int j = 0; j <= c; ++j) { if (j > 0) trans[j - 1][j] = (c - (j - 1)) * (c - (j - 1)); if (j < c) trans[j + 1][j] = (j + 1) * (n - c - (c - (j + 1))); trans[j][j] = (j * (c - j)) + (c - j) * (n - c - (c - j)); trans[j][j] += c * (c - 1) / 2 + (n - c) * (n - c - 1) / 2; } trans ^= k; dp *= trans; for (int j = 0; j <= c; ++j) inc(tot, dp[0][j]); printf("%d\n", mul(dp[0][c], modpower(tot, mod - 2))); return 0; }
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
//package Contest553; import java.io.PrintWriter; import java.util.Scanner; public class mainF { public static Scanner enter = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { long mod=(long)1e9+7; int n=enter.nextInt(); long s=enter.nextLong(); int[] arr=new int[n]; int m=0; long CON=n*(n-1)/2; CON=bin_pow(CON,mod-2,mod); for (int i = 0; i <n ; i++) { arr[i]=enter.nextInt(); m+=arr[i]; } int f_k=0; for (int i = 0; i <n-m ; i++) { f_k+=arr[i]; } int k=n-m; f_k=n-m-f_k; /*if(m<n-m){//Если Π½ΡƒΠ»Π΅ΠΉ большС Ρ‡Π΅ΠΌ Π΅Π΄ΠΈΠ½ΠΈΡ† k=m; f_k=n-m-f_k; m=n-m; }*/ //f_k- ΠΊΠΎΠ»-Π²ΠΎ Π½ΡƒΠ»Π΅ΠΉ Π² ΠΏΠ΅Ρ€Π²ΠΎΠΉ части Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ это Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎΠ΅ состояниС ΠΏΡ€ΠΈΡ‡Ρ‘ΠΌ ΠΊΠΎΠ»-Π²ΠΎ Π½ΡƒΠ»Π΅ΠΉ мСньшС ΠΊΠΎΠ»-Π²Π° Π΅Π΄ΠΈΠ½ΠΈΡ† long[][] CM=new long[k+1][k+1]; for (int i = Math.max(0,k-m); i <CM.length ; i++) { CM[i][i]=(1-(long)(i*(m-k+i)+(k-i)*(k-i))*CON); if(i<k) CM[i][i+1]=(long)(k-i)*(k-i)*CON; if(i>0)CM[i][i-1]=(long)i*(m-k+i)*CON; } MATRIX_mod(CM,mod); CM=bin_pow(CM,s,mod); System.out.println((CM[f_k][k]+mod)%mod); } public static long bin_pow(long a,long b, long mod){//a^b %mod long ans=1; while(b!=0){ if((b&1)==1) ans*=a; a*=a; ans%=mod; a%=mod; b>>=1; } return ans; } public static long[][] bin_pow(long[][] a,long b, long mod){//a^b %mod long[][] ans=new long[a.length][a.length]; for (int i = 0; i <ans.length ; i++) ans[i][i]=1; while(b!=0){ if((b&1)==1) ans=MATRIX_mult(ans,a); a=MATRIX_mult(a,a); MATRIX_mod(ans,mod); MATRIX_mod(a,mod); b>>=1; } return ans; } public static long[][] MATRIX_mult(long[][] a, long[][] b) { //b Π½Π΅ мСняСм a=a*b; long[][] c=new long[a.length][a.length]; for (int i = 0; i <a.length ; i++) { for (int j = 0; j <a.length ; j++) { for (int k = 0; k <a.length ; k++) { c[i][j]+=a[i][k]*b[k][j]; c[i][j]%=(long)(1e9+7); } } } return c; } public static void MATRIX_mod(long[][] a, long mod){ for (int i = 0; i <a.length ; i++) { for (int j = 0; j <a.length ; j++) { a[i][j]%=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; const long long MOD = 1000 * 1000 * 1000 + 7; long long mod(long long n) { if (n < 0) { if (n + MOD >= 0) return n + MOD; else return (n % MOD + MOD) % MOD; } else { if (n < MOD) return n; else if (n < (MOD << 1)) return n - MOD; else return n % MOD; } } long long fp(long long a, long long p) { long long ans = 1, cur = a; for (long long i = 0; (1 << i) <= p; ++i) { if ((p >> i) & 1) ans = mod(ans * cur); cur = mod(cur * cur); } return ans; } long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); } const long long N = 101; long long m[N][N], ans[N][N], t[N][N]; void add(long long a[N][N], long long b[N][N]) { for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { t[i][j] = 0; } } for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { for (long long k = 0; k < N; ++k) { if (!a[i][k] || !b[k][j]) continue; t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]); } } } for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { a[i][j] = t[i][j]; } } } void pw(long long p) { for (long long i = 0; i < N; ++i) ans[i][i] = 1; for (long long i = 0; (1 << i) <= p; ++i) { if ((p >> i) & 1) add(ans, m); add(m, m); } } bool a[N]; long long cnt[2]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; for (long long i = 0; i < n; ++i) { cin >> a[i]; ++cnt[a[i]]; } long long l = cnt[0]; long long r = n - l; long long op = n * (n - 1) / 2; for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) { long long l0 = l - l1; long long r0 = cnt[0] - l0; long long r1 = cnt[1] - l1; m[l1][l1] = op; if (l1) { m[l1][l1 - 1] = mod(l1 * r0); m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]); } m[l1][l1 + 1] = mod(l0 * r1); m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]); } pw(k); long long sum = 0; for (long long i = 0; i < l; ++i) sum += a[i]; cout << dv(ans[sum][0], fp(op, k)) << '\n'; }
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 = 2e6 + 10; const int mod = 1e9 + 7; int a[200], n, k, c, t; struct Matrix { long long m[105][105]; Matrix() { memset(m, 0, sizeof m); }; friend Matrix operator*(const Matrix &a, const Matrix &b) { Matrix temp; for (int i = 0; i <= 100; ++i) for (int j = 0; j <= 100; j++) for (int k = 0; k <= 100; k++) temp.m[i][j] = (temp.m[i][j] + a.m[i][k] * b.m[k][j] % mod + mod) % mod; return temp; } }; Matrix fast(Matrix x, int n) { Matrix ans; for (int i = 0; i <= 100; i++) ans.m[i][i] = 1; while (n) { if (n & 1) ans = ans * x; x = x * x; n >>= 1; } return ans; } long long fast1(long long x, int n) { long long ans = 1; while (n) { if (n & 1) ans = ans * x % mod; x = x * x % mod; n >>= 1; } return ans; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), c += (a[i] == 0); for (int i = 1; i <= c; i++) t += (a[i] == 0); Matrix a; a.m[0][t] = 1; Matrix ans; for (int j = 0; j <= c; j++) { ans.m[j][j] = 1ll * (c * (c - 1) / 2 % mod + (n - c) * (n - c - 1) / 2 % mod + j * (c - j) % mod + (c - j) * (n - 2 * c + j) % mod + mod) % mod; if (j) ans.m[j - 1][j] = 1ll * (c - j + 1) * (c - j + 1) % mod; if (j != c) ans.m[j + 1][j] = 1ll * (j + 1) * (n - 2 * c + j + 1) % mod; } ans = fast(ans, k); a = a * ans; long long ret = 0; for (int i = 0; i <= c; i++) ret = (ret + ans.m[0][i]) % mod; printf("%lld\n", (a.m[0][c] * fast1(ret, mod - 2) + mod) % mod); return 0; }
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") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("unswitch-loops") #pragma GCC optimize("fast-math") #pragma GCC optimize("section-anchors") #pragma GCC optimize("profile-reorder-functions") #pragma GCC optimize("profile-values") #pragma GCC optimize("tracer") #pragma GCC optimize("vpt") #pragma GCC target("sse2") #pragma GCC target("sse3") #pragma GCC target("ssse3") #pragma GCC target("sse4.1") #pragma GCC target("sse4.2") #pragma GCC target("avx") #pragma GCC target("avx2") #pragma GCC target("popcnt") #pragma GCC target("abm") #pragma GCC target("mmx") #pragma GCC target("tune=native") using namespace std; const long long MOD = 1000 * 1000 * 1000 + 7; long long mod(long long n) { if (n < 0) { if (n + MOD >= 0) return n + MOD; else return (n % MOD + MOD) % MOD; } else { if (n < MOD) return n; else if (n < (MOD << 1)) return n - MOD; else return n % MOD; } } long long fp(long long a, long long p) { long long ans = 1, cur = a; for (long long i = 0; (1 << i) <= p; ++i) { if ((p >> i) & 1) ans = mod(ans * cur); cur = mod(cur * cur); } return ans; } long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); } const long long N = 101; long long m[N][N], ans[N][N], t[N][N]; void add(long long a[N][N], long long b[N][N]) { for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { t[i][j] = 0; } } for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { for (long long k = 0; k < N; ++k) { if (!a[i][k] || !b[k][j]) continue; t[i][j] = mod(t[i][j] + a[i][k] * b[k][j]); } } } for (long long i = 0; i < N; ++i) { for (long long j = 0; j < N; ++j) { a[i][j] = t[i][j]; } } } void pw(long long p) { for (long long i = 0; i < N; ++i) ans[i][i] = 1; for (long long i = 0; (1 << i) <= p; ++i) { if ((p >> i) & 1) add(ans, m); add(m, m); } } bool a[N]; long long cnt[2]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; for (long long i = 0; i < n; ++i) { cin >> a[i]; ++cnt[a[i]]; } long long l = cnt[0]; long long r = n - l; long long op = n * (n - 1) / 2; for (long long l1 = 0; l1 <= cnt[0] && l1 <= cnt[1]; ++l1) { long long l0 = l - l1; long long r0 = cnt[0] - l0; long long r1 = cnt[1] - l1; m[l1][l1] = op; if (l1) { m[l1][l1 - 1] = mod(l1 * r0); m[l1][l1] = mod(m[l1][l1] - m[l1][l1 - 1]); } m[l1][l1 + 1] = mod(l0 * r1); m[l1][l1] = mod(m[l1][l1] - m[l1][l1 + 1]); } pw(k); long long sum = 0; for (long long i = 0; i < l; ++i) sum += a[i]; cout << dv(ans[sum][0], fp(op, k)) << '\n'; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int n, m; int a[55], b[55]; int dp[55][55][55]; const int Mod = 998244353; int nii[5555]; int ksmii(int a, int b) { if (!b) { return 1; } int x = ksmii(a, b >> 1); x = 1LL * x * x % Mod; if (b & 1) { x = 1LL * x * a % Mod; } return x; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i < 5555; i++) { nii[i] = ksmii(i, Mod - 2); } for (int i = 1; i <= n; i++) { scanf("%d", a + i); } for (int i = 1; i <= n; i++) { scanf("%d", b + i); } for (int i = 1; i <= n; i++) { memset((int*)dp, 0, sizeof(dp)); int proba = 0, probb = 0; for (int j = 1; j <= n; j++) { if (!a[j]) { probb += b[j]; } else { proba += b[j]; } } dp[0][0][0]++; for (int j = 0; j < m; j++) { for (int l = 0; l <= j; l++) { int m = j - l; for (int n = 0; n < 55; n++) { if (a[i]) { dp[j + 1][l + 1][n + 1] += 1LL * dp[j][l][n] * (n + b[i]) % Mod * nii[proba + probb + l - m] % Mod; if (dp[j + 1][l + 1][n + 1] >= Mod) { dp[j + 1][l + 1][n + 1] -= Mod; } } else { dp[j + 1][l][n + 1] += 1LL * dp[j][l][n] * (b[i] - n) % Mod * nii[proba + probb + l - m] % Mod; if (dp[j + 1][l][n + 1] >= Mod) { dp[j + 1][l][n + 1] -= Mod; } } if (a[i]) { dp[j + 1][l + 1][n] += 1LL * dp[j][l][n] * (proba + l - n - b[i]) % Mod * nii[proba + probb + l - m] % Mod; if (dp[j + 1][l + 1][n] >= Mod) { dp[j + 1][l + 1][n] -= Mod; } dp[j + 1][l][n] += 1LL * dp[j][l][n] * (probb - m) % Mod * nii[proba + probb + l - m] % Mod; if (dp[j + 1][l][n] >= Mod) { dp[j + 1][l][n] -= Mod; } } else { dp[j + 1][l + 1][n] += 1LL * dp[j][l][n] * (proba + l) % Mod * nii[proba + probb + l - m] % Mod; if (dp[j + 1][l + 1][n] >= Mod) { dp[j + 1][l + 1][n] -= Mod; } dp[j + 1][l][n] += 1LL * dp[j][l][n] * (probb - m - b[i] + n) % Mod * nii[proba + probb + l - m] % Mod; if (dp[j + 1][l][n] >= Mod) { dp[j + 1][l][n] -= Mod; } } } } } int all = 0; for (int j = 0; j < 52; j++) { for (int k = 0; k < 52; k++) { all += 1LL * dp[m][j][k] * (a[i] ? b[i] + k : b[i] - k) % Mod; if (all >= Mod) { all -= Mod; } } } printf("%d\n", all); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxm = 3001; const int maxn = 2e6; const long long mod = 998244353; long long add(long long a, long long b) { return (a + b) % mod; } long long sub(long long a, long long b) { return ((a - b) % mod + mod) % mod; } long long mul(long long a, long long b) { return (a * b) % mod; } long long ldp[maxm][maxm], ddp[maxm][maxm], tl, tdl; int n, m, like[maxn], weight[maxn]; long long power(long long a, long long b, long long m) { if (b == 0) return 1; long long tmp = power(a, b / 2, m); tmp = (tmp * tmp) % m; if (b & 1) tmp = (tmp * a) % m; return tmp; } long long disp[10000], st; void preInv() { st = tl + tdl + 5000; for (int i = 0; i < 10000; i++) { if (i > st) break; disp[i] = power(st - i, mod - 2, mod); } } long long inv(long long a) { return disp[st - a]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> like[i]; for (int i = 1; i <= n; i++) { cin >> weight[i]; if (like[i]) tl += weight[i]; else tdl += weight[i]; } preInv(); for (int i = 0; i <= m; i++) { if ((m - i) > tdl) continue; ldp[i][m - i] = 1; ddp[i][m - i] = 1; } for (int i = m - 1; i >= 0; i--) { for (int j = min(m - i - 1LL, tdl); j >= 0; j--) { ldp[i][j] = add(mul(2, mul(ldp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))), add(mul(tl + i - 1, mul(ldp[i + 1][j], inv(tl + i + max(0LL, tdl - j)))), mul(max(0LL, tdl - j), mul(ldp[i][j + 1], inv(tl + i + max(0LL, tdl - j)))))); ddp[i][j] = add(mul(max(0LL, tdl - j - 1), mul(ddp[i][j + 1], inv(tl + i + max(0LL, tdl - j)))), mul(tl + i, mul(ddp[i + 1][j], inv(tl + i + max(0LL, tdl - j))))); } } for (int i = 1; i <= n; i++) { if (like[i]) { cout << mul(weight[i], ldp[0][0]) << "\n"; } else { cout << mul(weight[i], ddp[0][0]) << "\n"; } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long sum_mod(long long a, long long b) { a += b; if (a >= 998244353ll) return a - 998244353ll; return a; } long long mul_mod(long long a, long long b) { return a * b % 998244353ll; } long long sub_mod(long long a, long long b) { if (b > a) return a + 998244353ll - b; return a - b; } long long pow_mod(long long a, long long b) { if (!b) return 1; if (b & 1) return mul_mod(a, pow_mod(a, b - 1)); long long d = pow_mod(a, b / 2); return mul_mod(d, d); } long long div_mod(long long a, long long b) { long long res = mul_mod(a, pow_mod(b, 998244353ll - 2)); return res; } const long long MAXN = 101; long long dp[MAXN][MAXN][MAXN]; long long SA, SB; long long calc_res1(long long w, long long m) { memset(dp, 0, sizeof(dp)); dp[w][0][0] = 1; for (long long sm = 0; sm < m; sm++) { for (long long i = 0; i <= sm; ++i) { for (long long wi = 0; wi + 1 < MAXN; ++wi) { long long j = sm - i; if (dp[wi][i][j] == 0) continue; long long obr = div_mod(1, sum_mod(sum_mod(SA, i), sub_mod(SB, j))); dp[wi + 1][i + 1][j] = sum_mod(dp[wi + 1][i + 1][j], mul_mod(mul_mod(wi, obr), dp[wi][i][j])); dp[wi][i + 1][j] = sum_mod( dp[wi][i + 1][j], mul_mod(mul_mod(sub_mod(sum_mod(SA, i), wi), obr), dp[wi][i][j])); dp[wi][i][j + 1] = sum_mod(dp[wi][i][j + 1], mul_mod(mul_mod(sub_mod(SB, j), obr), dp[wi][i][j])); } } } long long res = 0; for (long long i = 0; i <= m; ++i) { for (long long wi = 0; wi < MAXN; ++wi) { res = sum_mod(res, mul_mod(dp[wi][i][m - i], wi)); } } return res; } long long calc_res2(long long w, long long m) { memset(dp, 0, sizeof(dp)); dp[w][0][0] = 1; for (long long sm = 0; sm < m; sm++) { for (long long i = 0; i <= sm; ++i) { for (long long wi = 1; wi < MAXN; wi++) { long long j = sm - i; if (dp[wi][i][j] == 0) continue; long long obr = div_mod(1, sum_mod(sum_mod(SA, i), sub_mod(SB, j))); dp[wi - 1][i][j + 1] = sum_mod(dp[wi - 1][i][j + 1], mul_mod(mul_mod(wi, obr), dp[wi][i][j])); dp[wi][i][j + 1] = sum_mod( dp[wi][i][j + 1], mul_mod(mul_mod(sub_mod(sub_mod(SB, j), wi), obr), dp[wi][i][j])); dp[wi][i + 1][j] = sum_mod(dp[wi][i + 1][j], mul_mod(mul_mod(sum_mod(SA, i), obr), dp[wi][i][j])); } } } long long res = 0; for (long long i = 0; i <= m; ++i) { for (long long wi = 0; wi < MAXN; ++wi) { res = sum_mod(res, mul_mod(dp[wi][i][m - i], wi)); } } return res; } void Solve() { long long n, m; cin >> n >> m; vector<long long> a(n); for (signed i = 0; i < (n); i++) cin >> a[i]; ; vector<long long> w(n); for (signed i = 0; i < (n); i++) cin >> w[i]; ; SA = 0, SB = 0; for (long long i = 0; i < n; ++i) { if (a[i]) SA += w[i]; else SB += w[i]; } for (signed i = 0; i < (n); i++) { long long res = (a[i] ? calc_res1(w[i], m) : calc_res2(w[i], m)); cout << res << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); Solve(); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; pair<int, int> v[55]; int dp[2][105][55][55], inv[3005]; int liked, disliked; int exp(int a, int b) { int r = 1; while (b) { if (b & 1) r = ((long long)r * a) % MOD; b /= 2; a = ((long long)a * a) % MOD; } return r; } inline void add(int& a, int b) { a += b; if (a >= MOD) a -= MOD; } inline int mul(int a, int b) { return ((long long)a * b) % MOD; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &v[i].first); for (int i = 0; i < n; i++) { scanf("%d", &v[i].second); liked += v[i].first * v[i].second; disliked += (!v[i].first) * v[i].second; } for (int i = 0; i <= 3000; i++) inv[i] = exp(i, MOD - 2); for (int cur = 0; cur < n; cur++) { int c = v[cur].first; memset(dp, 0, sizeof dp); dp[c][v[cur].second][0][0] = 1; if (c) { for (int w = 1; w < 100; w++) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[c][w][i + 1][j + 1], mul(mul(dp[c][w][i][j], (liked + j - w)), aux)); add(dp[c][w][i + 1][j], mul(mul(dp[c][w][i][j], (disliked - k)), aux)); add(dp[c][w + 1][i + 1][j + 1], mul(mul(dp[c][w][i][j], w), aux)); } } else { for (int w = 100; w > 0; w--) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[c][w][i + 1][j], mul(mul(dp[c][w][i][j], (disliked - k - w)), aux)); add(dp[c][w][i + 1][j + 1], mul(mul(dp[c][w][i][j], (liked + j)), aux)); add(dp[c][w - 1][i + 1][j], mul(mul(dp[c][w][i][j], w), aux)); } } int ans = 0; for (int w = 0; w <= 100; w++) { for (int j = 0; j <= m; j++) add(ans, mul(w, dp[c][w][m][j])); } printf("%d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int MAXM = 3010; const int MOD = 998244353; int qpow(int a, int b) { int base = 1; while (b) { if (b & 1) base = 1ll * base * a % MOD; a = 1ll * a * a % MOD; b >>= 1; } return base; } int n, m, A[MAXN], W[MAXN], F[MAXM][MAXM], G[MAXM][MAXM], Inv[MAXM << 1], sum[10]; namespace io { inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } char buf[1 << 21]; inline void write(int x) { if (x == 0) { putchar('0'); return; } int tmp = x < 0 ? -x : x; if (x < 0) putchar('-'); int cnt = 0; while (tmp > 0) { buf[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0) putchar(buf[--cnt]); } } // namespace io using namespace io; int main() { n = read(), m = read(); for (register int i = 1; i <= n; ++i) A[i] = read(); for (register int i = 1; i <= n; ++i) W[i] = read(), sum[A[i]] += W[i], sum[2] += W[i]; for (register int i = 0 > m - sum[0] ? 0 : m - sum[0]; i <= 2 * m; ++i) Inv[i] = qpow(sum[2] + i - m, MOD - 2); for (register int i = m; i >= 0; --i) { F[i][m - i] = G[i][m - i] = 1; for (register int j = m - i - 1 < sum[0] ? m - i - 1 : sum[0]; j >= 0; --j) { F[i][j] = (1ll * (sum[1] + i + 1) * F[i + 1][j] + 1ll * (sum[0] - j) * F[i][j + 1]) % MOD * Inv[i - j + m] % MOD; G[i][j] = (1ll * (sum[1] + i) * G[i + 1][j] + 1ll * (sum[0] - j - 1) * G[i][j + 1]) % MOD * Inv[i - j + m] % MOD; } } for (register int i = 1; i <= n; ++i) write(int(1ll * W[i] * (A[i] ? F[0][0] : G[0][0]) % MOD)), putchar('\n'); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, tot, dep; long long ny(long long x) { return x == 1 ? 1 : ny(998244353 % x) * (998244353 - 998244353 / x) % 998244353; } long long dp[60][60][60], a[55], w[55], f[10]; int main() { cin >> n >> m; for (int i = (1); i <= (n); i++) cin >> a[i]; for (int i = (1); i <= (n); i++) { cin >> w[i]; f[a[i]] += w[i]; } for (int i = (1); i <= (n); i++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int j = (1); j <= (m); j++) { for (int x = (0); x <= (j - 1); x++) for (int y = (0); y <= (j - 1); y++) { long long ww = f[0] + f[1] + y - (j - 1 - y), wi = w[i] + (a[i] ? x : -x); if (ww <= 0) continue; if (wi > 0) (dp[j][x + 1][y + a[i]] += dp[j - 1][x][y] * wi % 998244353 * ny(ww) % 998244353) %= 998244353; (dp[j][x][y + 1] += dp[j - 1][x][y] * (f[1] + y - (a[i] ? wi : 0)) % 998244353 * ny(ww) % 998244353) %= 998244353; if (f[0] - (j - 1 - y) - (a[i] ? 0 : wi) > 0) (dp[j][x][y] += dp[j - 1][x][y] * (f[0] - (j - 1 - y) - (a[i] ? 0 : wi)) % 998244353 * ny(ww) % 998244353) %= 998244353; } } long long ans = 0; for (int x = (0); x <= (m); x++) for (int y = (0); y <= (m); y++) if (f[0] >= m - y && (a[i] || (w[i] + (a[i] ? x : -x)) > 0)) (ans += dp[m][x][y] * (w[i] + (a[i] ? x : -x)) % 998244353) %= 998244353; cout << ans << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 55; const int mod = 998244353; int like[maxn]; long long dp[maxn * 2][maxn][maxn][2]; int wi[maxn]; int n, m; long long quick(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } long long inv(long long a) { return quick(a, mod - 2); } bool cur = 0; int suma = 0, sumb = 0; long long dfs(int w, int k, int j) { if (k + j == m) return w; if (w <= 0) return 0; if (dp[w][k][j][cur]) return dp[w][k][j][cur]; long long &ret = dp[w][k][j][cur]; long long ud = inv(suma + k + sumb - j); long long curk = k + suma, curj = -j + sumb; if (cur) { ret = 1LL * w * ud % mod * dfs(w + 1, k + 1, j) % mod; ret = (ret + 1LL * (curk - w + mod) * ud % mod * dfs(w, k + 1, j) % mod) % mod; ret = (ret + curj * ud % mod * dfs(w, k, j + 1) % mod) % mod; } else { ret = 1LL * w * ud % mod * dfs(w - 1, k, j + 1) % mod; ret = (ret + 1LL * (curj - w + mod) * ud % mod * dfs(w, k, j + 1) % mod) % mod; ret = (ret + 1LL * curk * ud % mod * dfs(w, k + 1, j) % mod) % mod; }; return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &like[i]); for (int i = 0; i < n; i++) { scanf("%d", &wi[i]); if (like[i]) suma += wi[i]; else sumb += wi[i]; }; for (int i = 0; i < n; i++) { cur = like[i]; printf("%lld\n", dfs(wi[i], 0, 0)); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } const long long Mod = 998244353; const int N = 200007; const int M = 3005; long long A[N], W[N], n, m, a, b, X, Y, dp[M][M]; long long power(long long x, long long y) { if (y == 0) return 1ll; else { long long a = (power(x, y / 2)); long long b = (a * a) % Mod; if (y & 1) return (b * x) % Mod; else return b; } } long long add(long long a, long long b) { return (a + b) % Mod; } long long mult(long long a, long long b) { return (a * b) % Mod; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> A[i]; for (int i = 1; i <= n; i++) { cin >> W[i]; if (A[i]) X += W[i]; else Y += W[i]; } dp[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { long long a = X + j, b = Y - (i - j); long long inv = power(a + b, Mod - 2); dp[i + 1][j + 1] = add(dp[i + 1][j + 1], mult(dp[i][j], mult(a, inv))); dp[i + 1][j] = add(dp[i + 1][j], mult(dp[i][j], mult(b, inv))); } } long long ea = 0, eb = 0; for (int i = 0; i <= m; i++) { ea = (ea + mult(dp[m][i], X + i)) % Mod; eb = (eb + mult(dp[m][i], Y - (m - i))) % Mod; } a = power(X, Mod - 2); b = power(Y, Mod - 2); for (int i = 1; i <= n; i++) { if (A[i]) cout << mult(W[i], mult(ea, a)) << endl; else cout << mult(W[i], mult(eb, b)) << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int dp[102][55][55][55][2]; long long w[55]; long long a[55]; long long sa, sb; long long n, m; const long long MOD = 998244353; long long funct(long long x) { long long answer = 1, current = x; long long p = MOD - 2; while (p != 0) { if (p % 2 != 0) { answer = (answer * current) % MOD; } p /= 2; current = (current * current) % MOD; } return answer; } long long solve(long long w, long long x, long long y, long long pos, long long u) { if (pos == m) return w; if (w == 0) return 0; if (dp[w][x][y][pos][u] != -1) return dp[w][x][y][pos][u]; long long ans = 0; long long sumBad = -y + sb; long long sumGood = x + sa; if (u == 1) sumGood -= w; if (u == 0) sumBad -= w; if (sumBad != 0) ans += (((sumBad * funct(sumBad + sumGood + w)) % MOD) * solve(w, x, y + 1, pos + 1, u)) % MOD; if (sumGood != 0) ans += (((sumGood * funct(sumBad + sumGood + w)) % MOD) * solve(w, x + 1, y, pos + 1, u)) % MOD; if (w != 0) { if (u == 0) { ans += (((w * funct(sumGood + sumBad + w)) % MOD) * solve(w - 1, x, y + 1, pos + 1, u)) % MOD; } if (u == 1) { ans += (((w * funct(sumGood + sumBad + w)) % MOD) * solve(w + 1, x + 1, y, pos + 1, u)) % MOD; } } ans %= MOD; dp[w][x][y][pos][u] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; long long i, j; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) { cin >> w[i]; sa += a[i] * w[i]; sb += (1 - a[i]) * w[i]; } memset(dp, -1, sizeof(dp)); for (i = 1; i <= n; i++) { cout << solve(w[i], 0, 0, 0, a[i]) << endl; } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> const int64_t mod = 998244353; int gcd(int64_t a, int64_t b) { while (a != 0 && b != 0) { if (a >= b) a = a % b; else b = b % a; } return a + b; } struct Fraction { int64_t numerator; int64_t devider; Fraction(int64_t n = 0, int64_t d = 1) : numerator(n), devider(d){}; const Fraction& operator+(const Fraction& rv) { if (rv.devider == devider) { numerator += rv.numerator; int64_t g = gcd(numerator, devider); numerator /= g; devider /= g; numerator %= mod; return *this; } else { numerator *= rv.devider; numerator %= mod; numerator += rv.numerator * devider; numerator %= mod; devider *= rv.devider; int64_t g = gcd(numerator, devider); numerator /= g; devider /= g; devider %= mod; return *this; } } const Fraction& operator*(const Fraction& rv) { numerator *= rv.numerator; devider *= rv.devider; int64_t g = gcd(numerator, devider); numerator /= g; devider /= g; numerator %= mod; devider %= mod; return *this; } }; struct Item { Item(Fraction f, int64_t s) : possibility(f), sum(s){}; Fraction possibility; int64_t sum; }; void extended_euclid(int64_t a, int64_t b, int64_t& x, int64_t& y, int64_t& d) { int64_t q, r, x1, x2, y1, y2; if (b == 0) { d = a, x = 1, y = 0; return; } x2 = 1, x1 = 0, y2 = 0, y1 = 1; while (b > 0) { q = a / b, r = a - q * b; x = x2 - q * x1, y = y2 - q * y1; a = b, b = r; x2 = x1, x1 = x, y2 = y1, y1 = y; } d = a, x = x2, y = y2; } int main() { int64_t n, m; std::cin >> n >> m; std::vector<std::pair<int64_t, bool>> data; int64_t posWeight = 0, negWeight = 0; for (int64_t i = 0; i < n; ++i) { int64_t a; std::cin >> a; if (a == 0) data.push_back(std::make_pair(-1, false)); else data.push_back(std::make_pair(-1, true)); } for (auto& i : data) { int64_t w; std::cin >> w; i.first = w; if (i.second) posWeight += w; else negWeight += w; } std::map<int64_t, Item> posMap{ std::make_pair(posWeight, Item(Fraction(1, 1), posWeight + negWeight))}, negMap{std::make_pair(negWeight, Item(Fraction(1, 1), posWeight + negWeight))}; for (int64_t i = 0; i < m; ++i) { std::map<int64_t, Item> newPos, newNeg; for (auto j : posMap) { if (newPos.find(j.first + 1) == newPos.end()) { Fraction f(j.first, j.second.sum); f = f * j.second.possibility; newPos.insert(std::make_pair(j.first + 1, Item(f, j.second.sum + 1))); } else { Fraction f(j.first, j.second.sum); f = f * j.second.possibility; newPos.at(j.first + 1).possibility = newPos.at(j.first + 1).possibility + f; } if (newPos.find(j.first) == newPos.end() && j.second.sum > j.first) { Fraction f(j.second.sum - j.first, j.second.sum); f = f * j.second.possibility; newPos.insert(std::make_pair(j.first, Item(f, j.second.sum - 1))); } else if (j.second.sum > j.first) { Fraction f(j.second.sum - j.first, j.second.sum); f = f * j.second.possibility; newPos.at(j.first).possibility = newPos.at(j.first).possibility + f; } } for (auto j : negMap) { if (newNeg.find(j.first - 1) == newNeg.end() && j.first - 1 > 0) { Fraction f(j.first, j.second.sum); f = f * j.second.possibility; newNeg.insert(std::make_pair(j.first - 1, Item(f, j.second.sum - 1))); } else if (j.first - 1 > 0) { Fraction f(j.first, j.second.sum); f = f * j.second.possibility; newNeg.at(j.first - 1).possibility = newNeg.at(j.first - 1).possibility + f; } if (newNeg.find(j.first) == newNeg.end()) { Fraction f(j.second.sum - j.first, j.second.sum); f = f * j.second.possibility; newNeg.insert(std::make_pair(j.first, Item(f, j.second.sum + 1))); } else { Fraction f(j.second.sum - j.first, j.second.sum); f = f * j.second.possibility; newNeg.at(j.first).possibility = newNeg.at(j.first).possibility + f; } } posMap = newPos; negMap = newNeg; } Fraction pos, neg; for (auto i : posMap) pos = pos + Fraction(i.first, 1) * i.second.possibility; for (auto i : negMap) neg = neg + Fraction(i.first, 1) * i.second.possibility; for (auto i : data) { Fraction res; if (i.second) res = Fraction(i.first, posWeight) * pos; else res = Fraction(i.first, negWeight) * neg; int64_t d, x, y; extended_euclid(res.devider, mod, x, y, d); std::cout << (((x % mod + mod) % mod) * res.numerator) % mod << std::endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> const int MOD = 998244353; const int kN = 50 + 5; int n, m; int a[kN], w[kN]; int dp[kN][kN][kN]; const int kM = kN * kN * 20; int Inv[kM]; int inv(int x) { return x == 1 ? 1 : (MOD - MOD / x) * 1LL * inv(MOD % x) % MOD; } inline void add(int &a, int b) { a += b; if (a >= MOD) a -= MOD; } int main() { scanf("%d%d", &n, &m); Inv[1] = 1; for (int i = 2; i < kM; ++i) Inv[i] = inv(i); for (int i = 0; i < n; ++i) scanf("%d", a + i); for (int i = 0; i < n; ++i) scanf("%d", w + i); int negative = 0; int sum = 0; for (int i = 0; i < n; ++i) if (a[i] == 0) negative += w[i]; for (int i = 0; i < n; ++i) sum += w[i]; for (int i = 0; i < n; ++i) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int j = 0; j < m; ++j) { for (int k = 0; k <= j; ++k) { for (int r = 0; r <= j; ++r) if (dp[j][k][r]) { int c = a[i] == 1 ? w[i] + k : w[i] - k; if (c) { add(dp[j + 1][k + 1][r + (a[i] == 0)], dp[j][k][r] * 1LL * c % MOD * Inv[sum - r + j - r] % MOD); } int ne_ci = negative - (a[i] == 0 ? w[i] : 0); int sum_ci = sum - w[i]; int r_ci = r - (a[i] == 0 ? k : 0); int f_ci = j - r - (a[i] == 1 ? k : 0); if (ne_ci < sum_ci) { add(dp[j + 1][k][r], dp[j][k][r] * 1LL * (sum_ci - ne_ci + f_ci) % MOD * Inv[sum - r + j - r] % MOD); } if (ne_ci > r_ci) { add(dp[j + 1][k][r + 1], dp[j][k][r] * 1LL * (ne_ci - r_ci) % MOD * Inv[sum - r + j - r] % MOD); } } } } int result = w[i]; for (int k = 0; k <= m; ++k) for (int r = 0; r <= m; ++r) if (dp[m][k][r]) add(result, dp[m][k][r] * 1LL * (a[i] == 1 ? k : (MOD - k)) % MOD); printf("%d\n", result); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } template <typename T> void vec_print(vector<T> VEC) { for (long long i = (long long)0; i < (long long)VEC.size(); ++i) { cout << VEC[i] << " "; } cout << "\n"; }; template <typename T> void mat_print(vector<vector<T>> MAT) { for (long long i = (long long)0; i < (long long)MAT.size(); ++i) { for (long long j = (long long)0; j < (long long)MAT[i].size(); ++j) { cout << MAT[i][j] << " "; } cout << "\n"; } }; template <typename CLASS1, typename CLASS2> class HOGE { public: CLASS1 key; CLASS2 value; HOGE(void) { return; }; HOGE(CLASS1 key, CLASS2 value) { this->key = key; this->value = value; }; ~HOGE(void) { return; }; void print(void) { cout << "key : " << key << ", value : " << value << "\n"; return; }; bool operator==(const HOGE &obj) { return (this->value == obj.value); }; bool operator<(const HOGE &obj) { return (this->value < obj.value); }; bool operator>(const HOGE &obj) { return (this->value > obj.value); }; }; template <typename CLASS1, typename CLASS2> bool operator==(const HOGE<CLASS1, CLASS2> &hoge1, const HOGE<CLASS1, CLASS2> &hoge2) { return hoge1.value == hoge2.value; }; template <typename CLASS1, typename CLASS2> bool operator<(const HOGE<CLASS1, CLASS2> &hoge1, const HOGE<CLASS1, CLASS2> &hoge2) { return hoge1.value < hoge2.value; }; template <typename CLASS1, typename CLASS2> bool operator>(const HOGE<CLASS1, CLASS2> &hoge1, const HOGE<CLASS1, CLASS2> &hoge2) { return hoge1.value > hoge2.value; }; constexpr int INF = (1 << 30); constexpr long long INFLL = 1LL << 62; constexpr long double EPS = 1e-12; constexpr long long MOD = (long long)(998244353); vector<long long> inv(10000); void initialize_inv(void) { inv[1] = 1; for (int i = 2; i < 10000; ++i) { inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; } return; } vector<vector<long long>> dp_all(2557, vector<long long>(2557, 0)); set<pair<long long, long long>> st_all; void calc_all(long long wp, long long wm, long long m) { vector<map<pair<long long, long long>, long long>> vmp(m + 1); vmp[0][make_pair(wp, wm)] += 1; for (long long M = (long long)0; M < (long long)m; ++M) { for (auto itr : vmp[M]) { long long wp_tmp = itr.first.first; long long wm_tmp = itr.first.second; long long dp_tmp = itr.second; long long inv_sum = inv[wp_tmp + wm_tmp]; vmp[M + 1][make_pair(wp_tmp + 1, wm_tmp)] += (dp_tmp * ((wp_tmp * inv_sum) % MOD)) % MOD; vmp[M + 1][make_pair(wp_tmp + 1, wm_tmp)] %= MOD; if (wm_tmp - 1 >= 0) { vmp[M + 1][make_pair(wp_tmp, wm_tmp - 1)] += (dp_tmp * ((wm_tmp * inv_sum) % MOD)) % MOD; vmp[M + 1][make_pair(wp_tmp, wm_tmp - 1)] %= MOD; } } } for (auto itr : vmp[m]) { long long wp_tmp = itr.first.first; long long wm_tmp = itr.first.second; dp_all[wp_tmp][wm_tmp] = itr.second; pair<long long, long long> st_tmp = make_pair(wp_tmp, wm_tmp); st_all.emplace(st_tmp); } return; } long long calc_p(long long w, long long wp, long long m) { vector<vector<long long>> dp(107, vector<long long>(3000, 0)); dp[w][wp] = 1; set<pair<long long, long long>> st; pair<long long, long long> st_tmp = make_pair(w, wp); st.emplace(st_tmp); for (long long M = (long long)0; M < (long long)m; ++M) { set<pair<long long, long long>> st_new; for (auto itr : st) { long long w_tmp = itr.first; long long wp_tmp = itr.second; long long inv_sum = inv[w_tmp + wp_tmp]; dp[w_tmp + 1][wp_tmp] += (dp[w_tmp][wp_tmp] * ((w_tmp * inv_sum) % MOD)) % MOD; dp[w_tmp + 1][wp_tmp] %= MOD; pair<long long, long long> st_tmp = make_pair(w_tmp + 1, wp_tmp); st_new.emplace(st_tmp); dp[w_tmp][wp_tmp + 1] += (dp[w_tmp][wp_tmp] * ((wp_tmp * inv_sum) % MOD)) % MOD; dp[w_tmp][wp_tmp + 1] %= MOD; st_tmp = make_pair(w_tmp, wp_tmp + 1); st_new.emplace(st_tmp); } st = st_new; } long long ans = 0; for (auto itr : st_all) { long long wp_tmp = itr.first; long long wm_tmp = itr.second; long long k = wp_tmp - w - wp; long long count = 0; for (int i = 0; i <= k && wp_tmp - w - i >= 0; ++i) { ans += ((w + i) * ((dp_all[wp_tmp][wm_tmp] * dp[w + i][wp_tmp - w - i]) % MOD)) % MOD; ans %= MOD; count++; } } return ans; } long long calc_m(long long w, long long wm, long long m) { vector<vector<long long>> dp(107, vector<long long>(3000, 0)); dp[w][wm] = 1; set<pair<long long, long long>> st; pair<long long, long long> st_tmp = make_pair(w, wm); st.emplace(st_tmp); for (long long M = (long long)0; M < (long long)m; ++M) { set<pair<long long, long long>> st_new; for (auto itr : st) { long long w_tmp = itr.first; long long wm_tmp = itr.second; long long inv_sum = inv[w_tmp + wm_tmp]; if (w_tmp - 1 >= 0) { dp[w_tmp - 1][wm_tmp] += (dp[w_tmp][wm_tmp] * ((w_tmp * inv_sum) % MOD)) % MOD; dp[w_tmp - 1][wm_tmp] %= MOD; pair<long long, long long> st_tmp = make_pair(w_tmp - 1, wm_tmp); st_new.emplace(st_tmp); } if (wm_tmp - 1 >= 0) { dp[w_tmp][wm_tmp - 1] += (dp[w_tmp][wm_tmp] * ((wm_tmp * inv_sum) % MOD)) % MOD; dp[w_tmp][wm_tmp - 1] %= MOD; st_tmp = make_pair(w_tmp, wm_tmp - 1); st_new.emplace(st_tmp); } } st = st_new; } long long ans = 0; for (auto itr : st_all) { long long wp_tmp = itr.first; long long wm_tmp = itr.second; long long k = w + wm - wm_tmp; for (int i = 0; i <= k && w - i > 0; ++i) { if (wm_tmp - w + i >= 0) { ans += ((w - i) * ((dp_all[wp_tmp][wm_tmp] * dp[w - i][wm_tmp - w + i]) % MOD)) % MOD; ans %= MOD; } } } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); initialize_inv(); long long n, m; IN(n, m); vector<long long> a(n); for (long long i = (long long)0; i < (long long)n; ++i) IN(a[i]); vector<long long> w(n); long long wp_sum = 0, wm_sum = 0; for (long long i = (long long)0; i < (long long)n; ++i) { IN(w[i]); if (a[i] == 0) wm_sum += w[i]; else wp_sum += w[i]; } calc_all(wp_sum, wm_sum, m); for (long long i = (long long)0; i < (long long)n; ++i) { if (a[i] == 0) { OUT(calc_m(w[i], wm_sum - w[i], m)); } else { OUT(calc_p(w[i], wp_sum - w[i], m)); } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long lh[100005]; long long w[100005]; long long n, M; long long sumw = 0, suml = 0, sumh = 0; long long dp[55][55][55]; long long md = 998244353; long long modInverse(long long a, long long m) { if (a == 0) return 0; long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x = (m0 + x % m0); return x; } long long d(int i, int j, int k, int l) { long long o = (M + (md - (i + k) % md) % md) % md; if (dp[i][j][k] != -1) return dp[i][j][k]; if (i == 0) return dp[i][j][k] = 0; if (lh[l]) { long long p = (((w[l] + j) % md) * modInverse(((sumw + (k + (md - o) % md) % md) % md) % md, md)) % md; long long p2 = (((suml + (k + (md - (w[l] + j) % md) % md) % md) % md) * modInverse( (sumw + (k + (md - (o + ((w[l] + j) % md) % md) % md) % md) % md) % md, md)) % md; long long x = (1 + (md - p) % md) % md; long long xx = (x * (1 + (md - p2) % md) % md) % md; return dp[i][j][k] = ((((d(i - 1, j + 1, k + 1, l) + 1) % md) * p) % md + ((((d(i - 1, j, k + 1, l) * x) % md) * p2) % md + ((d(i - 1, j, k, l) * xx) % md) % md) % md) % md; } else { long long p = (((w[l] + (md - j) % md) % md) * modInverse((sumw + (k + (md - o) % md) % md) % md, md)) % md; long long p2 = (((sumh + md - (o + (w[l] + md - j) % md) % md) % md) * modInverse( (sumw + (k + md - (o + (w[l] + md - j) % md) % md) % md) % md, md)) % md; long long x = (1 + (md - p) % md) % md; long long xx = (x * (1 + (md - p2) % md) % md) % md; return dp[i][j][k] = ((((d(i - 1, j + 1, k, l) + (md - 1) % md) % md) * p) % md + ((((d(i - 1, j, k, l) * x) % md) * p2) % md + ((d(i - 1, j, k + 1, l) * xx) % md) % md) % md) % md; } } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> M; for (int i = 0; i < n; i++) { cin >> lh[i]; } for (int i = 0; i < n; i++) { cin >> w[i]; sumw = (sumw + w[i]) % md; if (lh[i]) suml = (suml + w[i]) % md; else sumh = (sumh + w[i]) % md; } for (int l = 0; l < n; l++) { for (int i = 0; i < 55; i++) { for (int j = 0; j < 55; j++) { for (int k = 0; k < 55; k++) { dp[i][j][k] = -1; } } } cout << (w[l] + d(M, 0, 0, l)) % md << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; void file(bool opt) { if (opt && fopen("in.txt", "r")) { freopen("in.txt", "r", stdin); } } const long long mod = 998244353; long long dp[2][3000 + 5][3000 + 5]; long long sum[2]; long long a[200000 + 5]; long long inv[6000 + 5]; bool check[200000 + 5]; long long poww(long long a, long long b) { long long res = 1; a %= mod; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &check[i]); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), sum[check[i]] += a[i]; for (int i = max(0ll, m - sum[0]); i <= 2 * m; i++) inv[i] = poww(sum[0] + sum[1] + i - m, mod - 2); for (int i = m; i >= 0; i--) { dp[0][i][m - i] = dp[1][i][m - i] = 1; for (int j = min((long long)m - i - 1, sum[0]); j >= 0; j--) { dp[1][i][j] = (dp[1][i + 1][j] * (long long)(sum[1] + i + 1) % mod + dp[1][i][j + 1] * (long long)(sum[0] - j) % mod) * inv[i - j + m] % mod; dp[0][i][j] = (dp[0][i + 1][j] * (long long)(sum[1] + i) % mod + dp[0][i][j + 1] * (long long)(sum[0] - j - 1) % mod) * inv[i - j + m] % mod; } } for (int i = 1; i <= n; i++) printf("%lld\n", a[i] * dp[check[i]][0][0] % mod); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int put(int a, int p) { int ans = 1; while (p) { if (p & 1) ans = 1LL * ans * a % 998244353; a = 1LL * a * a % 998244353; p /= 2; } return ans; } int invmod(int x) { return put(x, 998244353 - 2); } void add(int& a, int b) { a += b; if (a >= 998244353) a -= 998244353; } const int MMAX = 3010; const int NMAX = 200010; int w[NMAX], like[NMAX]; int n, m, s, splus, sminus; int dp[MMAX][MMAX]; int serieplus[NMAX]; int serieminus[NMAX]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", like + i); for (int i = 1; i <= n; i++) { scanf("%d", w + i); s += w[i]; if (like[i]) splus += w[i]; else sminus += w[i]; } dp[0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= i; j++) { int nrplus = splus + j; int nrminus = sminus - (i - j); if (nrminus < 0) assert(dp[i][j] == 0); int nrtot = nrplus + nrminus; int inv = invmod(nrtot); add(dp[i + 1][j], 1LL * dp[i][j] * nrminus % 998244353 * inv % 998244353); add(dp[i + 1][j + 1], 1LL * dp[i][j] * nrplus % 998244353 * inv % 998244353); } } int mult_plus = 0, mult_minus = 0; serieplus[0] = 1; for (int i = 1; i <= m; i++) serieplus[i] = 1LL * serieplus[i - 1] * (1 + invmod(splus + i - 1)) % 998244353; serieminus[0] = 1; for (int i = 1; i <= min(m, sminus); i++) serieminus[i] = 1LL * serieminus[i - 1] * (1 - invmod(sminus - i + 1) + 998244353) % 998244353; for (int i = 0; i <= m; i++) { add(mult_plus, 1LL * dp[m][i] * serieplus[i] % 998244353); add(mult_minus, 1LL * dp[m][m - i] * serieminus[i] % 998244353); } for (int i = 1; i <= n; i++) { int ans = 1LL * w[i] * (like[i] ? mult_plus : mult_minus) % 998244353; if (ans < 0) ans += 998244353; printf("%d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, mod = 998244353, M = 3e3 + 5; int n, m; long long a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; int ksm(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1LL * ans * x % mod; x = 1LL * x * x % mod; y >>= 1; } return ans; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (int i = 1; i <= n; i++) { scanf("%I64d", &w[i]); sum[a[i]] += w[i]; sum[2] += w[i]; } for (int i = 0; i <= 2 * m; i++) inv[i] = ksm(sum[2] + i - m, mod - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, (int)sum[0]); j >= 0; j--) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (int i = 1; i <= n; i++) cout << w[i] * (a[i] ? f[0][0] : g[0][0]) % mod << endl; return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long read() { int f = 1; long long res = 0; char ch; do { ch = getchar(); if (ch == '-') f = -f; } while (ch < '0' || ch > '9'); do { res = res * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); return f == 1 ? res : -res; } void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int N = 200010; const int M = 3010; const int mod = 998244353; const long long INF = 1e18; int n, m; int A[N], B[N]; char str[N]; int head[N], to[N * 2], nxt[N * 2], tot; void addEdge(int u, int v) { tot++; nxt[tot] = head[u]; to[tot] = v; head[u] = tot; } template <class T> T mmax(T a, T b) { return a < b ? b : a; } template <class T> T mmin(T a, T b) { return a < b ? a : b; } int countOne(long long set) { int res = 0; while (set) { res++; set &= set - 1; } return res; } bool contain(long long set, int i) { return (set & (1LL << i)) > 0; } long long myPow(long long a, int p) { if (p == 0) return 1; long long res = myPow(a, p / 2); res *= res; res %= mod; if (p % 2 == 1) { res *= a; res %= mod; } return res; } void addMode(long long &a, long long b) { a = (a + b) % mod; } long long mul(long long a, long long b) { return a * b % mod; } template <class T> void mySwap(T &a, T &b) { T tmp = a; a = b; b = tmp; } long long sum[2]; long long inv[M * 2]; long long f[M][M]; long long g[M][M]; int main() { fast_io(); cin >> n >> m; for (int i = 0; i < n; i++) cin >> A[i]; for (int i = 0; i < n; i++) { cin >> B[i]; sum[A[i]] += B[i]; } for (int i = mmax(0LL, m - sum[0] - sum[1]); i <= 2 * m; i++) { inv[i] = myPow(sum[0] + sum[1] - m + i, mod - 2); } for (int i = m; i >= 0; i--) { g[i][m - i] = f[i][m - i] = 1; for (int j = mmin((long long)m - i - 1, sum[0]); j >= 0; j--) { f[i][j] = (f[i + 1][j] * (sum[1] + i + 1) + f[i][j + 1] * (sum[0] - j)) % mod * inv[m + i - j] % mod; g[i][j] = (g[i + 1][j] * (sum[1] + i) + g[i][j + 1] * (sum[0] - j - 1)) % mod * inv[m + i - j] % mod; } } for (int i = 0; i < n; i++) { cout << (A[i] ? f[0][0] : g[0][0]) * B[i] % mod << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int mxn = 262144, md = 998244353; int rd() { int x = 0, flg = 1; char c = getchar(); for (; (c < 48 || c > 57) && c != '-'; c = getchar()) ; if (c == '-') flg = -1, c = getchar(); for (; c > 47 && c < 58; x = x * 10 + c - 48, c = getchar()) ; return x * flg; } int power(int x, int p, int num = 1) { for (; p; p >>= 1, x = 1ll * x * x % md) if (p & 1) num = 1ll * num * x % md; return num; } int n, m, sa, sb, tg[64], f[2][64][64][128], inv[16384]; int main() { n = rd(), m = rd(); for (int i = 1; i <= n; ++i) tg[i] = rd(); for (int i = 1, x; i <= n; ++i) x = rd(), f[0][i][0][x] = 1, sa += tg[i] * x, sb += (!tg[i]) * x; bool cur = 0; for (int t = 0; t < m; ++t) { cur ^= 1; memset(f[cur], 0, sizeof(f[cur])); for (int i = 1; i <= n; ++i) if (tg[i]) for (int j = 0; j <= t; ++j) { int inv = power(sa + sb - t + 2 * j, md - 2); for (int k = 0; k <= 100; ++k) if (f[cur ^ 1][i][j][k]) { f[cur][i][j + 1][k + 1] = (f[cur][i][j + 1][k + 1] + 1ll * f[cur ^ 1][i][j][k] * k % md * inv) % md; f[cur][i][j + 1][k] = (f[cur][i][j + 1][k] + 1ll * f[cur ^ 1][i][j][k] * (sa + j - k) % md * inv) % md; f[cur][i][j][k] = (f[cur][i][j][k] + 1ll * f[cur ^ 1][i][j][k] * (sb - t + j) % md * inv) % md; } } else for (int j = 0; j <= t; ++j) { int inv = power(sa + sb - t + 2 * j, md - 2); for (int k = 0; k <= 100; ++k) if (f[cur ^ 1][i][j][k]) { f[cur][i][j][k - 1] = (f[cur][i][j][k - 1] + 1ll * f[cur ^ 1][i][j][k] * k % md * inv) % md; f[cur][i][j][k] = (f[cur][i][j][k] + 1ll * f[cur ^ 1][i][j][k] * (sb - t + j - k) % md * inv) % md; f[cur][i][j + 1][k] = (f[cur][i][j + 1][k] + 1ll * f[cur ^ 1][i][j][k] * (sa + j) % md * inv) % md; } } } for (int i = 1; i <= n; ++i) { int ans = 0; for (int j = 0; j <= m; ++j) for (int k = 0; k <= 100; ++k) ans = (ans + 1ll * f[cur][i][j][k] * k) % md; printf("%d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long INF = INT_MAX; const long long MOD = 998244353; long long extgcd(long long a, long long b, long long &x, long long &y) { long long g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } long long invMod(long long a, long long m) { long long x, y; if (extgcd(a, m, x, y) == 1) return (x + m) % m; else return 0; } long long N, M; bool a[60]; long long w[60]; long long inc, decr; map<tuple<long long, long long, long long, long long>, long long> mp; long long dfs(long long i, long long j, long long k, long long in, long long de) { if (mp.count(make_tuple(j, k, in, de))) return mp[make_tuple(j, k, in, de)]; if (j == M) { return k; } long long ret = 0; if (k > 0) ret = (ret + k * dfs(i, j + 1, k + 2 * a[i] - 1, in, de) % MOD) % MOD; ret = (ret + in * dfs(i, j + 1, k, in + 1, de) % MOD) % MOD; if (de > 0) ret = (ret + de * dfs(i, j + 1, k, in, de - 1) % MOD) % MOD; ret = ret * invMod(k + in + de, MOD) % MOD; return mp[make_tuple(j, k, in, de)] = ret; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; for (long long i = 0; i < N; i++) cin >> a[i]; for (long long i = 0; i < N; i++) cin >> w[i]; for (long long i = 0; i < N; i++) { if (a[i]) inc += w[i]; else decr += w[i]; } for (long long i = 0; i < N; i++) { if (a[i]) inc -= w[i]; else decr -= w[i]; long long ans = dfs(i, 0, w[i], inc, decr); cout << ans << endl; if (a[i]) inc += w[i]; else decr += w[i]; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int Mod = 998244353; int n, m, a[55], w[55], f[55][105][55], g[55][105][55], x, sa, sb; int power(int b, int e) { if (!e) return 1; int tmp = power(b, e >> 1); tmp = 1ll * tmp * tmp % Mod; if (e & 1) tmp = 1ll * tmp * b % Mod; return tmp; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i <= n; i++) if (!a[i]) sa += w[i]; else sb += w[i]; for (int j = 0; j <= 100; j++) for (int k = 0; k <= m; k++) f[0][j][k] = j; for (int i = 1; i <= m; i++) for (int j = 1; j <= 100; j++) for (int k = 0; k <= m; k++) if (sb + k >= j && sa >= m - i - k) { f[i][j][k] = (1ll * j * f[i - 1][j + 1][k + 1] + 1ll * (sb + k - j) * f[i - 1][j][k + 1] + 1ll * (sa - m + i + k) * f[i - 1][j][k]) % Mod; f[i][j][k] = 1ll * f[i][j][k] * power(sa + sb + k - m + i + k, Mod - 2) % Mod; } for (int j = 0; j <= 100; j++) for (int k = 0; k <= m; k++) g[0][j][k] = j; for (int i = 1; i <= m; i++) for (int j = 1; j <= 100; j++) for (int k = 0; k <= m; k++) { g[i][j][k] = (1ll * j * g[i - 1][j - 1][k] + 1ll * (sb + k) * g[i - 1][j][k + 1] + 1ll * (sa - j - m + i + k) * g[i - 1][j][k]) % Mod; g[i][j][k] = 1ll * g[i][j][k] * power(sa + sb + k - m + i + k, Mod - 2) % Mod; } for (int i = 1; i <= n; i++) if (a[i]) cout << f[m][w[i]][0] << '\n'; else cout << g[m][w[i]][0] << '\n'; return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; long long b[55], a[55]; long long dp[55][55][55]; long long inv[200000]; int main() { inv[1] = 1; for (int i = 2; i <= 199999; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); if (a[i] == 0) a[i] = -1; } long long sum1 = 0, sum2 = 0; for (int i = 1; i <= n; i++) { scanf("%lld", &b[i]); if (a[i] == -1) sum1 += b[i]; else sum2 += b[i]; } for (int q = 1; q <= n; q++) { long long ans = 0; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) dp[i][j][k] = 0; dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j + i <= m; j++) { for (int k = 0; k + j + i <= m; k++) { long long pi = dp[i][j][k]; if (pi) { long long s = sum1 + sum2 + a[q] * i - j + k; long long t1 = b[q] + a[q] * i; long long t2, t3; if (a[q] == -1) { t2 = sum1 - b[q] - j; t3 = sum2 + k; } else { t2 = sum1 - j; t3 = sum2 - b[q] + k; } if (t1 < 0 || t2 < 0 || t3 < 0) { dp[i][j][k] = 0; continue; } if (t1) dp[i + 1][j][k] = (dp[i + 1][j][k] + pi * t1 % mod * inv[s] % mod) % mod; if (t2) dp[i][j + 1][k] = (dp[i][j + 1][k] + pi * t2 % mod * inv[s] % mod) % mod; if (t3) dp[i][j][k + 1] = (dp[i][j][k + 1] + pi * t3 % mod * inv[s] % mod) % mod; } } } } for (int i = 0; i <= m; i++) { for (int j = 0; j + i <= m; j++) { for (int k = 0; k + j + i <= m; k++) { if (i + j + k == m && dp[i][j][k]) { long long tmp = (b[q] + a[q] * i) * dp[i][j][k] % mod; ans = (ans + tmp + mod) % mod; } } } } printf("%lld\n", ans); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 50; int isgood[N], w[N]; int n, m; long long power(long long a, long long b) { long long x = 1, y = a; while (b > 0) { if (b & 1) { x = (x * y); if (x > MOD) x %= MOD; } y = y * y; if (y > MOD) y %= MOD; b >>= 1; } return x; } int inv(int x) { return power(x, MOD - 2); } int mul(int x, int y) { return 1LL * x * y % MOD; } int add(int x, int y) { int ret = x + y; if (ret >= MOD) ret -= MOD; return ret; } map<pair<pair<int, int>, pair<int, int> >, int> map1; int solve2(int idx, int me, int g, int b, bool flag) { if (idx == m) return me; if (map1.find(make_pair(make_pair(idx, me), make_pair(g, b))) != map1.end()) return map1[{{idx, me}, {g, b}}]; int ret = mul(mul(g, inv(g + b + me)), solve2(idx + 1, me, g + 1, b, flag)); if (b) ret = add( ret, mul(mul(b, inv(g + b + me)), solve2(idx + 1, me, g, b - 1, flag))); if (flag || me) ret = add(ret, mul(mul(me, inv(g + b + me)), solve2(idx + 1, me + (flag ? 1 : -1), g, b, flag))); return map1[{{idx, me}, {g, b}}] = ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; int g = 0, b = 0; for (int i = 0; i < int(n); i++) cin >> isgood[i]; for (int i = 0; i < int(n); i++) cin >> w[i]; for (int i = 0; i < int(n); i++) { if (isgood[i]) g += w[i]; else b += w[i]; } for (int i = 0; i < int(n); i++) { int cur = solve2(0, w[i], g - (isgood[i] ? w[i] : 0), b - (isgood[i] ? 0 : w[i]), isgood[i]); cout << cur << '\n'; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long w0, w1; long long a[200005], b[200005]; long long dp[3005][3005]; long long fpow(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } long long ans[200005]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; for (long long i = 1; i <= n; ++i) cin >> a[i]; for (long long i = 1; i <= n; ++i) cin >> b[i]; for (long long i = 1; i <= n; ++i) if (a[i]) w1 += b[i]; else w0 += b[i]; dp[0][0] = 1; for (long long i = 0; i <= m; ++i) { for (long long j = 0; j <= m; ++j) { dp[i][j] += (w1 + i - 1) * fpow(w0 + w1 + i - 1 - j, mod - 2) % mod * dp[i - 1][j] % mod; dp[i][j] += (w0 - j + 1) * fpow(w0 + w1 + i - j + 1, mod - 2) % mod * dp[i][j - 1] % mod; while (dp[i][j] > mod) dp[i][j] -= mod; } } long long a0 = 0, a1 = 0; for (long long i = 0; i <= m; ++i) { a0 += (m - i) * dp[i][m - i] % mod; a1 += i * dp[i][m - i] % mod; if (a0 > mod) a0 -= mod; if (a1 > mod) a1 -= mod; } for (long long i = 1; i <= n; ++i) { if (a[i]) ans[i] = b[i] + a1 * b[i] % mod * fpow(w1, mod - 2) % mod; else ans[i] = b[i] - a0 * b[i] % mod * fpow(w0, mod - 2) % mod; ans[i] %= mod; if (ans[i] < 0) ans[i] += mod; cout << ans[i] << endl; } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int N, M; int a[55], w[55]; long long qpow(long long x, int n) { long long res = 1; while (n) { if (n & 1) res = res * x % int(998244353); x = x * x % int(998244353); n /= 2; } return res; } long long inv(long long x) { return qpow(x, int(998244353) - 2); } long long s[2]; long long memo[55][55][55]; long long dfs(int id, int i, int j, int k) { if (memo[i][j][k] != -1) return memo[i][j][k]; long long w1; if (a[id] == 0) w1 = w[id] - j; if (a[id] == 1) w1 = w[id] + j; if (i == M + 1) return memo[i][j][k] = w1; long long p = w1 * inv(s[0] + s[1] + (i - 1) - 2 * k) % int(998244353); long long ans = 0; if (p > 0) ans = p * dfs(id, i + 1, j + 1, k + ((a[id] == 0) ? 1 : 0)) % int(998244353); if (p != 1) { long long p0 = (s[0] - k - ((a[id] == 0) ? w1 : 0)) * inv(s[0] + s[1] + (i - 1) - 2 * k) % int(998244353); long long p1 = (1 - p - p0 + 2 * int(998244353)) % int(998244353); ans = (ans + p0 * dfs(id, i + 1, j, k + 1) % int(998244353)) % int(998244353); ans = (ans + p1 * dfs(id, i + 1, j, k) % int(998244353)) % int(998244353); } return memo[i][j][k] = ans; } int main() { cin >> N >> M; for (int i = 1; i <= N; i++) { cin >> a[i]; } for (int i = 1; i <= N; i++) { cin >> w[i]; s[a[i]] += w[i]; } for (int i = 1; i <= N; i++) { memset(memo, -1, sizeof(memo)); cout << dfs(i, 1, 0, 0) << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long a[55], w[55], dp[55][55][55]; long long fast(long long x, long long b, long long res = 1) { while (b) { if (b & 1) res = res * x % 998244353; x = x * x % 998244353; b >>= 1; } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; long long sum = 0, s[2] = {0, 0}; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> w[i], sum += w[i]; s[a[i]] += w[i]; } for (int q = 1; q <= n; q++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long ans = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= j; k++) { long long tmp = (a[q] == 1 ? 1 : -1); if (s[1 - a[q]] > 0 && s[1 - a[q]] - tmp * (i - j - 1) > 0 && sum + tmp * (2 * j - i + 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k] * (s[1 - a[q]] - tmp * (i - j - 1)) % 998244353 * fast(sum + tmp * (2 * j - i + 1), 998244353 - 2) % 998244353) % 998244353; } if (s[a[q]] - w[q] > 0 && j > 0 && s[a[q]] + tmp * (j - 1 - k) - w[q] > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k] * (s[a[q]] + tmp * (j - 1 - k) - w[q]) % 998244353 * fast(sum + tmp * (2 * j - i - 1), 998244353 - 2) % 998244353) % 998244353; } if (j > 0 && k > 0 && w[q] + tmp * (k - 1) > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k - 1] * (w[q] + tmp * (k - 1)) % 998244353 * fast(sum + tmp * (2 * j - i - 1), 998244353 - 2) % 998244353) % 998244353; } if (i == m) ans = (ans + (w[q] + tmp * k) % 998244353 * dp[i][j][k] % 998244353) % 998244353; } } } cout << ans << '\n'; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; template <typename T> struct modular { constexpr modular() : val(0) {} constexpr modular(const modular<T>& _m) : val(_m.val) {} template <typename U> constexpr modular(const U& _r = U()) { val = -MOD <= _r && _r < MOD ? _r : _r % MOD; if (val < 0) { val += MOD; } } const T operator()() { return val; } template <typename U> explicit operator U() const { return static_cast<U>(val); } modular<T>& operator+=(const modular<T>& _m) { if ((val += _m.val) >= MOD) { val -= MOD; } return *this; } modular<T>& operator-=(const modular<T>& _m) { if ((val -= _m.val) < 0) { val += MOD; } return *this; } modular<T>& operator*=(const modular<T>& _m) { val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val)) .val; return *this; } modular<T>& operator/=(const modular<T>& _m) { T a = _m.val, b = MOD, u = 0, v = 1; while (a != 0) { T q = b / a; b -= q * a; swap(a, b); u -= q * v; swap(u, v); } return *this *= u; } modular<T>& operator=(const modular<T>& _m) { val = _m.val; return *this; } template <typename U> modular<T>& operator+=(const U& _r) { return *this += modular<T>(_r); } template <typename U> modular<T>& operator-=(const U& _r) { return *this -= modular<T>(_r); } template <typename U> modular<T>& operator*=(const U& _r) { return *this *= modular<T>(_r); } template <typename U> modular<T>& operator/=(const U& _r) { return *this /= modular<T>(_r); } template <typename U> modular<T>& operator=(const U& _r) { val = modular<T>(_r).val; return *this; } modular<T> operator-() { return modular<T>(-val); } template <typename U> friend bool operator==(const modular<U>&, const modular<U>&); friend std::istream& operator>>(std::istream& os, modular<T>& _m) { os >> _m.val; _m *= 1; return os; } friend std::ostream& operator<<(std::ostream& os, const modular<T>& _m) { return os << _m.val; } template <typename U> modular<T> exp(U e) { modular<T> res = 1; modular<T> b = val; if (e < 0) { b = 1 / b; e *= -1; } for (; e; e >>= 1) { if (e & 1) { res *= b; } b *= b; } return res; } private: T val; }; template <typename T> inline modular<T> operator+(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T, typename U> inline modular<T> operator+(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T, typename U> inline modular<T> operator+(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T> inline modular<T> operator-(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T, typename U> inline modular<T> operator-(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T, typename U> inline modular<T> operator-(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T> inline modular<T> operator*(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T, typename U> inline modular<T> operator*(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T, typename U> inline modular<T> operator*(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T> inline modular<T> operator/(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T, typename U> inline modular<T> operator/(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T, typename U> inline modular<T> operator/(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T> inline bool operator==(const modular<T>& _lhs, const modular<T>& _rhs) { return _lhs.val == _rhs.val; } template <typename T, typename U> inline bool operator==(const modular<T>& _lhs, const U& _rhs) { return _lhs == modular<T>(_rhs); } template <typename T, typename U> inline bool operator==(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) == _rhs; } template <typename T> inline bool operator!=(const modular<T>& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); } template <typename T, typename U> inline bool operator!=(const modular<T>& _lhs, const U& _rhs) { return !(_lhs == _rhs); } template <typename T, typename U> inline bool operator!=(const U& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); } const int N = 3003; modular<int> dp[N][N]; void solve() { memset(dp, 0, sizeof dp); int n, m; cin >> n >> m; vector<int> a(n); vector<int> w(n); for (auto& x : a) { cin >> x; } int H = 0, T = 0; for (int i = 0; i < n; i++) { int x; cin >> x; w[i] = x; if (a[i]) H += x; else T += x; } int tot = H + T; dp[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { if (i - j <= T) { dp[i + 1][j] += dp[i][j] * (T - (i - j)) / (tot + j - (i - j)); dp[i + 1][j + 1] += dp[i][j] * (H + j) / (tot + j - (i - j)); } } } modular<int> ad = 0; modular<int> mi = 0; for (int j = 0; j <= m; j++) { ad += dp[m][j] * j; mi += dp[m][j] * (m - j); } vector<modular<int> > res(n); for (int i = 0; i < n; i++) { if (a[i]) { res[i] = w[i] + ad * w[i] / H; } else { res[i] = w[i] - mi * w[i] / T; } } for (auto& x : res) { cout << x << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
import fileinput def D(a):print(a) def S(s,I):return int(s.split(" ")[I]) def inv(a):return pow(a,MOD-2,MOD) def main(): global MOD MOD=998244353 z=0 N=0 M=0 A=0 W=0 for l in fileinput.input(): z+=1 if(z<2): N=S(l,0) M=S(l,1) continue if(z<3): A=map(int,l.split(" ")) for i in range(N): if(A[i]==0): A[i]=-1 continue W=map(int,l.split(" ")) for i in range(N): X=0 Y=0 for j in range(N): if(i!=j): if(A[j]==1):X+=W[j] else: Y+=W[j] dp=[0]*51 for j in range(51): dp[j]=[0]*51 for k in range(51): dp[j][k]=[W[i]]*51 for x in xrange(50,-1,-1): for y in xrange(50,-1,-1): for z in xrange(50,-1,-1): if(x+y+z>=M or y>Y):continue v=0 q=W[i]+z*A[i] if(q<0):continue #D(str(x)+" / "+str(y)+" / "+str(z)+": "+str(X)+"|"+str(Y)) if(X):v+=dp[x+1][y][z]*(X+x)*inv(X+x+Y-y+q) if(Y-y>0):v+=dp[x][y+1][z]*(Y-y)*inv(X+x+Y-y+q) if(q>0):v+=(dp[x][y][z+1]+A[i])*q*inv(X+x+Y-y+q) dp[x][y][z]=v%MOD D(dp[0][0][0]) main()
PYTHON
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int add(int _a, int _b) { _a = (_a + 998244353) % 998244353; _b = (_b + 998244353) % 998244353; return (_a + _b) % 998244353; } int mul(int _a, int _b) { _a = (_a + 998244353) % 998244353; _b = (_b + 998244353) % 998244353; return ((long long int)((long long int)_a * (long long int)_b)) % 998244353; } int bigMod(int v, int p) { if (p == 0) { return 1; } int ret = bigMod(v, p / 2) % 998244353; if (p % 2 == 0) { return mul(ret, ret); } else { return mul(ret, mul(ret, v)); } } int n, m, ara[2 * 100010], bra[2 * 100010], mxa, mxb, suma, sumb, sum, inv[50 * 2 * 100010], f[2][52][52][102], g[2][52][52][102]; int frqa[52], frqb[52], ans[52]; void input() { int i, j; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", &ara[i]); } for (i = 0; i < n; i++) { scanf("%d", &bra[i]); } suma = sumb = sum = 0, mxa = mxb = -1; for (i = 0; i < n; i++) { if (ara[i] == 1) { suma += bra[i]; mxa = max(mxa, bra[i]); frqa[bra[i]]++; } else { sumb += bra[i], mxb = max(mxb, bra[i]); frqb[bra[i]]++; } sum += bra[i]; } } void pre() { int i, j; for (i = 1; i <= sum + m; i++) { inv[i] = bigMod(i, 998244353 - 2); } } void solve() { pre(); int i, j, ret = 0, v, k, l; for (i = 0; i <= m; i++) { if (i == 0) { for (j = m - i; j >= 0; j--) { for (k = min(m - j - i, sumb); k >= 0; k--) { for (l = mxa + j; l >= 0; l--) { f[i % 2][j][k][l] = l; } } } continue; } for (j = m - i; j >= 0; j--) { for (k = min(m - j - i, sumb); k >= 0; k--) { for (l = mxa + j; l >= 0; l--) { ret = 0; v = mul(mul(l, inv[suma + j + sumb - k]), f[(i - 1) % 2][j + 1][k][l + 1]); ret = add(ret, v); v = mul(mul(suma + j - l, inv[suma + j + sumb - k]), f[(i - 1) % 2][j + 1][k][l]); ret = add(ret, v); v = mul(mul(sumb - k, inv[suma + j + sumb - k]), f[(i - 1) % 2][j][k + 1][l]); ret = add(ret, v); f[i % 2][j][k][l] = ret; } } } } for (i = 0; i <= m; i++) { if (i == 0) { for (j = m - i; j >= 0; j--) { for (k = min(m - j - i, sumb); k >= 0; k--) { for (l = mxb; l >= 0; l--) { g[i % 2][j][k][l] = l; } } } continue; } for (j = m - i; j >= 0; j--) { for (k = min(m - j - i, sumb); k >= 0; k--) { for (l = mxb; l >= 0; l--) { ret = 0; if (l - 1 >= 0) { v = mul(mul(l, inv[suma + j + sumb - k]), g[(i - 1) % 2][j][k + 1][l - 1]); } else { v = 0; } ret = add(ret, v); v = mul(mul(sumb - k - l, inv[suma + j + sumb - k]), g[(i - 1) % 2][j][k + 1][l]); ret = add(ret, v); v = mul(mul(suma + j, inv[suma + j + sumb - k]), g[(i - 1) % 2][j + 1][k][l]); ret = add(ret, v); g[i % 2][j][k][l] = ret; } } } } for (i = 0; i < n; i++) { if (ara[i] == 1) { printf("%d", f[m % 2][0][0][bra[i]]); puts(""); } else { printf("%d", g[m % 2][0][0][bra[i]]); puts(""); } } } int main() { input(); solve(); }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long MOD = 998244353; int n, m; int a[200009]; long long w[200009]; long long sump = 0; long long sumn = 0; long long dp1[3009][3009]; long long dp2[3009][3009]; long long getinv(long long x) { x %= MOD; long long kit = MOD - 2; long long res = 1; while (kit > 0) { if (kit % 2) res = (res * x) % MOD; kit /= 2; x = (x * x) % MOD; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> w[i]; if (a[i] == 0) sumn += w[i]; else sump += w[i]; } for (int h = m; h >= 0; h--) { for (int i = 0; i <= h; i++) { int j = h - i; if (h == m) { dp1[i][j] = 1; dp2[i][j] = 1; continue; } dp1[i][j] = (dp1[i + 1][j] * (sump + i + 1)) % MOD; dp1[i][j] = (dp1[i][j] * getinv(sump + sumn + i - j + MOD)) % MOD; long long ppls = (dp1[i][j + 1] * (sumn - j + MOD)) % MOD; ppls = (ppls * getinv(sump + sumn + i - j + MOD)) % MOD; dp1[i][j] = (dp1[i][j] + ppls) % MOD; dp2[i][j] = (dp2[i + 1][j] * (sump + i)) % MOD; dp2[i][j] = (dp2[i][j] * getinv(sump + sumn + i - j + MOD)) % MOD; long long npls = (dp2[i][j + 1] * (sumn - j - 1 + MOD)) % MOD; npls = (npls * getinv(sump + sumn + i - j + MOD)) % MOD; dp2[i][j] = (dp2[i][j] + npls) % MOD; } } for (int i = 1; i <= n; i++) { if (a[i] == 0) cout << (w[i] * dp2[0][0]) % MOD << '\n'; else cout << (w[i] * dp1[0][0]) % MOD << '\n'; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 59; const long long mod = 998244353; int add[maxn]; long long w[maxn]; long long dp[52][52][52]; long long val[52][52][52]; bool vis[52][52][52]; long long Pow(long long a, long long b) { long long res = 1; a %= mod; while (b) { if (b & 1) res = res * a % mod; b >>= 1; a = a * a % mod; } return res; } inline long long inv(long long a) { return Pow(a, mod - 2); } int flag; long long dfs(int step, long long p, int i, int j, int k, long long x, long long y, long long z) { if (step == 0) return p * x % mod; if (vis[i][j][k]) { return val[i][j][k] * inv(dp[i][j][k]) % mod * p % mod; } dp[i][j][k] = p; val[i][j][k] += dfs(step - 1, p * x % mod * inv(x + y + z) % mod, i + 1, j, k, max(0ll, x + flag), y, z); val[i][j][k] += dfs(step - 1, p * y % mod * inv(x + y + z) % mod, i, j + 1, k, x, y + 1, z); val[i][j][k] += dfs(step - 1, p * z % mod * inv(x + y + z) % mod, i, j, k + 1, x, y, max(0ll, z - 1)); val[i][j][k] %= mod; vis[i][j][k] = 1; return val[i][j][k]; } int main() { int n, m; scanf("%d%d", &n, &m); long long ori, s0 = 0, s1 = 0; for (int i = 1; i <= n; i++) scanf("%d", add + i); for (int i = 1; i <= n; i++) { scanf("%lld", w + i); if (add[i]) s1 += w[i]; else s0 += w[i]; } ori = s0 + s1; int x, y, z, step; for (int i = 1; i <= n; i++) { memset(dp, 0, sizeof dp); memset(val, 0, sizeof val); memset(vis, 0, sizeof vis); if (add[i]) flag = 1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1 - w[i], s0); else flag = -1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1, s0 - w[i]); printf("%lld\n", val[0][0][0]); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, flag = 1; char ch = getchar(); while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') flag = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * flag; } const int MOD = 998244353; const int MAXN = 305; long long ksm(long long x, int k) { long long res = 1; for (; k; k >>= 1, x = x * x % MOD) if (k & 1) res = res * x % MOD; return res; } int N, M; bool A[MAXN]; int W[MAXN]; int w1, w2, sum; long long F[3005][3005]; long long G[3005][3005]; void prepare() { F[0][0] = 1; for (int i = 1; i <= M; i++) { for (int j = 0; j <= i; j++) { if (j && w2 >= i - j) F[i][j] = F[i - 1][j - 1] * (w1 + j - 1) % MOD * ksm(sum + (j - 1) - (i - 1 - (j - 1)), MOD - 2) % MOD; if (w2 >= i - j - 1) F[i][j] += F[i - 1][j] * (w2 - (i - j - 1)) % MOD * ksm(sum + j - (i - 1 - j), MOD - 2) % MOD; F[i][j] %= MOD; } } } void solve(int id) { for (int i = 0; i <= 100; i++) for (int j = 0; j <= 100; j++) G[i][j] = 0; G[0][0] = 1; for (int i = 1; i <= M; i++) { for (int j = 0; j <= i; j++) { if (A[id]) { G[i][j] = G[i - 1][j] * (w1 + i - 1 - (W[id] + j)) % MOD * ksm(w1 + i - 1, MOD - 2) % MOD; if (j) G[i][j] += G[i - 1][j - 1] * (W[id] + j - 1) % MOD * ksm(w1 + i - 1, MOD - 2) % MOD; G[i][j] %= MOD; } else { if (W[id] >= j) G[i][j] = G[i - 1][j] * (w2 - (i - 1) - (W[id] - j)) % MOD * ksm(w2 - (i - 1), MOD - 2) % MOD; if (W[id] >= j) G[i][j] += G[i - 1][j - 1] * (W[id] - (j - 1)) % MOD * ksm(w2 - (i - 1), MOD - 2) % MOD; G[i][j] %= MOD; } } } long long Ans = 0; for (int i = 0; i <= M; i++) { for (int j = 0; j <= i; j++) { if (A[id]) Ans += F[M][i] * G[i][j] % MOD * (W[id] + j) % MOD; else Ans += F[M][M - i] * G[i][j] % MOD * (W[id] - j) % MOD; Ans %= MOD; } } printf("%lld\n", Ans); } int main() { N = read(), M = read(); for (int i = 1; i <= N; i++) A[i] = read(); for (int i = 1; i <= N; i++) { W[i] = read(), sum += W[i]; if (A[i]) w1 += W[i]; else w2 += W[i]; } prepare(); for (int i = 1; i <= N; i++) solve(i); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long dp[1010000]; long long mpow(long long x, long long n) { long long ans = 1; while (n) { if (n & 1) ans = ans * x % 998244353; x = x * x % 998244353; n >>= 1; } return ans; } long long inv(long long x) { return mpow(x, 998244353 - 2); } int n, m; long long a[1010000]; int b[1010000]; long long sum[2]; long long ans[2]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[b[i]] += a[i]; } dp[0] = 1; for (int i = 0; i < m; i++) for (int j = i; j >= 0; j--) { if (i - j <= sum[0]) { long long sum0 = sum[0] - (i - j); long long sum1 = sum[1] + j; dp[j + 1] += dp[j] * sum1 % 998244353 * inv(sum0 + sum1) % 998244353; dp[j] = dp[j] * sum0 % 998244353 * inv(sum0 + sum1) % 998244353; } } for (int i = 0; i <= m; i++) { ans[0] += (sum[0] - (m - i)) * dp[i] % 998244353; ans[1] += (sum[1] + i) * dp[i] % 998244353; } for (int i = 1; i <= n; i++) cout << (ans[b[i]] * a[i] % 998244353 * inv(sum[b[i]]) % 998244353 + 998244353) % 998244353 << endl; return ~~(0 ^ 0 ^ 0); }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 50 + 5; const long long mod = 998244353; int n, m; long long qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans *= a, ans %= mod; a *= a; a %= mod; b >>= 1; } return ans; } long long inv(long long a) { return qpow(a, mod - 2); } bool vis[maxn][maxn][maxn]; long long val[maxn][maxn][maxn]; long long dfs(int pos, int i, int j, int k, int x, int y, int z, int p) { if (!pos) return x; if (vis[i][j][k]) return val[i][j][k]; long long &d = val[i][j][k]; d += x * inv(x + y + z) % mod * dfs(pos - 1, i + 1, j, k, max(0, x + p), y, z, p); d %= mod; d += y * inv(x + y + z) % mod * dfs(pos - 1, i, j + 1, k, x, y + 1, z, p); d %= mod; d += z * inv(x + y + z) % mod * dfs(pos - 1, i, j, k + 1, x, y, max(0, z - 1), p); d %= mod; vis[i][j][k] = 1; return d; } int a[maxn], b[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int sum1 = 0, sum2 = 0; for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); if (a[i]) sum1 += b[i]; else sum2 += b[i]; } for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); memset(val, 0, sizeof(val)); long long ans = 0; if (a[i]) ans = dfs(m, 0, 0, 0, b[i], sum1 - b[i], sum2, 1); else ans = dfs(m, 0, 0, 0, b[i], sum1, sum2 - b[i], -1); printf("%lld\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int maxn = 2e5 + 100; long long dp[52][52][52]; bool vis[52][52][52]; long long val[52][52][52]; int a[maxn], w[maxn]; long long qmod(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = a * a % mod; } return ans; } int flag; int dfs(int n, int i, int j, int k, long long p, long long v, long long sp, long long sn) { if (!n) { return 1LL * p * v % mod; } if (vis[i][j][k]) { return val[i][j][k] * qmod(dp[i][j][k], mod - 2) % mod * p % mod; } dp[i][j][k] = p; val[i][j][k] += dfs(n - 1, i + 1, j, k, 1LL * p * sp % mod * qmod(v + sp + sn, mod - 2) % mod, v, sp + 1, sn); val[i][j][k] += dfs(n - 1, i, j + 1, k, 1LL * p * sn % mod * qmod(v + sp + sn, mod - 2) % mod, v, sp, max(sn - 1, 0LL)); val[i][j][k] += dfs(n - 1, i, j, k + 1, 1LL * p * v % mod * qmod(v + sp + sn, mod - 2) % mod, max(v + flag, 0LL), sp, sn); val[i][j][k] %= mod; vis[i][j][k] = 1; return val[i][j][k]; } int main() { ios::sync_with_stdio(false); long long sn = 0, sp = 0; int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> w[i]; if (a[i]) { sp += w[i]; } else sn += w[i]; } for (int i = 0; i < n; ++i) { memset(dp, 0, sizeof dp); memset(vis, 0, sizeof vis); memset(val, 0, sizeof val); if (a[i]) { flag = 1; sp -= w[i]; } else { flag = -1; sn -= w[i]; } cout << dfs(m, 0, 0, 0, 1, w[i], sp, sn) << endl; if (a[i]) { sp += w[i]; } else sn += w[i]; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; const long long MOD = 998244353; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int INF = 1e9 + 7; const double PI = acos(-1.0); const double EPS = 1e-8; int _; using namespace std; long long powermod(long long n, long long k, long long MOD) { long long ans = 1; while (k) { if (k & 1) { ans = ((ans % MOD) * (n % MOD)) % MOD; } n = ((n % MOD) * (n % MOD)) % MOD; k >>= 1; } return ans; } int aa[105]; long long gg[105]; long long dp_sa[3005][3005]; long long dp_sb[3005][3005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int N, M; while (cin >> N >> M) { long long sa = 0; long long sb = 0; for (int i = 1; i <= N; ++i) cin >> aa[i]; for (int i = 1; i <= N; ++i) { cin >> gg[i]; if (aa[i]) sa += gg[i]; else sb += gg[i]; } memset(dp_sa, 0, sizeof(dp_sa)); memset(dp_sb, 0, sizeof(dp_sb)); for (int i = M; i >= 0; --i) { dp_sa[i][M - i] = dp_sb[i][M - i] = 1; for (int j = min(1LL * M - i - 1, sb); j >= 0; --j) { dp_sa[i][j] = ((sa + i + 1) % MOD * dp_sa[i + 1][j] % MOD * powermod(sa + sb + i - j, MOD - 2, MOD) % MOD + (sb - j) % MOD * dp_sa[i][j + 1] % MOD * powermod(sa + sb + i - j, MOD - 2, MOD) % MOD) % MOD; dp_sb[i][j] = ((sa + i) % MOD * dp_sb[i + 1][j] % MOD * powermod(sa + sb + i - j, MOD - 2, MOD) % MOD + (sb - j - 1 + MOD) % MOD * dp_sb[i][j + 1] % MOD * powermod(sa + sb + i - j, MOD - 2, MOD) % MOD) % MOD; } } for (int i = 1; i <= N; ++i) { if (aa[i]) { cout << gg[i] * dp_sa[0][0] % MOD << endl; } else { cout << gg[i] * dp_sb[0][0] % MOD << endl; } } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long Mod = 998244353; long long qpw(long long a, long long b) { long long ans = 1; a = (a % Mod + Mod) % Mod; while (b) { if (b & 1) ans = ans * a % Mod; a = a * a % Mod; b >>= 1; } return ans; } long long w[55], a[55], W0, W1; long long dp[55][55][55]; void add(long long &x, long long y) { x += y; if (x >= Mod) x -= Mod; if (x < 0) x += Mod; } long long inv(long long x) { return qpw(x, Mod - 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> w[i]; if (a[i]) W1 += w[i]; else W0 += w[i]; } for (int t = 0; t < n; ++t) { memset(dp, 0, sizeof dp); dp[0][0][0] = 1; int tmpW0 = W0, tmpW1 = W1; if (a[t]) tmpW1 -= w[t]; else tmpW0 -= w[t]; for (int i = 0; i < m; ++i) { for (int j = 0; j <= i && j <= tmpW0; j++) { for (int k = 0; k + j <= i; k++) { if (a[t]) { if (j + 1 <= tmpW0) add(dp[i + 1][j + 1][k], dp[i][j][k] * (tmpW0 - j) % Mod * inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod); add(dp[i + 1][j][k + 1], dp[i][j][k] * (tmpW1 + k) % Mod * inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod); add(dp[i + 1][j][k], dp[i][j][k] * (w[t] + i - j - k) % Mod * inv(tmpW0 - j + tmpW1 + k + w[t] + i - j - k) % Mod); } else if (!a[t] && w[t] >= i - j - k) { if (j + 1 <= tmpW0) add(dp[i + 1][j + 1][k], dp[i][j][k] * (tmpW0 - j) % Mod * inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod); add(dp[i + 1][j][k + 1], dp[i][j][k] * (tmpW1 + k) % Mod * inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod); if (i - j - k + 1 <= w[t]) add(dp[i + 1][j][k], dp[i][j][k] * (w[t] - i + j + k) % Mod * inv(tmpW0 - j + tmpW1 + k + w[t] - i + j + k) % Mod); } } } } long long ans = 0; for (int i = 0; i < 55; i++) for (int j = 0; j < 55 && m - i - j >= 0; ++j) { if (a[t]) { add(ans, dp[m][i][j] * (m - i - j + w[t]) % Mod); } else if (!a[t] && m - i - j <= w[t]) { add(ans, dp[m][i][j] * (w[t] - m + i + j) % Mod); } } cout << ans << "\n"; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 52 + 5; const int mod = 998244353; long long qp(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } template <class T> inline bool scan(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void out(T x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); } struct mint { int n; mint(int n_ = 0) : n(n_) {} }; mint operator+(mint a, mint b) { return (a.n += b.n) >= mod ? a.n - mod : a.n; } mint operator-(mint a, mint b) { return (a.n -= b.n) < 0 ? a.n + mod : a.n; } mint operator*(mint a, mint b) { return 1LL * a.n * b.n % mod; } mint &operator+=(mint &a, mint b) { return a = a + b; } mint &operator-=(mint &a, mint b) { return a = a - b; } mint &operator*=(mint &a, mint b) { return a = a * b; } ostream &operator<<(ostream &o, mint a) { return o << a.n; } istream &operator>>(istream &o, mint a) { return o >> a.n; } int n, m; mint dp[maxn][maxn][maxn]; int tp[maxn]; mint w[maxn]; mint inv[maxn * maxn]; int main() { for (int i = 0; i < maxn * maxn; ++i) inv[i] = qp(i, mod - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &tp[i]); } for (int i = 1; i <= n; ++i) { scanf("%d", &w[i].n); } int wgood = 0, wbad = 0; for (int i = 1; i <= n; ++i) { if (tp[i] == 1) wgood += w[i].n; else wbad += w[i].n; } for (int i = 1; i <= n; ++i) { memset(dp, 0, sizeof dp); dp[0][0][0] = 1; for (int step = 0; step <= m; ++step) { for (int j = 0; j <= step; ++j) { int k = step - j; int up = tp[i] ? j : min(w[i].n, k); mint invk = inv[wgood + wbad - k + j]; for (int hit = 0; hit <= up; ++hit) { mint tmp = dp[hit][j][k] * invk; if (tp[i]) { dp[hit][j + 1][k] += tmp * (wgood + j - (w[i] + hit)); dp[hit][j][k + 1] += tmp * (wbad - k); dp[hit + 1][j + 1][k] += tmp * (w[i] + hit); } else { dp[hit][j + 1][k] += tmp * (wgood + j); dp[hit][j][k + 1] += tmp * (wbad - k - (w[i] - hit)); dp[hit + 1][j][k + 1] += tmp * (w[i] - hit); } } } } mint res = 0; for (int j = 0; j <= m; ++j) { int k = m - j; int up = tp[i] ? j : min(w[i].n, k); for (int hit = 0; hit <= up; ++hit) { if (tp[i]) res += hit * dp[hit][j][k]; else res -= hit * dp[hit][j][k]; } } printf("%d\n", (res + w[i]).n); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long i, i0, n, m, a[55], w[55], dp[55][55][55][55], dic[3055], ddic[3055][3055]; long long inv(long long b) { return b == 1 ? 1 : (mod - mod / b) * inv(mod % b) % mod; } int main() { for (i = 1; i <= 3050; i++) dic[i] = inv(i); for (i = 1; i <= 3050; i++) { for (i0 = 1; i0 <= i; i0++) { ddic[i0][i] = i0 * dic[i] % mod; } } scanf("%lld %lld", &n, &m); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); long long sum0 = 0, sum1 = 0; for (i = 1; i <= n; i++) { scanf("%lld", &w[i]); if (a[i]) sum1 += w[i]; else sum0 += w[i]; } for (i = 1; i <= n; i++) { if (a[i]) { memset(dp, 0, sizeof(dp)); dp[0][0][0][0] = 1; for (i0 = 0; i0 < m; i0++) { for (int ia = 0; ia <= m; ia++) { for (int ib = 0; ib + ia <= m; ib++) { for (int ic = 0; ia + ib + ic <= m && ic <= sum0; ic++) { dp[i0 + 1][ia + 1][ib][ic] += dp[i0][ia][ib][ic] * ddic[w[i] + ia][sum0 + sum1 + ia + ib - ic]; dp[i0 + 1][ia][ib + 1][ic] += dp[i0][ia][ib][ic] * ddic[sum1 + ib - w[i]][sum0 + sum1 + ia + ib - ic]; dp[i0 + 1][ia][ib][ic + 1] += dp[i0][ia][ib][ic] * ddic[sum0 - ic][sum0 + sum1 + ia + ib - ic]; dp[i0 + 1][ia + 1][ib][ic] %= mod; dp[i0 + 1][ia][ib + 1][ic] %= mod; dp[i0 + 1][ia][ib][ic + 1] %= mod; } } } } long long ans = 0; for (int ia = 0; ia <= m; ia++) { for (int ib = 0; ib + ia <= m; ib++) { for (int ic = 0; ic + ia + ib <= m && ic <= sum0; ic++) { ans += (ia + w[i]) * dp[m][ia][ib][ic]; ans %= mod; } } } printf("%lld\n", ans); } else { memset(dp, 0, sizeof(dp)); dp[0][0][0][0] = 1; for (i0 = 0; i0 < m; i0++) { for (int ia = 0; ia <= w[i] && ia <= m; ia++) { for (int ib = 0; ib + ia <= m; ib++) { for (int ic = 0; ic + ib + ia <= m && ic <= sum0 - w[i]; ic++) { dp[i0 + 1][ia + 1][ib][ic] += dp[i0][ia][ib][ic] * ddic[w[i] - ia][sum0 + sum1 - ia + ib - ic]; dp[i0 + 1][ia][ib + 1][ic] += dp[i0][ia][ib][ic] * ddic[sum1 + ib][sum0 + sum1 - ia + ib - ic]; dp[i0 + 1][ia][ib][ic + 1] += dp[i0][ia][ib][ic] * ddic[sum0 - ic - w[i]][sum0 + sum1 - ia + ib - ic]; dp[i0 + 1][ia + 1][ib][ic] %= mod; dp[i0 + 1][ia][ib + 1][ic] %= mod; dp[i0 + 1][ia][ib][ic + 1] %= mod; } } } } long long ans = 0; for (int ia = 0; ia <= m; ia++) { for (int ib = 0; ib + ia <= m; ib++) { for (int ic = 0; ia + ib + ic <= m; ic++) { ans += (w[i] - ia) * dp[m][ia][ib][ic]; ans %= mod; } } } printf("%lld\n", ans); } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 55; const long long mod = 998244353; int n, m; int sum[2]; int a[MAXN], w[MAXN]; int memo[MAXN * 2][MAXN][MAXN][MAXN][2]; long long fastPow(long long x, long long p) { long long answer = 1, current = x; while (p != 0) { if (p % 2 != 0) { answer = (answer * current) % mod; } p /= 2; current = (current * current) % mod; } return answer; } long long inv(long long x) { return fastPow(x, mod - 2); } long long calcState(int curr, int diffGood, int diffBad, int turn, bool type) { if (curr == 0) return 0; if (turn == m) return curr; if (memo[curr][diffGood][diffBad][turn][type] != -1) { return memo[curr][diffGood][diffBad][turn][type]; } int sumBad = sum[0] - diffBad; int sumGood = sum[1] + diffGood; if (type == false) sumBad -= curr; else if (type == true) sumGood -= curr; long long answer = 0; if (sumBad != 0) answer += (((sumBad * inv(sumBad + sumGood + curr)) % mod) * calcState(curr, diffGood, diffBad + 1, turn + 1, type)) % mod; if (sumGood != 0) answer += (((sumGood * inv(sumBad + sumGood + curr)) % mod) * calcState(curr, diffGood + 1, diffBad, turn + 1, type)) % mod; if (curr != 0) { if (type == false) { answer += (((curr * inv(sumBad + sumGood + curr)) % mod) * calcState(curr - 1, diffGood, diffBad + 1, turn + 1, type)) % mod; } else { answer += (((curr * inv(sumBad + sumGood + curr)) % mod) * calcState(curr + 1, diffGood + 1, diffBad, turn + 1, type)) % mod; } } answer %= mod; memo[curr][diffGood][diffBad][turn][type] = answer; return answer; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> w[i]; } for (int i = 0; i < n; i++) { sum[a[i]] += w[i]; } memset(memo, -1, sizeof(memo)); for (int i = 0; i < n; i++) { cout << calcState(w[i], 0, 0, 0, a[i]) << '\n'; } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int dp[2555][105][55]; long long a[55], wi[55], cur, sp, sn, su, mo; long long expo(long long base, long long exponent, long long mod = 998244353) { long long ans = 1; while (exponent != 0) { if ((exponent & 1) == 1) { ans = ans * base; ans = ans % mod; } base = base * base; base %= mod; exponent >>= 1; } return ans % mod; } long long add(long long x, long long y, long long mod = 998244353) { x += y; if (x >= mod) x -= mod; return x; } long long sub(long long x, long long y, long long mod = 998244353) { x -= y; if (x < 0) x += mod; return x; } long long mul(long long x, long long y, long long mod = 998244353) { return (x * 1ll * y) % mod; } long long solve(long long w, long long s, long long m) { if (m == 0) return w; if (w == 0) return 0; if (dp[w][s][m] != -1) return dp[w][s][m]; long long ans; long long left = (mo - m) - abs(w - wi[cur]); long long curs = (s - w) - su; long long snu = (left - curs) / 2, spu = (left + curs) / 2; if (a[cur]) { ans = mul(w, solve(w + 1, s + 1, m - 1)); if (sn - snu) ans = add(ans, mul(sn - snu, solve(w, s - 1, m - 1))); if (sp + spu) ans = add(mul(sp + spu, solve(w, s + 1, m - 1)), ans); ans = mul(ans, expo(s, 998244353 - 2)); } else { ans = mul(w, solve(w - 1, s - 1, m - 1)); if (sn - snu) ans = add(ans, mul(sn - snu, solve(w, s - 1, m - 1))); if (sp + spu) ans = add(ans, mul(sp + spu, solve(w, s + 1, m - 1))); ans = mul(ans, expo(s, 998244353 - 2)); } dp[w][s][m] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { long long n, m; cin >> n >> m; mo = m; long long s = 0; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) { cin >> wi[i]; s += wi[i]; if (a[i]) sp += wi[i]; else sn += wi[i]; } su = s; for (long long i = 0; i < n; i++) { for (long long l = max(0LL, s - m - 1LL); l < s + m + 1; l++) for (long long j = max(0LL, wi[i] - m - 1); j < m + 1 + wi[i]; j++) for (long long k = 0; k < m + 1; k++) dp[j][l][k] = -1; cur = i; if (a[i]) sp -= wi[i]; else sn -= wi[i]; su -= wi[i]; cout << solve(wi[i], s, m) << '\n'; if (a[i]) sp += wi[i]; else sn += wi[i]; su += wi[i]; } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> void err() { std::cout << std::endl; } template <typename T, typename... Args> void err(T a, Args... args) { std::cout << a << ' '; err(args...); } using namespace std; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 100000 + 5; const int maxq = 3000 + 5; long long qpow(long long x, long long n) { long long r = 1; while (n > 0) { if (n & 1) r = r * x % mod; n >>= 1; x = x * x % mod; } return r; } long long imem[maxn * 2]; long long inv(int x) { if (imem[x]) return imem[x]; return imem[x] = qpow(x, mod - 2); } void add(long long& x, long long y) { x += y; if (x >= mod) x -= mod; } int n, m, a[maxn], w[maxn]; long long f[maxq][maxq]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", a + i); int sa = 0, sb = 0; for (int i = 1; i <= n; i++) { scanf("%d", w + i); if (a[i]) sa += w[i]; else sb += w[i]; } f[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { int a = sa + j, b = sb - (i - j); long long fm = inv(a + b); add(f[i + 1][j + 1], f[i][j] * a % mod * fm % mod); add(f[i + 1][j], f[i][j] * b % mod * fm % mod); } } long long ea = 0, eb = 0; for (int i = 0; i <= m; i++) { ; add(ea, f[m][i] * (sa + i) % mod); add(eb, f[m][i] * (sb - (m - i)) % mod); }; long long iva = inv(sa), ivb = inv(sb); for (int i = 1; i <= n; i++) { if (a[i]) { printf("%lld\n", 1ll * w[i] * iva % mod * ea % mod); } else { printf("%lld\n", 1ll * w[i] * ivb % mod * eb % mod); } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7, M = 3e3 + 7, mod = 998244353; int f[M * 2], d[M][M], n, m, i, j, k, a[N], b[N], s[2], s0, s1, b0, b1; long long pow_mod(long long x, long long n) { x = (x % mod + mod) % mod; long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { for (scanf("%d%d", &n, &m), i = 1; i <= n; ++i) scanf("%d", a + i); for (i = 1; i <= n; ++i) scanf("%d", b + i), s[a[i]] += b[i]; for (i = -m; i <= m; ++i) f[i + m] = pow_mod(s[0] + s[1] + i, mod - 2); d[0][0] = 1; for (i = 0; i < m; ++i) for (j = 0; j <= i; ++j) if (d[i][j]) { k = f[2 * j - i + m]; d[i + 1][j] = (d[i + 1][j] + 1LL * (s[0] - (i - j)) * k % mod * d[i][j] % mod) % mod; d[i + 1][j + 1] = (d[i + 1][j + 1] + 1LL * (s[1] + j) * k % mod * d[i][j] % mod) % mod; } s0 = pow_mod(s[0], mod - 2); s1 = pow_mod(s[1], mod - 2); for (i = 0; i <= m; ++i) { b0 = (b0 + 1LL * i * d[m][m - i]) % mod; b1 = (b1 + 1LL * i * d[m][i]) % mod; } b0 = (mod - b0) % mod; for (i = 1; i <= n; ++i) printf("%lld\n", a[i] == 0 ? (b[i] + 1LL * b[i] * s0 % mod * b0 % mod) % mod : (b[i] + 1LL * b[i] * s1 % mod * b1 % mod) % mod); }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long dp[52][52][52]; long long inv[3010], sum0, sum1, sum; long long a[55], b[55], n, m; long long qpow(long long x, long long y) { long long ans = 1; for (; y; y >>= 1) { if (y & 1) ans = (ans * x) % mod; x = (x * x) % mod; } return ans; } long long solve1(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) continue; int now = sum + 2 * j - i; dp[i + 1][j + 1][k + 1] += dp[i][j][k] * (x + k) % mod * inv[now] % mod; dp[i + 1][j + 1][k + 1] %= mod; dp[i + 1][j + 1][k] += dp[i][j][k] * (sum1 + j - k - x) % mod * inv[now]; dp[i + 1][j + 1][k] %= mod; if (sum0 - i + j > 0) { dp[i + 1][j][k] += dp[i][j][k] * (sum0 - i + j) % mod * inv[now] % mod; dp[i + 1][j][k] %= mod; } } long long ans = 0; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) ans = (ans + dp[m][i][j] * (j + x)) % mod; return ans; } long long solve0(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) continue; int now = sum + i - 2 * j; if (sum0 - j > 0) { if (k < x) { dp[i + 1][j + 1][k + 1] += dp[i][j][k] * (x - k) % mod * inv[now] % mod; dp[i + 1][j + 1][k + 1] %= mod; } dp[i + 1][j + 1][k] += dp[i][j][k] * (sum0 - (j - k) - x) % mod * inv[now]; dp[i + 1][j + 1][k] %= mod; } dp[i + 1][j][k] += dp[i][j][k] * (sum1 + i - j) % mod * inv[now] % mod; dp[i + 1][j][k] %= mod; } long long ans = 0; for (int i = 0; i <= m; i++) { if (i >= sum0) continue; for (int j = 0; j <= m; j++) { if (j >= x) continue; ans = (ans + dp[m][i][j] * (x - j)) % mod; } } return ans; } int main() { for (int i = 1; i <= 3000; i++) inv[i] = qpow(i, mod - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (int i = 1; i <= n; i++) { scanf("%lld", &b[i]); sum += b[i]; if (a[i] == 0) sum0 += b[i]; else sum1 += b[i]; } for (int i = 1; i <= n; i++) { if (a[i] == 0) printf("%lld\n", solve0(b[i])); else printf("%lld\n", solve1(b[i])); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long fpw(long long n, long long p) { if (p == 0) return 1LL; long long tmp = fpw(n, p / 2); tmp *= tmp; tmp %= 998244353; if (p % 2 == 1) { tmp *= n; tmp %= 998244353; } return tmp; } long long inv(long long k) { return fpw(k, 998244353 - 2); } long long pre[3005]; long long inv2(long long k) { if (k > 3000) return inv(k); return pre[k]; } long long n, m; long long a[100]; long long w[100]; long long dp[55][105][105]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> w[i]; } for (int i = 1; i <= 3000; i++) { pre[i] = inv(i); } for (int i = 0; i < n; i++) { long long cntoth = 0, wall = 0, w1 = 0, w0 = 0; for (int j = 0; j < n; j++) { if (i != j) { cntoth += a[j]; if (a[j] == 1) w1 += w[j]; else w0 += w[j]; } wall += w[j]; } long long ans = 0; dp[0][51][51] = 1; for (int j = 1; j <= m; j++) { for (int k = 1; k <= 101; k++) { for (int l = 1; l <= 51; l++) { int l2 = 51 + (j - abs(51 - k) - abs(51 - l)); if (l2 < 51) continue; dp[j][k][l] = 0; if (a[i] == 0) dp[j][k][l] += ((dp[j - 1][k + 1][l] * (w[i] + (k + 1) - 51)) % 998244353) * inv2(wall + (k + 1) - 51 + l - 51 + l2 - 51); else dp[j][k][l] += ((dp[j - 1][k - 1][l] * (w[i] + (k - 1) - 51)) % 998244353) * inv2(wall + (k - 1) - 51 + l - 51 + l2 - 51); dp[j][k][l] += ((dp[j - 1][k][l] * (w1 + ((l2 - 1) - 51))) % 998244353) * inv2(wall + k - 51 + l - 51 + (l2 - 1) - 51); dp[j][k][l] += ((dp[j - 1][k][l + 1] * (w0 + ((l + 1) - 51))) % 998244353) * inv2(wall + k - 51 + (l + 1) - 51 + l2 - 51); dp[j][k][l] %= 998244353; if (j == m) ans += dp[j][k][l] * (w[i] + k - 51); ans %= 998244353; } } } printf("%lld\n", ans); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long MOD = 998244353; inline long long Msum(long long x) { return x; } template <typename... Rest> inline long long Msum(long long x, Rest... rest) { return (x + Msum(rest...)) % MOD; } inline long long Mprod(long long x) { return x; } template <typename... Rest> inline long long Mprod(long long x, Rest... rest) { return x * Mprod(rest...) % MOD; } inline long long Mnorm(long long x) { return (x % MOD + MOD) % MOD; } long long Msq(long long x) { return x * x % MOD; } long long Mpow(long long b, long long e) { return e ? Mprod(Msq(Mpow(b, e >> 1)), (e & 1 ? b : 1)) : 1; } long long Minv(long long x) { return Mpow(x, MOD - 2); } const int MAXN = 51; int sns[MAXN], w[MAXN]; int wi, sn; int sl, sd, S; long long dp[MAXN][MAXN][MAXN]; long long f(int h, int hl, int hd, int t) { if (!t) return 0; long long &ret = dp[h][hl][hd]; if (ret != -1) return ret; ret = 0; long long cS = S + hl - hd + sn * h; ret += Mprod(wi + sn * h, Minv(cS), 1 + f(h + 1, hl, hd, t - 1)); ret = Mnorm(ret); ret += Mprod(sl + hl, Minv(cS), f(h, hl + 1, hd, t - 1)); ret = Mnorm(ret); ret += Mprod(sd - hd, Minv(cS), f(h, hl, hd + 1, t - 1)); ret = Mnorm(ret); assert(sd - hd + sl + hl + wi + sn * h == cS); return ret; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &sns[i]); sns[i] = sns[i] * 2 - 1; } for (int i = 0; i < n; i++) { scanf("%d", &w[i]); } for (int i = 0; i < n; i++) { S = sl = sd = 0; wi = w[i]; sn = sns[i]; for (int j = 0; j < (int)n; j++) { S += w[j]; if (j != i) { if (sns[j] == 1) sl += w[j]; else sd += w[j]; } } memset(dp, -1, sizeof(dp)); printf("%lld\n", Mnorm(wi + sn * f(0, 0, 0, m))); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int mod = 998244353; long long qpow(long long a, long long b) { long long ans = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } long long gcd(long long a, long long b) { return b > 0 ? gcd(b, a % b) : a; } int a[maxn]; int w[maxn]; int one, zero, num, sum; long long dp[60][60][60][60]; long long ans[60]; int main() { int n, m, T; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i], num += a[i]; for (int i = 1; i <= n; i++) cin >> w[i], sum = (sum + w[i]) % mod, one = (one + a[i] * w[i]) % mod; zero = (sum - one + mod) % mod; for (int i = 1; i <= n; i++) { if (a[i] == 0) { dp[1][i][0][1] = 1ll * qpow(sum, mod - 2) * w[i] % mod; dp[1][i][0][0] = 1ll * qpow(sum, mod - 2) * (zero - w[i]) % mod; dp[1][i][1][0] = 1ll * qpow(sum, mod - 2) * (one) % mod; } else { dp[1][i][1][1] = 1ll * qpow(sum, mod - 2) * w[i] % mod; dp[1][i][1][0] = 1ll * qpow(sum, mod - 2) * (one - w[i]) % mod; dp[1][i][0][0] = 1ll * qpow(sum, mod - 2) * (zero) % mod; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int t = 0; t <= i; t++) { for (int k = 0; k <= i; k++) { int state = ((sum + t - (i - t)) % mod + mod) % mod; int o0 = zero - (i - t); int o1 = one + t; if (a[j] == 0) { dp[i + 1][j][t][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o0 - (w[j] - k)) % mod; dp[i + 1][j][t][k] %= mod; dp[i + 1][j][t][k + 1] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * ((w[j] - k)) % mod; dp[i + 1][j][t][k + 1] %= mod; dp[i + 1][j][t + 1][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o1) % mod; dp[i + 1][j][t + 1][k] %= mod; } else { dp[i + 1][j][t + 1][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o1 - (w[j] + k)) % mod; dp[i + 1][j][t + 1][k] %= mod; dp[i + 1][j][t + 1][k + 1] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * ((w[j] + k)) % mod; dp[i + 1][j][t + 1][k + 1] %= mod; dp[i + 1][j][t][k] += 1ll * dp[i][j][t][k] * qpow(state, mod - 2) % mod * (o0) % mod; dp[i + 1][j][t][k] %= mod; } } } } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { int temp = a[i] ? 1 : -1; ans[i] = (ans[i] + (w[i] + k * temp) * dp[m][i][j][k]) % mod; } } cout << (ans[i] + mod) % mod << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; bool debug = false; int n, m; int a[55], b[55]; int sigG, sigA, sigB; long long dp[55][55][55]; long long fp[1000005]; const long long mod = 998244353; long long qp(long long a, long long b) { if (b == 0) { return 1; } long long ans = qp(a, b / 2); if (b % 2) { return ans * ans % mod * a % mod; } else { return ans * ans % mod; } } void add(int ti, int tj, int tk, int fi, int fj, int fk, long long num, long long dem) { if (num < 0 || dem <= 0) { return; } dp[ti][tj][tk] += dp[fi][fj][fk] * num % mod * fp[dem] % mod; dp[ti][tj][tk] %= mod; } long long getAns(int pos) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) { continue; } add(i + 1, j, k, i, j, k, b[pos] + i, sigA + i + j - k); add(i, j + 1, k, i, j, k, sigG + j + i - b[pos] - i, sigA + i + j - k); add(i, j, k + 1, i, j, k, sigB - k, sigA + i + j - k); } } } long long tot = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (i + j + k == m) { tot += 1ll * (b[pos] + i) * dp[i][j][k] % mod; tot %= mod; } } } } return tot; } long long getAns2(int pos) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (dp[i][j][k] == 0) { continue; } add(i + 1, j, k, i, j, k, b[pos] - i, sigA - i + j - k); add(i, j + 1, k, i, j, k, sigG + j, sigA - i + j - k); add(i, j, k + 1, i, j, k, sigB - k - i - b[pos] + i, sigA - i + j - k); } } } long long tot = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (i + j + k == m) { tot += 1ll * (b[pos] - i) * dp[i][j][k] % mod; tot %= mod; } } } } return tot; } int main(int argc, char* argv[]) { for (int i = 0; i <= 1000000; i++) { fp[i] = qp(i, mod - 2); } ios::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { sigA += b[i]; if (a[i]) { sigG += b[i]; } else { sigB += b[i]; } } for (int i = 0; i < n; i++) { if (a[i]) { cout << getAns(i) << endl; } else { cout << getAns2(i) << endl; } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, A, B, S, sl, fh, a[200010], w[200010], inv[3010 << 1], f[3010][3010], g[3010][3010]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int _pow(int k, int i) { int t = 1; while (i) { if (i & 1) t = 1ll * t * k % 998244353; k = 1ll * k * k % 998244353; i >>= 1; } return t; } int main() { n = rd(); m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) { w[i] = rd(); S += w[i]; if (a[i]) A += w[i]; else B += w[i]; } for (int i = 0; i <= m * 2; ++i) inv[i] = _pow(S + i - m, 998244353 - 2); for (int i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (int j = m - i - 1; ~j; --j) f[i][j] = (1ll * (A + i + 1) * f[i + 1][j] + 1ll * (B - j) * f[i][j + 1]) % 998244353 * inv[i - j + m] % 998244353, g[i][j] = (1ll * (B - j - 1) * g[i][j + 1] + 1ll * (A + i) * g[i + 1][j]) % 998244353 * inv[i - j + m] % 998244353; } A = f[0][0]; B = g[0][0]; for (int i = 1; i <= n; ++i) if (a[i]) printf("%lld\n", 1ll * A * w[i] % 998244353); else printf("%lld\n", 1ll * B * w[i] % 998244353); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 10000; const int mod = 998244353; long long a[maxn], w[maxn], inv[maxn], dp[100][100][100], A, B, m, n; void add(long long& x, long long y) { x = (x + y) % mod; } long long mul(long long x, long long y) { return (x * y) % mod; } void mul2(long long& x, long long y) { x = (x * y) % mod; } void solve(long long x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (long long i = 0; i <= m; i++) { for (long long j = 0; j <= i; j++) { for (long long k = 0; k <= j; k++) { if (a[x]) { long long wx = w[x] + k, wa = A + j, wb = B - (i - j); add(dp[i + 1][j + 1][k + 1], mul(mul(dp[i][j][k], wx), inv[wa + wb])); add(dp[i + 1][j + 1][k], mul(mul(dp[i][j][k], wa - wx), inv[wa + wb])); add(dp[i + 1][j][k], mul(mul(dp[i][j][k], wb), inv[wa + wb])); } else { long long wx = w[x] - k, wb = B - j, wa = A + (i - j); add(dp[i + 1][j + 1][k + 1], mul(mul(dp[i][j][k], wx), inv[wa + wb])); add(dp[i + 1][j + 1][k], mul(mul(dp[i][j][k], (wb - wx)), inv[wa + wb])); add(dp[i + 1][j][k], mul(mul(dp[i][j][k], wa), inv[wa + wb])); } } } } long long re = 0; for (long long i = 0; i <= m; i++) { for (long long j = 0; j <= i; j++) { if (a[x]) { long long val = w[x] + j; add(re, mul(val, dp[m][i][j])); } else { long long val = w[x] - j; add(re, mul(val, dp[m][i][j])); } } } printf("%I64d\n", re); } long long mypow(long long x, long long y) { long long re = 1; while (y) { if (y & 1LL) mul2(re, x); y >>= 1; mul2(x, x); } return re; } void prework() { for (long long i = 1; i <= 3000; i++) { inv[i] = mypow(i, mod - 2); } } int main() { prework(); scanf("%I64d%I64d", &n, &m); A = B = 0; for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (long long i = 1; i <= n; i++) { scanf("%I64d", &w[i]); if (a[i]) A += w[i]; else B += w[i]; } for (long long i = 1; i <= n; i++) solve(i); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, A, B; int a[200010], w[200010]; int quick_pow(int x, int p) { int an = 1; int po = x; while (p) { if (p & 1) an = 1ll * an * po % 998244353; po = 1ll * po * po % 998244353; p >>= 1; } return an; } int f[3010][3010], g[3010][3010]; int inv[3010 * 2]; int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) w[i] = read(); for (int i = 1; i <= n; i++) { A += (a[i] == 1) * w[i]; A %= 998244353; B += (a[i] == 0) * w[i]; B %= 998244353; } for (int i = 1; i <= 2 * m; i++) inv[i] = quick_pow(A + B + i - m, 998244353 - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, B); j >= 0; j--) { f[i][j] = (1ll * f[i + 1][j] * (A + i + 1) % 998244353 * inv[i - j + m] % 998244353 + 1ll * f[i][j + 1] * (B - j) % 998244353 * inv[i - j + m] % 998244353) % 998244353; g[i][j] = (1ll * g[i + 1][j] * (A + i) % 998244353 * inv[i - j + m] % 998244353 + 1ll * g[i][j + 1] * (B - j - 1) % 998244353 * inv[i - j + m] % 998244353) % 998244353; } } for (int i = 1; i <= n; i++) { if (a[i] == 1) printf("%d\n", 1ll * w[i] * f[0][0] % 998244353); else printf("%d\n", 1ll * w[i] * g[0][0] % 998244353); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int NN = 200020, MM = 3030; const int mod = 998244353; int power(int a, int b, int m = mod, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } unordered_map<int, int> mp; int inv(int a) { if (a < 1) return 0; if (mp.count(a)) return mp[a]; return mp[a] = power(a, mod - 2); } inline void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int a[NN]; int wei[NN]; int dp[MM][MM]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", wei + i); int good = 0, bad = 0; for (int i = 1; i <= n; i++) { if (a[i]) good += wei[i]; else bad += wei[i]; } dp[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { if (bad - (i - j) >= 0) { add(dp[i + 1][j], (long long)dp[i][j] * (bad - (i - j)) % mod * inv(good + j + bad - (i - j)) % mod); add(dp[i + 1][j + 1], (long long)dp[i][j] * (good + j) % mod * inv(good + j + bad - (i - j)) % mod); } } } int Bad = 0, Good = 0; for (int i = 0; i <= m; i++) { add(Good, (long long)(good + i) * dp[m][i] % mod); add(Bad, (long long)(bad - (m - i)) * dp[m][i] % mod); } int igood = inv(good), ibad = inv(bad); for (int i = 1; i <= n; i++) { if (a[i]) { printf("%d ", (long long)Good * wei[i] % mod * igood % mod); } else { printf("%d ", (long long)Bad * wei[i] % mod * ibad % mod); } } puts(""); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 998244353; int64_t powmo(int64_t a, int64_t b) { int64_t r = 1; for (; b; b >>= 1) { if (b & 1) r = r * a % MOD; a = a * a % MOD; } return r; } int64_t mdiv(int64_t a, int64_t b) { return a * powmo(b, MOD - 2) % MOD; } bitset<300005> like; int64_t w[300005]; int64_t ans[300005]; int64_t dp[3005][3005]; int32_t main() { { cin.tie(0); ios_base::sync_with_stdio(false); }; int64_t n, m, t, add, sub, sum; add = sub = 0; cin >> n >> m; for (int64_t i = 0; i < n; i++) { cin >> t; like[i] = t; } for (int64_t i = 0; i < n; i++) { cin >> w[i]; if (like[i]) add += w[i]; else sub += w[i]; } dp[0][0] = 1; sum = add + sub; for (int64_t x = 1; x <= m; x++) { for (int64_t i = x, j = 0; i >= 0 && j <= sub; i--, j++) { int64_t nowsum = sum + i - j; int64_t nowadd = add + i; int64_t nowsub = sub - j; dp[i][j] = 0; if (i > 0) dp[i][j] = (dp[i][j] + dp[i - 1][j] * mdiv(nowadd - 1, nowsum - 1) % MOD) % MOD; if (j > 0) dp[i][j] = (dp[i][j] + dp[i][j - 1] * mdiv(nowsub + 1, nowsum + 1) % MOD) % MOD; } } for (int64_t i = m, j = 0; i >= 0 && j <= sub; i--, j++) { for (int64_t k = 0; k < n; k++) { if (like[k]) ans[k] = (ans[k] + (w[k] + i * mdiv(w[k], add)) % MOD * dp[i][j]) % MOD; else ans[k] = (ans[k] + (w[k] - j * mdiv(w[k], sub) % MOD + MOD) % MOD * dp[i][j]) % MOD; } } for (int64_t i = 0; i < n; i++) cout << ans[i] << '\n'; return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m; int a[55], w[55]; int A, B; int f[55][55][55]; int inv[3010]; int quick_pow(int x, int p) { int an = 1; int po = x; while (p) { if (p & 1) an = 1ll * an * po % 998244353; po = 1ll * po * po % 998244353; p >>= 1; } return an; } void solve(int id) { memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { if (!f[i][j][k]) continue; if (a[id]) { int nowA = A + k, nowB = B - (i - k), sum = A + B + k - (i - k), noww = w[id] + j; f[i + 1][j + 1][k + 1] += 1ll * f[i][j][k] * noww % 998244353 * inv[sum] % 998244353; f[i + 1][j + 1][k + 1] %= 998244353; f[i + 1][j][k + 1] += 1ll * f[i][j][k] * (nowA - noww) % 998244353 * inv[sum] % 998244353; f[i + 1][j][k + 1] %= 998244353; f[i + 1][j][k] += 1ll * f[i][j][k] * nowB % 998244353 * inv[sum] % 998244353; f[i + 1][j][k] %= 998244353; } else { int nowA = A + k, nowB = B - (i - k), sum = A + B + k - (i - k), noww = w[id] - j; f[i + 1][j + 1][k] += 1ll * f[i][j][k] * noww % 998244353 * inv[sum] % 998244353; f[i + 1][j + 1][k] %= 998244353; f[i + 1][j][k + 1] += 1ll * f[i][j][k] * nowA % 998244353 * inv[sum] % 998244353; f[i + 1][j][k + 1] %= 998244353; f[i + 1][j][k] += 1ll * f[i][j][k] * (nowB - noww) % 998244353 * inv[sum] % 998244353; f[i + 1][j][k] %= 998244353; } } } } int res = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (!f[m][j][k]) continue; if (a[id]) res = (res + 1ll * (w[id] + j) * f[m][j][k] % 998244353) % 998244353; else res = (res + 1ll * (w[id] - j) * f[m][j][k] % 998244353) % 998244353; } } printf("%d\n", res); } int main() { n = read(); m = read(); for (int i = 1; i <= 3000; i++) inv[i] = quick_pow(i, 998244353 - 2); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) w[i] = read(); for (int i = 1; i <= n; i++) A += w[i] * (a[i] == 1), B += w[i] * (a[i] == 0); for (int i = 1; i <= n; i++) solve(i); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int64_t mod = 998244353; int64_t mod1 = 1e9 + 5; int64_t power(int64_t a, int64_t b) { if (b == 0) return 1; else if (b % 2 == 0) return power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod; else return (((a) % mod) * (power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod)) % mod; } inline int64_t inverse(int64_t a) { a %= mod; if (a < 0) a += mod; int64_t b = mod, u = 0, v = 1; while (a) { int64_t t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += mod; return u; } const int64_t ce = 1e9 + 7; int64_t fast_mod(int64_t input) { return input < ce ? input : input % ce; } int64_t full, toti, toti1; int64_t n, m; int64_t x; int64_t ch; int64_t dp[50][101][51][51]; int64_t solve(int64_t i, int64_t we, int64_t fav, int64_t fav1) { if (i == m) { return we; } int64_t &res = dp[i][we][fav][fav1]; if (res != -1) return res; int64_t tot = full; tot += fav; tot -= fav1; tot -= x; tot += we; res = 0; if (toti > 0) res += ((toti + fav) * inverse(tot)) % mod * solve(i + 1, we, fav + 1, fav1); res %= mod; if (ch) res += (we * inverse(tot)) % mod * solve(i + 1, we + 1, fav, fav1); else if (we > 0) res += (we * inverse(tot)) % mod * solve(i + 1, we - 1, fav, fav1); res %= mod; if (toti1 - fav1 > 0) res += (max((toti1 - fav1), (int64_t)0) * inverse(tot)) % mod * solve(i + 1, we, fav, fav1 + 1); res %= mod; return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; int64_t a[n + 1]; for (int64_t i = 1; i <= n; i++) cin >> a[i]; full = 0; toti = 0; toti1 = 0; int64_t w[n + 1]; for (int64_t i = 1; i <= n; i++) { cin >> w[i]; full += w[i]; if (a[i]) toti += w[i]; else toti1 += w[i]; } for (int64_t i = 1; i <= n; i++) { if (a[i]) { toti -= w[i]; } else { toti1 -= w[i]; } if (a[i]) ch = 1; else ch = 0; x = w[i]; memset(dp, -1, sizeof(dp)); cout << solve(0, w[i], 0, 0) << '\n'; if (a[i]) { toti += w[i]; } else { toti1 += w[i]; } } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int mod = 998244353; long long n, m, cnt[2], sum[2], a[55], p[55]; long long dp[55][55][55], val[55][55][55], vis[55][55][55]; long long qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = a * a % mod; } return ans; } long long inv(long long a) { return qpow(a, mod - 2); } long long dfs(long long c, long long p, long long i, long long j, long long k, long long x, long long y, long long z, long long ff) { if (!c) return p * x % mod; if (vis[i][j][k]) return val[i][j][k] * inv(dp[i][j][k]) % mod * p % mod; dp[i][j][k] = p; val[i][j][k] += dfs(c - 1, x * p % mod * inv(x + y + z) % mod, i + 1, j, k, max(0ll, x + ff), y, z, ff); val[i][j][k] += dfs(c - 1, y * p % mod * inv(x + y + z) % mod, i, j + 1, k, x, y + 1, z, ff); val[i][j][k] += dfs(c - 1, z * p % mod * inv(x + y + z) % mod, i, j, k + 1, x, y, max(0ll, z - 1), ff); val[i][j][k] %= mod; vis[i][j][k]++; return val[i][j][k]; } int main() { scanf("%lld", &n); scanf("%lld", &m); for (int i = 1; i < n + 1; ++i) scanf("%lld", &a[i]), cnt[a[i]]++; for (int i = 1; i < n + 1; ++i) scanf("%lld", &p[i]), sum[a[i]] += p[i]; for (int i = 1; i < n + 1; ++i) { memset(dp, 0, sizeof(dp)); memset(val, 0, sizeof(val)); memset(vis, 0, sizeof(vis)); printf("%lld\n", dfs(m, 1, 0, 0, 0, p[i], sum[1] - a[i] * p[i], sum[0] - (1 - a[i]) * p[i], a[i] * 2 - 1)); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 55; const long long mod = 998244353; long long fpow(long long x, long long y) { x %= mod; long long ret = 1; while (y) { if (y & 1) ret = (long long)ret * x % mod; y >>= 1; x = (long long)x * x % mod; } return ret; } int n, m; int a[N]; int w[N]; long long f[N][N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } int totlike = 0, totdislike = 0; for (int i = 1; i <= n; ++i) { cin >> w[i]; if (a[i]) totlike += w[i]; else totdislike += w[i]; } for (int i = 1; i <= n; ++i) { memset(f, 0, sizeof f); int sumlike = totlike - a[i] * w[i]; int sumdislike = totdislike - (1 - a[i]) * w[i]; f[0][0][0] = 1; for (int d = 0; d < m; ++d) { for (int like = 0; like <= d; ++like) { for (int dislike = 0; dislike <= d - like; ++dislike) { if (!f[d][like][dislike]) continue; int delta = d - like - dislike; int sum = sumlike + like + sumdislike - dislike + (w[i] + (a[i] ? 1 : -1) * delta); int prob = fpow(sum, mod - 2); if (a[i] || delta < w[i]) f[d + 1][like][dislike] += (f[d][like][dislike] * (w[i] + (a[i] ? 1 : -1) * delta) % mod) * prob % mod; f[d + 1][like + 1][dislike] += (f[d][like][dislike] * (sumlike + like) % mod) * prob % mod; if (dislike < sumdislike) f[d + 1][like][dislike + 1] += (f[d][like][dislike] * prob % mod) * (sumdislike - dislike) % mod; } } } long long ret = 0; for (int like = 0; like <= m; ++like) { for (int dislike = 0; dislike <= m - like; ++dislike) { int delta = m - like - dislike; ret += (w[i] + (a[i] ? 1 : -1) * delta) * f[m][like][dislike] % mod; ret %= mod; } } cout << ret << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; vector<long long> a, b; int cur = 0; long long su = 0; long long gpos = 0; long long gneg = 0; long long mod = 998244353; long long fpow(long long a, long long b, long long md) { long long res = 1; a = (a % md); while (b > 0) { if (b % 2 == 1) { res = (res * a) % md; } b = b / 2; a = (a * a) % md; } return res % md; } map<pair<pair<int, int>, pair<int, int> >, long long> dp; int n, m; long long f(int i, int my, int pos, int neg) { if (i == m) { return (b[cur] + my * a[cur]) % mod; } if (dp.count(make_pair(make_pair(i, my), make_pair(pos, neg)))) { return dp[make_pair(make_pair(i, my), make_pair(pos, neg))]; } long long denom = su + pos - neg; long long f1, f2, f3; f1 = f2 = f3 = 0; if (a[cur] == 1) { long long prob = b[cur] + my; if (prob > 0) { prob = (prob * fpow(denom, mod - 2, mod)) % mod; f1 = (prob * f(i + 1, my + 1, pos + 1, neg)) % mod; } long long probpos = gpos + pos - my - b[cur]; if (probpos > 0) { probpos = (probpos * fpow(denom, mod - 2, mod)) % mod; f2 = (probpos * f(i + 1, my, pos + 1, neg)) % mod; } long long probneg = gneg - neg; if (probneg > 0) { probneg = (probneg * fpow(denom, mod - 2, mod)) % mod; f3 = (probneg * f(i + 1, my, pos, neg + 1)) % mod; } dp[make_pair(make_pair(i, my), make_pair(pos, neg))] = (f1 + f2 + f3) % mod; return (f1 + f2 + f3) % mod; } else { long long prob = b[cur] - my; if (prob > 0) { prob = (prob * fpow(denom, mod - 2, mod)) % mod; f1 = (prob * f(i + 1, my + 1, pos, neg + 1)) % mod; } long long probpos = gpos + pos; if (probpos > 0) { probpos = (probpos * fpow(denom, mod - 2, mod)) % mod; f2 = (probpos * f(i + 1, my, pos + 1, neg)) % mod; } long long probneg = gneg - neg - b[cur] + my; if (probneg > 0) { probneg = (probneg * fpow(denom, mod - 2, mod)) % mod; f3 = (probneg * f(i + 1, my, pos, neg + 1)) % mod; } dp[make_pair(make_pair(i, my), make_pair(pos, neg))] = (f1 + f2 + f3) % mod; return (f1 + f2 + f3) % mod; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n >> m; a.resize(n + 1); b.resize(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; if (a[i] == 0) { a[i] = -1; gneg += b[i]; } else { gpos += b[i]; } su += b[i]; } for (int i = 0; i < n; i++) { dp.clear(); cur = i; cout << f(0, 0, 0, 0) << '\n'; } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long pow_mod(long long a, long long p) { if (p == 0) return 1; long long ret = pow_mod(a, p / 2); ret = ret * ret % mod; if (p % 2 == 1) ret = ret * a % mod; return ret; } long long _inv(long long a) { return pow_mod(a, mod - 2); } long long dp[55][55][55], inv[3005]; int w[55], a[55]; int main() { inv[0] = 1; for (int i = 1; i < 3005; i++) inv[i] = _inv(i); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; int like = 0, dislike = 0, tot = 0; for (int i = 1; i <= n; i++) { if (a[i] == 0) dislike += w[i]; else like += w[i]; tot += w[i]; } for (int i = 1; i <= n; i++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int j = 0; j < m; j++) for (int k = 0; k <= j; k++) for (int l = 0; l <= j; l++) { int cur = tot + 2 * l - j; if (cur < 0) continue; if (a[i] == 1) { int p1 = w[i] + k; int p2 = like + l - w[i] - k; int p3 = cur - like - l; dp[j + 1][k + 1][l + 1] = (dp[j + 1][k + 1][l + 1] + dp[j][k][l] * p1 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l + 1] = (dp[j + 1][k][l + 1] + dp[j][k][l] * p2 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l] = (dp[j + 1][k][l] + dp[j][k][l] * p3 % mod * inv[cur] % mod) % mod; } else if (a[i] == 0 && k <= w[i] && (j - l) <= dislike) { int p1 = w[i] - k; int p2 = dislike - (j - l) - (w[i] - k); int p3 = cur - dislike + (j - l); dp[j + 1][k + 1][l] = (dp[j + 1][k + 1][l] + dp[j][k][l] * p1 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l] = (dp[j + 1][k][l] + dp[j][k][l] * p2 % mod * inv[cur] % mod) % mod; dp[j + 1][k][l + 1] = (dp[j + 1][k][l + 1] + dp[j][k][l] * p3 % mod * inv[cur] % mod) % mod; } } long long ans = 0; for (int k = 0; k <= m; k++) for (int l = 0; l <= m; l++) { if (a[i] == 1) ans = (ans + dp[m][k][l] * (w[i] + k)) % mod; else if (k <= w[i]) ans = (ans + dp[m][k][l] * (w[i] - k)) % mod; } cout << ans << endl; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int rw = 998244353; int n, m; int a[52], w[52], sum1, sum2; int dp[52][52][52]; int ni[100005]; int ksm(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1LL * x * ans % rw; x = 1LL * x * x % rw; y >>= 1; } return ans; } int solve(int r) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { if (!dp[i][j][k]) continue; int v = dp[i][j][k]; if (a[r]) { dp[i + 1][j + 1][k + 1] = (dp[i + 1][j + 1][k + 1] + 1LL * v * (w[r] + j) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + 1LL * v * (sum1 + k - (w[r] + j)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k] = (dp[i + 1][j][k] + 1LL * v * (sum2 - (i - k)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; } else { dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + 1LL * v * (w[r] - j) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k] = (dp[i + 1][j][k] + 1LL * v * (sum2 - (i - k) - (w[r] - j)) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + 1LL * v * (sum1 + k) % rw * ni[sum1 + sum2 + 2 * k - i]) % rw; } } } } int ans = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (!dp[m][j][k]) continue; int v; if (a[r]) v = w[r] + j; else v = w[r] - j; ans = (ans + 1LL * v * dp[m][j][k]) % rw; } } return (ans + rw) % rw; } int main() { for (int i = 1; i <= 100000; i++) ni[i] = ksm(i, rw - 2); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); if (a[i]) (sum1 += w[i]) %= rw; else (sum2 += w[i]) %= rw; } for (int i = 1; i <= n; i++) { printf("%d\n", solve(i)); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; namespace FastIO { const int L = (1 << 20); char buf[L], *S, *T; inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, L, stdin); if (S == T) return EOF; } return *S++; } inline int read() { int s = 0, f = 1; char t = getchar(); while ('0' > t || t > '9') { if (t == '-') f = -1; t = getchar(); } while ('0' <= t && t <= '9') { s = (s << 1) + (s << 3) + t - '0'; t = getchar(); } return s * f; } } // namespace FastIO using FastIO::read; const int N = 55; const int Mod = 998244353; int dp[N][N][N * 2], sum, Sadd, Ssub; int w[N], a[N], n, m; int Inc(int x, int y) { return x + y >= Mod ? x + y - Mod : x + y; } int Dec(int x, int y) { return x - y < 0 ? x - y + Mod : x - y; } int Mul(int x, int y) { return (long long)x * y % Mod; } int Fpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = Mul(ans, a); b >>= 1; a = Mul(a, a); } return ans; } int Inv(int x) { return Fpow(x, Mod - 2); } void Dp(int c) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i < m; i++) { for (int k = 0; k <= i; k++) { int now = (a[c] ? w[c] + k : w[c] - k); if (now < 0) continue; for (int d = 0; d <= i; d++) { if (!dp[i][k][d]) continue; int Dw = sum + d - (i - d); Dw = Inv(Dw); int Up_add = Sadd + d; int Up_sub = Ssub - (i - d); if (a[c]) { dp[i + 1][k + 1][d + 1] = Inc(dp[i + 1][k + 1][d + 1], Mul(dp[i][k][d], Mul(now, Dw))); dp[i + 1][k][d + 1] = Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add - now, Dw))); dp[i + 1][k][d] = Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub, Dw))); } else { dp[i + 1][k + 1][d] = Inc(dp[i + 1][k + 1][d], Mul(dp[i][k][d], Mul(now, Dw))); dp[i + 1][k][d + 1] = Inc(dp[i + 1][k][d + 1], Mul(dp[i][k][d], Mul(Up_add, Dw))); dp[i + 1][k][d] = Inc(dp[i + 1][k][d], Mul(dp[i][k][d], Mul(Up_sub - now, Dw))); } } } } int Ans = 0; for (int k = 0; k <= m; k++) { int now = (a[c] ? w[c] + k : w[c] - k); if (now < 0) continue; for (int d = 0; d <= m; d++) { Ans = Inc(Ans, Mul(now, dp[m][k][d])); } } cout << Ans << '\n'; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) { w[i] = read(); if (a[i]) Sadd += w[i]; else Ssub += w[i]; sum += w[i]; } for (int i = 1; i <= n; i++) Dp(i); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long a[55], w[55], dp[55][55][55]; long long fast(long long x, long long b, long long res = 1) { while (b) { if (b & 1) res = res * x % 998244353; x = x * x % 998244353; b >>= 1; } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; long long sum = 0, s[2] = {0, 0}; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> w[i], sum += w[i]; s[a[i]] += w[i]; } for (int q = 1; q <= n; q++) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long ans = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= j; k++) { long long tmp = (a[q] == 1 ? 1 : -1); long long meow = fast(sum + tmp * (2 * j - i - 1), 998244353 - 2); if (s[1 - a[q]] > 0 && s[1 - a[q]] - tmp * (i - j - 1) > 0 && sum + tmp * (2 * j - i + 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k] * (s[1 - a[q]] - tmp * (i - j - 1)) % 998244353 * fast(sum + tmp * (2 * j - i + 1), 998244353 - 2) % 998244353) % 998244353; } if (s[a[q]] - w[q] > 0 && j > 0 && s[a[q]] + tmp * (j - 1 - k) - w[q] > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k] * (s[a[q]] + tmp * (j - 1 - k) - w[q]) % 998244353 * meow) % 998244353; } if (j > 0 && k > 0 && w[q] + tmp * (k - 1) > 0 && sum + tmp * (2 * j - i - 1) > 0) { dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k - 1] * (w[q] + tmp * (k - 1)) % 998244353 * meow) % 998244353; } if (i == m) ans = (ans + (w[q] + tmp * k) % 998244353 * dp[i][j][k] % 998244353) % 998244353; } } } cout << ans << '\n'; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long rev(const long long x) { if (x == 0) return 0; if (x == 1) return 1; return (998244353LL - 998244353LL / x) * rev(998244353LL % x) % 998244353LL; } long long a[55LL], w[55LL], SA, SB, revv[8 * 55LL * 55LL]; map<long long, long long> f, g; int n, m; long long df(int j, int i, int k, int l) { if (f.count(j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l)) return f[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l]; long long c = 0; if (i == 0) c = j; else { if (j) c = j * revv[k + l] % 998244353LL * df(j + 1, i - 1, k + 1, l) % 998244353LL; if (k > j) c = c + (k - j) * revv[k + l] % 998244353LL * df(j, i - 1, k + 1, l) % 998244353LL; if (l) c = c + l * revv[k + l] % 998244353LL * df(j, i - 1, k, l - 1) % 998244353LL; } c %= 998244353LL; f[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l] = c; return c; } long long dg(int j, int i, int k, int l) { if (g.count(j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l)) return g[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l]; long long c = 0; if (i == 0) c = j; else { if (k) c = k * revv[k + l] % 998244353LL * dg(j, i - 1, k + 1, l) % 998244353LL; if (j) c = c + j * revv[k + l] % 998244353LL * dg(j - 1, i - 1, k, l - 1) % 998244353LL; if (l > j) c = c + (l - j) * revv[k + l] % 998244353LL * dg(j, i - 1, k, l - 1) % 998244353LL; } c %= 998244353LL; g[j * 55LL * 55LL * 55LL * 55LL * 55LL + i * 55LL * 55LL * 55LL * 55LL + k * 55LL * 55LL + l] = c; return c; } int main() { for (int i = 0; i < 8 * 55LL * 55LL; i++) revv[i] = rev(i); n = read(); m = read(); for (int i = 0; i < n; i++) a[i] = read(); for (int i = 0; i < n; i++) w[i] = read(); for (int i = 0; i < n; i++) if (a[i]) SA += w[i]; else SB += w[i]; for (int i = 0; i < n; i++) if (a[i]) printf("%lld\n", df(w[i], m, SA, SB)); else printf("%lld\n", dg(w[i], m, SA, SB)); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int N = 200005, M = 3003; int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; inline long long qp(long long a, long long b) { if (b < 0) return b; if (b == 0) return 1; long long rt = 1; while (b) { if (b & 1) rt *= a; a = a * a; b >>= 1; rt %= mod, a %= mod; } return rt; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int i, j; cin >> n >> m; for (i = 1; i <= n; ++i) cin >> a[i]; for (i = 1; i <= n; ++i) { cin >> w[i]; sum[a[i]] += w[i]; sum[2] += w[i]; } for (i = max(0, m - sum[0]); i <= m * 2; ++i) inv[i] = qp(sum[2] + i - m, mod - 2); for (i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (j = min(m - i - 1, sum[0]); ~j; --j) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (i = 1; i <= n; ++i) cout << ((long long)w[i] * (a[i] ? f[0][0] : g[0][0])) % mod << endl; return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Andy Phan */ public class p1173e { static int MOD = 998244353; static int n; static int m; static long[] invert; static long[][] f; static long[][] g; static boolean[][] df; static boolean[][] dg; static int[] sum; public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); invert = new long[2*m]; int[] like = new int[n]; sum = new int[2]; int[] weight = new int[n]; for(int i = 0; i < n; i++) like[i] = in.nextInt(); for(int i = 0; i < n; i++) { weight[i] = in.nextInt(); sum[1-like[i]] += weight[i]; } for(int i = -(m-1); i < m; i++) invert[i+m-1] = modInv(sum[0]+sum[1]+i, MOD); f = new long[m][m]; g = new long[m][m]; df = new boolean[m][m]; dg = new boolean[m][m]; for(int i = 0; i < n; i++) if(like[i] == 0) System.out.println((g(0, 0)*weight[i])%MOD); else System.out.println((f(0, 0)*weight[i])%MOD); } static long f(int i, int j) { if(i+j == m) return 1; if(df[i][j]) return f[i][j]; df[i][j] = true; return f[i][j] = (((((sum[0]+i+1)*invert[i-j+m-1])%MOD)*f(i+1, j))%MOD+((((sum[1]-j)*invert[i-j+m-1])%MOD)*f(i, j+1))%MOD)%MOD; } static long g(int i, int j) { if(i+j == m) return 1; if(dg[i][j]) return g[i][j]; dg[i][j] = true; return g[i][j] = (((((sum[0]+i)*invert[i-j+m-1])%MOD)*g(i+1, j))%MOD+((((sum[1]-j-1)*invert[i-j+m-1])%MOD)*g(i, j+1))%MOD)%MOD; } //@ static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a / gcd(a, b) * b; } //@ // Computes the modular inverse of x // Returns 0 if the GCD of x and mod is not 1 // O(log n) : Can be converted to use BigIntegers static long modInv(long x, long mod) { if(x == 1) return 1; if(gcd(x, mod) != 1) return 0; long r = mod % x; return (modInv(r, x % r, 0, 1, mod / x, x / r)+mod)%mod; } static long modInv(long a, long b, long y0, long y1, long q0, long q1) { long y2 = y0 - y1*q0; return b == 0 ? y2 : modInv(b, a % b, y1, y2, q1, a / b); } }
JAVA