HDOJ 5351 MZL's Border 找规律

来源:互联网 发布:无锡作协网络分会成立 编辑:程序博客网 时间:2024/06/06 18:35


打出前i个串的kmp的fail指针:

p: ab0 0 0 p: aba0 0 0 1 p: abaab0 0 0 1 1 2 p: abaababa0 0 0 1 1 2 3 2 3 p: abaababaabaab0 0 0 1 1 2 3 2 3 4 5 6 4 5 p: abaababaabaababaababa0 0 0 1 1 2 3 2 3 4 5 6 4 5 6 7 8 9 10 11 7 8 p: abaababaabaababaababaabaababaabaab0 0 0 1 1 2 3 2 3 4 5 6 4 5 6 7 8 9 10 11 7 8 9 10 11 12 13 14 15 16 17 18 19 12 13 p: abaababaabaababaababaabaababaabaababaababaabaababaababa0 0 0 1 1 2 3 2 3 4 5 6 4 5 6 7 8 9 10 11 7 8 9 10 11 12 13 14 15 16 17 18 19 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 20 21 p: abaababaabaababaababaabaababaabaababaababaabaababaababaabaababaabaababaababaabaababaabaab0 0 0 1 1 2 3 2 3 4 5 6 4 5 6 7 8 9 10 11 7 8 9 10 11 12 13 14 15 16 17 18 19 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 33 34 

然后就发现规律了.....



MZL's Border

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


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=fibi1fibi2, 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[ni+1:n], then s[1:i] is called as a Border of s. In Borders of s, the longest Border is called as sLBorder. 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
24 35 5
 

Sample Output
12
 

Source
2015 Multi-University Training Contest 5
 



import java.util.*;import java.math.*;public class Main {final BigInteger mod = BigInteger.valueOf(258280327);BigInteger fib[] = new BigInteger[1100];BigInteger sum[] = new BigInteger[1100];BigInteger all[] = new BigInteger[1100];void init() {fib[1]=BigInteger.ONE; fib[2]=BigInteger.ONE;sum[1]=BigInteger.ZERO; sum[2]=BigInteger.ONE;for(int i=3;i<=1010;i++) {fib[i]=fib[i-1].add(fib[i-2]);sum[i]=sum[i-1].add(fib[i]);}all[1]=BigInteger.valueOf(2);for(int i=2;i<=1010;i++) {all[i]=all[i-1].add(fib[i].multiply(BigInteger.valueOf(2)));}}void solve(BigInteger x) {if(x.compareTo(BigInteger.valueOf(2))<1) {System.out.println(0);return ;}for(int i=1;i<=1000;i++) {int k=x.compareTo(all[i]);if(k==1) continue;else if(k==0) {System.out.println(sum[i].mod(mod));return ;}else{BigInteger y = x.subtract(all[i-1]);if(y.compareTo(fib[i])>0) {y=y.subtract(fib[i]);}System.out.println(y.add(sum[i-1]).mod(mod));return ;}}}Main(){init();Scanner in = new Scanner(System.in);int T_T;T_T=in.nextInt();while(T_T-->0) {int n=in.nextInt();BigInteger m = in.nextBigInteger();solve(m);}}public static void main(String[] args) {new Main();}}


1 0
原创粉丝点击