poj 3734 Blocks

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

考虑的角度是在涂好i个方块之后,去涂第i+1个,设ai为涂前i个红绿都是偶数的方案数,bi是红绿有一个为奇数的方案数,ci为红绿都是奇数的方案数。
那么ai+1 = ai * 2(涂另外两种颜色) + bi * 1(红绿哪个是奇数,涂哪个颜色) + ci * 0(怎么涂也不能满足两个偶数)
       bi+1 = ai * 2(红或绿) + bi * 2(另外两种颜色) + ci * 2(红或绿)
       ci+1 = ai * 0(不能满足两个奇数) + bi * 1(红绿哪个是偶数,涂哪个颜色) + ci * 2(涂另外两种颜色)
由于都可以用ai,bi,ci表示所以可以表示为矩阵
(ai+1 bi+1 ci+1)T = A(ai bi ci)T(T表示转置)
A =  ,(an bn cn)T = A^n(a0 b0 c0)T

 

未涂色时红绿都是0也满足所以a0 = 1.
如此可以用矩阵幂来计算。
代码:
#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\n",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\n",(d + 1) * d % mod);
    }
}

 

转载于:https://www.cnblogs.com/8023spz/p/9431616.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值