Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1057 Accepted Submission(s): 475
Problem Description
Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.
Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.
Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)
The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.
Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.
Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)
The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.
Input
The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.
Each of them will not exceed 1,000,000,000.
Output
For each line input, out the value described above.
Sample Input
2 1 4 100 2 0 4 100
Sample Output
21 12
这道题wa到无语了,


1 #include <iostream>
2 #include <cstring>
3
4 using namespace std;
5
6 typedef __int64 LL;
7 LL mod;
8
9 typedef struct Matrix
10 {
11 LL map[2][2];
12 Matrix()
13 {
14 map[0][1]=map[1][0]=map[1][1]=1;
15 map[0][0]=0;
16 }
17 Matrix operator+(Matrix a)
18 {
19 Matrix temp;
20 for(int i=0;i<2;i++)
21 for(int j=0;j<2;j++)
22 {
23 temp.map[i][j]=(map[i][j]+a.map[i][j])%mod;
24 }
25 return temp;
26 }
27 Matrix operator-(Matrix a)
28 {
29 Matrix temp;
30 for(int i=0;i<2;i++)
31 for(int j=0;j<2;j++)
32 {
33 temp.map[i][j]=(map[i][j]-a.map[i][j]+mod)%mod;
34 }
35 return temp;
36 }
37 Matrix operator*(Matrix a)
38 {
39 Matrix temp;
40 for(int i=0;i<2;i++)
41 for(int j=0;j<2;j++)
42 {
43 int t=0;
44 for(int k=0;k<2;k++)
45 {
46 t+=map[i][k]*a.map[k][j];
47 t%=mod;
48 }
49 temp.map[i][j]=t;
50 }
51 return temp;
52 }
53 }Matrix;
54 Matrix A;
55 Matrix muti1(LL x)
56 {
57 Matrix a=A,ans=A;
58 if(x==1) return A;
59 if(x==0)
60 {
61 ans.map[0][0]=ans.map[1][1]=1;
62 ans.map[0][1]=ans.map[1][0]=0;
63 return ans;
64 }
65 x--;
66 while(x)
67 {
68 if(x&1)
69 ans=ans*a;
70 a=a*a;
71 x>>=1;
72 }
73 return ans;
74 }
75 Matrix muti2(LL x)
76 {
77 Matrix a,ans;
78 if(x==1) return ans;
79 if(x==0)
80 {
81 ans.map[0][0]=ans.map[1][1]=1;
82 ans.map[0][1]=ans.map[1][0]=0;
83 return ans;
84 }
85 x--;
86 while(x)
87 {
88 if(x&1)
89 ans=ans*a;
90 a=a*a;
91 x>>=1;
92 }
93 return ans;
94 }
95
96 Matrix muti(LL x)
97 {
98 Matrix temp,ans;
99 if(x==0)
100 {
101 ans.map[0][0]=ans.map[1][1]=1;
102 ans.map[0][1]=ans.map[1][0]=0;
103 return ans;
104 }
105 if(x==1)
106 {
107 for(int i=0;i<2;i++)
108 for(int j=0;j<2;j++)
109 ans.map[i][j]=A.map[i][j];
110 for(int i=0;i<2;i++)
111 ans.map[i][i]+=1;
112 return ans;
113 }
114 else
115 {
116 if(x&1)
117 {
118 temp=muti(x/2);
119 Matrix temp1=muti1(x/2+1);
120 temp1=temp*temp1;
121 return temp1+temp;
122 }
123 else
124 {
125 temp=muti(x/2);
126 Matrix temp1=muti1(x/2);
127 Matrix temp2=temp1*temp;
128 return temp+temp2-temp1;
129 }
130 }
131 }
132 int main()
133 {
134 LL k,b,n;
135 while(cin>>k>>b>>n>>mod)
136 {
137 Matrix ans1,ans2,ans3,ans;
138 ans1=muti2(b);
139 A=muti2(k);
140 ans3=muti(n-1);
141 ans=ans1*ans3;
142 cout<<ans.map[1][0]<<endl;
143 }
144 return 0;
145 }


1 #include<stdio.h>
2 #include<string.h>
3 int m;
4
5 struct matrix
6 {
7 __int64 a[3][3];
8 };
9 matrix ans2;
10 matrix power(matrix un,matrix un1)
11 {
12 matrix ans;
13 int i,j,h;
14 for(i=1;i<=2;i++)
15 for(j=1;j<=2;j++)
16 {
17 ans.a[i][j]=0;
18 for(h=1;h<=2;h++)
19 {
20 ans.a[i][j]+=un.a[i][h]*un1.a[h][j];
21 ans.a[i][j]%=m;
22 }
23 }
24 return ans;
25 }
26 matrix mod(matrix un,int k)//快速幂
27 {
28 matrix ans;
29 ans.a[1][1]=1;
30 ans.a[1][2]=0;
31 ans.a[2][1]=0;
32 ans.a[2][2]=1;
33 while(k)
34 {
35 if(k%2) ans=power(ans,un);
36 un=power(un,un);
37 k/=2;
38 }
39 return ans;
40 }
41
42 matrix dfs(int n)//递归二分
43 {
44 matrix ans1,ans;
45 int i,j;
46 if(n==0)
47 {
48 for(i=1;i<=2;i++)
49 for(j=1;j<=2;j++)
50 ans.a[i][j]=0;
51 return ans;
52 }
53 if(n==1) return ans2;
54 ans1=mod(ans2,n/2);
55 ans1.a[1][1]++;
56 ans1.a[2][2]++;//(1 + A^(n/2) )
57 ans=dfs(n/2);//( A^1 + A^2 + ... + A^(n/2));
58 ans=power(ans1,ans);
59 if(n%2)//判奇偶
60 {
61 ans1=mod(ans2,n);
62 for(i=1;i<=2;i++)
63 for(j=1;j<=2;j++)
64 {
65 ans.a[i][j]+=ans1.a[i][j];
66 ans.a[i][j]%=m;
67 }
68 }
69 return ans;
70 }
71 int main()
72 {
73 int i,j,k,b,n;
74 matrix ans,un,ans1,mm;
75 while(scanf("%d%d%d%d",&k,&b,&n,&m)!=EOF)
76 {
77 un.a[1][1]=1;
78 un.a[1][2]=1;
79 un.a[2][1]=1;
80 un.a[2][2]=0;
81 ans=mod(un,b+1);//把f[n]放在右下角
82 //ans=mod(un,b+1);把f[n]放在右上角或者左下角
83
84 ans1.a[1][1]=1;
85 ans1.a[1][2]=0;
86 ans1.a[2][1]=0;
87 ans1.a[2][2]=1;//单位矩阵
88
89 ans2=mod(un,k);
90
91 n--;
92 mm=dfs(n);
93 for(i=1;i<=2;i++)
94 for(j=1;j<=2;j++)
95 {
96 mm.a[i][j]+=ans1.a[i][j];
97 mm.a[i][j]%=m;
98 }
99 mm=power(ans,mm);
100 printf("%d\n",mm.a[2][2]%m);//f[n]在右下角
101 //f[n]在右上角或者左下角时输出mm.a[1][2]%m
102 }
103 return 0;
104 }