D. Blocks 数学题

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 integerN(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

https://www.bnuoj.com/bnuoj/contest_show.php?cid=8507#problem/101988

一开始打表找规律,无果。
然后分析一下,开始的时候主要怕是2个红,然后绿色的可以4个,6个....这样分类很麻烦。
于是就转换思路。
枚举红色 + 绿色一共有多少个
枚举值就是0、2、4、6.....
我也会想到这样枚举会超时,但是先写公式出来,看看能不能化简,
每一次C(n, k)中,就是红色 + 绿色一共有k个,其中再枚举红色的个数,剩下的就会是绿色的个数了。
C(k, 0) + C(k, 2) + C(k, 4) .... 这个是标准的二项式系数奇次项的和。是2^(k - 1)
然后枚举红色 + 绿色一共有k个后,剩下的n - k个每个有两种选择,2^(n - k),于是一合并,就是2^(n - 1)
提取
2^(n - 1)
然后公式就出来了。

2^n + 2^(n - 1) * (2^(n - 1) - 1)
后面那个减1是因为没有2^0

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>

int n;
LL ans = 0;
int f[1111];
const int MOD = 10007;
void dfs(int cur) {
    if (cur == n + 1) {
        int one = 0, tow = 0;
        for (int i = 1; i <= n; ++i) {
            one += f[i] == 1;
            tow += f[i] == 2;
        }
        if (one % 2 == 0 && tow % 2 == 0) ans++;
//        ans %= MOD;
        return;
    }
    for (int i = 1; i <= 4; ++i) {
        f[cur] = i;
        dfs(cur + 1);
    }
}
int quick_pow(int a, int b) {
    int ans = 1;
    int base = a;
    while (b) {
        if (b & 1) {
            ans *= base;
            if (ans >= MOD) ans %= MOD;
//            ans %= MOD;
        }
        b >>= 1;
        base *= base;
        if (base >= MOD) base %= MOD;
//        base %= MOD;
    }
    return ans;
}
void work() {
//    for (n = 1; n <=14; ++n) {
//        ans = 0;
//        dfs(1);
////        cout << ans % 10007 << endl;
//        printf("n == %d  %I64d
", n, ans);
//    }
    int n;
    scanf("%d", &n);
    int ans = (quick_pow(2, n) + (quick_pow(2, n - 1) * ((quick_pow(2, n - 1) + MOD - 1) % MOD)) % MOD ) % MOD;
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    int t;
    cin >> t;
    while (t--) work();
    return 0;
}
View Code




原文地址:https://www.cnblogs.com/liuweimingcprogram/p/5935833.html