1402: Fibonacci Multiply
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 277 Solved: 58
[ Submit][ Status][ Web Board]
Description
In mathematics, the Fibonacci number are the number in the following integer sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34….
The nth Fibonacci number Fn, is defined by the recurrence relation
Fn = Fn - 1 + Fn - 2
with F0 = 0, F1 = 1.
Given two positive integers x, y, calculate (Fx * Fy) % 1000000007.
Input
The first line contains the number of test cases T (1 <= T <= 50).
For each test case, there is only one line with two integers x, y (1 <= x, y <= 106).
Output
For each test case, output (Fx * Fy) % 1000000007.
Sample Input
3
3 4
7 1
8 8
Sample Output
6
13
441
HINT
“%” denotes the modulo operation. a % n is the remainder of the division of a by n. For example 5 % 3 = 2, 3 % 5 = 3.
These formulas may help you:
(1) (a + b) % d = (a % d + b %d) % d
(2) (a * b) % d = ((a % d) * (b %d)) % d
Source
以前一直以为这个是一道快速幂的题目。
今天做了发现。原来是水题呀。
#include <stdio.h>
#define MOD 1000000007
#define N 1000005
long long dp[N];
void gettable()
{
dp[0]=0,dp[1]=1;
for(int i=2;i<=1000000;i++)
dp[i]=(dp[i-1]%MOD+dp[i-2]%MOD)%MOD;
}
int main()
{
int t;
scanf("%d",&t);
gettable();
while(t--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",(dp[x]%MOD)*(dp[y]%MOD)%MOD);
}
return 0;
}
/**************************************************************
Problem: 1402
User: 0905130123
Language: C
Result: Accepted
Time:60 ms
Memory:8776 kb
****************************************************************/