思路:对于计数问题,考虑两个数对于答案的贡献,考虑
a
i
a_i
ai 和
a
j
a_j
aj 作为子序列中的两个对称位置,其在多少个字符串中出现过,显然,若
a
i
a_i
ai 距离子序列字符串s的首位距离为 x, 那么
a
j
a_j
aj 距离子序列串的结尾的距离也为 x,那么在给的原串中,代表我们可以在前 i-1 位选择 x 个,在后 n-j 位选择 x 位,那么我们总共需要枚举 i ,j ,x,这样时间复杂度
n
3
n^3
n3 ,考虑优化,那么使用范德蒙卷积即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 5;
typedef long long ll;
const int maxv = 4e6 + 5;
typedef pair<ll, ll> pll;
typedef array<int,4> ar;
#define endl "\n"
template<const int T>
struct ModInt {
const static int mod = T;
int x;
ModInt(int x = 0) : x(x % mod) {}
ModInt(long long x) : x(int(x % mod)) {}
int val() { return x; }
ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
bool operator == (const ModInt &a) const { return x == a.x; };
bool operator != (const ModInt &a) const { return x != a.x; };
void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
void operator /= (const ModInt &a) { *this = *this / a; }
friend ModInt operator + (int y, const ModInt &a){ int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
friend ModInt operator - (int y, const ModInt &a){ int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
friend ModInt operator * (int y, const ModInt &a){ return ModInt(1LL * y * a.x % mod);}
friend ModInt operator / (int y, const ModInt &a){ return ModInt(y) / a;}
friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x;}
friend istream &operator>>(istream &is, ModInt &t){return is >> t.x;}
ModInt pow(int64_t n) const {
ModInt res(1), mul(x);
while(n){
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
ModInt inv() const {
int a = x, b = mod, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
if (u < 0) u += mod;
return u;
}
};
using mint = ModInt<1000000007>;
// constexpr mod = ...;
// using Mint = modint<mod>;
struct Fact {
vector<mint> fact, factinv;
const int n;
Fact(const int& _n) : n(_n), fact(_n + 1, mint(1)), factinv(_n + 1) {
for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
factinv[n] = fact[n].inv();
for (int i = n; i; --i) factinv[i - 1] = factinv[i] * i;
}
mint C(const int& n, const int& k) {
if (n < 0 || k < 0 || n < k) return 0;
return fact[n] * factinv[k] * factinv[n - k];
}
mint A(const int& n, const int& k) {
if (n < 0 || k < 0 || n < k) return 0;
return fact[n] * factinv[n - k];
}
};
Fact z(N);
void solve()
{
int n;
cin>>n;
vector<int> a(n+1);
for(int i=1;i<=n;i++) cin>>a[i];
mint ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
mint res=2;
ans+=z.C(i-1+n-j,n-j)*res.pow(j-i-1)*(a[i]!=a[j]);
}
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
{
solve();
}
system("pause");
return 0;
}