Fibonacci in the Pocket(ZOJ-4108)
Problem Description
DreamGrid has just found a Fibonacci sequence f1,f2,... and two integers a and b in his right pocket, where fk indicates the k-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 f1=1, f2=1 and fi=fi-1+fi-2 for i>=3 all .
Input
There are multiple test cases. The first line of the input contains an integer T (about 100), indicating the number of test cases. For each test case:
The first and only line contains two integers a and b (1<=a<=b<10^10000). 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 20190427201904272019042720190427Sample Output
0
0
1
0
0
1
题意:t 组数据,每组给出两个数 a、b,判断斐波那契数列的 a 项到第 b 项的和的奇偶,若和为奇数输出 1,若和为偶数输出 0
思路:
首先根据题目所给的数据范围可以判断是一个高精度题,故选用 Java 来做
根据斐波那契数列前几项:1,1,2,3,5,8,13,21,34,55,89,144...
可以得到关于斐波那契数列每一项的奇偶特性:从第一项开始,每三个一组,每组奇偶性为 奇、奇、偶
那么,对与斐波那契数列的第 n 项 x 有:
- x%3=1,组内第一个位置,奇数
- x%3=2,组内第二个位置,奇数
- x%3=0,组内第三个位置,偶数
然后考虑 a、b 是否在同一组内,从而进行分类讨论
若 a、b 在同一组内,首先将 a、b 模 3,然后比较值:
- 当 a、b 位于同一位置时:
- a%3=1,奇数
- a%3=2,奇数
- a%3=0,偶数
- 当 a、b 不位于同一位置时:
- a%3=1,b%3=2,偶数
- a%3=1,b%3=0,偶数
- a%3=2,b%3=0,奇数
若 a、b 不在同一组内,那么由于每组的和固定是一个偶数,只需考虑 a、b 在其分属组中的位置:
- a%3=1,b%3=1:奇数
- a%3=2,b%3=1:偶数
- a%3=0,b%3=1:偶数
- a%3=1,b%3=2:偶数
- a%3=2,b%3=2:奇数
- a%3=0,b%3=2:奇数
- a%3=1,b%3=0:奇数
- a%3=2,b%3=0:偶数
- a%3=0,b%3=0:偶数
学长指出一种前缀和做法,斐波那契数列前缀和奇偶性为 奇奇偶 奇奇偶 奇奇偶 奇奇偶,所以只需要模 3 判断即可。。。
Source Program
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int t;
t=input.nextInt();
while((t--)>0) {
BigInteger a,b;
BigInteger zero=new BigInteger("0");
BigInteger one=new BigInteger("1");
BigInteger two=new BigInteger("2");
BigInteger three=new BigInteger("3");
a=input.nextBigInteger();
b=input.nextBigInteger();
if( (b.divide(three)).compareTo(a.divide(three))==0 ) {//a、b在一个区间
a=a.mod(three);
b=b.mod(three);
if(a.compareTo(b)==0) {//a、b在一个位置
if(a.compareTo(one)==0||a.compareTo(two)==0)//取模后为1、2
System.out.println(1);
else if(a.compareTo(zero)==0)//取模后为0
System.out.println(0);
}
else {
if((a.compareTo(one)==0)&&(b.compareTo(two)==0))//取模后为1、2
System.out.println(0);
else if((a.compareTo(one)==0)&&(b.compareTo(zero)==0))//取模后为1、0
System.out.println(0);
else if((a.compareTo(two)==0)&&(b.compareTo(zero)==0))//取模后为2、0
System.out.println(1);
}
}
else {
a=a.mod(three);
b=b.mod(three);
if(a.compareTo(one)==0&&b.compareTo(one)==0)
System.out.println(1);
else if(a.compareTo(one)==0&&b.compareTo(two)==0)
System.out.println(0);
else if(a.compareTo(one)==0&&b.compareTo(zero)==0)
System.out.println(0);
else if(a.compareTo(two)==0&&b.compareTo(one)==0)
System.out.println(0);
else if(a.compareTo(two)==0&&b.compareTo(two)==0)
System.out.println(1);
else if(a.compareTo(two)==0&&b.compareTo(zero)==0)
System.out.println(1);
else if(a.compareTo(zero)==0&&b.compareTo(one)==0)
System.out.println(1);
else if(a.compareTo(zero)==0&&b.compareTo(two)==0)
System.out.println(0);
else if(a.compareTo(zero)==0&&b.compareTo(zero)==0)
System.out.println(0);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)