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

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
 
Source
/**
题意:给一个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的更多相关文章

  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 规律+大数

    题目大意:定义了一种fib字符串,问第n个fib串的前m个字母前后相等串的最大长度,大约就是这样的 其实主要读完题意的时候并没有思路,但是列几个fib字符串就会发现,除了fib1以外,所有串的前面都是 ...

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

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

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

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  10. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. BZOJ4105 [Thu Summer Camp 2015]平方运算 【线段树】

    题目链接 BZOJ4105 题解 平方操作orz,虽说应该是线段树,但是不会维护啊QAQ 小瞧一眼题解... 平方成环?环长\(lcm\)小于\(60\)? 果然还是打表找规律题.... 那就很好做了 ...

  2. ContestHunter暑假欢乐赛 SRM 06

    T1二分check...为什么这么显然的我没看出来TAT,还在想倒着加入并查集check什么的,题写太多思维定势啦QAQ T2是NOIP题的弱化版...当时没看出来,写了个DP.可以看出这一位比上一位 ...

  3. 【优先队列】【UVa11997】K Smallest Sums

    传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Inpu ...

  4. Picture POJ - 1177 (线段树-扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

  5. iptables使用总结

    推荐博文: http://www.zsythink.net/archives/1199 http://www.cnblogs.com/migongci0412/p/5198370.html 总结: 1 ...

  6. bzoj 1111 [POI2007]四进制的天平Wag 数位Dp

    1111: [POI2007]四进制的天平Wag Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 302  Solved: 201[Submit][St ...

  7. bzoj 1123 [POI2008]BLO Tarjan求割点

    [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1540  Solved: 711[Submit][Status][Discu ...

  8. 名人问题/名流问题/Celebrity

    问题描述:名人问题一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人.现在有一个N个人的集合,以及他们之间的认识关系.求一个算法找出其中的名人(如果有的话)或者判断出没有名人(如果没 ...

  9. android中dip、dp、px、sp和屏幕密度

    1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这    这个 ...

  10. LightOJ 1161 - Extreme GCD 容斥

    题意:给你n个数[4,10000],问在其中任意选四个其GCD值为1的情况有几种. 思路:GCD为1的情况很简单 即各个数没有相同的质因数,所以求所有出现过的质因数次数再容斥一下-- 很可惜是错的,因 ...