UVA 12446 How Many... in 3D! ( 递推 + 树状数组 )

C. How Many... in 3D!

Time Limit: 1000ms
Memory Limit: 131072KB
                                              64-bit integer IO format: %lld      Java class name: Main            
Submit                 Status                 PID: 20864              
            

[PDF Link]

Problem C: How Many... in 3D!

Given a 2x2xN box, in how many ways can you fill it with 1x1x2 blocks?

Input Format

The input starts with an integer T - the number of test cases (T <= 10,000). T cases follow on each subsequent line, each of them containing the integer N (1 <= N <= 1,000,000).

Output Format

For each test case, print the number of ways to fill the box modulo 1,000,000,007

Sample Input

3
1
2
3

Sample Output

2
9
32


今日组队赛卡住条水题,卡左成个下昼,我都唔知自己做紧咩。其他做得的题队友过晒。最后都AC左比较安慰。
写树状数组然后又唔记得update答案,好心酸。

讲下条题先,就是要用一个 1 x 1 x 2 的小立方体去填充一个 2 x 2 x N 的大立方体,有多少种方案 。

条公式就是 f( n ) = 2 * f ( n -1 ) + 5*f( n -2 ) + 4 * sigma f( n - 3 );
f(n)就表示叠满到第n层的数量
首先 f(n - 1) 层到 f(n)就只有两种方案而已 。
然后 f(n - 2) 层到 f(n)有5种 , 全部竖着放 1 种 , 两个横两个竖 4 种 。
而 4 * sigma f( n-3 )就是 f( k ) [ 0<= k <= n-3 ] 意思就是 到达 k 层是刚好覆盖了的 ,
然后 k ~ n 层是 竖着放 参差 不齐的
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 1000010;
const int mod = 1000000007;
LL c[N];
LL ans[N];

int lowbit(int x){return x&-x;}
void update(int pos,int key){
    while( pos<=1e6 ){
        c[pos] += key;
        c[pos] %= mod;
        pos += lowbit(pos);
    }
}

LL query(int pos ){
    LL res=0;
    while(pos>0){
        res += c[pos];
        res %= mod;
        pos -= lowbit(pos);
    }
    return res;
}
void init()
{
    update(1,1);
    update(2,2);

    ans[1] = 1;
    ans[2] = 2;

    for(int i= 3; i <=N-9 ;++i){
        LL res = ( 4*query( i-3 ) )%mod;
        res = (res + 2*ans[i-1]) % mod;
        res = (res + 5*ans[i-2]) % mod;
        ans[i] = res;
        update(i,ans[i]);
    }

}
int main()
{
    int _,n;
    init();
    scanf("%d",&_);
    while(_--){
        scanf("%d",&n);
        printf("%lld\n",ans[n+1]);
    }
    return 0;
}
View Code

 

posted @ 2014-08-28 20:24  hl_mark  阅读(229)  评论(0)    收藏  举报