C++ 你在哪里+;向量中的2个值来自?

C++ 你在哪里+;向量中的2个值来自?,c++,math,fibonacci,C++,Math,Fibonacci,我试图找到斐波那契数的最后一位。根据这个赋值,我首先生成Pisano周期,也就是取模n的Fibonacci数序列重复的周期。 模10的周期等于60。 我在C++中编写了实现皮萨诺周期的代码,但由于某种原因,最终我得到了62个值,而不确定这2个值来自哪里。 n项之和等于:F(n+2)-1,也在下面的代码中实现: #include <iostream> #include <vector> using namespace std; vector<uint64_t>

我试图找到斐波那契数的最后一位。根据这个赋值,我首先生成Pisano周期,也就是取模n的Fibonacci数序列重复的周期。 模10的周期等于60。 我在C++中编写了实现皮萨诺周期的代码,但由于某种原因,最终我得到了62个值,而不确定这2个值来自哪里。 n项之和等于:F(n+2)-1,也在下面的代码中实现:

#include <iostream>
#include <vector>
using namespace std;

vector<uint64_t> fibList; // Fibonacci numbers List
vector<uint64_t> pisanoSequence; // Pisano Sequence list
void generatePisanoSequence(int mod)
{
    fibList.push_back((*(fibList.end()-1) + *(fibList.end()-2)) % mod); // Get the last digits of the next Fibonacci number depending on the modulp. 
    pisanoSequence.push_back(*(fibList.end()-1)); //Put the last digit of the last Fibonacci number in the Pisano Sequence
    if (*(pisanoSequence.end() - 1) == 1 && *(pisanoSequence.end() - 2) == 0) // If we return to having 0 then 1 as inputs to the Pisano sequence that mean we have reached the end of the period of the sequence
    {
        return; // Stop Generating entries
    }
    else
    {
        generatePisanoSequence(mod); // Calculate the next entry.
    }
}
int main()
{
    fibList.push_back(0); // Put 0 to the Fibonacci sequence
    fibList.push_back(1); // Put 1 to the Fibonacci sequence
    pisanoSequence.push_back(0); // Put 0 to the Pisano Sequence
    pisanoSequence.push_back(1); // Put 1 to the Pisano sequence
    generatePisanoSequence(10); // An Examplry Modulos of 1000
    uint64_t n; // Input Fibonacci numbers
    cin >> n;
    n = n % ((pisanoSequence.size()) - 2); //Searching for the right i element // had to delete two of these unwanted elements here
    uint64_t n2 = pisanoSequence[n+2]; //Get the number of F(i)
    n2 = n2 - 1;
    cout << (n2 + 10 ) % 10;
    return 0;
}
#包括
#包括
使用名称空间std;
向量fibList;//斐波那契数列
向量Pisano序列;//皮萨诺序列表
空生成序列(整数模)
{
fibList.push_back((*(fibList.end()-1)+*(fibList.end()-2))%mod);//根据modulp获取下一个Fibonacci数的最后位数。
pisanoSequence.push_back(*(fibList.end()-1));//将最后一个Fibonacci数的最后一位放入Pisano序列中
如果(*(pisanoSequence.end()-1)==1&&*(pisanoSequence.end()-2)==0)//如果我们返回到0,则1作为Pisano序列的输入,这意味着我们已经到达序列周期的末尾
{
return;//停止生成条目
}
其他的
{
generatePanosequence(mod);//计算下一个条目。
}
}
int main()
{
fibList.push_back(0);//将0放入Fibonacci序列
fibList.push_back(1);//将1放入Fibonacci序列
Pisano序列。向后推(0);//将0放入Pisano序列
Pisano序列。向后推(1);//将1放入Pisano序列
generatePanosequence(10);//1000模的一个例子
uint64\u t n;//输入斐波那契数
cin>>n;
n=n%((pisanoSequence.size())-2);//搜索正确的i元素//必须在此处删除其中两个不需要的元素
uint64_t n2=pisanoSequence[n+2];//获取F(i)的个数
n2=n2-1;

当向量的末尾有另一个序列01时,你能停下来吗?因此,向量的开头和结尾都有0和1,但是一个正常的周期只包含它们一次。向量有62个数字01 2 3 5 8 1 4 9 4 3 7 0 7 4 1 6 1 7 8 8 19 9 8 5 2 7 6 7 3 9 9 4 3 5 2 7 2 9 10 1.当0 1重复时,您停止。但最后的这些0 1不是Pisano周期的一部分。您必须删除它们。因此,删除它们,周期大小为60。代码中存在不需要的内容。您的fibList和pisanoSequence具有完全相同的内容。您只需要一个。此外,在cout@Someprogrammerdude中,Fibonacci se的第一个元素序列是1,但第0个元素是0。序列是以第0个元素开始还是以第一个元素开始是有争议的。当您使用n个元素的模和公式时,您将需要访问元素n+2,因此使用n+2大小的向量(额外的0 1)可以方便地避免未定义的行为。