CSU 1402 Fibonacci Multiply

本文介绍了一个计算两个斐波那契数乘积并对1000000007取模的问题。通过预先计算并存储斐波那契数列,实现了快速查询与运算。该算法在中南大学程序设计竞赛中被提出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1402: Fibonacci Multiply

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 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 = F- 1 + F- 2
with F0 = 0, F1 = 1.
Given two positive integers xy, 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 xy (1 <= xy <= 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
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值