Lucky Coins Sequence (矩阵快速幂)

Lucky Coins Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 32
 
Problem Description
As we all know,every coin has two sides,with one side facing up and another side facing down.Now,We consider two coins's state is same if they both facing up or down.If we have N coins and put them in a line,all of us know that it will be 2^N different ways.We call a "N coins sequence" as a Lucky Coins Sequence only if there exists more than two continuous coins's state are same.How many different Lucky Coins Sequences exist?
 
Input
There will be sevaral test cases.For each test case,the first line is only a positive integer n,which means n coins put in a line.Also,n not exceed 10^9.
 
Output
You should output the ways of lucky coins sequences exist with n coins ,but the answer will be very large,so you just output the answer module 10007.
 
Sample Input
3
4
 
Sample Output
2
6
 
 
Source
2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU
 
这个题的线性方程就没有那么直白了,需要推导一下。
设a(n)为 长度为n的,Lucky Coins Sequences的个数
设 f(n)为长度为n的,连续face相同个数小于三的情况(2^n - f(n) == a(n))

若f(n)的后两位相同 则f(n)=f(n-2); 即在f(n-2)后面加上00或者11;

若f(n-1)的后两位不同 则f(n)=f(n-1)  即在f(n-1)后面加上0或者1;

因此可得f(n)=f(n-1)+f(n-2);

所以 2^n - f(n) == 2 ^ (n-1) - f(n-1) + 2 ^ (n-2) - f(n-2) + 2 ^ (n-2) -----> a(n) = a(n-1) + a(n-2) + 2^(n-2);

构造矩阵:| a(n)    |         | 1    1    1 |                | a(4).    |

  | a(n-1) |   *    |  1   0    0 | ^ (n-4)  = | a(3)     |

  |2^(n-1)| |  0   0    2 |                | 2^(n-2)|


#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <string.h>
#include <map>
#include <typeinfo>
using namespace std;
const int mod = 10007;
int width;
struct Matrix {
    long long a[35][35];
    void init() {
        memset(this->a, 0, sizeof(this->a));
    }
    Matrix& operator = (const Matrix& m) {
        for (int i = 0; i < width; ++i)
            for (int j = 0; j < width; ++j)
                a[i][j] = m.a[i][j];
        return *this;
    }
};

Matrix operator * (const Matrix& a, const Matrix& b) {
    Matrix c;
    c.init();
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < width; ++j) {
            for (int k = 0; k < width; ++k) {
                c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j]) % mod;
            }
        }
    }
    return c;
}

Matrix operator ^ (Matrix a, int k) {
    Matrix c;
    for (int i = 0; i < width; ++i)
        for (int j = 0; j < width; ++j) {
            if (i == j)
                c.a[i][j] = 1;
            else
                c.a[i][j] = 0;
        }
    while (k) {
        if (k&1)
            c = a * c;
        k >>= 1;
        a = a * a;
    }
    return c;
}

Matrix operator + (const Matrix& a, const Matrix& b) {
    Matrix c;
    c.init();
    for (int i = 0; i < width; ++i)
        for (int j = 0; j < width; ++j)
            c.a[i][j] = (a.a[i][j] + b.a[i][j]) % mod;
    return c;
}

int main() {
    Matrix t, temp;
    width = 4;
    t.init();
    t.a[0][0] = t.a[0][1] = t.a[0][2] = t.a[1][0] = 1;
    t.a[2][2] = 2;
    int a[] = {0, 0, 0, 2, 6};
    int n;
    while (cin >> n) {
        if (n <= 4) {
            cout << a[n] << endl;
        } else {
            temp.init();
            temp = t;
            temp = temp ^ (n-4);
            cout << (temp.a[0][0] * 6 + temp.a[0][1] * 2 + temp.a[0][2] * 8) % mod << endl;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值