标签:
打出前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
2 4 3 5 5
1 2
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(); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/47298991