hdu5351 MZL\'s Border(java写大数,斐波那契数列)

MZL's Border

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 657    Accepted Submission(s): 203


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)  fib1=b
  
  2)  fib2=a
  
  3)  fibi=fibi?1fibi?2, i>2
  
For instance,  fib3=ab, fib4=aba, fib5=abaab .

Assume that a string  s  whose length is  n  is  s1s2s3...sn . Then  sisi+1si+2si+3...sj  is 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  LBorderi .

Now you are given 2 numbers  n  and  m . MZL wonders what  LBorderm  of  fibn  is. For the number can be very big, you should just output the number modulo  258280327(=2×317+1) .

Note that  1T100, 1n103, 1m|fibn| .
 

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.
 

Output
The output consists of  T  lines. Each has one number, meaning  fibn 's  LBorderm  modulo  258280327(=2×317+1) .
 

Sample Input
    
    
2 4 3 5 5
 

Sample Output
    
    
1 2
 

import java.io.*;
import java.math.*;
import java.util.*;
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger[] f = new BigInteger[3004];
f[0]=BigInteger.ONE;
f[1]=BigInteger.ONE;
int i;
for(i=2;i<3003;i++){
f[i]=f[i-1].add(f[i-2]);
}
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for(;T>=1;T--){
int n;
BigInteger m;
n=cin.nextInt();
m=cin.nextBigInteger();
BigInteger ans;
BigInteger mod = BigInteger.valueOf(258280327);
for(i=0;i<=n;i++){
if(m.compareTo(f[i].subtract(BigInteger.ONE))==-1) break;
}
ans=m.subtract(f[i-2]);
ans=ans.mod(mod);
System.out.println(ans);
}

}

}



猜你喜欢

转载自blog.csdn.net/nature_ran/article/details/79125326