MZL's Border
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1102 Accepted Submission(s): 353
Problem Description
As is known to all, MZL is an extraordinarily lovely girl. One day, MZL was playing with her favorite data structure, strings.
MZL is really like Fibonacci Sequence
, so she defines
Fibonacci Strings
in the similar way. The definition of
Fibonacci Strings
is given below.
1) fib
1
=b
2) fib
2
=a
3) fib
i
=fib
i−1
fib
i−2
, i>2
For instance, fib
3
=ab, fib
4
=aba, fib
5
=abaab
.
Assume that a string s
whose length is
n
is
s
1
s
2
s
3
...s
n![]()
. Then
s
i
s
i+1
s
i+2
s
i+3
...s
j![]()
i s called as a substring of
s
, which is written as
s[i:j]
.
Assume that i<n
. If
s[1:i]=s[n−i+1:n]
, then
s[1:i]
is called as a
Border
of
s
. In
Borders
of
s
, the longest
Border
is called as
s
'
LBorder
. Moreover,
s[1:i]
's
LBorder
is called as
LBorder
i![]()
.
Now you are given 2 numbers n
and
m
. MZL wonders what
LBorder
m![]()
of
fib
n![]()
is. For the number can be very big, you should just output the number modulo
258280327(=2×3
17
+1)
.
Note that 1≤T≤100, 1≤n≤10
3
, 1≤m≤|fib
n
|
.
MZL is really like Fibonacci Sequence
1) fib
2) fib
3) fib
For instance, fib
Assume that a string s
Assume that i<n
Now you are given 2 numbers n
Note that 1≤T≤100, 1≤n≤10
Input
The first line of the input is a number
T
, which means the number of test cases.
Then for the following T
lines, each has two positive integers
n
and
m
, whose meanings are described in the description.
Then for the following T
Output
The output consists of
T
lines. Each has one number, meaning
fib
n![]()
's
LBorder
m![]()
modulo
258280327(=2×3
17
+1)
.
Sample Input
2 4 3 5 5
Sample Output
1 2
Author
SXYZ
Source
Recommend
import java.io.*;
import java.util.*;
import java.math.*;
public class Main{
static BigInteger f[] = new BigInteger[2000];
static BigInteger F = new BigInteger("258280327");
public static void main(String[] arg){
Scanner cin = new Scanner(System.in);
f[2] = f[1]= new BigInteger("1");
for(int i=3;i<=1000;i++) f[i]=f[i-1].add(f[i-2]);
int T=cin.nextInt();
while ((T--)>0) {
int n=cin.nextInt();
BigInteger m = cin.nextBigInteger();
m=m.add(BigInteger.ONE);
int i=1;
while (m.compareTo(f[i])>=0) ++i;
System.out.println((m.subtract(BigInteger.ONE).subtract(f[i-2])).mod(F));
}
}
}