QOJ.ac
QOJ
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#1237 | #771671 | #7219. The Mighty Spell | ASnown | ASnown | Failed. | 2024-11-22 17:00:48 | 2024-11-22 17:00:49 |
Details
Extra Test:
Accepted
time: 1855ms
memory: 51428kb
input:
200000 50 5 47 26 19 23 19 13 41 44 2 15 47 21 46 48 29 22 49 8 48 31 11 44 38 21 20 16 25 41 4 4 7 4 2 11 21 6 41 11 32 1 37 45 32 8 12 31 8 6 7 18 24 6 42 24 16 12 50 32 9 7 32 25 21 16 37 28 47 44 1 48 7 7 5 33 46 33 19 30 37 8 20 19 48 25 34 49 38 10 41 45 12 27 19 49 20 37 24 38 33 14 38 26 40 ...
output:
301574003
result:
ok answer is '301574003'
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#771671 | #7219. The Mighty Spell | ASnown | AC ✓ | 1861ms | 51788kb | C++17 | 4.6kb | 2024-11-22 14:58:24 | 2024-11-22 16:50:00 |
answer
#include<bits/stdc++.h>
#define file(F) freopen(#F".in","r",stdin),freopen(#F".out","w",stdout)
#define lowbit(x) ((x)&-(x))
#define ALL(x) (x).begin(),(x).end()
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define popc(x) (__builtin_popcountll((x)))
#define abs(x) ((x)>=0?(x):-(x))
using namespace std;
using ll=long long;
using uint=uint32_t;
template<typename T>
inline bool Max(T &x,T y) { return std::less<T>()(x,y)&&(x=y,true); }
template<typename T>
inline bool Min(T &x,T y) { return std::less<T>()(y,x)&&(x=y,true); }
void Solve();
namespace asnown {
using i32=int32_t;
using u32=uint32_t;
using i64=int64_t;
using u64=uint64_t;
using i128=__int128_t;
using u128=__uint128_t;
constexpr i32 inverse(i32 a,i32 m) {
i64 r=1;
while(a>1) { r=r*(m-m/a)%m,a=m%a; }
return r;
}
template<i32 m>
class static_modint {
using mint=static_modint;
public:
constexpr static_modint() : v(0) {}
constexpr static_modint(i32 v) : v(normal(v%m)) {}
constexpr static_modint(u32 v) : v(v%m) {}
constexpr static_modint(i64 v) : v(normal(v%m)) {}
constexpr static_modint(u64 v) : v(v%m) {}
constexpr static_modint(i128 v) : v(normal(v%m)) {}
constexpr static_modint(u128 v) : v(v%m) {}
constexpr u32 normal(i32 v) { if(v>=m) v-=m; return v+((v>>31)&m); }
constexpr u32 val() const { return v; }
constexpr static u32 mod() { return m; }
constexpr explicit operator bool() const { return v; }
constexpr mint inv() const { assert(v!=0); return mint(inverse(v,m)); }
constexpr mint pow(i64 b) const { assert(b>=0);
mint a=*this,res=1; for(;b;b>>=1,a*=a) if(b&1) res*=a; return res; }
constexpr mint operator+() const { return *this; }
constexpr mint operator-() const { return mint()-*this; }
constexpr mint& operator+=(const mint &p) { return v=normal(v+p.v),*this; }
constexpr mint& operator-=(const mint &p) { return v=normal(v-p.v),*this; }
constexpr mint& operator++() { return (*this)+=1; }
constexpr mint& operator--() { return (*this)-=1; }
constexpr mint& operator++(i32) { auto tmp=*this; return ++*this,tmp; }
constexpr mint& operator--(i32) { auto tmp=*this; return --*this,tmp; }
constexpr mint& operator*=(const mint &p) { v=i64(v)*p.v%m; return *this; }
constexpr mint& operator/=(const mint &p) { return *this *= p.inv(); }
constexpr friend mint operator+(const mint &p,const mint &q) { return mint(p)+=q; }
constexpr friend mint operator-(const mint &p,const mint &q) { return mint(p)-=q; }
constexpr friend mint operator*(const mint &p,const mint &q) { return mint(p)*=q; }
constexpr friend mint operator/(const mint &p,const mint &q) { return mint(p)/=q; }
constexpr friend mint operator++(mint &v,i32) { auto tmp=v; v+=1; return tmp; }
constexpr friend mint operator--(mint &v,i32) { auto tmp=v; v-=1; return tmp; }
constexpr friend std::istream& operator>>(std::istream &is,mint &a) { i64 v; is>>v; a=v; return is; }
constexpr friend std::ostream& operator<<(std::ostream &os,const mint &a) { os<<a.val(); return os; }
constexpr bool operator==(const mint& p) const { return val()==p.val(); }
constexpr bool operator!=(const mint& p) const { return val()!=p.val(); }
private:
u32 v;
};
using modint998244353=static_modint<998244353>;
using modint107=static_modint<1000000007>;
};
using mint=asnown::modint107;
const int N = 2e5+25,M = 55;
int n,m;
int a[N];
mint f[N],g[N],pw[N];
int pre[N],bot[M];
int cnt[N][M];
mint ans[N];
signed main() {
#ifndef ONLINE_JUDGE
#endif
cin.tie(nullptr)->sync_with_stdio(false);
Solve();
}
void Solve() {
cin>>n>>m;
pw[0]=1; for(int i=1;i<=n;i++) pw[i]=pw[i-1]*2;
for(ll i=1;i<=n;i++) f[i]=asnown::i64(2*i*i*i+3*i*i+3*i+3);
g[1]=f[1]; for(int i=2;i<=n;i++) g[i]=f[i]-2*f[i-1]+f[i-2];
for(int i=1;i<=n;i++) {
cin>>a[i];
pre[i]=bot[a[i]];
bot[a[i]]=i;
memcpy(cnt[i],cnt[i-1],sizeof cnt[i]);
++cnt[i][a[i]];
} pre[n+1]=n+1;
for(int i=1;i<=m;i++) bot[i]=n+1;
for(int l=n;l>=1;l--) {
vector<int> r;
bot[a[l]]=l;
for(int i=1;i<=m;i++) if(pre[bot[i]]<=l)
r.emplace_back(bot[i]);
r.emplace_back(n+1);
sort(ALL(r));
static bool vis[M]; memset(vis,0,sizeof vis);
for(int i=0;i+1<r.size();i++) {
vis[a[r[i]]]=true;
mint res=pw[r[i]-l+1];
for(int x=1;x<=m;x++) res*=pw[cnt[l-1][x]+cnt[n][x]-cnt[r[i]][x]]-(!vis[x]);
ans[r[i]-l+1]+=res,ans[r[i+1]-l+1]-=res;
}
}
mint sum=0,iv=1;
for(int i=1;i<=n;i++) {
ans[i]+=ans[i-1];
iv*=500000004;
sum+=ans[i]*iv*g[i];
}
printf("%d\n",sum.val());
}