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
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|.
Then for the following T lines, each has two positive integers n and m, whose meanings are described in the description.
4 3
5 5
2
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream; /**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author xyiyy@www.cnblogs.com/fraud
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task1009 solver = new Task1009();
solver.solve(1, in, out);
out.close();
} static class Task1009 {
Scanner in;
PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) {
this.in = in;
this.out = out;
run();
} void run() {
BigInteger dp[] = new BigInteger[2010];
BigInteger a[] = new BigInteger[2010];
dp[0] = BigInteger.ONE;
dp[1] = BigInteger.ONE;
dp[2] = BigInteger.ONE;
dp[3] = BigInteger.valueOf(3);
dp[4] = BigInteger.valueOf(5);
a[0] = BigInteger.ZERO;
a[1] = BigInteger.ZERO;
a[2] = BigInteger.ONE;
a[3] = BigInteger.ONE;
a[4] = BigInteger.valueOf(2);
for (int i = 5; i < 2010; i++) {
dp[i] = dp[i - 1].add(dp[i - 2]);
a[i] = a[i - 2].add(dp[i - 2]);
}
for (int i = 1; i < 2010; i++) {
dp[i] = dp[i].add(dp[i - 1]);
}
BigInteger m;
int t, n;
t = in.nextInt();
while (t != 0) {
t--;
n = in.nextInt();
m = in.nextBigInteger();
if (m.compareTo(BigInteger.ONE) == 0) {
out.println(1);
continue;
}
int i = 0;
for (i = 0; i < 2010; i++) {
if (dp[i].compareTo(m) >= 0) break;
}
i--;
out.println(a[i + 1].add(m.subtract(dp[i].add(BigInteger.ONE))).mod(BigInteger.valueOf(258280327)));
}
} } static class Scanner {
BufferedReader br;
StringTokenizer st; public Scanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
eat("");
} private void eat(String s) {
st = new StringTokenizer(s);
} public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
} public boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
} public String next() {
hasNext();
return st.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
} public BigInteger nextBigInteger() {
return new BigInteger(next());
} }
}
hdu5351 MZL's Border(规律题,java)的更多相关文章
- HDU 5351 MZL's Border (规律,大数)
[HDU 5351 MZL's Border]题意 定义字符串$f_1=b,f_2=a,f_i=f_{i-1}f_{i-2}$. 对$f_n$的长度为$m$的前缀$s$, 求最大的$k$满足$s[1] ...
- HDU 5351——MZL's Border——————【高精度+找规律】
MZL's Border Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 多校-HDU 5351 MZL's Border 数学规律
f[1] = 'b', f[2] = 'a', f[i] = f[i - 1] + f[i - 2] 斐波那契数列的字符串,给你n和m,前m位中,最长的前缀等于后缀的长度是多少.1≤n≤1000, 1 ...
- 2015 Multi-University Training Contest 5 1009 MZL's Border
MZL's Border Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5351 Mean: 给出一个类似斐波那契数列的字符串序列 ...
- ACM_送气球(规律题)
送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...
- hdoj--1005--Number Sequence(规律题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- LightOJ1010---Knights in Chessboard (规律题)
Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
随机推荐
- php DOMDocument 递归 格式化缩进HTML文档
function format(\DOMNode $node, $treeIndex = 0) { //不格式化的标签 if (in_array($node->nodeName, array(& ...
- 容器 list
(1) 插入操作,不能使用MyTestContain.begin()+3 之类?!(要对数据进行复制) list<int>::iterator pr=MyTestContain.begin ...
- Linux下关闭node应用
今天在折腾用node接入微信公众号时,碰到了node应用启动后卡死退出,需要找出该进程关闭的问题,由于对shell脚本不是很熟悉,记录如下: 我们在用npm start启动应用后,通常要关闭时,ctr ...
- VS2010使用Qt库
有参考文件可以看出,应该还是简单的. = =! Qt库的安装目录bin下,我把它添加到Path环境变量中也无用,真是搞不懂,按理来说windows搜索完当前目录会去Path指定的路径下搜索啊?为什么必 ...
- Linux cat和EOF的使用
在某些场合,可能我们需要在脚本中生成一个临时文件,然后把该文件作为最终文件放入目录中.(可参考ntop.spec文件)这样有几个好处,其中之一就是临时文件不是唯一的,可以通过变量赋值,也可根据不同的判 ...
- Linux企业级项目实践之网络爬虫(19)——epoll接口
由于要实现爬虫程序的快速抓取,显然如果采用阻塞型的I/O方式,那么系统可能很长时间都处在等待内核响应的状态中,这样爬虫程序将大大地降低效率.然而,如果采用非阻塞I/O,那么就要一直调用应用进程,反复对 ...
- Codeforces Round #322 (Div. 2) —— F. Zublicanes and Mumocrates
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- Java中BigDecimal的8种舍入模式是怎样的
Java中BigDecimal的8种舍入模式是怎样的?下面长沙欧柏泰克软件学院和大家一起来学习下吧: java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecima ...
- Http(get,post)及HttpClient(get,post)的简单使用
1. 使用 Http 的 Get 方式读取网络数据 import java.io.BufferedReader; import java.io.IOException; import java.io. ...
- 解决 在IE与firefox宽度不一致的问题
浏览器默认不同的字体问题,字体分为“等宽”和“不等宽”字体,所以 在IE与firefox内间距是不等的. 解决办法: body{font-family: 宋体, simsun; ...