hdu4990---Reading comprehension(矩阵快速幂)

Problem Description

Read the program below carefully then answer the question.

pragma comment(linker, “/STACK:1024000000,1024000000”)

include

include

include

include

include

include

const int MAX=100000*2;

const int INF=1e9;

int main()

{

int n,m,ans,i;

while(scanf(“%d%d”,&n,&m)!=EOF)

{

ans=0;

for(i=1;i<=n;i++)

{

if(i&1)ans=(ans*2+1)%m;

else ans=ans*2%m;

}

printf(“%d\n”,ans);

}

return 0;

}

Input

Multi test cases,each line will contain two integers n and m. Process to end of file.

[Technical Specification]

1<=n, m <= 1000000000

Output

For each case,output an integer,represents the output of above program.

Sample Input

1 10 3 100

Sample Output

1 5

Source

BestCoder Round #8

Recommend

heyang | We have carefully selected several similar problems for you: 5189 5188 5186 5185 5184

n很大,线性方法不行

第一个奇数是1,产生的1经过了n-1次乘法,最终是2^(n-1)

第二个奇数是3,产生的1经过了n-3次乘法,最终是2^(n-3)

……

因此最后就是2^(n - 1) + 2 ^ (n -3 ) + …..

可以分n奇偶考虑

奇数: 2^0 + 2 ^ 2 + …. + 2 ^ (n-1) -> 4^0 + 4^1 + …+ 4 ^(n-1)/2

偶数:2 * (4^0 + 4^1 + … + 4^(n -2) / 2)

->矩阵快速幂求出即可

/*************************************************************************
    > File Name: hdu4990.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月15日 星期日 20时32分47秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

LL mod, n;

class MARTIX
{
    public:
        LL mat[5][5];
        MARTIX();
        MARTIX operator * (const MARTIX &b)const;
        MARTIX& operator = (const MARTIX &b);
}A;

MARTIX::MARTIX()
{
    memset (mat, 0, sizeof(mat));
}

MARTIX MARTIX :: operator * (const MARTIX &b)const
{
    MARTIX ret;
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for (int k = 0; k < 3; ++k)
            {
                ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
                ret.mat[i][j] %= mod;
            }
        }
    }
    return ret;
}

MARTIX& MARTIX :: operator = (const MARTIX &b)
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            this -> mat[i][j] = b.mat[i][j];
        }
    }
    return *this;
}

MARTIX fastpow(MARTIX ret, LL n)
{
    MARTIX ans;
    for (int i = 0; i < 3; ++i)
    {
        ans.mat[i][i] = 1;
    }
    while (n)
    {
        if (n & 1)
        {
            ans = ans * ret;
        }
        ret = ret * ret;
        n >>= 1;
    }
    return ans;
}

void Debug(MARTIX A)
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%lld ", A.mat[i][j]);
        }
        printf("\n");
    }
}

int main ()
{
    while (~scanf("%lld%lld", &n, &mod))
    {
        A.mat[0][0] = A.mat[0][2] = 4 % mod;
        A.mat[0][1] = A.mat[2][2] = 1 % mod;
        MARTIX F;
        F.mat[0][0] = 4 % mod;
        F.mat[0][1] = 1 % mod;
        F.mat[0][2] = 5 % mod;
        if (n == 0 || n == 1 || n == 2)
        {
            printf("%lld\n", n % mod);
            continue;
        }
        if (n & 1)
        {
            int cnt = ((n - 1) >> 1);
            MARTIX ans = fastpow(A, cnt - 1);
            ans = F * ans;
            printf("%lld\n", ans.mat[0][2]);
        }
        else
        {
            MARTIX ans = fastpow(A, ((n - 2) >> 1) - 1);
            ans = F * ans;
            printf("%lld\n", ans.mat[0][2] * 2 % mod);
        }
    }
    return 0;
}
时间: 2024-08-10 21:22:46

hdu4990---Reading comprehension(矩阵快速幂)的相关文章

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(找规律+矩阵快速幂)

题目链接: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

Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)

for(i=1;i<=n;i++) { if(i&1)ans=(ans*2+1)%m; else ans=ans*2%m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 i奇数:ans[i] = 2^(i-1) + 2^(i-3) ... + 1; i偶数:ans[i] = 2^(i-1) + 2^(i-3) ... + 2; 故可以用等比数列求和公式. 公式涉及除法.我也没弄懂为啥不能用逆元,貌似说是啥逆元

hdu4990矩阵快速幂

就是优化一段代码,用矩阵快速幂(刚开始想到了转移矩阵以为是错的) 在搜题解时发现了一个神奇的网站:http://oeis.org/ 用来找数列规律 的神器.... 规律就是an=an-1+2*an-2+1 然后构造矩阵就行了 还有特例1的时候记得%m #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector>

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: 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); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k