ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDUOJ 4990 Reading comprehension

2020-02-27 19:44:19  阅读:270  来源: 互联网

标签:Reading end matrix int HDUOJ comprehension ans include odd


HDUOJ 4990 Reading comprehension

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 <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.
[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

看到n,m的范围立刻想到要用矩阵快速幂,为理解透彻,我将两种类型的矩阵都打了出来:
n为奇数时变换矩阵A:
[Fn1]=[2101][Fn11] \left[ \begin{matrix} F_n\\ 1\\ \end{matrix} \right] =\left[ \begin{matrix} 2 &1\\ 0 & 1 \\ \end{matrix} \right] \left[ \begin{matrix} F_{n-1}\\ 1\\ \end{matrix} \right] [Fn​1​]=[20​11​][Fn−1​1​]
n为偶数时变换矩阵B:
[Fn1]=[2001][Fn11] \left[ \begin{matrix} F_n\\ 1\\ \end{matrix} \right] =\left[ \begin{matrix} 2 &0\\ 0 & 1 \\ \end{matrix} \right] \left[ \begin{matrix} F_{n-1}\\ 1\\ \end{matrix} \right] [Fn​1​]=[20​01​][Fn−1​1​]
那么我们不难发现,只要把这两个矩阵乘起来 ABA*BA∗B 就是总的变换矩阵,变换次数即为 n/2n/2n/2,但要注意 nnn 为奇数时要再左乘一次 AAA,注意左右乘即可,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2;//矩阵的大小
ll n,mod;
struct mat
{
    ll m[N][N];
}odd,even,a,ans;

mat mul(mat a,mat b)
{
    mat tmp;
    memset(tmp.m,0,sizeof(tmp.m));
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
            for(int k=0;k<N;k++)
             {
                 tmp.m[i][j]=(tmp.m[i][j]%mod+a.m[i][k]*b.m[k][j]%mod)%mod;
                 if(tmp.m[i][j]<0) tmp.m[i][j]=(tmp.m[i][j]+mod)%mod;//防越界
             }
     return tmp;
}

void mat_pow(mat a,ll k)
{
    memset(ans.m,0,sizeof(ans.m));
    ans.m[0][0]=0,ans.m[1][0]=1;
    while(k>0)
    {
        if(k&1) ans=mul(a,ans);
        a=mul(a,a);
        k>>=1;
    }
}

int main(){
    while(cin>>n>>mod){
        n++;
        even.m[0][0]=2,even.m[0][1]=1,even.m[0][2]=0,even.m[0][3]=1;
        odd.m[0][0]=2,odd.m[0][1]=0,odd.m[0][2]=0,odd.m[0][3]=1;
        a=mul(even,odd);
        mat_pow(a,n/2);
        if(n%2) ans=mul(odd,ans);
        cout<<ans.m[0][0]<<endl;
    }
}
旺 崽 发布了311 篇原创文章 · 获赞 18 · 访问量 2万+ 私信 关注

标签:Reading,end,matrix,int,HDUOJ,comprehension,ans,include,odd
来源: https://blog.csdn.net/qq_43765333/article/details/104543055

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有