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 ...
随机推荐
- django 自定用户系统 以及 Django Model 定义语法
http://www.tuicool.com/articles/jMzIr2 django使用自己的用户系统 http://www.jianshu.com/p/c10be59aad7a Django ...
- web标准(复习)--7 横向导航菜单
今天我们开始学习html列表,包含以下内容和知识点: 横向列表菜单 用图片美化的横向导航 css Sprites 一.横向列表菜单前边学习过纵向导航菜单,又学习了float属性,那么要实现横向导航菜单 ...
- AngularJS指令的作用域
参考文章:https://segmentfault.com/a/1190000002773689
- App轮播图
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- MySQL性能分析脚本
#!/usr/bin/python #!coding:utf-8 import mysql.connector as connector import json """ ...
- 继网易博客后搜狐博客也增加了nofollow标签
继网易博客后搜狐博客也增加了nofollow标签 今天在搜狐博客发表了篇文章,在末端添加上我的版权,结果回头查看是发现,这个锚文本被加上了nofollow标签,也就是说这样的外链已经没有传递权重的作用 ...
- library cache: mutex X
我们先来看看 library cache: mutex X . 是个什么东西 The library cache mutex is acquired for similar purposes that ...
- 【转】FAE及其发展前景
原文网址:http://blog.sina.com.cn/s/blog_6e80c27b0100okd9.html FAE Field Application Engineer(现场应用工程师) ,其 ...
- 用Update Select批量更新某一字段的值[可以跨库]
SQL:UPDATE test1 SET name = (SELECT y.name FROM DB2.dbo.test2 y WHERE test1.id = y.id)
- Java基础 -- 冒泡排序算法(带详细注释)
冒泡排序的要点: 1.多轮排序,每轮排序中选出最大的元素放在最顶端,并且下次排序不再使用该元素; 2. 使用双for循环,外层for循环控制要排序的次数(轮数), 内层for循环控制当前要排序的元素并 ...