MZL's Border

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 944    Accepted Submission(s): 306

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
 
题目大意:这个题目定义了一些新概念,而且比较绕,所以题意不是太好理解。给T组测试数据,每组有n,m。表示从fibn这个字符串中截取子串s[1:m]。然后在这个字串中找到最大的i满足s[1:i]=s[m-i+1:m],即公共前后缀。当时理解成了跟n有关系,即s[1:i]=s[n-i+1:n],主要受那个题的描述影响了。
 
解题思路:其实这个题目计算只跟m有关系,因为n只是进行限制了m的大小,跟n没多大关系。我们列出几项会发现,m跟LBorderm的差值是成斐波那契数列的。
 
import java.io.*;
import java.util.*;
import java.math.*;
public class Main{
public static void main(String [] args){
BigInteger one=BigInteger.ONE, zero = BigInteger.ZERO ;
BigInteger fib[] = new BigInteger [1200];
fib[1]=one;
fib[2]=one;
for(int i=3;i<=1005;i++){
fib[i]=fib[i-1].add( fib[i-2] );
}
Scanner cin = new Scanner (System.in);
int t=cin.nextInt();
while(t>0){
t--;
int n= cin.nextInt();
BigInteger m = cin.nextBigInteger();
for(int i=1;i<=1005;i++){
if(fib[i].compareTo(m.add( one ) )==1){
System.out.println( m.subtract(fib[i-2]).mod(BigInteger.valueOf(258280327 )));
break;
}
}
}
}
}

  

 

HDU 5351——MZL's Border——————【高精度+找规律】的更多相关文章

  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 数学规律

    f[1] = 'b', f[2] = 'a', f[i] = f[i - 1] + f[i - 2] 斐波那契数列的字符串,给你n和m,前m位中,最长的前缀等于后缀的长度是多少.1≤n≤1000, 1 ...

  3. hdu 2865 Polya计数+(矩阵 or 找规律 求C)

    Birthday Toy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. 递推+高精度+找规律 UVA 10254 The Priest Mathematician

    题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...

  5. bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2234  Solved: 1227[Submit][Statu ...

  6. HDU 4388 Stone Game II 博弈论 找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4388 http://blog.csdn.net/y1196645376/article/details/5214 ...

  7. HDU 4349 Xiao Ming's Hope 找规律

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...

  8. HDU 4731 Minimum palindrome 打表找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...

  9. HDU 4588 Count The Carries(找规律,模拟)

    题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...

随机推荐

  1. 验证视图状态MAC失败的解决办法

    在网上搜寻了很久看了很多关于MAC验证视图状态失败的解决方法.大部分人都说是在页里或web.config里加 EnableEventValidation="false" Enabl ...

  2. 继承&多态

    继承: 概念: 基类,超累,父类 访问权限: Public :无限制,自由访问 Private:不可继承 protected :包内,包外,可访问,继承 default:没有指明任何权限下,默认在同一 ...

  3. help手册使用

    属性的方法名的一般规律: 设置的属性名: set+属性名 获取属性值: 1.如果是bool类型,可能是 is+属性名 或者 属性名 2.不是bool类型,就是属性名

  4. OAuth实现腾讯微博第三方登录

    前言 还是得弱弱的写下这个技术的背后,大概是这个样子的,看到OAuth这个单词,我就想到了权限这个词,不知道为什么,又想起了第三方登录这个技术,于是自己脑补了一下,应该这两个东西是有关系的.再就是去动 ...

  5. loj#6040. 「雅礼集训 2017 Day5」矩阵(线性代数+递推)

    题面 传送门 题解 我的线代学得跟屎一样看题解跟看天书一样所以不要指望这题我会写题解 这里 //minamoto #include<bits/stdc++.h> #define R reg ...

  6. c# 中virtual与abstract

    在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Virtual方法(虚方法) virtual ...

  7. Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositoryDa ...

  8. C# repeater控件序号绑定

    做项目列表经常会需要列表有一个序号列,根据控件自身的属性就可以很方便的实现这个功能,如下图 这个不能用翻页功能,如果翻页重新刷新控件的话,序号会又重新开始!

  9. hdu2553 N皇后问题(dfs+回溯)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. Appium——appium之mac环境安装

    一.安装brew:Homebrew是一款Mac OS平台下的软件包管理工具执行:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserco ...