3734 Blocks DP 매트릭스 최적화 시작
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3665
Accepted: 1611
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
----- #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int M=10007;
typedef vector<int>vec;
typedef vector<vec>mat;
mat mul(mat &A,mat &B){
mat C(A.size(),vec(B[0].size()));
for (int i=0;i<(int)A.size();i++){
for (int k=0;k<(int)B.size();k++){
for (int j=0;j<(int)B[0].size();j++){
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
}
}
}
return C;
}
mat pow(mat A,LL n){
mat B(A.size(),vec(A.size()));
for (int i=0;i<(int)A.size();i++){
B[i][i]=1;
}
while (n>0){
if (n&1) B=mul(B,A);
A=mul(A,A);
n>>=1;
}
return B;
}
const int f[3][3]={ {2,1,0},{2,2,2},{0,1,2} };
const int a[1][3]={ {1,0,0} };
int main()
{
int T;
scanf("%d",&T);
while (T--){
int n;
scanf("%d",&n);
mat A(3,vec(3));
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
A[i][j]=f[i][j];
A=pow(A,n);
mat B(1,vec(3));
for (int i=0;i<3;i++) B[0][i]=a[0][i];
A=mul(A,B);
printf("%d
",A[0][0]);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.