XTOJ 1243 2016【矩阵快速幂取模】

来源:互联网 发布:壮游科技和塔人网络 编辑:程序博客网 时间:2024/06/04 18:00

2016

Accepted : 100 Submit : 366Time 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 called 2016 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

Source

XTU OnlineJudge


原题链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1243


当时做的时候没注意n的范围,矩阵快速幂取模,n对2016取模,具体为什么也还没有想清楚。

剩下的就是模板的事了。

AC代码:

/**  * 博客地址:http://blog.csdn.net/hurmishine*/#include <iostream>#include <cstdio>using namespace std;const int maxn=100000+5;struct Matrix{    int m[2][2];};Matrix I={    1,0,    0,1};Matrix p;Matrix mul(Matrix a,Matrix b){    Matrix ans;    for(int i=0; i<2; i++)    {        for(int j=0; j<2; j++)        {            ans.m[i][j]=0;            for(int k=0; k<2; k++)            {                ans.m[i][j]+=(a.m[i][k]*b.m[k][j])%7;                ans.m[i][j]%=7;            }        }    }    return ans;}Matrix quick_mod(Matrix a,int p){    Matrix ans=I;    Matrix tmp=a;    while(p)    {        if(p&1)            ans=mul(ans,tmp);        tmp=mul(tmp,tmp);        p>>=1;    }    return ans;}int main(){    char a[maxn];    while(cin>>a)    {        cin>>p.m[0][0]>>p.m[0][1]>>p.m[1][0]>>p.m[1][1];        int n=0;        for(int i=0; a[i]; i++)        {            n=(n*10+a[i]-'0')%2016;        }        Matrix ans=quick_mod(p,n);        cout<<ans.m[0][0]<<" "<<ans.m[0][1]<<endl;        cout<<ans.m[1][0]<<" "<<ans.m[1][1]<<endl;    }    return 0;}


尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine


0 0