[UCSD白板题] Huge Fibonacci Number modulo m
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的更多相关文章
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- [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_ ...
- [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 ...
- 【LeetCode每天一题】Fibonacci Number(斐波那契数列)
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
随机推荐
- Python:XXX missing X required positional argument: 'self'
代码的一个小小Bug有时候会让人焦头烂额,费了很大劲搞明白的问题,如果不记录下来,是很容易遗忘的! 定义一个类,如果按照以下的方式使用,则会出现TypeError: testFunc() missin ...
- 在xaf 14 中实现 Tonyyang原文中的action权限
具体实现过程,主要修改了以下几个地方: 第一:角色和用户类中需要修改成SecurityStrategy的方式: 具体代码 MySecurityRole: using System; using Sys ...
- java多线程详解(1)-多线程入门
一.多线程的概念 线程概念 线程就是程序中单独顺序的流控制. 线程本身不能运行,它只能用于程序中. 说明:线程是程序内的顺序控制流,只能使用分配给程序的资源和环境. 进程:操作系统中执行的程序 程序是 ...
- nodejs npm常用命令
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install m ...
- Nginx密码验证 ngx_http_auth_basic_module模块
有时候我们需要限制某些目录只允许指定的用户才可以访问,我们可以给指定的目录添加一个用户限制. nginx给我们提供了ngx_http_auth_basic_module模块来实现这个功能. 模块ngx ...
- 在Sqlserver下巧用行列转换日期的数据统计
在Sqlserver下巧用行列转换日期的数据统计 前言 在SQLSERVER 中有很多统计函数的基础语法,有使用Group By 或 partition by 后配合Sum,Count(*) 等用法. ...
- 关于Linkedlist 和 ArrayDeque的讨论
转自:http://blog.csdn.net/f2006116/article/details/51375470 其中:我们要讨论的LinkedList和ArrayDeque都是实现Deque接口, ...
- windows qt 编译 boost 添加 路径
win32-msvc2015: { contains(QMAKE_HOST.arch, x86):{ INCLUDEPATH += D:\3SDK\boost_1_61_0 LIBS += -LD:\ ...
- jetbrains产品激活方式(WebStorm,Pycharm有效)
注册时,在打开的License Activation窗口中选择"activation code",在输入框输入下面的注册码 43B4A73YYJ-eyJsaWNlbnNlSWQiO ...
- aa12
option = { backgroundColor: '#1b1b1b', color: ['gold','aqua','lime'], title : { text: '模拟迁徙', subtex ...