[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 ...
随机推荐
- JavaScript中的事件
1.冒泡事件:事件按照特定的的事件目标到最不特定的事件目标顺序触发(它是按照DOM的层次节后依次做出的反应) 2.捕获事件:事件从不确定的对象document 开始触发然后到最精确(也可以在窗口级别捕 ...
- mvc 项目下 webservice 程序无法运行
错误描述: 可以出现调用HelloWorld的界面 点击调用按钮报无法找到该资源 错误分析: 把webservice当成controller了. 解决: global中 添加 routes.Igno ...
- SQL Cumulative Sum累积求和
期望结果: ID VAL CumSum 1 10 10 2 20 30 3 30 60 方法一: 使用分析函数 select id,val,sum(val) over ( or ...
- ASP.NET MVC中使用DropDownList
在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...
- [OC笔记] protocol之我的见解
OC中的protocol就是和JAVA中interface差不多的东西,但是又不是完全一样的.这个protocol常用来实现委托,也就是自己不实现,当事件产生的时候去回调委托者. 让委托者去执行响应的 ...
- oc 单例
单例模式: //static id _instace; // //+ (id)allocWithZone:(struct _NSZone *)zone //{ // static dispatch_o ...
- QT5.2.1大BUG
本来以为5.2.1是release版本 谁知道编译某个程序,执行老是crash 换5.3.2就ok了. 坑啊
- odoo 10 生产自动领料
分析源码 当 原材料的 补货规则 的 "补货位置" location_id 是 生产单 的 原材料 "目标位置 ", 并且 原材料的 补货规则 的 " ...
- Entity Framework 6, database-first with Oracle
Entity Framework 6, database-first with Oracle 转载自http://csharp.today/entity-framework-6-database-fi ...
- 【CMD】日常总结
命令脚本可以提升工作效率,之前用过也写过一些脚本,但时间一长就忘记了.写篇随笔记录一下,随用随记哈. 调用程序 //切换到某个路径下 cd D:\Glodon\GDW\GDW\Release\Bin ...