*[hackerrank]Girlfriend & Necklace

https://www.hackerrank.com/contests/w8/challenges/gneck

有点意思。是DP,最优解包含最优子问题。F(X)=F(X-1)+F(X-3)。因为F(X-1)右边放个“R”和F(X-3)右边放"RRB"是左右可以互推的,包含全了最右是R和B的两种情况。

#include <iostream>
using namespace std;

const uint64_t MOD = 1000000007;

uint64_t resolve(uint64_t x) {
    uint64_t a = 3; //2
    uint64_t b = 4; //3
    uint64_t c = 6; //4
    if (x == 2)
        return a;
    if (x == 3)
        return b;
    if (x == 4)
        return c;
    for (int64_t i = 5; i <= x; i++) {
        int64_t r = a + c;
        a = b % MOD;
        b = c % MOD;
        c = r % MOD;
    }
    return c;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        uint64_t N;
        cin >> N;
        cout << resolve(N) << endl;
    }
    return 0;
}

  

posted @ 2014-08-11 20:16  阿牧遥  阅读(219)  评论(0编辑  收藏  举报