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. iOS的架构

    根据多年的iOS开发经验,常用的iOS开发架构有:MVC.MVVM.CDD等,在这里我就不一一列举了. 做一个项目一般首先要搭建主流框架界面:常见的有TabBar控制器可以切换子控制器,上面又有Nav ...

  2. M站开发规范——By Klax

    M站开发的规范,根据具体情况,涉及代码组织的模式,代码编码风格,模块化等,经...研究...决定: 1.采用AMD 规范(RequireJS)实现js模块化. 2.单个文件尽量采用面向对象编程和模块化 ...

  3. (引用)Python 生成随机数小结

    转载:http://blog.csdn.net/shuaijiasanshao/article/details/51339438

  4. JAVA 之print,printf,println

    print:将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后. println: 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始. printf:是格式 ...

  5. 第一章 删掉centos原有的openjdk并安装sun jdk

    一.卸载原有openjdk rpm -qa | grep java 之后,将展示出来的全部卸载掉,我这里是5个 rpm -e --nodeps java-1.7.0-openjdk-1.7.0.111 ...

  6. 关于yii2框架活动记录activeRecord添加默认字段的问题

    平时使用sql的时候可以如下添加默认字段flag: "select a.*,0 as flag from user_info a", 对于yii2框架则需要这样: $query = ...

  7. 10个使用Java最广泛的现实领域

    10个使用Java最广泛的现实领域 如果你是一个初学者,刚刚开始学习Java,你可能会想Java有什么用呢?除了Minecraft貌似也看不到其他用Java写的游戏,像Adobe Acrobat和Mi ...

  8. ORACLE 导出(exp) & 导入(imp)

    导出(exp) & 导入(imp)     利用Export可将数据从数据库中提取出来,就是将select的结果存到一个FS二进制文件上    利用Import则可将提取出来的数据送回到Ora ...

  9. 对Linux(Unix)的基础知识归纳

    前言,不论是原生APP(Android&IOS),还是大型架构级基础环境(.NET&J2EE,或LAMP阵营等), 基本都不可避免的涉及到Linux(Unix),故还是觉得有必要把自己 ...

  10. spark 运行问题记录

    在CDH5.5.2上运行spark1.5的程序,运行起来就直接shutdown,并报出如下的异常:  INFO YarnClientSchedulerBackend: SchedulerBackend ...