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

本文介绍了一种使用矩阵快速幂解决特定数学问题的方法,并提供了完整的C++代码实现。该算法适用于处理大规模数值输入,尤其当问题涉及递推序列时表现优异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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. 
[Math Processing Error]TechnicalSpecification 
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
(盗一下图,希望不被骂2333

//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while

//矩阵快速幂板子
inline int Scan()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')flag=1;
    else if(ch>='0' && ch<='9')res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
    return flag ? -res : res;
}

struct Matrix
{
    LL  m[5][5];
} I,A,B,T;

LL a,b,n, mod;
int ssize = 3;  //矩阵size

Matrix Mul(Matrix a,Matrix b)  //
{
    int i,j,k;
    Matrix c;
    for (i = 1; i <= ssize; i++)
    {
        for(j = 1; j <= ssize; j++)
        {
            c.m[i][j]=0;
            for(k = 1; k <= ssize; k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
                c.m[i][j]%=mod;
            }
        }
    }
    return c;
}

Matrix quickpagow(int n)
{
    Matrix m=A, b=I;
    while(n)
    {
        if(n&1)
            b=Mul(b,m);
        n=n>>1;
        m=Mul(m,m);
    }
    return b;
}
void init()//矩阵的初始化
{
    for(int i=1;i<=ssize;i++)
    {
        I.m[i][i]=1;
    }
    return ;
}

int main()
{
    W(cin>>n>>mod)
    {
        me(A.m);
        me(B.m);
        me(T.m);
        init();
        B.m[1][1]=1;
        B.m[1][2]=2;
        B.m[1][3]=1;
        A.m[2][1]=A.m[2][2]=A.m[3][2]=A.m[3][3]=1;
        A.m[1][2]=2;
        if(n==1||n==2)
        {
            cout<<n%mod<<endl;
            continue;
        }
        T=quickpagow(n-2);
        T=Mul(B,T);
        cout<<T.m[1][2]%mod<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值