标签:style blog http color os java io ar for
Given a 2x2xN box, in how many ways can you fill it with 1x1x2 blocks?
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).
For each test case, print the number of ways to fill the box modulo 1,000,000,007
3 1 2 3
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 * E k( 0 ~ n - 3 ) f( k );
#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; }
标签:style blog http color os java io ar for
原文地址:http://www.cnblogs.com/YRETSIM/p/3942348.html