首先,构造出从f[][i]->f[][i+1]的转移矩阵a,和从f[i][m]->f[i+1][1]的转移矩阵b,

那么从f[1][1]转移到f[n][m]就是init*(a^(m-1)*b)^(n-1)*(a^(m-1))。

然后用用十进制快速幂(因为输入用的是10进制,这样就避免了高精度除法)。

第一次写十进制快速幂,大概的思想是维护当前位是1~9的要乘的矩阵,然后再通过这9个矩阵自己转移。

 /**************************************************************
Problem: 3240
User: idy002
Language: C++
Result: Accepted
Time:5352 ms
Memory:2764 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#define N 1000010
#define Mod 1000000007
using namespace std; typedef long long dnt;
struct Matrix {
dnt v[][];
void make_unit() {
for( int i=; i<; i++ )
for( int j=; j<; j++ )
v[i][j] = (i==j);
}
inline const dnt* operator[]( int i ) const { return v[i]; }
Matrix(){}
Matrix( int aa, int ab, int ba, int bb ) {
v[][] = aa, v[][] = ab, v[][] = ba, v[][] = bb;
}
Matrix operator*( const Matrix &b ) const {
const Matrix &a = *this;
return Matrix( , (b[][]+a[][]*b[][])%Mod,
, a[][]*b[][]%Mod );
}
Matrix operator^( const char *b ) const {
Matrix rt, q[]; q[] = *this;
for( int i=; i<=; i++ )
q[i] = q[i-]*q[]; rt.make_unit();
for( int i=; b[i]; i++ ) {
if( b[i]-'' ) rt = rt*q[b[i]-''];
q[] = q[]*q[];
for( int j=; j<=; j++ )
q[j] = q[j-]*q[];
}
return rt;
}
}; char sn[N], sm[N];
int ln, lm;
int a, b, c, d;
Matrix ma, mb, ans; void subone( char s[] ) {
int i = ;
s[i]--;
while( s[i]<'' ) {
s[i] += ;
s[i+]--;
i++;
}
}
int main() {
scanf( "%s%s%d%d%d%d", sn, sm, &a, &b, &c, &d );
ln = strlen(sn), lm = strlen(sm);
reverse( sn, sn+ln );
reverse( sm, sm+lm );
subone(sn), subone(sm);
ma = Matrix(,b,,a)^sm;
mb = Matrix(,d,,c);
ans = ((ma*mb)^sn)*ma;
printf( "%lld\n", (ans[][]+ans[][]) % Mod );
}

bzoj 3240 矩阵乘法+十进制快速幂的更多相关文章

  1. bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化

    3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 613  Solved: 256[Submit][Status] ...

  2. Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)

    Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...

  3. Luogu T7152 细胞(递推,矩阵乘法,快速幂)

    Luogu T7152 细胞(递推,矩阵乘法,快速幂) Description 小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并 观察细胞分裂的规律. 他选取了一种特别的细胞,每 ...

  4. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  5. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

  6. BZOJ_1009_[HNOI2008]_GT考试_(动态规划+kmp+矩阵乘法优化+快速幂)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串全部由0~9组成,给出一个串s,求一个长度为n的串,不包含s的种类有多少. 分析 ...

  7. 【HDOJ5950】Recursive sequence(矩阵乘法,快速幂)

    题意:f[1]=a,f[2]=b,f[i]=2f[i-2]+f[i-1]+i^4(i>=3),多组询问求f[n]对2147493647取模 N,a,b < 2^31 思路:重点在于i^4的 ...

  8. 2019牛客多校B generator 1——十进制快速幂

    题目 已知 $x_i = ax_i + bx_{i-1}$,求 $x_n \% MOD$.($1\leq n\leq 10^{(10^6)}$) 分析 写成矩阵快速幂的形式,相当于求转移矩阵的 $n$ ...

  9. 十进制快速幂(牛客多校第五场)-- generator 1

    思路: 十进制快速幂. #include <stdio.h>//sprintf #include <cstdlib>////malloc exit strcat itoa sy ...

随机推荐

  1. Count of Smaller Number before itself

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  2. shell 数组基础->

    数组其实也算是变量, 传统的变量只能存储一个值, 但数组可以存储多个值. 普通数组:只能使用整数 作为数组索引 [有序 0 1 2 3 4 ]关联数组:可以使用字符串 作为数组索引 [无序 name ...

  3. 说一下怎么搭建外网来访问SVN服务器

    一.搭建SVN服务器 1.所需软件 TortoiseSVN,下载地址http://tortoisesvn.net/downloads.html TortoiseSVN中文语言包,下载地址http:// ...

  4. springboot配置fastjson后端往前端传输格式化

    import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.Spri ...

  5. Docker中安装wiki Confluence

    一下内容在centos 7安装成功. 一.安装docker 1.yum安装docker yum update # 更新yum yum install docker # yum安装docker 2.开启 ...

  6. 洛谷P2812校园网络

    传送门啦 其实这个题只要读懂分析好题意就不是很难. 就是将一个有向图进行缩点操作,把一个强连通分量看成一个点,求入度为 0 的点和出度为 0 的点各有多少. 在这里先向大家推荐两个题目,建议大家先去看 ...

  7. Linux基础 - tmux

    安装 yum install tmux 类似vim当中存在命令行模式以及编辑模式,从编辑模式进入命令行模式需要先按ESC键,在tmux当中进行操作也要先准备好"姿势"再操作,默认情 ...

  8. Linux密码策略-密码长度-密码复杂度

    1.设置密码长度 vim /etc/pam.d/system-authpassword requisite pam_cracklib.so try_first_pass retry=3 minlen= ...

  9. Elasticsearch介绍及安装部署

    本节内容: Elasticsearch介绍 Elasticsearch集群安装部署 Elasticsearch优化 安装插件:中文分词器ik 一.Elasticsearch介绍 Elasticsear ...

  10. 在jenkins里使用SCM管理jenkinsfile

    注意,这样作的前提是,插件里一定要安装pipeline和git. 设置就比较简单了. 在gitlat里生成一个演示的jenkinsfile pipeline { agent { node { labe ...