hdu 5351 MZL's Border 打表+高精度
2015-08-04 20:35
399 查看
MZL's Border
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 462 Accepted Submission(s): 127
[align=left]Problem Description[/align]
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|.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The output consists of T lines. Each has one number, meaning fibn's LBorderm modulo 258280327(=2×317+1).
[align=left]Sample Input[/align]
2
4 3
5 5
[align=left]Sample Output[/align]
1
2
题意:
给出一个类似斐波那契数列的字符串序列。
f[1] = b
f[2] = a
f[3] = f[2]+f[1] = ab
f[4] = aba
f[5] = abaab
要你求给出的f
字符串中截取前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.
暴力打表找一下规律,然后用大数做
总结一下常用的BigInteger的用法:
这个类在java.math中.
构造方法:BigInteger b = new BigInteger(""); //用字符串构造.
常量:BigInteger.ONE; BigInteger.ZERO; BigInteger.TEN;
加法:add 减法:subtract 乘法:multiply 除法:divide 取余:remainder
比较大小 compareTo 返回0 表示相等,返回1表示比后者大,返回-1表示比后者小。
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger[] fib1 = new BigInteger[1010]; static BigInteger[] fib2 = new BigInteger[1010]; static BigInteger[] fib3 = new BigInteger[1010]; public static void main(String []args) { fib1[1] = new BigInteger("2"); fib1[2] = new BigInteger("2"); fib2[1] = BigInteger.ONE; fib2[2] = BigInteger.ONE; fib3[1] = BigInteger.ZERO; fib3[2] = BigInteger.ONE; for(int i = 3; i <= 1005; i++) { fib1[i] = fib1[i-1].add(fib1[i-2]); fib2[i] = fib2[i-1].add(fib2[i-2]); } for(int i = 3; i <= 1005; i++) { fib3[i] = fib3[i-1].add(fib2[i-1]); } Scanner in = new Scanner(System.in); int T = in.nextInt(), maxN = 0; for(int cast = 1; cast <= T; cast++) { int n; BigInteger m; n = in.nextInt(); m = in.nextBigInteger(); int i; for(i = 1; i <= 1005; i++) { if(m.compareTo(fib1[i]) == 1) { m = m.subtract(fib1[i]); } else break; } if(m.compareTo(fib1[i].divide(new BigInteger("2"))) == 1) { m = m.subtract(fib1[i].divide(new BigInteger("2"))); BigInteger ans = m.add(fib3[i]); ans = ans.subtract(BigInteger.ONE); ans = ans.remainder(new BigInteger("258280327")); System.out.println(ans.toString()); } else { BigInteger ans = m.add(fib3[i]); ans = ans.subtract(BigInteger.ONE); ans = ans.remainder(new BigInteger("258280327")); System.out.println(ans.toString()); } } } }
相关文章推荐
- 【effective c++读书笔记】【第5章】实现(1)
- 项目分析(2)
- 手势识别器(UIImageView)
- HDU - 4360As long as Binbin loves Sangsang最短路问题多状态记录
- uva 101
- zoj题目分类详细的
- thread safe lazy initialization singleton
- 啊哈,算法! -----dfs1
- Spring--Spring容器
- [MetaHook] R_SparkShower
- [C++学习笔记]--成员对象和封闭类
- Mysql在大型网站的应用架构演变
- Haffman integration by java(Algorithm)
- 在eclipse下操作HDFS
- Redis的安装(CentOS 7下)
- SVN 版本控制
- HDU 4343 多查询求区间内的最大不相交区间个数-思维&贪心-卡时间&二分&剪枝
- Java套接字----------------
- springMVC中Controller的返回值处理和参数绑定
- python下selenium测试报告整合