HDOJ 5351 MZL's Border의 법칙 찾기

4629 단어
이전 i 문자열의 kmp에 대한 fail 포인터를 입력하십시오:
p: ab
0 0 0 
p: aba
0 0 0 1 
p: abaab
0 0 0 1 1 2 
p: abaababa
0 0 0 1 1 2 3 2 3 
p: abaababaabaab
0 0 0 1 1 2 3 2 3 4 5 6 4 5 
p: abaababaabaababaababa
0 0 0 1 1 2 3 2 3 4 5 6 4 5 6 7 8 9 10 11 7 8 
p: abaababaabaababaababaabaababaabaab
0 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: abaababaabaababaababaabaababaabaababaababaabaababaababa
0 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: abaababaabaababaababaabaababaabaababaababaabaababaababaabaababaabaababaababaabaababaabaab
0 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=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 
is[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 
1≤T≤100, 1≤n≤103, 1≤m≤|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

 
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();
	}
}

좋은 웹페이지 즐겨찾기