-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fast-Kitamasa.cpp
217 lines (202 loc) · 6.89 KB
/
Fast-Kitamasa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// codechef RNG (Random Number Generator)
// BOJ 13725
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define IDX(v, x) (lower_bound(all(v), x) - v.begin())
using namespace std;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
template<int M>
struct MINT{
int v;
MINT() : v(0) {}
MINT(ll val){
v = (-M <= val && val < M) ? val : val % M;
if(v < 0) v += M;
}
friend istream& operator >> (istream &is, MINT &a) { ll t; is >> t; a = MINT(t); return is; }
friend ostream& operator << (ostream &os, const MINT &a) { return os << a.v; }
friend bool operator == (const MINT &a, const MINT &b) { return a.v == b.v; }
friend bool operator != (const MINT &a, const MINT &b) { return a.v != b.v; }
friend MINT pw(MINT a, ll b){
MINT ret= 1;
while(b){
if(b & 1) ret *= a;
b >>= 1; a *= a;
}
return ret;
}
friend MINT inv(const MINT a) { return pw(a, M-2); }
MINT operator - () const { return MINT(-v); }
MINT& operator += (const MINT m) { if((v += m.v) >= M) v -= M; return *this; }
MINT& operator -= (const MINT m) { if((v -= m.v) < 0) v += M; return *this; }
MINT& operator *= (const MINT m) { v = (ll)v*m.v%M; return *this; }
MINT& operator /= (const MINT m) { *this *= inv(m); return *this; }
friend MINT operator + (MINT a, MINT b) { a += b; return a; }
friend MINT operator - (MINT a, MINT b) { a -= b; return a; }
friend MINT operator * (MINT a, MINT b) { a *= b; return a; }
friend MINT operator / (MINT a, MINT b) { a /= b; return a; }
operator int32_t() const { return v; }
operator int64_t() const { return v; }
};
namespace fft{
template<int W, int M>
static void NTT(vector<MINT<M>> &f, bool inv_fft = false){
using T = MINT<M>;
int N = f.size();
vector<T> root(N >> 1);
for(int i=1, j=0; i<N; i++){
int bit = N >> 1;
while(j >= bit) j -= bit, bit >>= 1;
j += bit;
if(i < j) swap(f[i], f[j]);
}
T ang = pw(T(W), (M-1)/N); if(inv_fft) ang = inv(ang);
root[0] = 1; for(int i=1; i<N>>1; i++) root[i] = root[i-1] * ang;
for(int i=2; i<=N; i<<=1){
int step = N / i;
for(int j=0; j<N; j+=i){
for(int k=0; k<i/2; k++){
T u = f[j+k], v = f[j+k+(i>>1)] * root[k*step];
f[j+k] = u + v;
f[j+k+(i>>1)] = u - v;
}
}
}
if(inv_fft){
T rev = inv(T(N));
for(int i=0; i<N; i++) f[i] *= rev;
}
}
template<int W, int M>
vector<MINT<M>> multiply_ntt(vector<MINT<M>> a, vector<MINT<M>> b){
int N = 2; while(N < a.size() + b.size()) N <<= 1;
a.resize(N); b.resize(N);
NTT<W, M>(a); NTT<W, M>(b);
for(int i=0; i<N; i++) a[i] *= b[i];
NTT<W, M>(a, true);
return a;
}
}
template<int W, int M>
struct PolyMod{
using T = MINT<M>;
vector<T> a;
// constructor
PolyMod(){}
PolyMod(T a0) : a(1, a0) { normalize(); }
PolyMod(const vector<T> a) : a(a) { normalize(); }
// method from vector<T>
int size() const { return a.size(); }
int deg() const { return a.size() - 1; }
void normalize(){ while(a.size() && a.back() == T(0)) a.pop_back(); }
T operator [] (int idx) const { return a[idx]; }
typename vector<T>::const_iterator begin() const { return a.begin(); }
typename vector<T>::const_iterator end() const { return a.end(); }
void push_back(const T val) { a.push_back(val); }
void pop_back() { a.pop_back(); }
// basic manipulation
PolyMod reversed() const {
vector<T> b = a;
reverse(b.begin(), b.end());
return b;
}
PolyMod trim(int n) const {
return vector<T>(a.begin(), a.begin() + min(n, size()));
}
PolyMod inv(int n){
PolyMod q(T(1) / a[0]);
for(int i=1; i<n; i<<=1){
PolyMod p = PolyMod(2) - q * trim(i * 2);
q = (p * q).trim(i * 2);
}
return q.trim(n);
}
// operation with scala value
PolyMod operator *= (const T x){
for(auto &i : a) i *= x;
normalize();
return *this;
}
PolyMod operator /= (const T x){
return *this *= (T(1) / T(x));
}
// operation with poly
PolyMod operator += (const PolyMod &b){
a.resize(max(size(), b.size()));
for(int i=0; i<b.size(); i++) a[i] += b.a[i];
normalize();
return *this;
}
PolyMod operator -= (const PolyMod &b){
a.resize(max(size(), b.size()));
for(int i=0; i<b.size(); i++) a[i] -= b.a[i];
normalize();
return *this;
}
PolyMod operator *= (const PolyMod &b){
*this = fft::multiply_ntt<W, M>(a, b.a);
normalize();
return *this;
}
PolyMod operator /= (const PolyMod &b){
if(deg() < b.deg()) return *this = PolyMod();
int sz = deg() - b.deg() + 1;
PolyMod ra = reversed().trim(sz), rb = b.reversed().trim(sz).inv(sz);
*this = (ra * rb).trim(sz);
for(int i=sz-size(); i; i--) push_back(T(0));
reverse(all(a));
normalize();
return *this;
}
PolyMod operator %= (const PolyMod &b){
if(deg() < b.deg()) return *this;
PolyMod tmp = *this; tmp /= b; tmp *= b;
*this -= tmp;
normalize();
return *this;
}
// operator
PolyMod operator * (const T x) const { return PolyMod(*this) *= x; }
PolyMod operator / (const T x) const { return PolyMod(*this) /= x; }
PolyMod operator + (const PolyMod &b) const { return PolyMod(*this) += b; }
PolyMod operator - (const PolyMod &b) const { return PolyMod(*this) -= b; }
PolyMod operator * (const PolyMod &b) const { return PolyMod(*this) *= b; }
PolyMod operator / (const PolyMod &b) const { return PolyMod(*this) /= b; }
PolyMod operator % (const PolyMod &b) const { return PolyMod(*this) %= b; }
};
constexpr int W = 3, MOD = 104857601;
using mint = MINT<MOD>;
using poly = PolyMod<W, MOD>;
mint kitamasa(poly c, poly a, ll n){
poly d = vector<mint>{1};
poly xn = vector<mint>{0, 1};
poly f;
for(int i=0; i<c.size(); i++) f.push_back(-c[i]);
f.push_back(1);
while(n){
if(n & 1) d = d * xn % f;
n >>= 1; xn = xn * xn % f;
}
mint ret = 0;
for(int i=0; i<=a.deg(); i++) ret += a[i] * d[i];
return ret;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
ll K, N; cin >> K >> N;
vector<mint> v_dp(K), v_rec(K);
for(int i=0; i<K; i++){
int t; cin >> t; v_dp[i] = mint(t);
}
for(int i=0; i<K; i++){
int t; cin >> t; v_rec[i] = mint(t);
}
reverse(all(v_rec));
poly dp(v_dp), rec(v_rec);
cout << kitamasa(rec, dp, N-1);
}