2016湘潭邀请赛 xtu 1243 矩阵快速幂

来源:互联网 发布:经济学数据库 编辑:程序博客网 时间:2024/06/05 09:02

2016

 Accepted : 105 Submit : 394Time 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 by7.

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

21 11 220161 11 2

Sample Output

2 33 51 00 1



就是考矩阵快速幂

然后题目说本题很难,2016这个数字有寓意,然后就是n%2016就好了



#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define LL long long#define MAXN 100005#define mod 7char 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;}


0 0