Elegant fibonacci numbers again

2024-05-30 20:18
文章标签 numbers fibonacci elegant

本文主要是介绍Elegant fibonacci numbers again,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem Description

Fibonacci numbers are nice simple integers. All of you are familiar with it, aren’t you?
The Fibonacci sequence <F[n]> are defined by the recurrence:
F[0]=0;
F[1]=1;
F[n]=F[n-1]+F[n-2], for n>1
You know that the value of F[n] increases rapidly when the n becomes larger. So for each test case,output the value F[n] mod m will be ok.

Input

The first line of the input is a positive integer.It is the number of the test cases followed. Each test case contains two integers n (0<=n<2^32) and m (0<m<10000). There may be one or several spaces between the two integers.

Output

The output of the program should consist of one line of output for each test case.The output of each test case only contains one integer which equals to the value F[n] mod m. No any redundant spaces are needed.

Sample Input

2
1 1000
2 100

Sample Output

1
1
// 关键字:  斐波拉契数列的矩阵求法
//标程:
#include<stdio.h>
struct ss
{
	int a[2][2];
};
ss  fun(ss x,ss y,__int64 m)
{
ss  ret;
	ret.a[0][0]=(x.a[0][0]*y.a[0][0]%m+x.a[0][1]*y.a[1][0]%m)%m;
	ret.a[0][1]=(x.a[0][0]*y.a[0][1]%m+x.a[0][1]*y.a[1][1]%m)%m;
	ret.a[1][0]=(x.a[1][0]*y.a[0][0]%m+x.a[1][1]*y.a[1][0]%m)%m;
ret.a[1][1]=(x.a[1][0]*y.a[0][1]%m+x.a[1][1]*y.a[1][1]%m)%m;
	return ret;
}
ss f(ss ans,__int64 n,__int64 m)
{
	ss ret,temp;
	if(n==0) 
	{
		ret.a[0][0]=1,  ret.a[0][1]=0;
		ret.a[1][0]=0,  ret.a[1][1]=1;
		return ret;
	}
	if(n==1) return ans;
	temp=f(ans,n/2,m);
	ret=fun(temp,temp,m);
	if(n%2==1) ret=fun(ret,ans,m);
	return ret;
}
int main()
{
	//freopen("a.txt","r",stdin);
__int64 n,m;
	int t,i;
	scanf("%d",&t);
	while(t--)
	{
scanf("%I64d%I64d",&n,&m);
		if(n==0) { printf("0\n");  continue;}
ss  ans;
		ans.a[0][0]=1,  ans.a[0][1]=1;
		ans.a[1][0]=1,  ans.a[1][1]=0;
ss  s=f(ans,n-1,m);
		printf("%d\n",s.a[0][0]);
	}
	return 0;
}

这篇关于Elegant fibonacci numbers again的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1016816

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

js,给定一个数,如何求Fibonacci值

/*给定一个数,如何求Fibonacci值::F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2)*/ function fibonacci(n){        if(n<2){             return n;          }        return fibonacci(n-1)+fibonacci(n-2); } alert(fibonacci(6)

【练习7】Fibonacci数列

链接:https://www.nowcoder.com/practice/18ecd0ecf5ef4fe9ba3f17f8d00d2d66 分析: 当n为15的时候,可以用Math.min(c-n,n-b)来判断哪个是变成斐波那契数的最小步数。 public class Main {public static void main(String[] args) {Scanner i

【codeforces】55D. Beautiful numbers 数位DP

传送门:【codeforces】55D. Beautiful numbers 题目分析:被每一位整除则用二进制记录已经包括的数字的个数,以及对2520取模后的状态。由于对5整除当且仅当最后一个数为0或5,对2整除当且仅当最后一个数为偶数,且1~9的最小公倍数为2520,不包括2,5后的最小公倍数为252,所以除最后一层对2520取模,其余时候都对252取模即可。由于整除的状态有限,最多只有

【SGU】113. Nearly prime numbers 合数分解

传送门:【SGU】113. Nearly prime numbers 题目分析:O(sqrt(N))。。 代码如下: #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std ;#define rep( i , a , b ) for

USACO Section 3.1 Humble Numbers

题意: 已知一个集合S  由S的任意子集作为因子  可构造出一个数字  求  这些构造出的数字中第k大的数字是多少 思路: 拿到这题就被“数字不是很多而且比较连续暴力枚举就好”这个思路迷惑了  果断TLE… 跪了一次后想到通过bfs构造可取  这时用了queue维护bfs  用priority_queue维护答案(大顶堆  内部最多k个数字)  用set判重复(5*2=2*5)  写

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5 Example 2: Inpu