Gym - 100625E Encoded Coordinates 矩阵快速幂

题意:

一直TLE我也是醉了,,不爽!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <fstream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <deque>
 7 #include <vector>
 8 #include <queue>
 9 #include <string>
10 #include <cstring>
11 #include <map>
12 #include <stack>
13 #include <set>
14 #define LL long long
15 #define MAXN 100005
16 #define INF 0x3f3f3f3f
17 #define MAXN 100005
18 #define eps 1e-8
19 using namespace std;
20 LL ax, ay, bx, by, cx, cy, kx, ky, nx, ny, x, p;
21 struct Martix
22 {
23     LL m[10][10];
24     Martix()
25     {
26         memset(m, 0, sizeof(m));
27     }
28 };
29 void m_quick_power(Martix &a, Martix &b)
30 {
31     Martix c;
32     for(int i = 1; i <= 5; i++){
33         for(int j = 1; j <= 5; j++){
34             for(int k = 1; k <= 5; k++){
35                 c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j]) % p;
36             }
37         }
38     }
39     a = c;
40 }
41 LL work(int s, LL n)
42 {
43     Martix u, v;
44     u.m[1][1] = ax, u.m[1][2] = bx, u.m[1][3] = cx, u.m[1][4] = s;
45     v.m[2][1] = v.m[3][1] = v.m[4][2] = v.m[1][3] = v.m[3][4] = 1, v.m[1][2] = v.m[2][3] = kx;
46     while(n){
47         if(n & 1){
48             m_quick_power(u, v);
49         }
50         m_quick_power(v, v);
51         n >>= 1;
52     }
53     return u.m[1][1];
54 }
55 int main()
56 {
57 #ifndef ONLINE_JUDGE
58     freopen("in.txt", "r", stdin);
59     //freopen("out.txt", "w", stdout);
60 #endif // OPEN_FILE
61     int T;
62     scanf("%d", &T);
63     while(T--){
64         scanf("%I64d", &p);
65         scanf("%I64d%I64d%I64d%I64d%I64d", &ax, &bx, &cx, &kx, &nx);
66         scanf("%I64d%I64d%I64d%I64d%I64d", &ay, &by, &cy, &ky, &ny);
67         scanf("%I64d", &x);
68         nx--;
69         ny--;
70         int cnt = 0;
71         int j = 0;
72         for(int i = 1; i <= p; i++){
73             if(work(i, nx) == x){
74                 cnt++;
75                 j = i;
76             }
77         }
78         if(cnt != 1){
79             printf("UNKNOWN\n");
80             continue;
81         }
82         ax = ay, bx = by, cx = cy, kx = ky;
83         LL ans = work(j, ny) % p;
84         printf("%I64d\n", ans);
85     }
86 }

 

posted on 2015-08-11 08:49  张济  阅读(179)  评论(0编辑  收藏  举报

导航