Description
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
![](https://images2018.cnblogs.com/blog/1202543/201808/1202543-20180806174209387-1973283466.png)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; void mul(int (*a)[3],int (*b)[3]) { int d[3][3] = {0}; for(int i = 0;i < 3;i ++) { for(int j = 0;j < 3;j ++) { for(int k = 0;k < 3;k ++) { d[i][j] = (d[i][j] + a[i][k] * b[k][j]) % 10007; } } } for(int i = 0;i < 3;i ++) { for(int j = 0;j < 3;j ++) { a[i][j] = d[i][j]; } } } int compute(int n) { int d[3][3] = {1,0,0,0,1,0,0,0,1}; int add[3][3] = {2,1,0,2,2,2,0,1,2}; while(n) { if(n % 2) { mul(d,add); } n /= 2; mul(add,add); } return d[0][0]; } int main() { int t,n; scanf("%d",&t); while(t --) { scanf("%d",&n); printf("%d ",compute(n)); } }
组合数方法,公式得记住啊。。题意也不能理解错
代码:
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #define mod 10007 using namespace std; int pow_(int a,int b) { a %= mod; int d = 1; while(b) { if(b % 2 == 1) d = (d * a) % mod; a = (a * a) % mod; b /= 2; } return d; } int main() { int t,d; scanf("%d",&t); while(t --) { scanf("%d",&d); d = pow_(2,d - 1); printf("%d ",(d + 1) * d % mod); } }