poj 3734 Blocks 【矩阵快速幂】

Blocks

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 4529 Accepted: 2044

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

PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Simon

思路:

假设染色第i个方块时,红绿都为偶数方案数ai ,一个奇数一个偶数方案bi,均为奇数方案ci;

则 ai+1 = 2*ai + bi;

bi+1 = 2*ai + 2*bi+2*ci;

ci+1 = 2*ci + bi;

所以可构造矩阵:

2 1 0

2 2 2

0 1 2

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <queue>

using namespace std;

const long long MOD = 10007;

struct node
{
    long long m[3][3];
}ans, base;
long long a, b;

node multi(node a, node b)
{
    node tmp;
    for (int i = 0; i<3; i++)
        for (int j = 0; j<3; j++)
        {
            tmp.m[i][j] = 0;
            for (int k = 0; k<3; k++)
            {
                tmp.m[i][j] = ((tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD + MOD) % MOD;
            }
        }
    return tmp;
}

void fast_mod(int n)// 求矩阵 base 的  n 次幂
{
    base.m[0][0] = 2; base.m[0][1]=1; base.m[0][2] = 0;
    base.m[1][0] = 2; base.m[1][1]=2; base.m[1][2] = 2;
    base.m[2][0] = 0; base.m[2][1]=1; base.m[2][2] = 2;
    memset(ans.m,0,sizeof(ans.m));
    ans.m[0][0] = ans.m[1][1] = 1; ans.m[2][2] = 1;// ans 初始化为单位矩阵
    while (n)
    {
        if (n & 1) //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t
            ans = multi(ans, base);
        base = multi(base, base);
        n >>= 1;
    }
}

int t;
int n;

int main()
{
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        fast_mod(n);
        long long anss = ((ans.m[0][0] % MOD) + MOD) % MOD;
        printf("%lld\n", anss);
    }
    return 0;
}
时间: 2024-10-24 16:01:20

poj 3734 Blocks 【矩阵快速幂】的相关文章

[POJ 3734] Blocks (矩阵快速幂、组合数学)

Blocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3997   Accepted: 1775 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 paint

POJ 3734 Blocks (矩阵快速幂)

题目链接:http://poj.org/problem?id=3734 <挑战程序设计竞赛>202页.与单纯的用递推式与矩阵快速幂求第N项不同,设染到第i个方块为止,红绿都是偶数的方案数目为a,红绿恰有一个是偶数方案数目为b,红绿都是奇数方案数目为c, 则: a[i+1] = 2 * a[i] + b[i] b[i+1] = 2 * a[i]+2 * b[i]+2 * c[i] c[i+1] = b[i] + 2 * c[i] 之后构建3*3矩阵求解 代码: 1 typedef vector&

POJ 3734 Blocks(矩阵快速幂加递推)

Blocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 2931 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 paint

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>

uva 10743 - Blocks on Blocks(矩阵快速幂)

题目链接:uva 10743 - Blocks on Blocks 题目大意:问说n联骨牌有多少种,旋转镜像后相同不算同一组,一行的格子必须连续,如果答案大于10000,输出后四位. 解题思路:想了一下午的递推式,实在受不了,把推出的序列在网上搜了一下,有公式ai=5?ai?1?7?ai?2+4?ai?3 (i≥5) PS:哪位神人知道怎么推出来的请留言我,大恩不言谢~ #include <cstdio> #include <cstring> #include <algori

poj 3070 Fibonacci 矩阵快速幂

题目链接:http://poj.org/problem?id=3070 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for t

poj 3070 Fibonacci (矩阵快速幂乘/模板)

题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int N=2,M=2,P=2; const int MOD=10000; struct Matrix { ll m[N][N]; }; Matrix

POJ——3070Fibonacci(矩阵快速幂)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12329   Accepted: 8748 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequenc

poj 3735 稀疏矩阵矩阵快速幂

设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 & 0 & 1 \\0 & 1 & 0 & 0 \\0 & 0 & 1 & 0 \\0 & 0 & 0 & 1\end{bmatrix}\times\begin{bmatrix}x \\y \\z \\1 \\\end{

POJ 3070 Fibonacci(矩阵快速幂)

Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, - An alternative formula for the Fibonacci sequence is