首页 \ 问答 \ 如何找到第n个Fibonacci数的二进制表示(How to find binary representation for n'th Fibonacci number)

如何找到第n个Fibonacci数的二进制表示(How to find binary representation for n'th Fibonacci number)

这是我在这里的第一篇文章,所以如果我犯了一些错误,请告诉我。

我得到了一个赋值,其中一部分需要第n个Fibonacci数的二进制表示。 Constraints-

  1. )C ++必须用作prog。 语言。
  2. )n'th fib。 数字必须以lg(n)时间计算。

我有一个函数,但它适用于整数。 但我必须计算的最大值约为10 ^ 6。 所以,我被困在这里。 无论我知道什么,我都无法在这种情况下应用,因为我可以产生第n个纤维。 使用字符串,但会有线性时间复杂度。 以下是该功能,

void multiply(long int F[2][2], long int M[2][2]);
void power(long int F[2][2], long int n);

// Function to Calculate n'th fibonacci in log(n) time
long int fib(long int n)
{
    long int F[2][2] = {{1,1},{1,0}};
    if(n == 0)
        return 0;
    power(F, n-1);
    return F[0][0];
}

void power(long int F[2][2], long int n)
{
    if( n == 0 || n == 1)
        return;
    long int M[2][2] = {{1,1},{1,0}};

    power(F, n/2);
    multiply(F, F);

    if( n%2 != 0 )
        multiply(F, M);
}

void multiply(long int F[2][2], long int M[2][2])
{
    long int x =  (F[0][0]*M[0][0])%mod + (F[0][1]*M[1][0])%mod;
    long int y =  (F[0][0]*M[0][1])%mod + (F[0][1]*M[1][1])%mod;
    long int z =  (F[1][0]*M[0][0])%mod + (F[1][1]*M[1][0])%mod;
    long int w =  (F[1][0]*M[0][1])%mod + (F[1][1]*M[1][1])%mod;  
    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
}

int main(){
    int n; cin >> n; cout << fib(n)<<endl; getchar();
}

可以看出,此功能中只能使用预定义的数据类型。


Its my first post here, so if I commit some mistake please let me know.

I have been given a assignment, and a part of it requires the binary representation of n'th Fibonacci number. Constraints-

  1. ) C++ has to be used as prog. language.
  2. ) n'th fib. number has to be calculated in lg(n) time.

I have a function but it works on integers. But the maximum value for which I have to do calculations is about 10^6. So, I am badly stuck here. Whatever I know, I can't apply in this scenario, because I can generate n'th fib. using strings but that will have linear time complexity. following is the function,

void multiply(long int F[2][2], long int M[2][2]);
void power(long int F[2][2], long int n);

// Function to Calculate n'th fibonacci in log(n) time
long int fib(long int n)
{
    long int F[2][2] = {{1,1},{1,0}};
    if(n == 0)
        return 0;
    power(F, n-1);
    return F[0][0];
}

void power(long int F[2][2], long int n)
{
    if( n == 0 || n == 1)
        return;
    long int M[2][2] = {{1,1},{1,0}};

    power(F, n/2);
    multiply(F, F);

    if( n%2 != 0 )
        multiply(F, M);
}

void multiply(long int F[2][2], long int M[2][2])
{
    long int x =  (F[0][0]*M[0][0])%mod + (F[0][1]*M[1][0])%mod;
    long int y =  (F[0][0]*M[0][1])%mod + (F[0][1]*M[1][1])%mod;
    long int z =  (F[1][0]*M[0][0])%mod + (F[1][1]*M[1][0])%mod;
    long int w =  (F[1][0]*M[0][1])%mod + (F[1][1]*M[1][1])%mod;  
    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
}

int main(){
    int n; cin >> n; cout << fib(n)<<endl; getchar();
}

As it can be seen, only predefined data types can be used in this function.


原文:https://stackoverflow.com/questions/9859115
更新时间:2022-03-09 13:03

最满意答案

当你指定array2 = array1你实际上正在分配一个数组引用,你不是在复制任何东西,浅或其他。

如果没有对此数组的其他强引用,则将释放引用为array2数组。 由于数组被释放,如果没有其他变量持有对其中任何一个的强引用,那么数组中的对象( obj4obj5obj6 )将被释放

所以,最初你有

array1 = {obj1,obj2,obj3}
array2 = {obj4,obj5,obj6}

完成任务后,你有

array1      array2
  \\         //
 {obj1,obj2,obj3}

并且对象4-6与保持它们的阵列一起被释放


