hdu 4990 Reading comprehension

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990

思路:题目难点就是找矩阵。。。。。

code:

#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;

typedef long long LL;

struct Matrix
{
    LL x[5][5];
    friend Matrix operator*(Matrix &a,Matrix &b);
    friend Matrix operator*(Matrix a,LL k);
};

int n,mod;

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

Matrix operator^(Matrix a,LL k)
{
    Matrix unit;
    memset(unit.x,0,sizeof(unit.x));
    for(int i=0;i<=3;i++)
    {
        unit.x[i][i]=1;
    }
    while(k>0)
    {
        if(k&1) unit=unit*a;
        a=a*a;
        k=k/2;
        //printf("AAAAA\n");
    }
    return unit;
}

Matrix A,B;

int main()
{
    while(scanf("%d%d",&n,&mod)==2)
    {
        memset(A.x,0,sizeof(A.x));
        A.x[1][2]=2,A.x[2][1]=1,A.x[2][2]=1;
        A.x[3][2]=A.x[3][3]=1;
        B.x[1][1]=1%mod;
        B.x[1][2]=2%mod;
        B.x[1][3]=1;
        if(n==1)
            printf("%lld\n",B.x[1][1]);
        else if(n==2)
        {
            printf("%lld\n",B.x[1][2]);
        }
        else
        {
            A=A^(n-2);
            B=B*A;
            printf("%lld\n",B.x[1][2]%mod);
        }
    }
    return 0;
}
时间: 2024-10-11 18:18:00

hdu 4990 Reading comprehension的相关文章

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&

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 4990 Reading comprehension(等比数列法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 思路:曾经有一个矩阵乘法的做法请戳这儿.. . . 開始我们把数都不模... 能够得到一个规律 n:1        ans:1      4^0                          n:2     ans:2         2*(4^0) 2                 5      4^0+4^1                        4           

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 4990 Reading comprehension (矩阵快速幂)

题意:读程序,找规律 思路:我们把程序输出发现序列为1,2,5,10,21,42,85,170,递推式f(n)=2*f(n-2)+f(n-1)+1 代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #define ll long long using namespace std; const int N=3,M=3,P=3; ll mod; stru

[矩阵快速幂] hdu 4990 Reading comprehension

题意: 初始值为零,后面奇数项成二加一,偶数项乘二. 思路: 其实区别就在于这个加一. 就是构造一个-1每次相成,然后1-1+1就ok了. 就是 |  -1   1   0  | | -1  1  0 | * |   0   1   1  |  =  | 1  0  1 | |   0   0   2  | 依次类推就好了. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #inc

HDU 4990 Reading comprehension(BestCoder Round #8)

Problem Description: Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include<iostream>#include <cstring>#include <cmath>#include <algor

hdu-4990 Reading comprehension(快速幂+乘法逆元)

题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:1024000000,1024000000"

hdu 4990(数学,等比数列求和)

Reading comprehension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1270    Accepted Submission(s): 512 Problem Description Read the program below carefully then answer the question.#pragma co