HDOJ 5351 MZL's Border의 법칙 찾기
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
i
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();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.