您的位置:首页 > 其它

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

2017-07-13 22:41 27 查看
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. 
[Math Processing Error]TechnicalSpecification 

1<=n, m <= 1000000000
OutputFor 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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: