3240: [Noi2013]矩阵游戏

Time Limit: 10 Sec   Memory Limit: 256 MB

Submit: 123  
Solved: 73

[
Submit][
Status]

Description

婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储)。她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i][j]满足下面的递推式:

F[1][1]=1
F[i,j]=a*F[i][j-1]+b (j!=1)
F[i,1]=c*F[i-1][m]+d (i!=1)
递推式中a,b,c,d都是给定的常数。

现在婷婷想知道F[n][m]的值是多少,请你帮助她。由于最终结果可能很大,你只需要输出F[n][m]除以1,000,000,007的余数。

Input

一行有六个整数n,m,a,b,c,d。意义如题所述

Output

包含一个整数,表示F[n][m]除以1,000,000,007的余数

Sample Input

3 4 1 3 2 6

Sample Output

85

HINT

样例中的矩阵为:

1 4 7 10

26 29 32 35

76 79 82 85

Source

 

NOI赛场上拿了70分(没用费马小定理)

PS:实际原因是没算复杂度囧。。。。

好吧。。。这题让我们回顾一下费马小定理:

a^b mod (phi(p))=1 (mod p)

所以可以mod ,特判a=c=1的特殊情况

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (1000000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
ll a,b,c,d,n,m,phiF;
ll getint(char s[]) //_dec_1
{
char c;
ll x=0;
for(int i=0,c=s[i];s[i];c=s[++i])
{
x=(x*10+c-48)%phiF;
}
x=(x-1+phiF)%phiF;
return x;
}
struct M
{
int n,m;
ll a[3][3];
M(){n=m=2;MEM(a);}
M(ll a1,ll a2,ll b1,ll b2){n=m=2;MEM(a) a[1][1]=a1,a[1][2]=a2,a[2][1]=b1,a[2][2]=b2; }
friend M operator*(M a,M b)
{
M c;
For(k,2)
For(i,2)
For(j,2)
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%F;
/*
For(i,2)
{
For(j,2) cout<<c.a[i][j]<<' ';cout<<endl;
}*/
return c;
}
void make_I()
{
n=m=2;MEM(a)
For(i,n) a[i][i]=1;
}
}A,B,C,D,I;
void print(M a)
{
For(i,2)
{
For(j,2) cout<<a.a[i][j]<<' ';cout<<endl;
}
}
M pow2(M a,ll b)
{
M c=I;
static bool a2[MAXN];
int n=0;while (b) a2[++n]=b&1,b>>=1;
For(i,n)
{
if (a2[i]) c=c*a;
a=a*a;
}
return c;
}
char s1[MAXN],s2[MAXN];
int main()
{
scanf("%s%s",s1,s2);
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
if (a==1&&c==1) phiF=F;else phiF=F-1;
n=getint(s1);m=getint(s2);
A=M(a,b,0,1);B=M(c,d,0,1);I=M(1,0,0,1);
C=pow2(A,m);//print(C);
D=B*C;//print(D);
D=pow2(D,n);
D=C*D;//print(D);
cout<<(D.a[1][2]+D.a[1][1])%F<<endl; return 0;
}

顺便再附一份读文件的特殊写法:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (1000000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
ll a,b,c,d,n,m,phiF;
ll getint() //_dec_1
{
char c;
while (c=getchar(),(!isdigit(c)));
//cout<<(int)c;
ll x=c-48;
while (c=getchar(),(isdigit(c)))
{
x=(x*10+c-48)%phiF;
}
x=(x-1+phiF)%phiF;
return x;
}
struct M
{
int n,m;
ll a[3][3];
M(){n=m=2;MEM(a);}
M(ll a1,ll a2,ll b1,ll b2){n=m=2;MEM(a) a[1][1]=a1,a[1][2]=a2,a[2][1]=b1,a[2][2]=b2; }
friend M operator*(M a,M b)
{
M c;
For(k,2)
For(i,2)
For(j,2)
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%F;
/*
For(i,2)
{
For(j,2) cout<<c.a[i][j]<<' ';cout<<endl;
}*/
return c;
}
void make_I()
{
n=m=2;MEM(a)
For(i,n) a[i][i]=1;
}
}A,B,C,D,I;
void print(M a)
{
For(i,2)
{
For(j,2) cout<<a.a[i][j]<<' ';cout<<endl;
}
}
M pow2(M a,ll b)
{
M c=I;
static bool a2[MAXN];
int n=0;while (b) a2[++n]=b&1,b>>=1;
For(i,n)
{
if (a2[i]) c=c*a;
a=a*a;
}
return c;
} int main()
{
freopen("matrix.in","r",stdin);
freopen("matrix.out","w",stdout);
scanf("%*s%*s");
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
if (a==1&&c==1) phiF=F;else phiF=F-1;
freopen("matrix.in","r",stdin);
n=getint();m=getint();
A=M(a,b,0,1);B=M(c,d,0,1);I=M(1,0,0,1);
C=pow2(A,m);//print(C);
D=B*C;//print(D);
D=pow2(D,n);
D=C*D;//print(D);
cout<<(D.a[1][2]+D.a[1][1])%F<<endl; return 0;
}

BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)的更多相关文章

  1. BZOJ 3240 [Noi2013]矩阵游戏 ——费马小定理 快速幂

    发现是一个快速幂,然而过不去. 怎么办呢? 1.十进制快速幂,可以用来练习卡时. 2.费马小定理,如果需要乘方的地方,可以先%(p-1)再计算,其他地方需要%p,所以需要保存两个数. 然后就是分类讨论 ...

  2. bzoj3240 [Noi2013]矩阵游戏——费马小定理+推式子

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3240 n 和 m 太过巨大,不难想到应该用费马小定理什么的来缩小范围: 总之就是推式子啦,看 ...

  3. 【bzoj5118】Fib数列2 费马小定理+矩阵乘法

    题目描述 Fib定义为Fib(0)=0,Fib(1)=1,对于n≥2,Fib(n)=Fib(n-1)+Fib(n-2) 现给出N,求Fib(2^n). 输入 本题有多组数据.第一行一个整数T,表示数据 ...

  4. HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂

    MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i )   ( i>=3) mod 1000000007 是质数 , 依据费马小定理  a^phi( p ) = 1 ( ...

  5. HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

    M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  6. Fib数列2 费马小定理+矩阵乘法

    题解: 费马小定理 a^(p-1)=1(mod p) 这里推广到矩阵也是成立的 所以我们可以对(2^n)%(p-1) 然后矩阵乘法维护就好了 模数较大使用快速乘

  7. bzoj5118: Fib数列2(费马小定理+矩阵快速幂)

    题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...

  8. HDOJ 5667 Sequence//费马小定理 矩阵快速幂

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意:如题给了一个函数式,给你a,b,c,n,p的值,叫你求f(n)%p的值 思路:先对函数取以a为 ...

  9. 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列

    [题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...

随机推荐

  1. 使用iscroll4可能会遇到的问题(转:记录)

    1.在iscroll4的滚动容器范围内,点击input框.select等表单元素时没有响应这个问题原因在于iscroll需要一直监听用户的touch操作,以便灵敏的做出对应效果,所以它把其余的默认事件 ...

  2. PHP学习笔记5-类的继承/方法重写

    更改Man.php文件名为People.php,加入代码: public function hi(){ return $this->_name.' say hi'; } 新建文件Man.php: ...

  3. 转移服务器,DEDE网站遇到no input file specified!

    公司新配置了服务器,需要从旧服务器上把原来的站点迁移到新服务器,迁移.NET网站没有遇到任何问题,但是在迁移过来一个用DEDE做的网站后,访问首页出现No Input File Specified,后 ...

  4. IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案)

    原文 IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案) 对于IIS网站,大伙用的比较多,就不啰嗦了.   今天和说说大伙比较少使用的"IIS应用程序”和虚拟目录的区别 ...

  5. HDU 1848 Fibonacci again and again

    题解:尼姆博弈,对于1至1000计算SG函数,每次取最小的前继值,SG值异或为0则为P-position. #include <cstdio> #include <cstring&g ...

  6. Android 关于调用系统内已安装的相机问题

    Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,1); 调用系统内已安 ...

  7. opencv中遇到的的一些错误

    一:错误提示:OpenCV Error:Bad argument<src and dst have different formats> in unkown function,file.. ...

  8. pomelo

    简介 Pomelo 是基于 Node.js 的高性能.分布式游戏服务器框架.它包括基础的开发框架和相关的扩展组件(库和工具包),可以帮助你省去游戏开发枯燥中的重复劳动和底层逻辑的开发.Pomelo 不 ...

  9. 柯里化函数之Javascript

    柯里化函数之Javascript 定义 依据定义来说,柯里化就是将一个接收"多个"參数的函数拆分成一个或者很多个接收"单一"參数的函数.定义看起来是比較抽象的. ...

  10. CentOS, 高速设置ssh无password登录

    首先.保证能够ping通 然后运行例如以下命令, master登录slave master上面运行例如以下指令: 2.4 确认本机sshd的配置文件(root) $ vi/etc/ssh/sshd_c ...