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. Python导入模块出现“no module named **”可能是这样的原因

    因为Python中其他文件中海油和这些模块一样的取名,这个时候Python就找不到到底是哪个文件了,所以只需要将其中一个文件的名字修改成不一样的就可以了.

  2. 初学c# -- 学习笔记(五) winfrom无边框四周阴影

    刚用到这个功能,网上扯淡的东西太多了,都是2边阴影,还什么窗口叠加.ps作图啥的,什么玩意.还是老外实在,google找的,无边框窗体,四边透明阴影. public partial class For ...

  3. Ubuntu如何更新源

    Ubuntu的源其实就是更新各种软件包需要用到镜像网站, 当大家在虚拟机上安装Linux镜像的时候肯定会遇到各种Linux软件没有安装,当你用apt-get安装的时候它会提示无效的网址,这个时候你就需 ...

  4. 在iis中设置文件下载而不是在浏览器上打开

    点击网页链接的*.txt,*.jpg,*.xml等文件时会在浏览器上直接显示,并没有像*.doc那样弹出下载提示框. 解决方法: 在部署的网站上,选择存放文件的目录,选择 HTTP响应标头 ,添加一个 ...

  5. AndroidStudio安装教程(Windows环境下)

    AndroidStudio官网下载:http://android-studio.org/    可以更具自己喜欢的版本下载,个人推荐2.2版本以上,因为开发和运行效率快,高很多. Android St ...

  6. Struts框架——(二)Struts原理with登录实例

    二. Struts基本工作流程 假设现在有以下情景: 用户正在浏览一个用STRUTS的技术构建的网站主页,主页上有个登陆表单,用户填好登陆名和密码,单击"登陆"按钮,就激活了以下一 ...

  7. Android 获取当前时间问题1

    获取的写法如下: Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int year = c.get(Calendar.YEAR); int mon ...

  8. SQL Server 2008 修改表所有者,架构

    ALTER SCHEMA 新架构 TRANSFER 旧架构.对象名称

  9. 类的序列化和反序列化(ObjectOutputStream和ObjectInputStream)

    1.需要序列化的类 import java.io.Serializable; /** * 必须继承 Serializable 接口才能实现序列化 */ public class Employee im ...

  10. 苗子sale record

    2016年7月11日 橘白/阳光 发广州 220  中通 719373064939 成功2016年7月11日 横纹RA    发潮州 250  中通 719373064940 成功2016年7月18日 ...