back to the menu

Flag Counter TopCoder SRM 593 - 1000: WolfDelaymasterHard

SRM 593 1 1000 WolfDelaymasterHard


Problem Statement

给定一个由'w','o','?'三种字符组成的字符串 s ,
问有多少种方案将'?'变为'w'或'o',
使得新字符串为若干个长度为偶数且前一半全部为'w',
后一半全部为'o'的字符串连接形成。答案对10^9+7取模。
s的长度:2000001

Wolf Sothe is playing the game Delaymaster. In this game, he can create new words according to the following rules:
  1. For each positive integer n, the string which consists of n copies of 'w' followed by n copies of 'o' is a valid word.
  2. The concatenation of two valid words is a valid word.
  3. Only the words that can be obtained by rules 1 and 2 are valid. There are no other valid words.
Thus, for example:
  • By rule 1, "wo", "wwoo", and "wwwwwooooo" are valid words.
  • Then, by rule 2, "wowwoo" is a valid word.
  • By applying rule 2 twice, "wowwoowo" is a valid word.
  • The strings "w", "ow", "woow", and "wwwoo" are not valid words.
Sothe has a very long string s. Each character of s is one of 'w', 'o', and '?'. Sothe wants to replace each '?' with either 'w' or 'o' in such a way that the result is a valid word. Return the number of different valid words he can obtain this way, modulo 1,000,000,007.

As s can be very long, you will have to generate it in your program. You are given nine ints: N, wlen, w0, wmul, wadd, olen, o0, omul, and oadd. Use the following pseudocode to generate s:

s = "???...???";				// Concatenation of N '?'s

signed_64bit_integer x = w0;
for(i=0;i<wlen;i++){
	s[x] = 'w';				// Rewrite the x-th (0-based) character of s
	x = (x * wmul + wadd) % N;
}

x = o0;
for(i=0;i<olen;i++){
	s[x] = 'o';				// Rewrite the x-th (0-based) character of s
	x = (x * omul + oadd) % N;
}

Definition

(be sure your method is public)

Limits

Constraints

Test cases

  1. input
    • N 8
    • wlen 5
    • w0 2
    • wmul 3
    • wadd 2
    • olen 0
    • o0 6
    • omul 7
    • oadd 1
    output
    Returns 6
    note
    s is "w?w?????". He can obtain six valid words: "wwwwoooo", "wwwooowo", "wowwwooo", "wowwoowo", "wowowwoo", and "wowowowo".
  2. input
    • N 20
    • wlen 19
    • w0 12
    • wmul 9
    • wadd 15
    • olen 1
    • o0 8
    • omul 11
    • oadd 1
    output
    Returns 26
    note
    s is "??ww????o???ww??????".
  3. input
    • N 40
    • wlen 24
    • w0 34
    • wmul 5
    • wadd 11
    • olen 33
    • o0 35
    • omul 23
    • oadd 27
    output
    Returns 296
    note
    s is "?w?o??w????w????o????w????w????wo?wow???".
  4. input
    • N 60
    • wlen 35
    • w0 8
    • wmul 55
    • wadd 3
    • olen 11
    • o0 20
    • omul 9
    • oadd 29
    output
    Returns 10058904
    note
    s is "????????w???????????o??w?????o????????????????????o????????o".
  5. input
    • N 2000
    • wlen 183
    • w0 994
    • wmul 862
    • wadd 468
    • olen 148
    • o0 433
    • omul 1247
    • oadd 1989
    output
    Returns 532199331
  6. input
    • N 2000000
    • wlen 419443
    • w0 1305303
    • wmul 1761823
    • wadd 1007025
    • olen 874640
    • o0 1516339
    • omul 229519
    • oadd 1473199
    output
    Returns 861766906
  7. input
    • N 8
    • wlen 6
    • w0 0
    • wmul 1
    • wadd 1
    • olen 3
    • o0 3
    • omul 6
    • oadd 1
    output
    Returns 0
    note
    s is "wwwoww??". He can't obtain any valid words.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.