2016弱校联盟十一专场10.7(12点场) D (poj 3734)

题目链接:

https://acm.bnu.edu.cn/v3/contest_show.php?cid=8507#problem/D

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

 Hint:

题意:

给你一个长为n的直线,在直线上看可以放4种不同颜色的砖块,有红,绿,蓝,黄,其中红和绿的砖块的数量必须是偶数个(0也算是偶数),求放置这些砖块的方法的总数。

题解:

自己本来准备写dp的,但是队友比较给力,直接把公式给推了出来,就是4^n-1+2^n-1。

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define met(a,b) memset(a,b,sizeof(a));
#define inf  0x3f3f3f3f
const int mod  = 10007;

ll q_mul(ll a, ll b, ll mod )
{
    ll ans = 0;
    while(b)
    {
        if(b & 1)
        {
            b--;
            ans =(ans+ a)%mod;
        }
        b /= 2;
        a = (a + a) % mod;
    }
    return ans;
}

ll q_pow( ll a, ll b, ll mod )
{
    ll ans = 1;
    while(b)
    {
        if(b & 1)
        {
            ans = q_mul( ans, a, mod );
        }
        b /= 2;
        a = q_mul( a, a, mod );
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll num1=q_pow(4,n-1,mod);
        ll num2=q_pow(2,n-1,mod);
        ll num3=(num1+num2)%mod;
        cout<<num3<<endl;
    }
}
原文地址:https://www.cnblogs.com/TAT1122/p/5936582.html