[英]How can I reduce the execution time in this code?
Consider the sequence D of the last decimal digits of the first N Fibonacci numbers, ie D = (F0%10,F1%10,…,FN−1%10)
.考虑前 N 个斐波那契数的最后一个十进制数字的序列 D,即D = (F0%10,F1%10,…,FN−1%10)
。
Now, you should perform the following process:现在,您应该执行以下过程:
Let D=(D1,D2,…,Dl)
If l=1, the process ends.如果 l=1,则过程结束。
Create a new sequence创建一个新序列
E=(D2,D4,…,D2⌊l/2⌋)
In other words, E is the sequence created by removing all odd-indexed elements from D换句话说,E 是通过从 D 中删除所有奇数索引元素创建的序列
Change D to E将 D 更改为 E
When this process terminates, the sequence D contains only one number.当这个过程终止时,序列 D 只包含一个数字。 You have to find this number.你必须找到这个号码。
The first line of the input contains a single integer T denoting the number of test cases.输入的第一行包含一个 integer T 表示测试用例的数量。
The description of T test cases follows. T 测试用例的描述如下。 The first and only line of each test case contains a single integer N每个测试用例的第一行也是唯一一行包含一个 integer N
For each test case, print a single line containing one integer ― the last remaining number.对于每个测试用例,打印一行,其中包含一个 integer - 最后一个剩余数字。
#include <stdio.h>
#include <stdlib.h>
int test(int *arr, int n);
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int *arr;
arr = (int *)malloc((n + 1) * sizeof(int));
arr[1] = 0;
arr[2] = 1;
for (int i = 3; i <= n; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
/*
for(int k=1;k<=n;k++){
printf("%d ",arr[k] );
}
printf("\n");
*/
printf("%d\n", (test(arr, n)) % 10);
}
}
int test(int *arr, int n) {
if (n == 1) {
return arr[1];
} else {
for (int i = 1; i <= (n / 2); i++) {
arr[i] = arr[2 * i];
}
return test(arr, n / 2);
}
}
Using the algorithm from https://math.stackexchange.com/questions/681674/recursively-deleting-every-second-element-in-a-list ,使用https://math.stackexchange.com/questions/681674/recursively-deleting-every-second-element-in-a-list中的算法,
A
, such that 2^A < N
.找到最大的 integer A
,使得2^A < N
。Fibonnaci(2^A - 1) % 10
求Fibonnaci(2^A - 1) % 10
Adding to Bill Lynch 's answer , which is itself based on this other answer by happymath :添加到比尔林奇的答案,它本身是基于happymath的另一个答案:
You will always end up getting 2 n − 1 where n is maximum integer such that 2 n < K你最终会得到 2 n - 1 其中n是最大值 integer 使得 2 n < K
I'd like to point out another useful mathematical property.我想指出另一个有用的数学属性。
In number theory, the nth Pisano period, written π(n), is the period with which the sequence of Fibonacci numbers taken modulo n repeats.在数论中,第 n 个 Pisano 周期,记作 π(n),是对 n 取模的斐波那契数列重复的周期。 ( https://en.wikipedia.org/wiki/Pisano_period ) ( https://en.wikipedia.org/wiki/Pisano_period )
Here we need to consider the case where n = 10, π(10) = 60 and the last decimal digits correspond to the OEIS sequence A003893 :这里我们需要考虑 n = 10, π(10) = 60 并且最后一个十进制数字对应于 OEIS 序列A003893的情况:
0, 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1 0, 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1
So that there's no need to calculate the actual Fibonacci number, nor to generate all the sequence up to N.这样就不需要计算实际的斐波那契数,也不需要生成最多 N 的所有序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.