2016 | ||
Accepted : 100 | Submit : 366 | |
Time Limit : 2000 MS | Memory Limit : 65536 KB |
2016Given a 2×2 matrix
A=(a11a21a12a22),
find
An
where
A1=A,An=A×An−1
. 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
InputThe input contains at most 40 sets. For each set: The first line contains an integer n ( 1≤n<10100000 ). The second line contains 2 integers a11,a12 . The third line contains 2 integers a21,a22 . ( 0≤aij<7 , (a11a22−a12a21) is not a multiple of 7 ) OutputFor each set, a 2×2 matrix denotes the remainder of An after division by 7 . Sample Input2 1 1 1 2 2016 1 1 1 2 Sample Output2 3 3 5 1 0 0 1 SourceXTU OnlineJudge |
原题链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1243
当时做的时候没注意n的范围,矩阵快速幂取模,n对2016取模,具体为什么也还没有想清楚。
剩下的就是模板的事了。
AC代码:
/**
* 博客地址:http://blog.youkuaiyun.com/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;
}