改良的斐波那契问题 [英] Modified fibonacci problem

查看:98
本文介绍了改良的斐波那契问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们在Fibonacci系列中所知,前两个之后的每个数字都是前两个数字的总和。



而不是添加两个前面的数字,乘以它们并打印结果模10 ^ 9 + 7.



因为这很容易,所以让它变得有点困难。假设有K个数字开头。



你必须找到第n个数字,其中第n个数字将是k个前面数字的乘积10 ^ 9 +7。



限制条件

1< = t< = 10



1< = n< = 10 ^ 6



1< = k< = 10



1< ; = k [i]< = 100



输入格式

第一行包含T个测试用例,



在每个测试用例中



第一行包含两个整数n,k由空格分隔



第二行包含以空格分隔的k个整数



输出

T行,每行包含修改后的斐波纳契数modulo 109 + 7





说明

示例1



输入



1



4 3



1 2 3



输出



6



解释



第4次修改的斐波那契数将为1 * 2 * 3 = 6



示例2



输入



1



10 3



1 2 3



输出



845114970



说明



第4,第5,第6修改后的斐波纳契数为6,36, 648分别



同样10号修改后的斐波纳契数字将为845114970





请写下代码



我尝试过:



i didn'尝试因为我无法理解代码

As we know in Fibonacci series every number after the first two is the sum of the two preceding ones.

Instead of adding two preceding numbers, multiply them and print the result modulo 10^9+7.

Since this is easy, let’'s make it bit difficult. Let'’s say there are K numbers to begin with.

You have to find nth number, where nth number will be product of k previous numbers modulo 10^9+7.

Constraints
1<=t<=10

1<=n<=10^6

1<=k<=10

1<=k[i]<=100

Input Format
First line contains T number of test case,

In each test case

First line contains two integers n, k delimited by space

Second line contains k integers delimited by space

Output
T lines, each line contains modified Fibonacci number modulo 109+7


Explanation
Example 1

Input

1

4 3

1 2 3

Output

6

Explanation

4th modified Fibonacci number will be 1*2*3=6

Example 2

Input

1

10 3

1 2 3

Output

845114970

Explanation

4th , 5th , 6th modified Fibonacci numbers are 6 , 36 , 648 respectively

Similarly 10th modified Fibonacci number will be 845114970


please write the code

What I have tried:

i didn't tried because i was unable to understand the code

推荐答案

引用:

请写代码

没有。

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!

No.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


引用:

请写代码



请支付工作费用。 :)


Please pay for the job. :)

引用:

我没试过,因为我无法理解代码

i didn't tried because i was unable to understand the code



然后你没有通过挑战。



我们不做你的HomeWork。

HomeWork未设置为测试你乞求别人做你的工作的技巧,它会让你思考并帮助你的老师检查你对你所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并进行必要的调整以适应你的实际问题。


Then you failed the challenge.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


#include<stdio.h>
#define Modulo 1000000007
long long int Num2Mod(long long int A,long long int B);
main()
{
	int K[10],n,k,i,j,t;
	long long int product,list[1000000];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		for(i=0;i<k;i++)
		{
			scanf("%d",&K[i]);
			list[i]=K[i];
		}
		for(i=0;i<n-2;i++)
		{
			product = 1;
			for(j=0;j<k;j++)
				product = Num2Mod(product,list[i+j]);
			list[i+k] = product;
		}
		printf("%lld",list[n-1]);
	}
}
long long int Num2Mod(long long int A,long long int B)
{
	return ((A%Modulo*B%Modulo)%Modulo);
}


这篇关于改良的斐波那契问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