P10035 (Official)
这个题目是一个数学题,打表可以发现答案是……
好了,言归正传。
下文设 C++/STL/string 中的字符串拼接。
设
A_N 和B_N 中与C_N 匹配次数较少的字符串为G_N (另一个为H_N ),则\operatorname{mc}(G_{N},C_{N})=\dfrac{3^N-1}{2} ,且G_N 和C_N 的最后一位一定不同。
证明:
首先,
接下来,从
下面的性质很显然,证明不给了。
$B_{N+1}=B_N+A_N+B_N$; $C_{N+1}=C_N+C_N+D_N$($D_N$ 是 $C_N$ 改变最后一位,即 $\texttt{1}$ 变成 $\texttt{0}$,$\texttt{0}$ 变成 $\texttt{1}$ 后的字符串)。
可以发现,此时
若
而由于当
也就是说,
它们之间差
同理,
因此,
此外,由于
而由于
根据数学归纳法,得证。
也就是说,答案就是
std:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read() {
ll x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
return x * f;
}
const ll mod = 1000000007;
ll T;
inline ll power(ll a, ll b) {
ll ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
signed main() {
T = read();
while(T--) {
ll x = read(); x %= mod - 1;
ll a = power(3, x) - 1;
printf("%lld\n", ((a & 1) ? a + mod : a) / 2);
}
return 0;
}