When you assign array2 = array1 you are actually assigning an array reference, you are not copying anything, shallow or otherwise.

The array that was referenced array2 will be released if there is no other strong reference to this array. As a result of the array being released, the objects in the array (obj4, obj5 & obj6) will be released if there is no other variable holding a strong reference to any of them

So, initially you have

array1 = {obj1,obj2,obj3}
array2 = {obj4,obj5,obj6}

After the assignment you have

array1      array2
  \\         //
 {obj1,obj2,obj3}

and objects 4-6 have been released along with the array that held them

相关问答

更多
  • 在data.table , :=和所有 set*函数通过引用更新对象。 这是在2012年IIRC左右推出的。 而在这个时候,R基本没有浅拷贝,而是被深拷贝了。 浅拷贝是从3.1.0开始引入的。 这是一个冗长冗长的答案,但我认为这回答了你的前两个问题: 这种基本的R方法与data.table的等价方法有什么不同? 差异只与速度或内存使用有关吗? 在基本的R v3.1.0 +中,当我们这样做时: DF1 = data.frame(x=1:5, y=6:10, z=11:15) DF2 = DF1[, c("x" ...
  • 浅拷贝重复尽可能少。 集合的浅层副本是集合结构的副本,而不是元素。 有一个浅层的副本,现在两个集合共享各个元素。 深层拷贝重复一切。 集合的深层副本是两个集合,原始集合中的所有元素都重复。 Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two ...
  • 执行浅拷贝时,是否需要“创建新实例”? 是的,您必须创建一个实例来创建对象的副本( shallow或deep )。 只是执行引用分配只会创建一个指向同一实例copy of reference 。 您使用了创建copy的non-static method 。 但一般来说我更喜欢两种方式: - 使用copy-constructor : - public A(A obj) { copy.aValue = obj.aValue; } 并使用它像: - A first = new A(); A copy = ...
  • 没有这样的注释。 在这种情况下,你所能做的就是使用@DiffIgnore忽略person或Person类。 想想贡献,这个注释可以命名为@Shallow 。 There is no such annotation. All you can do in this case is to ignore person property or Person class using @DiffIgnore. Think about contribution, this annotation could be named ...
  • 作业是副本。 你的第二个函数接近,你只需要解引用s 。 这会将*Server s复制到c c := new(Server) *c = *s 对于深层复制,您需要遍历这些字段,并确定需要递归复制的内容。 根据*httprouter.Router是什么,如果它包含未导出字段中的数据,则可能无法进行深层复制。 Assignment is a copy. Your second function comes close, you just need to dereference s. This copies th ...
  • 当你指定array2 = array1你实际上正在分配一个数组引用,你不是在复制任何东西,浅或其他。 如果没有对此数组的其他强引用,则将释放引用为array2数组。 由于数组被释放,如果没有其他变量持有对其中任何一个的强引用,那么数组中的对象( obj4 , obj5和obj6 )将被释放 所以,最初你有 array1 = {obj1,obj2,obj3} array2 = {obj4,obj5,obj6} 完成任务后,你有 array1 array2 \\ // {obj ...
  • 是的,请参阅演示: var o = [{ x: 1 }] var o2 = o.slice(0) o2[0].x = 2 console.log(o[0].x) Yes,see demo: var o = [{ x: 1 }] var o2 = o.slice(0) o2[0].x = 2 console.log(o[0].x)
  • 值类可以包含句柄类,如果您修改它,您将更改句柄类的实例。 例如(请注意, containers.Map是一个内置类,它是一个句柄 - 没什么特别的,我只是为了方便选择它: >> a = containers.Map; a('hello') = 1; >> b = struct('field1', 1, 'field2', a); >> isa(b, 'handle') ans = logical 0 >> b.field2('hello') = 2; >> a('hello') ans = ...
  • 字符串和整数是不可变的。 修改数据结构以使用包含引用的可变结构,例如数组或集合。 例如 public class Shallow { private Object[] properties = new Object[2]; public Shallow(String name, int number) { this.properties[0] = name; this.properties[1] = number; } public Sha ...
  • 如果使用“浅拷贝”,则意味着在赋值包含数组的struct后,数组将指向原始struct的数据,然后:它不能。 数组中的每个元素都必须复制到新的struct 。 如果你的结构有指针,那么“浅拷贝”会出现在图片中。 如果没有,你不能做一个浅拷贝。 将包含数组的struct赋值给某个值时,它不能进行浅拷贝,因为这意味着分配给数组,这是非法的。 因此,您获得的唯一副本是深层复制。 考虑: #include struct data { char message[6]; }; int m ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)