简体   繁体   English

如何减少此代码的执行时间?

[英]How can I reduce the execution time in this code?

Problem问题

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.你必须找到这个号码。

Input输入

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

Output Output

For each test case, print a single line containing one integer ― the last remaining number.对于每个测试用例,打印一行,其中包含一个 integer - 最后一个剩余数字。

Code代码

#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中的算法,

  1. Find the largest integer A , such that 2^A < N .找到最大的 integer A ,使得2^A < N
  2. Find Fibonnaci(2^A - 1) % 10Fibonnaci(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.

相关问题 如何减少C ++中以下代码的执行时间? - How to reduce execution time in C++ for the following code? 我如何减少此代码的时间复杂度 - How do i reduce time complexity of this code 通过数组/缓存减少执行时间的代码??? 谷歌应用脚本 - Code to reduce execution time by array / caching??? Google App Script 如何减少此代码的javascript代码,使其不那么重复? - How can I reduce this code javascript code so it's not as repetitve? 如何减少 DASK 数组 map 块执行时间? - How to reduce DASK array map blocks execution time? 如何等待 For 循环完全执行然后运行下一个代码? - How can I wait for complete execution of For loop and then run next code? 如何减少以下代码? 我不想放置“ for循环”,并希望接受输入作为整数列表 - How can I reduce the below code? I do not want to put the 'for loop' and want to accept input as a list of integers 我如何减少从阵列中选取图像的时间复杂度 - how can I reduce time complexity from picking images from array 我怎样才能更有效地编写这个算法? 以降低时间复杂度的方式? - how can i write this algorithm more efficiently ? in a way that reduce time complexity? 如何减少编译时间以及这段代码在哪些情况下会失败 - How to reduce compilation time and which cases will this code fail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM