题解:P14461 【MX-S10-T2】『FeOI-4』青年晚报
前情提要:
本做法为笔者赛时找规律做法,没有对结论加以证明,如需证明请看其他大佬题解,谢谢。
考虑:
继续拆:
继续:
依次类推,我们观察一下系数:
发现拆开
然后如果
然后由于
时间复杂度
#include <bits/stdc++.h>
#define int long long
constexpr int N = 5005,mod = 1e9 + 7;
int n,m,fac[N],inv[N];
struct Poly{
std::vector<int> F;
Poly(){
F.resize(m + 1,0);
}
};
Poly operator + (const Poly & a,const Poly & b){
Poly c;
for(int j = 0; j <= m; ++ j){
c.F[j] = (a.F[j] + b.F[j]) % mod;
}
return c;
}
Poly operator - (const Poly & a,const Poly & b){
Poly c;
for(int j = 0; j <= m; ++ j){
c.F[j] = (a.F[j] - b.F[j] + mod) % mod;
}
return c;
}
Poly der(Poly a){
Poly c;
for(int j = 0; j < m; ++ j){
c.F[j] = 1ll * (j + 1) * a.F[j + 1] % mod;
}
return c;
}
Poly Mul(Poly a,int k){
Poly c;
for(int j = 0; j <= m; ++ j){
c.F[j] = k * a.F[j] % mod;
}
return c;
}
inline int ksm(int x,int y){
int ans = 1;
while(y){
if(y & 1) ans = ans * x % mod;
x = x * x % mod; y >>= 1;
}
return ans;
}
inline int C(int x,int y){
int ans = 1;
for(int i = x - y + 1; i <= x; ++ i) ans = ans * i % mod;
return ans * inv[y] % mod;
}
signed main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> m;
Poly F,G,ans_F,ans_G,F_,G_;
for(int i = 0; i <= m; ++ i){
std::cin >> F.F[i];
}
for(int i = 0; i <= m; ++ i){
std::cin >> G.F[i];
}
F_ = F; G_ = G;
fac[0] = inv[0] = 1;
for(int i = 1; i <= m; ++ i){
fac[i] = fac[i - 1] * i % mod;
}
inv[m] = ksm(fac[m],mod - 2);
for(int i = m - 1; i >= 1; -- i){
inv[i] = inv[i + 1] * (i + 1) % mod;
}
if(n & 1){
Poly last = F;
F = G + der(G);
G = last - der(last);
F_ = F; G_ = G; -- n;
}
// if(n % 2 == 0){
// F_ = der(F_);
// F_ = der(F_);
// for(int i = 0; i <= m; ++ i){
// std::cout << F_.F[i] << ' ';
// }
// std::cout << '\n';
for(int i = 0; i <= n / 2; ++ i){
if(i * 2 > m) break;
if(i & 1){
ans_F = ans_F - Mul(F_,C(n / 2,i));
ans_G = ans_G - Mul(G_,C(n / 2,i));
}else{
ans_F = ans_F + Mul(F_,C(n / 2,i));
ans_G = ans_G + Mul(G_,C(n / 2,i));
}
// for(int j = 0; j <= m; ++ j){
// std::cout << ans.F[j] << ' ';
// }
// std::cout << '\n';
F_ = der(F_);
F_ = der(F_);
G_ = der(G_);
G_ = der(G_);
}
// }
for(int i = 0; i <= m; ++ i){
std::cout << ans_F.F[i] << ' ';
}
std::cout << '\n';
for(int i = 0; i <= m; ++ i){
std::cout << ans_G.F[i] << ' ';
}
return 0;
}
:::