P1397 [NOI2013]矩阵游戏(递推)
一波化式子,$f[1][m]=a^{m-1}+b\sum_{i=0}^{m-2}a^i$,用快速幂+逆元求等比数列可以做到$logm$
设$v=a^{m-1},k=\sum_{i=0}^{m-2}a^i$
那么$f[1][m]=v+bk$
再对纵列化一波式子,$f[i][m]=f[i-1][m]*vc+bk+vd$
如果你直接上个矩乘可以拿到65的好分数
#include<iostream>
#include<cstdio>
#include<cstring>
#define ri register int
using namespace std;
typedef long long ll;
const ll P=1e9+;
const ll W=1e9;
char q[];
struct bnum{
ll a[],len;
bnum(){memset(a,,sizeof(a));len=;}
void init(){
scanf("%s",q); int z=; len=;
for(ri i=strlen(q)-;i>=;--i){
a[len]+=(q[i]-)*z; z*=;
if(z==W) z=,++len;
}
while(!a[len]&&len) --len;
}
ll mod(){
ll re=;
for(ri i=;i<=len;++i) re=(re*W+a[i])%P;
return re;
}
void rem1(){
--a[];
for(ri i=;a[i]<;++i) a[i]+=W,--a[i+];
while(!a[len]&&len) --len;
}
bnum div2(){
bnum c; c.len=len; ll x=;
for(ri i=len;i;--i)
x=x*W+a[i],c.a[i]=x/,x%=;
while(!c.a[c.len]&&c.len) --c.len;
return c;
}
}n,m;
struct mat{
ll a[][];
mat(){memset(a,,sizeof(a));}
mat operator * (const mat &b) const{
mat c;
for(ri i=;i<;++i)
for(ri j=;j<;++j)
for(ri k=;k<;++k)
c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j]%P)%P;
return c;
}
}s,p;
ll Pow(ll x,ll y){
ll re=;
for(;y;y>>=,x=x*x%P) if(y&) re=re*x%P;
return re;
}
ll Pow_b(ll x,bnum y){
ll re=;
for(;y.len;y=y.div2(),x=x*x%P) if(y.a[]&) re=re*x%P;
return re;
}
mat Pow_m(mat x,bnum y){
mat re; for(ri i=;i<;++i) re.a[i][i]=;
for(;y.len;y=y.div2(),x=x*x) if(y.a[]&) re=re*x;
return re;
}
int main(){
ll a,b,c,d,v,k;
n.init(); m.init(); n.rem1(); m.rem1();
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
v=Pow_b(a,m);
if(a>) k=(v-+P)%P*Pow((a-+P)%P,P-)%P;
else k=m.mod();
s.a[][]=(b*k+v)%P; s.a[][]=(b*k%P+v*d%P)%P;
p.a[][]=v*c%P; p.a[][]=p.a[][]=;
p=Pow_m(p,n); s=s*p;
printf("%lld",s.a[][]);
return ;
}
65pts
观察发现,这个矩乘可以再化:
$f[n][m]=(v+bk)(vc)^{n-1}+(bk+vd)\sum_{i=0}^{n-2}(vc)^i$
观察这个式子,复杂度主要在快速幂上,复杂度$O(logn+logm)$
考虑缩小$n,m$
假设存在:$a^n=a^{n-x}\, mod \; p$,$p$为质数
$\therefore a^x=1\, mod \; p$
根据费马小定理,$x=p-1$
$\therefore a^n=a^{n\, mod\, p-1}\, mod \; p$
输入$n,m$时记下它们$mod\, p,p-1$的值,代入式子即可
注意等比数列公比$=1$时需要特判
#include<iostream>
#include<cstdio>
#include<cstring>
#define ri register int
using namespace std;
typedef long long ll;
const ll P=1e9+;
ll a,b,c,d,v,k,n,m,_n,_m,a_,k_,v_;
void init(){
char c=getchar();
while(c<''||c>'') c=getchar();
while(''<=c&&c<=''){
n=(n*+c-)%P;
_n=(_n*+c-)%(P-);
c=getchar();
}c=getchar();
while(c<''||c>'') c=getchar();
while(''<=c&&c<=''){
m=(m*+c-)%P;
_m=(_m*+c-)%(P-);
c=getchar();
}
}
ll Pow(ll x,ll y){
ll re=;
for(;y;y>>=,x=x*x%P) if(y&) re=re*x%P;
return re;
}
int main(){
init(); scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
n=(n-+P)%P; _n=(_n-+P-)%(P-);
m=(m-+P)%P; _m=(_m-+P-)%(P-);
v=Pow(a,_m);
k=a>?(v-+P)*Pow(a-+P,P-)%P:m;
a_=v*c%P; v_=Pow(a_,_n);
k_=a_>?(v_-+P)*Pow(a_-+P,P-)%P:n;
printf("%lld",((v+b*k)%P*v_%P+(b*k+v*d)%P*k_%P)%P);
return ;
}
P1397 [NOI2013]矩阵游戏(递推)的更多相关文章
- luogu P1397 [NOI2013]矩阵游戏
传送门 题目中那两个递推式显然可以写成矩乘的形式,然后十进制快速幂即可.这里不再赘述 只有两个递推式,我们可以考虑一波推式子,首先第一行的元素应该分别是\(1,a+b,a^2+ab+b,a^3+a^2 ...
- 洛谷P1397 [NOI2013]矩阵游戏
矩阵快速幂+费马小定理 矩阵也是可以跑费马小定理的,但是要注意这个: (图是盗来的QAQ) 就是说如果矩阵a[i][i]都是相等的,那么就是mod p 而不是mod p-1了 #include< ...
- 洛谷P1397 [NOI2013]矩阵游戏(十进制矩阵快速幂)
题意 题目链接 Sol 感觉做这题只要对矩阵乘法理解的稍微一点就能做出来对于每一行构造一个矩阵A = a 1 0 b列与列之间的矩阵为B = c 1 0 d最终答案为$A^{n - ...
- P1397 [NOI2013]矩阵游戏
传送门 首先显然可以矩乘快速幂然后 $T$ 飞 看一眼题解发现因为这一题矩阵的特殊性所以可以对矩阵的次数欧拉降幂 然而我并不懂证明,所以我选择暴力乱搞的做法 十进制快速幂,然后注意一下常数,还有矩阵乘 ...
- bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 613 Solved: 256[Submit][Status] ...
- BZOJ 3240: [Noi2013]矩阵游戏
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1586 Solved: 698[Submit][Status ...
- BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 123 Solved: 73 [ Submit][ St ...
- (十进制高速幂+矩阵优化)BZOJ 3240 3240: [Noi2013]矩阵游戏
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3240 3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec M ...
- HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)
题解见X姐的论文 矩阵乘法递推的优化.仅仅是mark一下. .
随机推荐
- word和画图
文档和画图收费文档:edu.51cto.com/course/course_id-4992.htmledu.51cto.com/course/course_id-4991.html
- hdu 1695 欧拉函数+容斥原理
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- x-pack邮件报警功能
1):修改elasticsearch.yml xpack.notification.email.account: work: profile: standard email_defaults: ...
- android日志优先级
Android 的日志分为如下几个优先级(priority): V —— Verbose(最低,输出得最多) D —— Debug I —— Info W —— Warning E —— Error ...
- ES排序值相同顺序随机的问题
ES排序值相同顺序随机的问题 code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: ...
- 使用SharpZIpLib写的压缩解压操作类
使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary&g ...
- linux 汇编函数调用
edi第一个参数 esi第二个参数 edx第三个参数 rax保存结果 C++代码如下: char* demo(char* a,int b){ static char* buf=0; if(!buf)b ...
- Word2Vec模型参数 详解
用gensim函数库训练Word2Vec模型有很多配置参数.这里对gensim文档的Word2Vec函数的参数说明进行翻译,以便不时之需. class gensim.models.word2vec.W ...
- FreeBSD上安装Cassandra 3.10
哈哈,你居然点进来了,来吧,一起吐槽FreeBSD啊,装了一上午Cassandra 3.10都没有装成功, 终于,鄙人一条 shutdown -p now 结束了FreeBSD,默默打开了CentOS ...
- HTML学习之==>HTML标签
前端的三把利器: HTML:一个人 CSS:这个人的衣服 JS:这个人的行为 HTML(超文本标记语言) html代码实际上就是一套能够被浏览器所识别的规则代码,由一个个标签组成.html代码就是一大 ...