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. SQL数据库的应用一(Day 24)

    哈哈,又到了新的一周.我们也开始学习新的知识了,从今天开始学习SQL数据库的一些知识.今天主要讲了一些数据库.表的创建管理,和一些约束的定义使用.(这里使用的是SQL语句)下面我就具体总结一下. 总结 ...

  2. Java 网络编程(二) 两类传输协议:TCP UDP

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951841.html 两类传输协议:TCP,UDP TCP TCP是Transfer C ...

  3. 重定向输入输出流--freopen

    freopen是被包含于C标准库头文件<stdio.h>中的一个函数,用于重定向输入输出流.该函数可以在不改变代码原貌的情况下改变输入输出环境. C99函数声明: FILE *freope ...

  4. MFC 简单实现 DES 算法

    前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...

  5. php Smarty详细配置

    1.在Smarty官网下载 路径:https://github.com/smarty-php/smarty/releases 2.把下载下来的Smarty解压出来 3.把解压出来的Smarty里面的l ...

  6. win7 64位 Xsheel

    Xsheel5安装后,找不到.dll文件,系统支持不好,不用 Xshell4有时提示 1152:Error 就使用 链接:http://pan.baidu.com/s/1c1s42Y0 密码:098x ...

  7. [LeetCode]题解(python):123-Best Time to Buy and Sell Stock III

    题目来源: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意分析: 和上题类似,array[i]代表第i天物品 ...

  8. sqlite详细介绍

    ------------------------------------------------------------------------------SQLite简介-------------- ...

  9. HDU 1091 A+B for Input-Output Practice (III)

    #include <cstdio> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) if ( ...

  10. C标准函数库中获取时间与日期、对时间与日期数据操作及格式化

    表示时间的三种数据类型[编辑] 日历时间(calendar time),是从一个标准时间点(epoch)到现在的时间经过的秒数,不包括插入闰秒对时间的调整.开始计时的标准时间点,各种编译器一般使用19 ...