标签:printf 包含 data set pad 一个 mat one pull
定义:f0=f1=1, fn=fn-1+fn-2(n>=2)。{fi}称为Fibonacci数列。
输入n,求fn mod q。其中1<=q<=30000。
第一行一个数T(1<=T<=10000)。
以下T行,每行两个数,n,q(n<=109, 1<=q<=30000)
文件包含T行,每行对应一个答案。
3
6 2
7 3
7 11
1
0
10
1<=T<=10000
n<=109, 1<=q<=30000
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 5 using namespace std; 6 7 const int N = 2; 8 int mod; 9 10 struct Matrix{ 11 int a[N][N]; 12 Matrix(){ 13 this->clear(); 14 } 15 void clear(){ 16 memset(a,0,sizeof(a)); 17 } 18 void setone(){ 19 this->clear(); 20 for (int i=0; i<N; ++i) 21 a[i][i] = 1; 22 } 23 Matrix operator * (const Matrix &x) const 24 { 25 Matrix c; 26 for (int k=0; k<N; k++) 27 for (int i=0; i<N; ++i) 28 for (int j=0; j<N; ++j) 29 c.a[i][j] = (c.a[i][j]+1ll*a[i][k]*x.a[k][j])%mod; 30 return c; 31 } 32 }; 33 34 int fibn(int n) 35 { 36 Matrix x,s; 37 x.a[0][0] = x.a[0][1] = x.a[1][0] = 1; 38 s.setone(); 39 for (; n; n>>=1) 40 { 41 if (n&1) s = s*x; 42 x = x*x; 43 } 44 return (s.a[0][0]+s.a[0][1])%mod; 45 } 46 int main() 47 { 48 int t,n; 49 scanf("%d",&t); 50 while (t--) 51 { 52 scanf("%d%d",&n,&mod); 53 n++; 54 if (n==1||n==2) printf("%d\n",1); 55 else printf("%d\n",fibn(n-2)); 56 } 57 return 0; 58 }
标签:printf 包含 data set pad 一个 mat one pull
原文地址:http://www.cnblogs.com/mjtcn/p/7307954.html