zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    深入理解泛型之JAVA泛型的继承和实现、泛型擦除
    hadoop过程中遇到的错误与解决方法
    微服务拆分到什么粒度合适——康威定律
    墨菲定律(设计系统)和康威定律(系统划分)
    Hadoop-Impala学习笔记之SQL参考
    Hadoop-Impala学习笔记之管理
    Hadoop2-HDFS学习笔记之入门(不含YARN及MR的调度功能)
    Hadoop-Impala学习笔记之入门
    解决 Invalid character found in method name. HTTP method names must be tokens 异常信息
    从康威定律和技术债看研发之痛
  • 原文地址:https://www.cnblogs.com/8023spz/p/9431616.html
Copyright © 2011-2022 走看看