Problem Introduction

The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_{i-1}+F_{i-2}\) for $ i \geq 2$.

Problem Description

Task.Given two integers \(n\) and \(m\), output \(F_n \ mod \ m\)(that is, the remainder of \(F_n\) when divided by \(m\)).

Input Format.The input consists of two integers \(n\) and \(m\) given on the same line(separated by a space).

Constraints. \(1 \leq n \leq 10^{18}, 2 \leq m \leq 10^5\).

Output Format.Output \(F_n \ mod \ m\)

Sample 1.
Input:

281621358815590 30524

Output:

11963

Solution

# Uses python3
import sys

def get_fibonaccihuge(n, m):
    x, y = 0, 1
    pisano = []
    while True:
        pisano.append(x)
        x, y = y % m, (x+y) % m
        if x == 0 and y == 1:
            break
    return pisano[n % len(pisano)]

if __name__ == '__main__':
    input = sys.stdin.read()
    n, m = map(int, input.split())
    print(get_fibonaccihuge(n, m))

[UCSD白板题] Huge Fibonacci Number modulo m的更多相关文章

  1. [UCSD白板题 ]Small Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  2. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  3. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  4. 【LeetCode每天一题】Fibonacci Number(斐波那契数列)

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  5. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  6. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  7. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  8. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  9. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

随机推荐

  1. 初学c# -- 学习笔记(八)

    RichTextBox我服了,背景透明.宽度高度自适应.图片背景透明.gif动画等等都tm实现困难无比,搞来搞去,就最后一题做不出来了: RichTextBox不管什么背景,透明背景也好,图片背景也罢 ...

  2. pooling的原理与Python实现

    本文首先阐述pooling所对应的操作,然后分析pooling背后蕴含的一些道理,最后给出pooling的Python实现. 一.pooling所对应的操作 首先从整体上对pooling有一个直观的概 ...

  3. 在Win10系统中关闭Hyper-V

    1.将鼠标移至开始图标,单击右键(注意:是右键,不是左键!!!): 2.点击“控制面板”: 3.点击“程序”: 4.点击“启用或关闭windows功能”: 5.去掉“Hyper-V”的勾选,确定:

  4. android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项

    1 Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3VGA     640*480 (Video Graphics Array)QVGA  320*240 (Quarter VGA ...

  5. less预处理的好处,补充关于之前发表的rem单位的运用于计算

    我认识的less 优点:优雅,好用,简单,可复用性强, 缺点:less并其实不能为我们减少沉余css代码,还是要靠自己的CSS基础去判断哪些是沉余代码或者是可以合并的代码 之前发表的一篇文章一看就懂得 ...

  6. 博弈SG

    先转一篇看得比较懂的,以后有时间自己再归纳下 转自:http://blog.csdn.net/logic_nut/article/details/4711489 博弈问题若你想仔细学习博弈论,我强烈推 ...

  7. 嵌入式linux学习笔记1—内存管理MMU之虚拟地址到物理地址的转化

    一.内存管理基本知识 1.S3C2440最多会用到两级页表:以段的方式进行转换时只用到一级页表,以页的方式进行转换时用到两级页表.页的大小有三种:大页(64KB),小页(4KB),极小页(1KB).条 ...

  8. mysql学习(3)-linux下mysql主从复制

    前言:为什么MySQL要做主从复制(读写分离)?通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低.为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻 ...

  9. 修改了chrome的官方的有道词典插件,添加了生词本的功能

    项目地址+导入教程 https://github.com/cclient/chrome-extensions-youdaowithwordnode

  10. agsXMPP参考代码

    agsXMPP 1.删除好友 XmppCon.RosterManager.RemoveRosterItem(node.RosterItem.Jid); 2.注销用户 void userConn_OnL ...