hdu Gauss Fibonacci

本文详细解析了一个结合高斯求和与斐波那契数列的算法竞赛题目——GaussFibonacci问题。通过矩阵快速幂和递归二分的方法高效计算特定数列的前n项和,并提供了两种不同的实现思路及其代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

 

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.
 

 

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到无语了,
View Code
  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 }
别人的正确的代码
View Code
  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 }


转载于:https://www.cnblogs.com/one--world--one--dream/archive/2011/11/12/2246662.html

### 使用多种编程语言实现输出斐波那契数列的前四项 以下是几种常见编程语言实现输出斐波那契数列前四项的方法: #### C++ 实现 在C++中可以通过简单的循环来计算并打印斐波那契数列的前几项。 ```cpp #include <iostream> using namespace std; int main() { cout << "Fibonacci数列的前4项如下:" << endl; int a = 1, b = 1; // 初始化前两项 cout << a << " " << b << " "; // 打印前两项 for (int i = 1; i <= 2; ++i) { // 计算并打印后续两项 int nextTerm = a + b; cout << nextTerm << " "; a = b; b = nextTerm; } cout << endl; return 0; } ``` 此代码片段基于引用中的逻辑[^1],简化为仅输出前四项。 --- #### Python 实现 Python 提供了一种简洁的方式来生成斐波那契数列。通过列表推导或其他方法可轻松完成任务。 ```python def fibonacci_four_terms(): terms = [1, 1] # 初始两个值 for _ in range(2): # 添加接下来的两项 terms.append(terms[-1] + terms[-2]) return terms[:4] result = fibonacci_four_terms() print("Fibonacci数列的前4项:", result) ``` 上述代码利用了动态数组的概念,类似于引用中的描述[^2],但调整为了只生成四个数值。 --- #### Java 实现 Java 中可以借助 `ArrayList` 来存储和操作斐波那契序列。 ```java import java.util.ArrayList; public class FibonacciFourTerms { public static void main(String[] args) { ArrayList<Integer> fabList = new ArrayList<>(); fabList.add(1); fabList.add(1); for (int i = 2; i < 4; i++) { fabList.add(fabList.get(i - 1) + fabList.get(i - 2)); } System.out.println("Fibonacci数列的前4项:"); for (Integer num : fabList) { System.out.print(num + " "); } } } ``` 这段代码参考了 Java 的实现方式[^5],并对范围进行了修改以便适应当前需求。 --- #### C 实现 对于更基础的语言如C,则可以直接采用数组或者变量交换的方式处理。 ```c #include <stdio.h> void print_fibonacci_first_four() { int first = 1, second = 1; printf("%d %d ", first, second); // 输出前两项目 for(int i = 3; i <= 4; i++) { // 继续计算剩余部分直到第四项为止 int third = first + second; printf("%d ", third); first = second; second = third; } } int main(){ print_fibonacci_first_four(); return 0; } ``` 该版本遵循传统迭代模式构建结果集,并且保持简单明了结构设计思路来自其他例子[^3]^。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值