[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 ...
随机推荐
- mysql 存储过程 游标 判断游标是否为空
BEGIN DECLARE id long; DECLARE Done INT DEFAULT 0; DECLARE cashamount DECIMAL(10,2) DEFAULT 0.00; DE ...
- SecureCRT和SecureFx设置中文乱码
SecureCRT和SecureFx设置中文乱码 SecureCRT和SecureFx连接服务器时中文显示乱码,找了好多资料好久都没整出来,后来整出来了,因此把个人的解决办法提供出来,已变帮助更多的人 ...
- 日常维护sql
修复mysqlcheck -u -p --repair pmdb prefix="/export/data/mysql/bin/mysql -u -p -e" domain=机房 ...
- NSURLCache详解和使用
使用缓存的目的是为了使应用程序能更快速的响应用户输入,是程序高效的运行.有时候我们需要将远程web服务器获取的数据缓存起来,以空间换取时间,减少对同一个url多次请求,减轻服务器的压力,优化客户端网络 ...
- UITableView 使用
关键字 •UITableView •UITableViewDataSource •UITableViewDelegate •UITableViewCell •MVC 运行结果
- jqGrid的选中行事件
http://blog.csdn.net/u014381863/article/details/50375121
- mysql中的SUBSTRING_INDEX
SUBSTRING_INDEX(str,delim,count) Returns the substring from string str before count occurrences of t ...
- web页面全角&半角
根据Unicode编码,全角空格为12288,半角空格为32 : 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 全角-->半角函数 //半角转换 ...
- iOS9系统分享失败问题解决
因为iOS9系统需要设置打开QQ和微信的白名单,如果出现无法分享或者直接提示分享失败,试一下在infoPlist中添加以下白名单 http://wiki.mob.com/ios9-对sharesdk的 ...
- 接入百度语音SDK的步骤
1.导入依赖库 SystemConfiguration.framework AudioToolbox.framework UIkit.framework AVFoundation.framework ...