hdu5351 MZL's Border(规律题,java)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
MZL's Border
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 905 Accepted Submission(s): 295
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<n. If s[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|.
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<n. If s[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.
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
1 import java.io.OutputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.PrintWriter; 5 import java.util.StringTokenizer; 6 import java.math.BigInteger; 7 import java.io.IOException; 8 import java.io.BufferedReader; 9 import java.io.InputStreamReader; 10 import java.io.InputStream; 11 12 /** 13 * Built using CHelper plug-in 14 * Actual solution is at the top 15 * 16 * @author xyiyy@www.cnblogs.com/fraud 17 */ 18 public class Main { 19 public static void main(String[] args) { 20 InputStream inputStream = System.in; 21 OutputStream outputStream = System.out; 22 Scanner in = new Scanner(inputStream); 23 PrintWriter out = new PrintWriter(outputStream); 24 Task1009 solver = new Task1009(); 25 solver.solve(1, in, out); 26 out.close(); 27 } 28 29 static class Task1009 { 30 Scanner in; 31 PrintWriter out; 32 33 public void solve(int testNumber, Scanner in, PrintWriter out) { 34 this.in = in; 35 this.out = out; 36 run(); 37 } 38 39 void run() { 40 BigInteger dp[] = new BigInteger[2010]; 41 BigInteger a[] = new BigInteger[2010]; 42 dp[0] = BigInteger.ONE; 43 dp[1] = BigInteger.ONE; 44 dp[2] = BigInteger.ONE; 45 dp[3] = BigInteger.valueOf(3); 46 dp[4] = BigInteger.valueOf(5); 47 a[0] = BigInteger.ZERO; 48 a[1] = BigInteger.ZERO; 49 a[2] = BigInteger.ONE; 50 a[3] = BigInteger.ONE; 51 a[4] = BigInteger.valueOf(2); 52 for (int i = 5; i < 2010; i++) { 53 dp[i] = dp[i - 1].add(dp[i - 2]); 54 a[i] = a[i - 2].add(dp[i - 2]); 55 } 56 for (int i = 1; i < 2010; i++) { 57 dp[i] = dp[i].add(dp[i - 1]); 58 } 59 BigInteger m; 60 int t, n; 61 t = in.nextInt(); 62 while (t != 0) { 63 t--; 64 n = in.nextInt(); 65 m = in.nextBigInteger(); 66 if (m.compareTo(BigInteger.ONE) == 0) { 67 out.println(1); 68 continue; 69 } 70 int i = 0; 71 for (i = 0; i < 2010; i++) { 72 if (dp[i].compareTo(m) >= 0) break; 73 } 74 i--; 75 out.println(a[i + 1].add(m.subtract(dp[i].add(BigInteger.ONE))).mod(BigInteger.valueOf(258280327))); 76 } 77 } 78 79 } 80 81 static class Scanner { 82 BufferedReader br; 83 StringTokenizer st; 84 85 public Scanner(InputStream in) { 86 br = new BufferedReader(new InputStreamReader(in)); 87 eat(""); 88 } 89 90 private void eat(String s) { 91 st = new StringTokenizer(s); 92 } 93 94 public String nextLine() { 95 try { 96 return br.readLine(); 97 } catch (IOException e) { 98 return null; 99 } 100 } 101 102 public boolean hasNext() { 103 while (!st.hasMoreTokens()) { 104 String s = nextLine(); 105 if (s == null) 106 return false; 107 eat(s); 108 } 109 return true; 110 } 111 112 public String next() { 113 hasNext(); 114 return st.nextToken(); 115 } 116 117 public int nextInt() { 118 return Integer.parseInt(next()); 119 } 120 121 public BigInteger nextBigInteger() { 122 return new BigInteger(next()); 123 } 124 125 } 126 }
分类:
HDU
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
· golang自带的死锁检测并非银弹
· 一个适用于 .NET 的开源整洁架构项目模板
· API 风格选对了,文档写好了,项目就成功了一半!
· 【开源】C#上位机必备高效数据转换助手
· .NET 9.0 使用 Vulkan API 编写跨平台图形应用
· .NET 依赖注入中的 Captive Dependency