CodeForces 794G. Replace All

来源:互联网 发布:农村金融机构数据 编辑:程序博客网 时间:2024/05/16 02:00

Igor the analyst is at work. He learned about a feature in his text editor called "Replace All". Igor is too bored at work and thus he came up with the following problem:

Given two strings x and y which consist of the English letters 'A' and 'B' only, a pair of strings (s, t) is called good if:

  • s and t consist of the characters '0' and '1' only.
  • 1 ≤ |s|, |t| ≤ n, where |z| denotes the length of string z, and n is a fixed positive integer.
  • If we replace all occurrences of 'A' in x and y with the string s, and replace all occurrences of 'B' in x and y with the string t, then the two obtained from x and y strings are equal.

For example, if x = AABy = BB and n = 4, then (010101) is one of good pairs of strings, because both obtained after replacing strings are "01010101".

The flexibility of a pair of strings x and y is the number of pairs of good strings (s, t). The pairs are ordered, for example the pairs (01)and (10) are different.

You're given two strings c and d. They consist of characters 'A', 'B' and '?' only. Find the sum of flexibilities of all possible pairs of strings (c', d') such that c' and d' can be obtained from c and d respectively by replacing the question marks with either 'A' or 'B', modulo 109 + 7.

Input

The first line contains the string c (1 ≤ |c| ≤ 3·105).

The second line contains the string d (1 ≤ |d| ≤ 3·105).

The last line contains a single integer n (1 ≤ n ≤ 3·105).

Output

Output a single integer: the answer to the problem, modulo 109 + 7.

Examples
input
A??3
output
2
input
AB10
output
2046
Note

For the first sample, there are four possible pairs of (c', d').

If (c', d') = (AA, A), then the flexibility is 0.

If (c', d') = (AB, A), then the flexibility is 0.

If (c', d') = (AA, B), then the flexibility is 2, as the pairs of binary strings (1, 11)(0, 00) are the only good pairs.

If (c', d') = (AB, B), then the flexibility is 0.

Thus, the total flexibility is 2.

For the second sample, there are 21 + 22 + ... + 210 = 2046 possible binary strings of length not greater 10, and the set of pairs of good strings is precisely the set of pairs (s, s), where s is a binary string of length not greater than 10.

题意:给两个包含A,B,?的串,对于?为每个A/B的情况,求下面这个东西的和:统计有多少对01串(S,T)使得把所有'A'换成S,'B'换成T使得两个串相等

题解:

我来大力翻译一波

先考虑没有问号的情况
(定义字符串减法:如果S + X = T,那么T - S = X)
(为了方便表示,定义S、T为01串,a、b为原串的'A','B',原串为A、B)
1、定义两个字符串(S, T)(不妨令|S| <= |T|)互质当且仅当(S = T)或者S是T的一个前缀且(T - S, S)互质
容易发现这个等价于S和T有一个公共循环节m,且S[1...m] == T[1...m]
2、对于一个合法的答案(S,T),可以证明S,T互质或者原串A,B相等
现在我们不考虑原串A,B相等的情况
对于原串A,B,我们先去掉A和B的lcp,这样A[1] != B[1],它们一个是S一个是T,且一定存在前缀关系
然后把T拆成S和X,重复以上过程,得证
3、由于S + T = T + S,所以我们可以交换相邻的S和T,即可以交换相邻的a和b,答案只与a、b的数量有关,即da表示cntA[a] - cntB[a], db = cntA[b] - cntB[b]
4、若da = db = 0,则变为统计长度不超过n的互质字符串对数
若|S| = p, |T| = q, 答案显然是2 ^ gcd(p, q),即统计∑[i = 1 to n]∑[j = 1 to n] 2 ^ gcd(i, j)
若da, db >= 0或者da, db <= 0,显然无解
现在讨论da * db < 0的情况,为方便表述,令da = |da|, db = |db|,则da * S = db * T
如果da = db,那么S = T,答案是sum[i = 1 to n]2 ^ i = 2 ^ (n + 1) - 2
否则不妨设da > db,则|S| < |T|,记X = T - S,则da * S = db * (S + X),即(da - db) * S = db * X,这就意味着我们可以转化为一个子问题
而这个子问题一定会到da = db的情况,我们注意到最后一定是某个串X重复若干次,即S = db * X, T = da * T,故答案是2 ^ (n / max(da / gcd(da, db), db / gcd(da, db)) + 1) - 2
现在考虑有问号的情况
假设A中有p个问号,其中x个是a,B中有q个问号,其中y个是a
那么现在的(da, db) = (da + x - y, db + (p + q) - (x - y)),记x - y = d,则我们可以快速求出答案
且对于每个d,答案是∑∑C(p, x) * C(q, y) = ∑∑C(p, x) * C(q, q - y) = ∑C(p, x) * C(q, q + d - x) = C(p + q, q + d)

