转载请注明出处: 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|.

 



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.

 



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
 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)的更多相关文章

  1. 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] ...

  2. HDU 5351——MZL's Border——————【高精度+找规律】

    MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. 多校-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 ...

  4. 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: 给出一个类似斐波那契数列的字符串序列 ...

  5. ACM_送气球(规律题)

    送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...

  6. hdoj--1005--Number Sequence(规律题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 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 ...

  8. 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 + ...

  9. 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 ...

随机推荐

  1. Oracle字符串分割函数

    今天在创建视图的时候,碰到一个问题,问题如下: 将字符格式为“XXX,YYY”分割出来,并且分割后作为两个字段放入视图中. 考虑使用字符分割函数,但是查找资料Oracle没有字符分割的函数(我对Ora ...

  2. 乱七八糟 Nodejs 系列一:试水

    看Nodejs比较舒服的人,个人认为主要是: 以前是后端,转成前端的人: 前端巨牛的人: 后端巨牛的人: 巨牛的人... 当然还有我这种,脑抽不止的人~~ 不过学习的过程中发现,如果不是上来就用exp ...

  3. Gradle sync failed: failed to find Build Tools revision 21.1.2

    从github上下载了一个开源项目到Android Studio 出现以下问题: 下午2:56:05 Gradle sync started下午3:00:11 Gradle sync failed: ...

  4. unexpected problem

    一个比较有趣的字符串问题,问题描述如下 大体意思就是给定一个字符串s以及一个整数m,找出一个能满足以上三个条件的字符串t的个数对10e9 + 7 取余输出. 第二三条是关键,t.s = s.t 举个例 ...

  5. iPhone开发 数据持久化总结(终结篇)—5种数据持久化方法对比

    iPhone开发 数据持久化总结(终结篇)—5种数据持久化方法对比   iphoneiPhoneIPhoneIPHONEIphone数据持久化 对比总结 本篇对IOS中常用的5种数据持久化方法进行简单 ...

  6. windows设备驱动安装接口(自己仿写)

    /***************************************** Author:foo_hack This is File named:Setup.h The Funtion Im ...

  7. 一位搬家师傅的O2O之旅

    一位搬家师傅的O2O之旅 By 诸神之黄昏 | 2014/08/14 [核心提示] 一位普通的搬家师傅,无意中被卷入如火如荼的 O2O 浪潮,起初,互联网让他尝到了甜头,后来则是更多的困惑和不解. 再 ...

  8. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  9. 减少leftJoin的使用 (转)

    作为开发,你是否经常碰到下面需要转换用户ID成用户名称的情况: 可惜你的这些业务表出于最少冗余设计要求,只有UserId,而没有UserName,这时你不得不破坏你一个类封装一个表的美好想法, 在你的 ...

  10. AS3给显示对象加边框

    给显示对象加边框,可以有以下三种方法1.根据相交路径的缠绕规则的奇偶规则法(使用奇偶缠绕规则时,任何相交路径都交替使用开放填充与闭合填充.如果使用同一填充绘制的两个正方形相交,则不会填充相交的区域.通 ...