HDU-5351
MZL's Border
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 671 Accepted Submission(s): 209
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.
/**
题意:给一个A串,一个B串,然后str[i] = str[i-1]+str[i-2];
求解对于第n个串的第前m位最长的LBorder,LBorder 是指s[1:i]=s[n−i+1:n],
做法:画几个串可以找到规律,比如第九个串
a b a a b a b a a b a a b a b a a b a b a a b a a b a b a a b a
[0] [0 1] [1 2 3] [2 3 4 5 6] [4 5 6 7 8 9 10 11] [7 8 9 10 11 12 13 14 15 16 17 18 19]
Java
**/
import java.util.*;
import java.math.*; public class Main
{
public static void main(String args[])
{
BigInteger AA[] = new BigInteger [1000+5];
BigInteger A[] = new BigInteger [1000+5];
A[1] = BigInteger.ONE;
A[2] = BigInteger.valueOf(2);
BigInteger MOD = BigInteger.valueOf(258280327);
BigInteger mm = BigInteger.valueOf(1);
for (int i = 3; i <= 1001; i++) {
A[i] = A[i-1].add(A[i-2]);
}
for(int i=2;i<=1001;i++)
{
A[i] = A[i].add(A[i-1]);
}
AA[1] = BigInteger.ZERO;
AA[2] = BigInteger.ZERO;
for(int i=3;i<=1001;i++)
{
AA[i] = AA[i-1].add(AA[i-2]).add(mm);
}
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0)
{
int n = in.nextInt();
BigInteger m = in.nextBigInteger();
if (m.equals(BigInteger.valueOf(1)) || m.equals(BigInteger.valueOf(2)))
{
System.out.println(0);
continue;
}
boolean ok = false;
int tt = 0;
for (int i = 1; i <= 1001; i++)
{
int yy = A[i].compareTo(m);
if(yy == 1) break;
tt = i;
}
if(A[tt].compareTo(m) == 0)
{
BigInteger temp = BigInteger.ZERO;
BigInteger res = A[tt].subtract(A[tt-1]);
BigInteger tmp = AA[tt].add(res) ;
tmp = tmp.subtract(mm);
System.out.println(tmp.mod(MOD));
}
else
{
BigInteger temp = BigInteger.ZERO;
BigInteger res = m.subtract(A[tt]);
BigInteger tmp = AA[tt+1].add(res);
tmp = tmp.subtract(mm);
System.out.println(tmp.mod(MOD));
}
}
}
}
HDU-5351的更多相关文章
- 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 规律+大数
题目大意:定义了一种fib字符串,问第n个fib串的前m个字母前后相等串的最大长度,大约就是这样的 其实主要读完题意的时候并没有思路,但是列几个fib字符串就会发现,除了fib1以外,所有串的前面都是 ...
- 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 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- syslog服务器配置笔记
syslog服务器可以用作一个网络中的日志监控中心,rsyslog是一个开源工具,被广泛用于Linux系统以通过TCP/UDP协议转发或接收日志消息.本文我们来讲讲在 Linux 上配置一个 sysl ...
- bzoj4321: queue2(DP)
woc万能的OEIS大法!这题居然是有递推式的QAQ http://oeis.org/A002464 这题的状态想不出来T^T... f[i][j][0/1]表示前i个编号,有j对相邻的编号位置上相邻 ...
- bzoj2058: [Usaco2010 Nov]Cow Photographs(逆序对)
题目大意:给出n个数的序列,每次可以交换相邻的两个数,问把序列变成编号i在编号i+1左边,编号1在编号n右边(一个环)最少需要多少步.如:35421最少交换两次变为34512. 一开始看到这题,只会O ...
- 你会喜欢的前端^o^!
前端那些事儿 网页设计常用色彩搭配表 很漂亮的alert弹出框 一个让你想到即可做到的web弹窗/层解决方案 基于HTML5的在绘图特效平台(酷炫)
- mysql 密码忘记
新版本 再去掉 /etc/my.cnf
- Sqlserver中如何创建链接服务器
链接服务器在跨数据库/跨服务器查询时非常有用(比如分布式数据库系统中),我将以图文方式详细说明如何利用SQL Server Management Studio在图形界面下创建链接服务器 方法/步骤 ...
- CursorAdapter中getView newView bindView异同
Adapter的作用是界面与数据之间的桥梁,通过设置适配器至ListView控件后(如调用ListView的 setAdapter(ListAdapter adapter) ...
- git代码冲突
如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突: error: Your local changes to the f ...
- java 反射和new的区别
关于java 反射和new的区别,在这里我不做多讲,因为网上有大把资料,描述得很详细. 今天我只讲一点,为什么要用反射?直接用new不行么?干嘛弄得那么麻烦! 1.基本上效果差不多,但是new对象,无 ...
- 字符串类dp的题目总结
熟练掌握回文串吧,大致有dp或者模拟类的吧 ①dp+预处理,懂得如何枚举回文串(一) ②dp匹配类型的题目(二) ③dp+预处理 子串类型 (三) ④字符串的组合数(四) 一:划分成回文串 UVA11 ...