# CodeForces 794 G.Replace All

## CodeForces 794 G.Replace All

$2^{\dfrac{n}{\dfrac{\max(|\Delta a|,|\Delta b|)}{g}}+1}-2$

#### code

/*program by mangoyang*/
#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 1000005, mod = 1e9 + 7;
char s[N], t[N];
int inv[N], js[N], pw[N], f[N], cnts, cntt, da, db, lens, lent, n, ans, total;
inline int Pow(int a, int b){
int ans = 1;
for(; b; b >>= 1, a = 1ll * a * a % mod)
if(b & 1) ans = 1ll * ans * a % mod;
return ans;
}
inline int C(int x, int y){
return 1ll * js[x] * inv[y] % mod * inv[x-y] % mod;
}
int main(){
scanf("%s", s + 1), scanf("%s", t + 1), read(n);
lens = strlen(s + 1), lent = strlen(t + 1);
for(int i = 1; i <= lens; i++){
if(s[i] == 'A') da++; if(s[i] == 'B') db++; if(s[i] == '?') cnts++;
}
for(int i = 1; i <= lent; i++){
if(t[i] == 'A') da--; if(t[i] == 'B') db--; if(t[i] == '?') cntt++;
}
inv[0] = js[0] = pw[0] = 1;
for(int i = 1; i <= n + 1; i++) pw[i] = 2ll * pw[i-1] % mod;
for(int i = 1; i <= lens + lent; i++)
js[i] = 1ll * js[i-1] * i % mod, inv[i] = Pow(js[i], mod - 2);
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] += mod - f[j]) %= mod;
total = (total + 1ll * f[i] * pw[i] % mod) % mod;
}
for(int d = -cntt; d <= cnts; d++){
int A = da + d, B = db + cnts - cntt - d, x = C(cnts + cntt, cntt + d);
if(!A && !B) (ans += 1ll * x * total % mod) %= mod;
if(1ll * A * B >= 0) continue;
int g = __gcd(abs(A), abs(B));
A = abs(A) / g, B = abs(B) / g;
(ans += (1ll * x * (pw[n/max(A,B)+1] - 2) + mod) % mod) %= mod;
}
if(lens == lent){
int flag = 1, res = 1;
for(int i = 1; i <= lens; i++){
if(s[i] != '?' && t[i] != '?' && s[i] != t[i]) flag = 0;
if(s[i] == '?' && t[i] == '?') res = 2ll * res % mod;
}
if(!flag) return cout << ans << endl, 0;
(ans += 1ll * res * (1ll * (pw[n+1] - 2) * (pw[n+1] - 2) % mod - total) % mod) %= mod;
ans = (ans % mod + mod) % mod;
}
cout << ans << endl;
return 0;
}

posted @ 2019-04-15 19:37  Joyemang33  阅读(200)  评论(0编辑  收藏