• 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
    ",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);
        }
    }
  • 相关阅读:
    VSCode集成TypeScript编译
    http模拟登陆及发请求
    1​1​.​0​5​9​2​M​晶​振​与12M晶振
    单片机定时器2使用
    Altium Designer 小记
    sql-mysql
    java英文缩写
    Altium Design
    Tomcat使用
    jar/war/ear文件的区别
  • 原文地址:https://www.cnblogs.com/8023spz/p/9431616.html
Copyright © 2020-2023  润新知