zoj-4108 Fibonacci in the Pocket(斐波那契数列奇偶性规律+Java大数)

Fibonacci in the Pocket
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

题意:

给你斐波那契数列下标a和b,求奇偶。

分析:

我们发现斐波那契数列的1 1 2 3 5 8 13

原本数组:        奇奇偶  奇奇偶  奇奇偶  奇奇偶

前缀和奇偶性:    奇偶偶  奇偶偶  奇偶偶  奇偶偶

所以只需要模3判断一下,奇-奇 偶-偶为偶数,其他为奇数。

import java.math.*;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		
		BigInteger a,b;
		BigInteger one = new BigInteger("1");
		BigInteger two = new BigInteger("2");
		BigInteger zero = new BigInteger("0");
		BigInteger three= new BigInteger("3");
	   Scanner scanner=new Scanner(System.in);
	   int T=scanner.nextInt();
	   while((T--)!=0) {
		   a=scanner.nextBigInteger();
		   b=scanner.nextBigInteger();
		   a=a.subtract(new BigInteger("1"));
		   a=a.mod(three);
		   b=b.mod(three);
		   if((!a.equals(one)&&b.equals(one))||(a.equals(one)&&!b.equals(one)))
			   System.out.println("1");
		   else
			   System.out.println("0");
	   }
	}
}

猜你喜欢

转载自blog.csdn.net/w1304636468/article/details/89713098