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 ...
随机推荐
- [bzoj] 3669 NOI2014 魔法森林 || LCT
原题 copy一篇题解:原链接 将边按照a排序,然后从小到大枚举,加入图中. 在图中用lct维护一棵两点之间b最大值尽量小的生成树. 当加入一条边(u, v)时: 如果(u, v)不连通,则直接加入这 ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- BZOJ4503 两个串 【fft】
题目链接 BZOJ4503 题解 水水题. 和残缺的字符串那题几乎是一样的 同样转化为多项式 同样TLE 同样要手写一下复数才A #include<algorithm> #include& ...
- ContestHunter暑假欢乐赛 SRM 03
你们也没人提醒我有atcoderQAQ... A题曼哈顿距离=欧拉距离就是在同一行或者同一列,记录下i,j出现过的次数,减去就行,直接map过. B题一开始拿衣服了,一直以为排序和不排序答案是一个样的 ...
- UVA:11183:Teen Girl Squad (有向图的最小生成树)
Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...
- mybatis主键返回的实现
向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...
- 题解【luogu P2421 bzoj P1407 [NOI2002]荒岛野人】
洛谷题目链接 bzoj题目链接 题目大意:给定\(n\)组\(C_i, P_i, L_i\),求最小的\(M\)使得对于任意的\(i,j (1 \leq i, j \leq n)\) \[C_i + ...
- bzoj 1218 [HNOI2003]激光炸弹 二维前缀和
[HNOI2003]激光炸弹 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3022 Solved: 1382[Submit][Status][Di ...
- FreeRTOS ------ 栈、堆、任务栈
1.任务的栈资源(创建任务分配的资源,单位是4字节)来自 configTOTAL_HEAP_SIZE 定义的堆,如果任务栈总量超过 configTOTAL_HEAP_SIZE,任务创建失败: 2.如果 ...
- css渐变知识知多少
<!DOCTYPE html><html><head><meta charset="utf-8"> <title>教程( ...