码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "Lego Blocks"

时间:2015-08-02 06:23:17      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

A combination of different DP passes. https://www.hackerrank.com/challenges/lego-blocks/editorial

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

typedef unsigned long long ULL;

const ULL mod = 1000000007;

ULL pow(ULL a, int p)
{
    ULL ans = 1;
    while (p)
    {
        if (p % 2) ans = ans * a % mod;
        a = a * a % mod;
        p /= 2;
    }
    return ans;
}

int main(void)
{
    //    Pre-calculation: one row count DP
    vector<ULL> f(1111, 0);
    f[0] = 1;
    for (int i = 1; i <= 1000; i++)
        for (int j = 1; j <= 4; j++)
            if (i - j >= 0) f[i] = (f[i] + f[i - j]) % mod;

    int t; cin >> t;
    while (t--)
    {
        int n, m; cin >> n >> m;

        //    All-row count
        vector<ULL> g(m + 1, 0);
        for (int i = 1; i <= m; i++) g[i] = pow(f[i], n);

        vector<ULL> h(m + 1, 0);
        h[1] = 1; // only possible with 1x1x1
        for (int i = 2; i <= m; i++)
        {
            h[i] = g[i];
            //    remove all un-solid cases
            ULL tmp = 0;
            for (int j = 1; j<i; j++)
                tmp = (tmp + h[j] * g[i - j]) % mod;
            h[i] = (h[i] - tmp + mod) % mod;
        }
        cout << h[m] << "\n";
    }

    return 0;
}

HackerRank - "Lego Blocks"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4695028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!