题意:
给出一个类似斐波那契数列的字符串序列。
f[1] = b
f[2] = a
f[3] = f[2]+f[1] = ab
f[4] = aba
f[5] = abaab
要你求给出的f[n]字符串中截取前m位的字符串s中
s[1…i] = s[s.size()-i+1….s.size()]的最大长度。
…
给出n = 5, m = 5;
则f[5] = abaab.截取前m位是abaab.
所以最长的就是s[0-1] = s[3-4] = ab 答案= 2;
如果 n = 5, m = 4;
f[5] = abaab,截取前4位 就是abaa
那么最长应该是s[0] = s[3] = a.答案 = 1.暴力打表找规律
解析:
找到第一个使m+1<F[i]的i值,答案就是m−F[i−2](F为斐波那契数列)
my code
import java.math.*;
import java.util.*;
public class Main {
static final int MAXN = 1005;
static BigInteger MOD = BigInteger.valueOf(258280327);
static BigInteger[] fib = new BigInteger[MAXN];
static Scanner cin = new Scanner(System.in);
/**
* 初始化fib数列
*/
static void init() {
fib[0] = BigInteger.ZERO;
fib[1] = BigInteger.ONE;
fib[2] = BigInteger.ONE;
for(int i = 3; i < MAXN; i++) {
fib[i] = fib[i-1].add(fib[i-2]);
}
}
public static void main(String[] args) {
int n;
BigInteger m;
init();
int T;
T = cin.nextInt();
while(T-- > 0) {
n = cin.nextInt();
m = cin.nextBigInteger();
int pos = -1;
for(int i = 1; i < MAXN; i++) {
if(fib[i].compareTo(m.add(BigInteger.ONE)) > 0) {
pos = i;
break;
}
}
BigInteger ans = (m.subtract(fib[pos-2])).mod(MOD);
System.out.println(ans);
}
}
}