HDU6030 Happy Necklace(推导+矩阵快速幂)

HDU6030 Happy Necklace

推导或者可以找规律有公式:\(f[n] = f[n-1] + f[n-3]\) 。

构造矩阵乘法:
\[
\begin{pmatrix} f_i \\ f_{i-1} \\ f_{i-2} \end{pmatrix} = \begin{pmatrix} 1 & 0 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{pmatrix}\begin{pmatrix} f_{i-1} \\ f_{i-2} \\ f_{i-3} \end{pmatrix}
\]

#include<bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
int t;
long long n;
struct Matrix{
    long long a[5][5];
};

Matrix mul(Matrix M1, Matrix M2)
{
    Matrix ret;
    memset(ret.a, 0, sizeof(ret.a));
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            for(int k = 0; k < 3; k++){
                ret.a[i][j] = (M1.a[i][k] * M2.a[k][j] + ret.a[i][j]) % mod;
            }
        }
    }
    return ret;
}
void matrix_pow(long long x)
{
    Matrix ret;
    memset(ret.a, 0, sizeof(ret.a));
    for(int i = 0; i < 3; i++) ret.a[i][i] = 1;
    Matrix tmp;
    memset(tmp.a, 0, sizeof(tmp.a));
    tmp.a[0][0] = tmp.a[0][2] = tmp.a[1][0] = tmp.a[2][1] = 1;
    while(x){
        if(x & 1LL) ret = mul(ret, tmp);
        tmp = mul(tmp, tmp);
        x >>= 1LL;
    }
    long long ans = (ret.a[0][0] * 4 + ret.a[0][2] * 2 + ret.a[0][1] * 3) % mod;
    cout << ans << endl;
}
int main()
{
    for(scanf("%d", &t); t--; ){
        scanf("%lld", &n);
        if(n == 1) {puts("2"); continue;}
        else if(n == 2) {puts("3"); continue;}
        else if(n == 3) {puts("4"); continue;}
        else{
            matrix_pow(n - 3);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/solvit/p/11445300.html

时间: 2024-09-30 02:38:15

HDU6030 Happy Necklace(推导+矩阵快速幂)的相关文章

[ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了一发没过 上网看了一下才知道是快速幂 而且特征方程的推导简直精妙 尤其是共轭相抵消的构造 真的是太看能力了 (下图转自某大神博客) 特征方程是C^2=-2*a*C+(a*a-b) 然后用快速幂求解 临时学了下矩阵快速幂 从这道题能看出来 弄ACM真的要数学好 这不是学校认知的高数 线代 概率分数 而

LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^64,定义成unsigned long long 类型,因为无符号类型超过最大范围的数与该数%最大范围 的效果是一样的. AC代码: #include<stdio.h> #include<string.h> #define LL unsigned long long struct Ma

LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂

http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \((ab)C_{n}^{r}a^{n-r}b{r} = C_{n+2}^{r}a^{n-r+2}b{r} - a^{n+2} - b^{n+2} \) 综上\( f(n) = (a+b)f(n-1)-(ab)f(n-2) \) /** @Date : 2016-12-19-19.53 * @Author

CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)

题目链接  Broken Clock   中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 令$f(x) = \frac{g(x)}{l^{x}}$ $\frac{g(x)}{l^{x}} = \frac{2d}{l} * \frac{g(x-1)}{l^{x-1}} - \frac{g(x-2)}{l^{x-2}}$ $g(x) = 2dg(x-1) - l^{2}g(x-2)$ 原文地址:ht

bnu 34985 Elegant String(矩阵快速幂+dp推导公式)

Elegant String Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory      2-SAT     Articulation/Bridge/Biconnected Component      Cy

(hdu 6030) Happy Necklace 找规律+矩阵快速幂

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads. Little Q desperately wants to impress his girlfriend,

2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1146    Accepted Submission(s): 491 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single

POJ3420 递推+矩阵快速幂

POJ3420 很有趣的覆盖问题 递归推导如下: f[n] = f[n-1] + 4*f[n-2] + 2 * [ f[n-3] + f[n-5] + f[n-7] +.... ] + 3 *  [ f[n-4] + f[n-6] + f[n-8] +.... ] ; (1) f[n - 2] = f[n-3] + 4*f[n-4] + 2 * [ f[n-5] + f[n-7] + f[n-9] +.... ] + 3 *  [ f[n-6] + f[n-8] + f[n-10] +....

SHUOJ1857 Yaoge鸡排系列之九——好多鸡排!!!【矩阵快速幂】

转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://202.121.199.212/JudgeOnline/problem.php?id=1857 1857: Yaoge鸡排系列之九——好多鸡排!!! Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 98  Solved: 6 Description Yaoge买了n块鸡排,其中第n块鸡排的质量为M(n),同时