Fibonacci in the Pocket

本文介绍了一种高效判断斐波那契数列中任意项奇偶性的算法,通过分析输入数据的范围和特性,找到快速解决方法。文章通过一个具体的编程实例,展示了如何利用数学规律优化代码,提高算法效率。

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

Time Limit: 1 Second Memory Limit: 65536 KB

DreamGrid has just found a Fibonacci sequence and two integers and in his right pocket, where indicates the -th element in the Fibonacci sequence.

Please tell DreamGrid if is even or is odd.

Recall that a Fibonacci sequence is an infinite sequence which satisfies , and for all .

Input

There are multiple test cases. The first line of the input contains an integer (about 100), indicating the number of test cases. For each test case:

The first and only line contains two integers and (). Their meanings are described above.

Output

For each test case output one line. If is even output “0” (without quotes); If is odd output “1” (without quotes).

Sample Input
6
1 2
1 3
1 4
1 5
123456 12345678987654321
123 20190427201904272019042720190427

Sample Output
0
0
1
0
0
1

Hint

The first few elements of the Fibonacci sequence are: , , , , , …
代码:

#include<iostream>
#include<math.h>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main() {
    int T;
    cin >>T;
    int fa=0, kb=1,que[7]={0,1,1,0,1,1,0},i;
    while(T--)
    {
        string   a ,b;
        cin >>a>>b;
        int lena = a.length();
        int lenb = b.length();
        int last =0,k=0,s=0;
        for(int i=0;i<lena;++i)
        {
            s = a[i]-'0';
            k = last*10+s;
            last = k%3;
        }
        s=0,k = 0;
        int last1=0;
        for(int i=0;i<lenb;++i)
        {
            s = b[i]-'0';
            k = last1*10+s;
            last1 = k%3;
        }
        int sum=0;
        if(last1<last)
            last1+=3;
        for(i=last;i<=last1;i++){
            sum+=que[i];
        }
        cout<<sum%2<<endl;
    }
}

错误原因:
1.没注意输入数据的范围,导致w掉了
2.这种类型的题目是属于找规律,很多题目都是一样的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值