LOJ#3090. 「BJOI2019」勘破神机

为了这题我去学习了一下BM算法。。

很容易发现这2的地方是\(F_{1} = 1,F_{2} = 2\)的斐波那契数列

3的地方是\(G_{1} = 3,G_{2} = 11\)其中下标表示长度的\(\frac{1}{2}\),可以得到\(G_{3} = 4G_{2} - G_{1}\)

然后我们列一波特征根方程,可以得到

\(m = 2\)时

\[\left\{\begin{matrix}
x_{1} = \frac{1 + \sqrt{5}}{2}
\\ x_{2} = \frac{1 - \sqrt{5}}{2}
\\ c_{1} = \frac{5 + \sqrt{5}}{10}
\\ c_{2} = \frac{5 - \sqrt{5}}{10}

\end{matrix}\right.
\]

\(m = 3\)时

\[\left\{\begin{matrix}
x_{1} = 2 + \sqrt{3}
\\ x_{2} = 2 - \sqrt{3}
\\ c_{1} = \frac{3 + \sqrt{3}}{6}
\\ c_{2} = \frac{3 - \sqrt{3}}{10}

\end{matrix}\right.
\]

然后我们会可以把阶乘\(n(n - 1)(n - 2)...(n - k + 1)\)当成一个k次多项式

然后暴力算出每一项的系数,然后我们要算的就是

\(\sum_{i = l}^{r} f_{i}^{k}\)

然后把通项带进去

\(\sum_{i = l}^{r} (c_{1}x_{1}^{i} + c_{2}x_{2}^{i})^{k}\)

\(\sum_{i = l}^{r}\sum_{j = 0}^{k}\binom{k}{j}c_{1}^{j}x_{1}^{ij}c_{2}^{k - j}x_{2}^{i(k - j)}\)

\(\sum_{j = 0}^{k}\binom{k}{j}c_{1}^{j}c_{2}^{k - j}\sum_{i = l}^{r}(x_{1}^{j}x_{2}^{k - j})^{i}\)

后面是等比数列求和,算一下就好了

复杂度是\(k^{2}\log r\)的

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 2005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
template<int T>
struct Complex {
int r,i;
Complex(int _r = 0,int _i = 0) {r = _r;i = _i;}
friend Complex operator + (const Complex &a,const Complex &b) {
return Complex(inc(a.r,b.r),inc(a.i,b.i));
}
friend Complex operator - (const Complex &a,const Complex &b) {
return Complex(inc(a.r,MOD - b.r),inc(a.i,MOD - b.i));
}
friend Complex operator * (const Complex &a,const Complex &b) {
return Complex(inc(mul(a.r,b.r),mul(T,mul(a.i,b.i))),inc(mul(a.r,b.i),mul(b.r,a.i)));
}
friend Complex fpow(Complex x,int64 c) {
Complex res(1,0),t = x;
while(c) {
if(c & 1) res = res * t;
t = t * t;
c >>= 1;
}
return res;
}
friend Complex Query(Complex x,int64 n) {
if(x.r == 1 && !x.i) return (n + 1)% MOD;
Complex u = 1 - fpow(x,n + 1),d = 1 - x,t;
t = d;t.i = MOD - t.i;
int iv = fpow((t * d).r,MOD - 2);
u = u * t * iv;
return u;
}
};
int m,k,f[1005],fac[1005],invfac[1005];
int64 l,r;
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
namespace mequal2 {
Complex<5> x1,x2,c1,c2;
void Main() {
int inv2 = fpow(2,MOD - 2),inv10 = fpow(10,MOD - 2);
x1 = Complex<5>(inv2,inv2);x2 = Complex<5>(inv2,MOD - inv2);
c1 = Complex<5>(mul(5,inv10),inv10);c2 = Complex<5>(mul(5,inv10),MOD - inv10);
int ans = mul(invfac[k],fpow((r - l + 1) % MOD,MOD - 2));
Complex<5> res;
for(int j = 1 ; j <= k ; ++j) {
Complex<5> s;
for(int h = 0 ; h <= j ; ++h) {
Complex<5> t = C(j,h) * fpow(c1,h) * fpow(c2,j - h),q = fpow(x1,h) * fpow(x2,j - h),p;
p = Query(q,r) - Query(q,l - 1);
s = s + t * p;
}
res = res + s * f[j];
}
ans = mul(ans,res.r);
out(ans);enter;
}
}
namespace mequal3 {
Complex<3> x1,x2,c1,c2;
void Main() {
int inv6 = fpow(6,MOD - 2);
x1 = Complex<3>(2,1);x2 = Complex<3>(2,MOD - 1);
c1 = Complex<3>(mul(3,inv6),inv6);c2 = Complex<3>(mul(3,inv6),MOD - inv6);
int ans = mul(invfac[k],fpow((r - l + 1) % MOD,MOD - 2));
r = r / 2;
if(l & 1) l = (l + 1) / 2;
else l = l / 2; Complex<3> res;
if(l <= r) {
for(int j = 1 ; j <= k ; ++j) {
Complex<3> s;
for(int h = 0 ; h <= j ; ++h) {
Complex<3> t = C(j,h) * fpow(c1,h) * fpow(c2,j - h),q = fpow(x1,h) * fpow(x2,j - h),p;
p = Query(q,r) - Query(q,l - 1);
s = s + t * p;
}
res = res + s * f[j];
}
}
ans = mul(ans,res.r);
out(ans);enter;
}
}
void Solve() {
read(l);read(r);read(k);
memset(f,0,sizeof(f));
f[1] = 1;
for(int i = 1 ; i < k ; ++i) {
for(int j = i + 1 ; j >= 1 ; --j) {
f[j] = inc(mul(MOD - i,f[j]),f[j - 1]);
}
}
if(m == 2) mequal2::Main();
else mequal3::Main();
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int T;
read(T);read(m);
fac[0] = 1;
for(int i = 1 ; i <= 1000 ; ++i) fac[i] = mul(fac[i - 1],i);
invfac[1000] = fpow(fac[1000],MOD - 2);
for(int i = 999 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
for(int i = 1 ; i <= T ; ++i) {
Solve();
}
}

【LOJ】#3090. 「BJOI2019」勘破神机的更多相关文章

  1. LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域

    题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...

  2. loj 3090 「BJOI2019」勘破神机 - 数学

    题目传送门 传送门 题目大意 设$F_{n}$表示用$1\times 2$的骨牌填$2\times n$的网格的方案数,设$G_{n}$$表示用$1\times 2$的骨牌填$3\times n$的网 ...

  3. Loj #3089. 「BJOI2019」奥术神杖

    Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...

  4. Loj #3093. 「BJOI2019」光线

    Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...

  5. [BJOI2019]勘破神机(斯特林数,数论)

    [BJOI2019]勘破神机(斯特林数,数论) 题面 洛谷 题解 先考虑\(m=2\)的情况. 显然方案数就是\(f_i=f_{i-1}+f_{i-2}\),即斐波那契数,虽然这里求出来是斐波那契的第 ...

  6. [BJOI2019]勘破神机

    [BJOI2019]勘破神机 推式子好题 m=2,斐波那契数列,$f_{n+1}$项 不妨$++l,++r$,直接求$f_n$ 求$\sum C(f_n,k)$,下降幂转化成阶乘幂,这样都是多项式了, ...

  7. LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划

    题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...

  8. LOJ 3094 「BJOI2019」删数——角标偏移的线段树

    题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...

  9. LOJ 3093 「BJOI2019」光线——数学+思路

    题目:https://loj.ac/problem/3093 考虑经过种种反射,最终射下去的光线总和.往下的光线就是这个总和 * a[ i ] . 比如只有两层的话,设射到第二层的光线是 lst ,那 ...

随机推荐

  1. hdu 5556 Land of Farms 最大团+暴力

    Land of Farms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. 【转载】C++ STL priority_queue用法

    priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为 ...

  3. 【luoguP1991】 无线通讯网--最小生成树

    题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...

  4. plotly绘图

    import plotly.plotly as plt import plotly.offline as pltoff from plotly.graph_objs import * # 生成折线图 ...

  5. C++虚函数和成员函数内存 this指针问题

    father* p=new son; p->disp(...); 发现有好多人this指针的本质有错误的认识,估计不少人一说起this指针,脑袋立即反应出:那个类里使用的this指针,那么这个指 ...

  6. 线程的join()方法

    官网描述 join public final void join() throws InterruptedException Waits for this thread to die. An invo ...

  7. P2089 烤鸡

    题目背景 猪猪hanke得到了一只鸡 题目描述 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末.孜然等),每种配料可以放1—3克, ...

  8. ARTS打卡计划第十六周

    Algorithms: https://leetcode-cn.com/problems/min-stack/submissions// Review: https://www.infoq.cn/ar ...

  9. 打印li索引值

    <ul> <li>这是第一条alert(0)</li> <li>这是第二条alert(1)</li> <li>这是第三条aler ...

  10. redis常见7种使用场景

    一,简单字符串缓存实例 $redis->connect('127.0.0.1', 6379); $strCacheKey = 'Test_bihu'; //SET 应用 $arrCacheDat ...