跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F2864. Fibonacci Strings

    ID: 2870 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>矩阵运算字符串数学挑战难度共享题库Codeforces英文题面题目来源题面语言

Fibonacci Strings

题目描述

G2. Fibonacci Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Fibonacci strings are defined as follows:

  • f1 = «a»
  • f2 = «b»
  • fn = fn-1fn-2, n>2

Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba".

You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substring.

Input

The first line contains two space-separated integers k and m − the number of a Fibonacci string and the number of queries, correspondingly.

Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters "a" and "b".

The input limitations for getting 30 points are:

  • 1≤k≤3000
  • 1≤m≤3000
  • The total length of strings si doesn't exceed 3000

The input limitations for getting 100 points are:

  • 1≤k≤1018
  • 1≤m≤104
  • The total length of strings si doesn't exceed 105

Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use cin, cout streams or the %I64d specifier.

Output

For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109+7). Print the answers for the strings in the order in which they are given in the input.

Examples
Input
6 5
a
b
ab
ba
aba
Output
3
5
3
3
1

题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 2535 题。保留原作者与原赛事署名。