简体   繁体   English

数字很​​大的数字组成

[英]composition of a number where a number is very large

I am trying to calculate number of ways of composition of a number using numbers 1 and 2. This can be found using fibonacci series where F(1)=1 and F(2)=2 and 我正在尝试使用数字1和2计算一个数的合成方式数。这可以使用斐波那契数列找到,其中F(1)=1F(2)=2并且

F(n)=F(n-1)+F(n-2)

Since F(n) can be very large I just need F(n)%1000000007 .To speed up the process I am using fibonacci exponentiation .I have written two codes for the same problem(both are almost similar).But one of them fails for large numbers.I am not able to figure out which one is correct ? 由于F(n)可能非常大,所以我只需要F(n)%1000000007 。为了加快处理过程,我使用了fibonacci求幂方法。我针对同一问题编写了两个代码(两者几乎相似)。但是其中一个大量失败。我不知道哪一个是正确的?

CODE 1

http://ideone.com/iCPEyz http://ideone.com/iCPEyz

CODE 2

http://ideone.com/Un5p2S http://ideone.com/Un5p2S

Though I have a feeling first one should be correct.I am not able to figure what would happen when there is a case like when we are multiplying say a and b and value of a has already exceeded the upper limit of a and when we multiply this by b ,then how sure can I be that a*b is correct. 虽然我有一种感觉,第一个应该是correct.I我不能数字会发生什么时,有当我们乘说这样的情况下, ab和价值的a已经超过了上限a ,当我们乘用b ,那么我怎么能确定a*b是正确的。 As per my knowledge if a value is above its data type limits then the value starts again from the lowest value like in below example. 按照我的知识,如果a值高于它的数据类型的限制,则该值从最低值再次开始像在下面的例子。

#include<iostream>
#include<limits.h>
using namespace std;
int main()
{
    cout<<UINT_MAX<<endl;
    cout<<UINT_MAX+2;
}

Output

4294967295

1

"Overflow" (you don't really call it that for unsigneds, they wrap around) of unsigned n-bit types will preserve values modulo 2^n only, not modulo an arbitrary modulus (how could they? Try to reproduce the steps with pen and paper). 无符号n位类型的“溢出”(您实际上并不称其为无符号,它们会环绕)将仅保留模2 ^ n的值,而不保留任意模数的模数(它们怎么可能?笔和纸)。 You therefore have to make sure that no operation ever goes over the limits of your type in order to maintain correct results mod 100000007. 因此,您必须确保没有操作超出您的类型限制,以保持正确的结果mod 100000007。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM