2016CCPC 中南地区邀请赛 A 矩阵快速幂

A

A^n=A^(n%2016)%7;

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <sstream>
  5 #include <string>
  6 #include <algorithm>
  7 #include <list>
  8 #include <map>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <cstdlib>
 14 // #include <conio.h>
 15 using namespace std;
 16 #define clc(a,b) memset(a,b,sizeof(a))
 17 #define inf 0x3f3f3f3f
 18 const int N=100010;
 19 const int MOD = 1e9+7;
 20 #define LL long long
 21 double const pi = acos(-1);
 22 // void fre() {
 23 //     freopen("in.txt","r",stdin);
 24 // }
 25 // inline int r() {
 26 //     int x=0,f=1;char ch=getchar();
 27 //     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
 28 //     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
 29 // }
 30 
 31 int mod(string s){
 32   int len;
 33   int i;
 34   int ans=0;
 35   len=s.length();
 36   // cout<<s<<endl;
 37   for(i=0;i<len;i++)
 38   ans=(ans*10+s[i]-'0')%2016; 
 39   return ans;
 40 }
 41 
 42 struct matrix
 43 {
 44     int m[2][2];
 45 }ans, base;
 46 
 47 matrix multi(matrix a, matrix b)
 48 {
 49     matrix tmp;
 50     for(int i = 0; i < 2; ++i)
 51     {
 52         for(int j = 0; j < 2; ++j)
 53         {
 54             tmp.m[i][j] = 0;
 55             for(int k = 0; k < 2; ++k)
 56                 tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % 7;
 57         }
 58     }
 59     return tmp;
 60 }
 61 
 62 void fast_mod(int n)  
 63 {
 64     int a,b,c,d;
 65     scanf("%d%d%d%d",&a,&b,&c,&d);
 66     base.m[0][0] =a; 
 67     base.m[0][1] =b;
 68     base.m[1][0] =c;
 69     base.m[1][1] =d;
 70     ans.m[0][0] = ans.m[1][1] = 1;  
 71     ans.m[0][1] = ans.m[1][0] = 0;
 72     if(n==0){
 73         printf("1 0\n");
 74         printf("0 1\n");
 75         return;
 76     }
 77     while(n)
 78     {
 79         if(n & 1)  
 80         {
 81             ans = multi(ans, base);
 82         }
 83         base = multi(base, base);
 84         n >>= 1;
 85     }
 86     printf("%d %d\n",ans.m[0][0],ans.m[0][1]);
 87     printf("%d %d\n",ans.m[1][0],ans.m[1][1]);
 88     return;
 89 }
 90 
 91 
 92 int main(){
 93     // fre();
 94     string s;
 95     while(cin>>s){
 96          int n;
 97          n=mod(s);
 98          // cout<<n<<endl;
 99          fast_mod(n);
100     }
101     return 0;
102 }

 

posted @ 2016-06-14 11:36  yyblues  阅读(318)  评论(0编辑  收藏  举报