HDU 4291 A Short problem(矩阵快速幂+循环节)

题目链接“:

http://acm.hdu.edu.cn/showproblem.php?pid=4291

题意:

g(0)=0,g(1)=1;

g(n) = 3g(n - 1) + g(n - 2);

求g(g(g(n))) mod 109 + 7

分析:

首先我们得认识到,如果一层一层算是必定会超时的。

其次,取模运算是有循环节的。

step1我们找出g(x)%1000000007的循环节 mod1

step2 设g(g(n)) = g(x) x=g(n) 对mod1 取模得到mod2.

剩下的工作就是进行三次的快速幂运算 从内到外进行。

代码如下:

#include <iostream>
#include <cstdio>
using namespace std;

typedef long long LL;

const LL mod = 1e9+7;

const LL mod1 = 222222224;

const LL mod2 = 183120;

//´ò±íÕÒÑ­»·½Ú
//mod 1e9+7 ===> mod1 ,mod mod1 ===>mod2;
/*
int main()
{
    LL f0=0,f1=1;
    for(LL i=1;;i++){
        LL tmp = (3 * f1 + f0)%mod;
        f0 = f1 ;
        f1 = tmp;
        if(f0==0&&f1==1){
            printf("ans: %d\n",i);
            break;
        }
    }
    return 0;
}
*/

struct matrix{
    LL a[2][2];
};

matrix I={
    1,0,
    0,1
};

matrix multi(matrix A,matrix B,int mod){
    matrix C;
    for(int i=0;i<2;i++){
        for(int j = 0; j < 2;j++){
            C.a[i][j]=0;
            for(int k=0;k<2;k++)
                C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j])%mod;
        }
    }
    return C;
}

LL pow(matrix A ,LL n,LL mod){
    matrix tmp = I;
    while(n){
        if(n&1)
            tmp= multi(tmp,A,mod);
        n>>=1;
        A=multi(A,A,mod);
    }
    return tmp.a[0][0];
}

int main(){
    LL n;
    while(~scanf("%lld",&n)){
        matrix A= {
            3,1,
            1,0
        };
        if(n>=2) n = pow(A,n-1,mod2);
        if(n>=2) n = pow(A,n-1,mod1);
        if(n>=2) n = pow(A,n-1,mod);
        printf("%I64d\n",n);
    }
    return 0;
}



时间: 2024-08-11 04:38:04

HDU 4291 A Short problem(矩阵快速幂+循环节)的相关文章

HDU - 1588 Gauss Fibonacci (矩阵快速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob

hdu 1588 Gauss Fibonacci(矩阵快速幂)

Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2090    Accepted Submission(s): 903 Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题目链接:hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N: 矩阵C = A*B 矩阵M=CN?N 将矩阵M中的所有元素取模6,得到新矩阵M' 计算矩阵M'中所有元素的和 解题思路:因为矩阵C为N*N的矩阵,N最大为1000,就算用快速幂也超时,但是因为C = A*B, 所以CN?N=ABAB-AB=AC′N?N?1B,C' = B*A, 为K*K的矩阵,K最大为6,完全可以接受. #include <cstdio> #inc

HDU 2294 Pendant (DP+矩阵快速幂降维)

HDU 2294 Pendant (DP+矩阵快速幂降维) ACM 题目地址:HDU 2294 Pendant 题意: 土豪给妹子做首饰,他有K种珍珠,每种N个,为了炫富,他每种珍珠都要用上.问他能做几种长度[1,N]的首饰. 分析: 1 ≤ N ≤ 1,000,000,000简直可怕. 首先想dp,很明显可以想到: dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j](dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个) 然后遇到N太大的话,

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

HDU 4291 A Short problem(矩阵+循环节)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2711    Accepted Submission(s): 951 Problem Description According to a research, VIM users tend to have shorter fingers, compared