您的位置:首页 > 其它

2016长城信息杯中国大学生程序设计竞赛中南邀请赛 xtu 1243 2016

2016-06-20 13:44 288 查看
http://www.dengwenhuo.cn/?id=194

Given a2×2 matrix

A=(a 11 a 21  a 12 a 22  ), 

 

findA n  whereA 1 =A,A n =A×A n−1  .
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 called 
2016
 if you either:

find it hard to solve;

or, solved all the other problems easily.


Input

The input contains at most40 sets.
For each set:

The first line contains an integern (1≤n<10 100000  ).

The second line contains2 integersa 11 ,a 12  .

The third line contains2 integersa 21 ,a 22  .

(0≤a ij <7 ,(a 11 a 22 −a 12 a 21 ) is
not a multiple of7 )


Output

For each set, a2×2 matrix
denotes the remainder ofA n  after
division by7 .


Sample Input

2
1 1
1 2
2016
1 1
1 2



Sample Output

2 3
3 5
1 0
0 1


#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
using namespace std;

typedef struct
{
int m[2][2];
} Matrix;
Matrix p;
Matrix I= {1,0,0,1};
Matrix matrixmul(Matrix a,Matrix b)
{
int i,j,k;
Matrix c;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
{
c.m[i][j]=0;
for(k=0; k<2; k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
c.m[i][j]%=7;
}
return c;
}
Matrix quickpow(ll n)
{
Matrix m=p,b=I;
while(n>=1)
{
if(n&1)
b=matrixmul(b,m);
n>>=1;
m=matrixmul(m,m);
}
return b;
}
int main()
{
Matrix ans;
int n;
char a[100005];
while(~scanf("%s",a))
{
scanf("%d %d %d %d",&p.m[0][0],&p.m[0][1],&p.m[1][0],&p.m[1][1]);
n=0;
for(int i=0; a[i]; i++)
{
n=(n*10+(a[i]-'0'))%2016;
}
ans=quickpow(n);
printf("%d %d\n%d %d\n",ans.m[0][0],ans.m[0][1],ans.m[1][0],ans.m[1][1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: