Reading comprehension HDU - 4990

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; 
}

InputMulti test cases,each line will contain two integers n and m. Process to end of file. 
[Technical Specification] 
1<=n, m <= 1000000000OutputFor each case,output an integer,represents the output of above program.Sample Input

1 10
3 100

Sample Output

1
5

直接利用源程序暴力打出 1,2,5,10,21,42 找出规律 fn = fn-1 + 2*fn-2+1

数据比较大,直接求矩阵快速幂。

推出 转化矩阵为:

1 2 1
1 0 0
0 0 1

 初始矩阵为

1 2 1

 直接上代码:

//Asimple
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define test() cout<<"============"<<endl
#define debug(a)  cout << #a << " = "  << a <<endl
#define dobug(a, b)  cout << #a << " = "  << a << " " << #b << " = " << b << endl
using namespace std;
typedef long long ll;
const int N=3;  
//const ll MOD=10000007; 
const int INF = ( 1<<20 );
const double PI=atan(1.0)*4;
const int maxn = 10+5;
const ll mod = 10005;
int n, m, len, ans, sum, v, w, T, num;
int MOD;

struct Matrix {
    long long grid[N][N];  
    int row,col;  
    Matrix():row(N),col(N) {  
        memset(grid, 0, sizeof grid);  
    }  
    Matrix(int row, int col):row(row),col(col) {  
        memset(grid, 0, sizeof grid);  
    }
    
    //矩阵乘法
    Matrix operator *(const Matrix &b) {  
        Matrix res(row, b.col);  
        for(int i = 0; i<res.row; i++)  
            for(int j = 0; j<res.col; j++)  
                for(int k = 0;k<col; k++)  
                    res[i][j] = (res[i][j] + grid[i][k] * b.grid[k][j] + MOD) % MOD;  
        return res;  
    }
    
    //矩阵快速幂
    Matrix operator ^(long long exp) {  
        Matrix res(row, col);
        for(int i = 0; i < row; i++)
            res[i][i] = 1;
        Matrix temp = *this;
        for(; exp > 0; exp >>= 1, temp = temp * temp)
            if(exp & 1) res = temp * res;
        return res;
    }
    
    long long* operator[](int index) {
        return grid[index];
    }
    
    void print() {
    
        for(int i = 0; i <row; i++) {
            for(int j = 0; j < col-1; j++)  
                printf("%d ",grid[i][j]);  
            printf("%d\n",grid[i][col-1]);  
        }  
    }  
};

void input(){
    ios_base::sync_with_stdio(false);
    while( cin >> n >> MOD ) {
        Matrix A;
        A[0][0] = A[0][2] = 1;
        A[0][1] = 2;
        A[1][0] = A[2][2] = 1;
        A = A^n;
        Matrix B;
        B[0][0] = B[0][2] = 1;
        B[0][1] = 2;
        A = A*B;
        if( n%2 ) cout << A[0][0] << endl;
        else cout << A[1][1] << endl;
    }
}

int main(){
    input();
    return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务