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. 如何在cmd下切换不同版本的Python

    (1)分别安装python-2.7.12.amd64.msipython-3.5.2-amd64.exe(python官网下载的)顺序无所谓(为了看着方便,我把安装路径修改统一了)(2)配置环境变量D ...

  2. gdnz

    更新yum库:yum updat      yum install epel-release 查看是否安装mysql:rpm -qa|grep -i mysql 移除列表mysql :yum remo ...

  3. Maven Archetype

    1. 从project创建archetype 在项目根目录下,运行 mvn archetype:create-from-project 创建的archetype工程在app_folder/target ...

  4. jquery链接多个jquery方法

    <!DOCTYPE html><html><head><script src="/jquery/jquery-1.11.1.min.js" ...

  5. 1_UILabel

    // // ViewController.swift // 1_UILabel // // Created by Larry on 2016/12/7. // Copyright © 2016年 nf ...

  6. mysql中的游标使用案例

    DELIMITER $$ DROP PROCEDURE IF EXISTS `curTest`$$ CREATE PROCEDURE curTest(IN _myId INT) BEGIN DECLA ...

  7. 使用HttpClient 4.3.4 自动登录并抓取中国联通用户基本信息和账单数据,GET/POST/Cookie

    一.什么是HttpClient? HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 ...

  8. SpringMVC对异常进行全局处理,并区分对待ajax和普通请求

    异常信息应统一进行处理. 程序员开发过程中,应尽量少用try..catch.避免因为catch造成的业务歧义.而在web开发中,普通的页面提交动作,和ajax提交动作,处理方式不一样,因为跳转后直接显 ...

  9. AppScan 测试需要输入用户名密码的网站

    Cisco有专门的网页版的AppScan,使用前需要向有关的team申请account和权限. account和权限申请成功后,登录AppScan网站,创建自己的文件夹目录,然后在自己的目录下新建sc ...

  10. Android开发学习笔记:浅谈显示Intent和隐式Intent

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/655132 ...