3240: [Noi2013]矩阵游戏
Time Limit: 10 Sec
Memory Limit: 256 MB
Submit: 1304
Solved: 556
[ Submit][ Status][ Discuss]
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
1<=N,M<=10^1000 000,a<=a,b,c,d<=10^9
Source
[ Submit][ Status][ Discuss]
矩阵乘法。。。变态的矩阵乘法
大概你无法用高精完成它
首先矩阵乘法满足结合律,于是只要把m-1个a矩阵和一个b矩阵乘出,再拿这个东西n次方,得到矩阵乘以原来的东西,在往上走一步即可
不过奇妙的是这东西可以用费马小定理消次方从而避免高精,不过学长建议我写10进制快速幂。。然而T了(一定是我姿势不够优美)。。。。然后然后就写了个费马小定理的(矩阵A的1E9+7次方刚好等于矩阵A,然而当矩阵A的a系数等于1时次方变为1E9+8)具体可以手算证明。。。。4.6s
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL mo = 1000000007;
const int maxn = 1E6 + 10;
struct data{
LL a[2][2];
data () {
memset(a,0,sizeof(a));
for (int i = 0; i < 2; i++) a[i][i] = 1;
}
data operator * (const data &b) {
data c;
memset(c.a,0,sizeof(c.a));
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int l = 0; l < 2; l++)
c.a[i][j] = (c.a[i][j] + a[i][l]*b.a[l][j]%mo)%mo;
return c;
}
bool operator == (const data &b) const {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
if (a[i][j] != b.a[i][j])
return 0;
return 1;
}
};
char n[maxn],m[maxn];
LL a,b,c,d,s[2],ans[2],pn,pm;
int ln,lm;
data ks(data now,LL y)
{
data ret;
for (; y; y >>= 1LL) {
if (y & 1) ret = ret*now;
now = now*now;
}
return ret;
}
data kn(data now)
{
data ret;
for (int i = 0; i < ln; i++) {
//ret = ret*ks(now,n[i]-'0');
for (int j = '0'; j < n[i]; j++) ret = ret*now;
now = ks(now,10);
//for (int i = 0; i < 10; i++) now = now*now;
}
return ret;
}
data km(data now)
{
data ret;
for (int i = 0; i < lm; i++) {
//ret = ret*ks(now,m[i]-'0');
for (int j = '0'; j < m[i]; j++) ret = ret*now;
now = ks(now,10);
}
return ret;
}
LL ksm(LL x,LL y)
{
LL ret = 1;
for (; y; y >>= 1LL) {
if (y & 1) ret = ret*x%mo;
x = x*x%mo;
}
return ret;
}
int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif
scanf("%s",&n); scanf("%s",&m);
cin >> a >> b >> c >> d;
ln = strlen(n); lm = strlen(m);
//for (int i = 0; i < ln/2; i++) swap(n[i],n[ln-1-i]);
//for (int i = 0; i < lm/2; i++) swap(m[i],m[lm-1-i]);
data A,B; A.a[1][0] = b; A.a[0][0] = a; B.a[0][0] = c; B.a[1][0] = d;
int pm = c == 1?mo:mo-1;
int pn = a == 1?mo:mo-1;
LL N,M; N = M = 0;
for (int i = 0; i < ln; i++) N = (N*10LL%pn + 1LL*(n[i]-'0'))%pn;
for (int i = 0; i < lm; i++) M = (M*10LL%pm + 1LL*(m[i]-'0'))%pm;
data x = ks(A,(M-1+pm)%pm);
x = x*B;
data y = ks(x,N);
s[0] = s[1] = 1;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
ans[i] = (ans[i] + s[j]*y.a[j][i]%mo)%mo;
LL ANS = ans[0];
ANS = (ANS - d + mo)%mo;
ANS = ANS*ksm(c,mo-2LL)%mo;
cout << ANS;
return 0;
}