2016 | ||
Accepted : 71 | Submit : 251 | |
Time Limit : 2000 MS | Memory Limit : 65536 KB |
2016Given a 2×2 matrix
A=(a 11 a 21 a 12 a 22 ),
find
A n
where
A 1 =A,A n =A×A n−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<10 100000 ). The second line contains 2 integers a 11 ,a 12 . The third line contains 2 integers a 21 ,a 22 . ( 0≤a ij <7 , (a 11 a 22 −a 12 a 21 ) is not a multiple of 7 ) OutputFor each set, a 2×2 matrix denotes the remainder of A n after division by 7 . Sample Input2 1 1 1 2 2016 1 1 1 2 Sample Output2 3 3 5 1 0 0 1 |
#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
int m[3][3];
} ans,base;
char t[1000005];
int mod(char str[],int num)
{
int flag = 0;
for (int i = 0; str[i] != '\0'; i++)
flag = ((flag * 10) + (str[i] - '0') ) % num;
return flag;
}
node mulite(node a,node b)
{
node temp;
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
temp.m[i][j]=0;
for(int k=0; k<3; k++)
{
temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%7;
}
}
}
return temp;
}
int show(node a)
{
printf("%d %d\n",a.m[0][0],a.m[0][1]);
printf("%d %d\n",a.m[1][0],a.m[1][1]);
}
node quick(int t,node base)
{
ans.m[0][0]=1;
ans.m[0][1]=0;
ans.m[1][0]=0;
ans.m[1][1]=1;
while(t)
{
if(t%2==1)
ans=mulite(ans,base);
base=mulite(base,base);
t=t/2;
}
show(ans);
}
int main()
{
while(~scanf("%s",t))
{
scanf("%d%d%d%d",&base.m[0][0],&base.m[0][1],&base.m[1][0],&base.m[1][1]);
int we;
we=mod(t,2016);
quick(we,base);
}
}