多校-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≤m≤length(f[n])

规律题,虽然我不知道为什么。

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.*;
 4 public class Main{
 5     //Scanner cin = Scanner(System.in);
 6     static BigInteger f[] = new BigInteger[1005];
 7     public static void main(String[] args){
 8         f[1] = new BigInteger("1");
 9         f[2] = new BigInteger("2");
10         for(int i = 3; i <= 1001; i++){
11             //f[i].valueOf(f[i - 1);
12             //f[i] = f[i - 1];
13             f[i] = f[i - 1].add(f[i - 2]);
14             //f[i].add(f[i - 2]);
15             //System.out.println(f[i]);
16         }
17         Scanner cin = new Scanner(System.in);
18         int T = cin.nextInt();
19         int n;
20         BigInteger m;
21         for(int cas = 1; cas <= T; cas++){
22             n = cin.nextInt();
23             m = cin.nextBigInteger();
24             BigInteger mm = m.add(new BigInteger("1"));
25             int p = 0;
26             for(int i = 1; i <= 1001; i++){
27                 if(f[i].compareTo(mm) > 0){
28                     p = i;
29                     break;
30                 }
31             }
32             BigInteger ans = m.subtract(f[p - 2]);
33             System.out.println(ans.mod(new BigInteger("258280327")));
34         }
35         
36     }
37 }

 

posted on 2015-08-04 20:31  张济  阅读(254)  评论(0编辑  收藏  举报

导航