标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 671 Accepted Submission(s): 209
/** 题意:给一个A串,一个B串,然后str[i] = str[i-1]+str[i-2]; 求解对于第n个串的第前m位最长的LBorder,LBorder 是指s[1:i]=s[n−i+1:n], 做法:画几个串可以找到规律,比如第九个串 a b a a b a b a a b a a b a b a a b a b a a b a a b a b a a b a [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] Java **/ import java.util.*; import java.math.*; public class Main { public static void main(String args[]) { BigInteger AA[] = new BigInteger [1000+5]; BigInteger A[] = new BigInteger [1000+5]; A[1] = BigInteger.ONE; A[2] = BigInteger.valueOf(2); BigInteger MOD = BigInteger.valueOf(258280327); BigInteger mm = BigInteger.valueOf(1); for (int i = 3; i <= 1001; i++) { A[i] = A[i-1].add(A[i-2]); } for(int i=2;i<=1001;i++) { A[i] = A[i].add(A[i-1]); } AA[1] = BigInteger.ZERO; AA[2] = BigInteger.ZERO; for(int i=3;i<=1001;i++) { AA[i] = AA[i-1].add(AA[i-2]).add(mm); } Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); BigInteger m = in.nextBigInteger(); if (m.equals(BigInteger.valueOf(1)) || m.equals(BigInteger.valueOf(2))) { System.out.println(0); continue; } boolean ok = false; int tt = 0; for (int i = 1; i <= 1001; i++) { int yy = A[i].compareTo(m); if(yy == 1) break; tt = i; } if(A[tt].compareTo(m) == 0) { BigInteger temp = BigInteger.ZERO; BigInteger res = A[tt].subtract(A[tt-1]); BigInteger tmp = AA[tt].add(res) ; tmp = tmp.subtract(mm); System.out.println(tmp.mod(MOD)); } else { BigInteger temp = BigInteger.ZERO; BigInteger res = m.subtract(A[tt]); BigInteger tmp = AA[tt+1].add(res); tmp = tmp.subtract(mm); System.out.println(tmp.mod(MOD)); } } } }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4704088.html