1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 ll x[2][2],y[2][2],m,a,c,x0,n,g;
 7 ll chen1(ll a1,ll a2)
 8 {
 9     long long a3;
10     if(!a2)
11       return 0;
12     a3=chen1(a1,a2>>1);
13     a3=(a3+a3)%m;
14     if(a2&1)
15       a3=(a3+a1)%m;
16     return a3;
17 }
18 void chen(ll x1[2][2],ll x2[2][2])
19 {
20     long long x3[2][2];
21     memset(x3,0,sizeof(x3));
22     for(int i=0;i<2;i++)
23       for(int j=0;j<2;j++)
24         for(int k=0;k<2;k++)
25           x3[i][j]=(x3[i][j]+chen1(x1[i][k],x2[k][j]))%m;
26     for(int i=0;i<2;i++)
27       for(int j=0;j<2;j++)
28         x1[i][j]=x3[i][j];
29 }
30 int main()
31 {
32     scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x0,&n,&g);
33     x[0][0]=1;
34     x[1][1]=1;
35     y[0][0]=a;
36     y[0][1]=c;
37     y[1][1]=1;
38     for(;n;)
39       {
40         if(n%2)
41           chen(x,y);
42         chen(y,y);
43         n>>=1;
44       }
45     printf("%lld\n",(chen1(x[0][0],x0)+x[0][1])%m%g);
46     return 0;
47 }

矩阵乘法

posted on 2016-03-22 22:55  xiyuedong  阅读(147)  评论(0编辑  收藏  举报