#include <bits/stdc++.h>#define xx first#define yy second#define mp make_pair#define pb push_back#define fill( x, y ) memset( x, y, sizeof x )#define copy( x, y ) memcpy( x, y, sizeof x )using namespace std;typedef long long LL;typedef pair < int, int > pa;inline int read(){int sc = 0, f = 1; char ch = getchar();while( ch < '0' || ch > '9' ) { if( ch == '-' ) f = -1; ch = getchar(); }while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();return sc * f;}const int MAXN = 600005;const int mod = 1e9 + 7;char a[MAXN], b[MAXN];int n, da, db, p, q, fac[MAXN], inv[MAXN], f[MAXN], c, table[MAXN], lena, lenb, ans;inline int qpow(int x, int y) { int ret = 1; for( ; y ; y >>= 1, x = 1LL * x * x % mod ) if( y & 1 ) ret = 1LL * ret * x % mod; return ret; }inline int C(int x, int y) { return 1LL * fac[ x ] * inv[ y ] % mod * inv[ x - y ] % mod; }inline void init(){table[ 0 ] = fac[ 0 ] = inv[ 0 ] = 1;for( int i = 1 ; i <= max( max( lena, lenb ), n + 1 ) ; i++ ) table[ i ] = ( table[ i - 1 ] << 1 ) % mod;for( int i = 1 ; i <= lena + lenb ; i++ ) fac[ i ] = 1LL * fac[ i - 1 ] * i % mod;inv[ lena + lenb ] = qpow( fac[ lena + lenb ], mod - 2 );for( int i = lena + lenb - 1 ; i ; i-- ) inv[ i ] = 1LL * inv[ i + 1 ] * ( i + 1 ) % mod;for( int i = n ; i ; i-- ){f[ i ] = 1LL * ( n / i ) * ( n / i ) % mod;for( int j = i + i ; j <= n ; j += i )f[ i ] = ( f[ i ] - f[ j ] + mod ) % mod;}for( int i = 1 ; i <= n ; i++ ) c = ( 1LL * f[ i ] * table[ i ] + c ) % mod;}int main(){#ifdef wxh010910freopen( "data.in", "r", stdin );#endifscanf( "%s%s", a + 1, b + 1 ); n = read();lena = strlen( a + 1 ); lenb = strlen( b + 1 );init();for( int i = 1 ; i <= lena ; i++ ) if( a[ i ] == 'A' ) da++; else if( a[ i ] == 'B' ) db++; else p++;for( int i = 1 ; i <= lenb ; i++ ) if( b[ i ] == 'A' ) da--; else if( b[ i ] == 'B' ) db--; else q++;for( int d = -q ; d <= p ; d++ ){int t = C( p + q, q + d ), A = da + d, B = db + p - q - d;if( A == 0 && B == 0 ) { ans = ( 1LL * t * c + ans ) % mod; continue; }if( 1LL * A * B < 0 ){A = abs( A ), B = abs( B );ans = ( 1LL * t * ( table[ n / ( max( A, B ) / __gcd( A, B ) ) + 1 ] - 2 + mod ) + ans ) % mod;}}if( lena == lenb ){bool same = true; int cnt = 0;for( int i = 1 ; i <= lena ; i++ )if( a[ i ] != '?' && b[ i ] != '?' && a[ i ] != b[ i ] ) same = false;else if( a[ i ] == '?' && b[ i ] == '?' ) cnt++;if( same ){ans = ( ans - 1LL * table[ cnt ] * c % mod + mod ) % mod;ans = ( ans + 1LL * table[ cnt ] * ( table[ n + 1 ] - 2 + mod ) % mod * ( table[ n + 1 ] - 2 + mod ) ) % mod;}}return printf( "%d\n", ans ), 0;}


原创粉丝点击