HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension

题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]。

先不取模,计算前十个找规律。得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1

然后快速幂解决之。

给出一个神奇的网站(找数列通项):http://oeis.org/

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#define ll __int64
struct Matrix
{
    ll m[10][10];
};
struct Matrix I,s;
ll n,kmod;

Matrix Mul(Matrix a,Matrix b)
{
    Matrix c;
    ll i,j,k;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            c.m[i][j]=0;
            for(k=0;k<3;k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
                c.m[i][j]%=kmod;
            }
        }
    }
    return c;
}

Matrix Quickpow(Matrix a,ll n)
{
    Matrix m,b;
    m=a,b=I;
    while(n)
    {
        if(n%2)
            b=Mul(b,m);
        n/=2;
        m=Mul(m,m);
    }
    return b;
}

int main()
{
    ll i,j;
    ll k,a,b;
    while(scanf("%I64d %I64d",&n,&kmod)!=EOF)
    {
        Matrix ans,p,q;
        memset(I.m,0,sizeof I.m);
        memset(p.m,0,sizeof p.m);
        memset(q.m,0,sizeof q.m);
        for(i=0;i<3;i++)
            I.m[i][i]=1;
        q.m[0][0]=1;
        q.m[0][1]=2;
        q.m[0][2]=1;

        p.m[1][0]=1;
        p.m[0][1]=2;
        p.m[1][1]=1;
        p.m[2][1]=1;
        p.m[2][2]=1;
        if(n==1)
            printf("%I64d\n",1%kmod);
        else if(n==2)
            printf("%I64d\n",2%kmod);
        else
        {
            ans=Quickpow(p,n-2);
            ans=Mul(q,ans);
            printf("%I64d\n",ans.m[0][1]%kmod);
        }
    }

    return 0;
}
时间: 2024-10-25 22:04:06

HDU 4990 Reading comprehension (找规律+矩阵快速幂)的相关文章

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

(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,

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as

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

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次,可以变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个只有6x6,就可以用矩阵快速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N]

HDU 2256 Problem of Precision (矩阵快速幂)

HDU 2256 Problem of Precision (矩阵快速幂) ACM 题目地址:HDU 2256 Problem of Precision 题意: 给出一个式子,求值. 分析: 推起来最后那步会比较难想. 具体过程见: 表示共轭只听说过复数的和图的... 这构题痕迹好明显... 跟基友开玩笑说:如果遇到这种题,推到Xn+Yn*sqrt(6)这步时,打表最多只能打到10就爆int了,这是输出正解和Xn,说不定眼神好能发现ans = Xn * 2 - 1呢.= =... 代码: /*

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

HDU 4990 Reading comprehension (矩阵快速幂)

题意:给一个数列a[i]=2a[i-1](如果i是偶数) a[i]=2a[i-1]+1(如果i是奇数):求a[n]%m (1<=n, m <= 1000000000) 思路:明显用矩阵快速幂,可以推出通项:a[n]=2*a[n-2]+a[n-1]+1 当然并不需要动脑...直接当成偶数处理就好,是奇数的话单独再递推一项就好.也就是a[i]=4a[i-2]+2 //4990 0MS 1620K 1196 B C++ #include<cstdio> #include<iostr

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 2256 Problem of Precision(矩阵快速幂)+ HDU 4565

HDU 2256 题意: 计算?(2√+3√)2n?mod1024 思路: ∵f(n)=(2√+3√)2n=(5+26√)n=An+Bn?6√ ∴f(n?1)=An?1+Bn?1?6√ 又∵f(n)=(5+26√)?f(n?1) ∴f(n)=(5?An?1+12?Bn?1)+(2?An?1+5?Bn?1)?6√ 所以递推矩阵就是: (52125)?(An?1Bn?1)=(AnBn) A1=5,B1=2. 然后套矩阵快速幂模板即可求出An,Bn. 又∵(5+26√)n=An+Bn?6√ ∴(5?2