AtCoder Dwango Programming Contest V E.Cyclic GCD (DP+Polynomials) 日付: 11月 25, 2018 リンクを取得 Facebook × Pinterest メール 他のアプリ [AtCoder - E - Cyclic GCD](https://dwacon5th-prelims.contest.atcoder.jp/tasks/dwacon5th_prelims_e) ## Problem Given an array of nnn elements A1,⋯ ,AnA_1, \cdots, A_nA1,⋯,An, there are n!n!n! permutations. For any permutation ppp, elements with index iii and pip_ipi is connected and some cycles are formed. Define the beauty of a permutation ppp the *product* of smallest element in all cycles. And Define bib_ibi the *sum* of beauty of all permutations that form exactly iii cycles. The task is to calculate GCD of b1,⋯ ,bnb_1, \cdots, b_nb1,⋯,bn mod 998244353. **Constraints:** 1≤n≤105,1≤Ai≤1091\le n \le 10^5, 1 \le A_i \le 10^91≤n≤105,1≤Ai≤109 ## Solution It is easy to observe that order does not matter, we can sort AAA increasingly. Let f(i,j)f(i,j)f(i,j) be current bjb_jbj considering the first iii elements of AAA, when adding the (i+1)(i+1)(i+1)-th element, there are 2 cases: 1. Add Ai+1A_{i+1}Ai+1 to any position in any cycle that are formed by previous elements. This will not change the beauty of permutation. 2. Ai+1A_{i+1}Ai+1 itself forms a new cycle, this will multiply the beauty of permutation by Ai+1A_{i+1}Ai+1. The **state-transition equation** is obvious:f(i+1,j+1)=i⋅f(i,j+1)+A[i]⋅f(i,j)f(i+1,j+1) = i\cdot f(i, j+1) + A[i] \cdot f(i, j)f(i+1,j+1)=i⋅f(i,j+1)+A[i]⋅f(i,j) However, calculating all f(n,i)f(n,i)f(n,i) is of time complexity O(n2)O(n^2)O(n2). It's not affordable. We can construct such **polynomial** Pi(t)=∑j=0nf(i,j)⋅tjP_i(t)=\sum_{j=0}^n f(i,j)\cdot t^jPi(t)=j=0∑nf(i,j)⋅tj The transition equation of PPP does not depend on jjj Pi+1(t)=(Ai+1t+i)Pi(t)P_{i+1}(t)=(A_{i+1}t+i)P_i(t)Pi+1(t)=(Ai+1t+i)Pi(t) The 1-shift of jjj in the original dp is convenient to be expressed in the polynomial form (just by multiplying ttt). The answer is GCD of all coefficients of Pn(t)P_n(t)Pn(t) and P0(t)=1P_0(t)=1P0(t)=1 Another observation is that: **GCD of coeffients of P⋅QP\cdot QP⋅Q equals to the product of GCD of coefficnets of PPP and QQQ**. Then the answer is ∏i=0ngcd(Ai+1,i)\prod_{i=0}^n gcd(A_{i+1}, i)∏i=0ngcd(Ai+1,i) ## Code ```c++ #include <bits/stdc++.h> using namespace std; using LL = long long; #define FI(i,a,b) for(int i=(a);i<=(b);++i) const int Mod = 998244353; int main() { int n; scanf("%d", &n); vector<int> A(n + 1); FI(i, 1, n) scanf("%d", &A[i]); sort(A.begin() + 1, A.end()); int ans = A[1] % Mod; FI(i, 2, n) ans = 1LL * ans * __gcd(A[i], i - 1) % Mod; cout << ans << endl; } ``` コメント
コメント
コメントを投稿