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

2016

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

2
1 1
1 2
2016
1 1
1 2

Sample Output

2 3
3 5
1 0
0 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值