2016湘潭邀請賽 xtu 1243 矩陣快速冪


2016

 
Accepted : 105   Submit : 394
Time Limit : 2000 MS   Memory Limit : 65536 KB

2016

Given a 2×2 matrix

A=(a11a21a12a22),
find An where A1=A,An=A×An1.As the result may be large,you are going to find only the remainder after division by 7.

Special Note:The problem is intended to be easy.Feel free to think why the problem is called2016 if you either:

  1. find it hard to solve;
  2. or, solved all the other problems easily.

Input

The input contains at most 40 sets. For each set:

The first line contains an integer n (1n<10100000).

The second line contains 2 integers a11,a12.

The third line contains 2 integers a21,a22.

(0aij<7,(a11a22a12a21) is not a multiple of 7)

Output

For each set, a 2×2 matrix denotes the remainder of An after division by 7.

Sample Input

2
1 1
1 2
2016
1 1
1 2

Sample Output

2 3
3 5
1 0
0 1



就是考矩陣快速冪

然后題目說本題很難,2016這個數字有寓意,然后就是n%2016就好了



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define LL long long
#define MAXN 100005
#define mod 7
char str[MAXN];
struct mat
{
    int m[3][3];
}base,ans;

mat multiply(mat a,mat b)
{
    mat temp;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++){
            temp.m[i][j]=0;
            for(int k=0;k<2;k++)
                temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
        }
    return temp;
}

mat quick_mod(LL n)
{
    ans.m[0][0]=ans.m[1][1]=1;
    ans.m[1][0]=ans.m[0][1]=0;

    while(n)
    {
        if(n&1)
            ans=multiply(ans,base);
        base=multiply(base,base);
        n>>=1;
    }
    return ans;
}

int main()
{
    int num;
    //freopen("in.txt","r",stdin);
    while(scanf("%s",str)!=EOF)
    {
        num=0;
        int len=strlen(str);
        for(int i=0;i<len;i++)
            num=(num*10+str[i]-'0')%2016;
        scanf("%d%d",&base.m[0][0],&base.m[0][1]);
        scanf("%d%d",&base.m[1][0],&base.m[1][1]);

        base=quick_mod(num);
        printf("%d %d\n",base.m[0][0],base.m[0][1]);
        printf("%d %d\n",base.m[1][0],base.m[1][1]);
    }
    return 0;
}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com