Blocks
Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.
Input
The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.
Output
For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.
Sample Input
2
1
2
Sample Output
2
6
Source
PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Simon
题目类型:递推+矩阵快速幂取模
算法分析:设Ai为前i个块中红绿块数量都为偶数的方案数,Bi为前i个块中红绿块数量一奇一偶的方案数,Ci为前i个块中红绿块数量都为奇数的方案数,则可以推出Ai = 2 * Ai-1 + Bi, Bi = 2 * Ai-1 + 2 * Bi-1 + 2 * Ci-1和Ci = Bi-1 + 2 * Ci-1。最后转成矩阵计算即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
/************************************************* Author :supermaker Created Time :2016/3/19 12:52:51 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e4 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 3; int n = 3; struct Mat { LL mat[maxn][maxn]; }; Mat operator * (Mat a, Mat b) { Mat c; memset (c.mat, 0, sizeof (c.mat)); for (LL k = 0; k < n; k++) { for (LL i = 0; i < n; i++) { if (a.mat[i][k] == 0) continue; for (LL j = 0; j < n; j++) { if (b.mat[k][j] == 0) continue; c.mat[i][j] = (c.mat[i][j] + ((a.mat[i][k] % MOD) * (b.mat[k][j] % MOD) % MOD)) % MOD; } } } return c; } Mat operator ^ (Mat a, LL k) { Mat c; for (LL i = 0; i < n; i++) for (LL j = 0; j < n; j++) c.mat[i][j] = (i == j); while (k) { if (k & 1) c = c * a; a = a * a; k >>= 1; } return c; } int main() { //CFF; //CPPFF; Mat aa, bb; aa.mat[0][0] = 2, aa.mat[0][1] = 1, aa.mat[0][2] = 0; aa.mat[1][0] = 2, aa.mat[1][1] = 2, aa.mat[1][2] = 2; aa.mat[2][0] = 0, aa.mat[2][1] = 1, aa.mat[2][2] = 2; bb.mat[0][0] = 1, bb.mat[1][0] = 0, bb.mat[2][0] = 0; int t; scanf ("%d", &t); while (t--) { LL num; scanf ("%lld", &num); Mat tt = aa ^ num; tt = tt * bb; printf ("%lld\n", tt.mat[0][0] % MOD); } return 0; } |