QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#181713#7219. The Mighty SpellZCKevinRE 22ms8260kbC++204.2kb2023-09-16 22:48:012023-09-16 22:48:02

Judging History

你现在查看的是最新测评结果

  • [2024-11-22 16:49:37]
  • hack成功,自动添加数据
  • (/hack/1236)
  • [2023-09-16 22:48:02]
  • 评测
  • 测评结果:RE
  • 用时:22ms
  • 内存:8260kb
  • [2023-09-16 22:48:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 200010, MAX_M = 51, p = 1e9 + 7;

using ap = array<int, MAX_M>;
int n, m;
int c[MAX_N];
ll cnt[MAX_M];
ll w[MAX_N], iw[MAX_N], p2[MAX_N];
ll wb[MAX_N], iwb[MAX_N];
ll pf[MAX_N];
ap lst[MAX_N];

ll inv[MAX_N];
ll g(ll x) {
	vector<int> c = {2, 3, 3, 3};
	ll s = 0;
	for (ll z: c)
		s = (s * x + z) % p;
	return s;
	return (2 * x * x % p * x % p
		+ 3 * x * x % p
		+ 3 * x + 3) % p;
}
void mul(ll &a, ll b) {
	a = (a * b) % p;
}
void add(ll &a, ll b) {
	a = (a + b) % p;
}
ll bin_pow(ll v, ll t) {
	ll ret = 1;
	for (;t;t>>=1, mul(v, v))
		if (t&1) mul(ret, v);
	return ret;
}

void init() {
	inv[0] = inv[1] = 1;
	p2[0] = 1;
	p2[1] = 2;
	for (int i = 2;i < MAX_N;++i) {
		inv[i] = (p-p/i) * inv[p % i] % p;
		p2[i] = p2[i-1] * 2 % p;
	}
	for (int i = 1;i < MAX_N;++i)
		pf[i] = (pf[i-1] + g(i) * bin_pow(inv[2], i)) % p;
}
//ll get_g(ll l, ll r, ll clen) {
//	ll ret = 0;
//	for (int i = l;i <= r;++i) {
//		add(ret, p2[clen - i] * f(i) % p);
//	}
//	return ret;
//}

ll cont(ll l, ll r) {
	vector<int> has(m+1);
	for (int i = l;i <= r;++i)
		has[ c[i] ] = true;
	
	vector<int> cnt(m+1);
	for (int i = 1;i+1 < l;++i)
		++cnt[ c[i] ];
	for (int i = r+2;i <= n;++i)
		++cnt[ c[i] ];

	ll ret = g(r - l + 1);
	for (int i = 1;i <= m;++i) {
		if (has[i]) mul(ret, p2[cnt[i]]);
		else mul(ret, p2[cnt[i]] - 1);
	}
	return ret;

}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	init();
	DE(g(1), g(2), g(3), g(2) + g(3) + 2  * g(1));
	cin >> n >> m;
	for (int i = 1;i <= n;++i)
		cin >> c[i];
	for (int i = 1;i <= n;++i) {
		lst[i] = lst[i-1];
		lst[i][c[i]] = i;
		++cnt[c[i]];
	} 
	for (int i = 1;i <= m;++i) {
		w[i] = p2[cnt[i]] - 1;
		iw[i] = bin_pow(w[i], p-2);
		if (cnt[i] > 0)
			wb[i] = p2[cnt[i]-1] - 1;
		else
			wb[i] = 0;
		iwb[i] = bin_pow(wb[i], p-2);
		//DE(i, w[i]);
	}
	ll res = 0;

	for (int r = 1;r <= n;++r) {
		if (r+1 <= n && cnt[ c[r+1] ] == 1) continue;

		int nxt_c = c[r+1];
		if (nxt_c) {
			--cnt[nxt_c];
			swap(w[nxt_c], wb[nxt_c]);
			swap(iw[nxt_c], iwb[nxt_c]);
		}

		ll cur_v = 1;
		for (int i = 1;i <= m;++i) {
			DE(i, cnt[i]);
			//mul(cur_v, p2[ cnt[i] ]-1);
			mul(cur_v, w[i]);
		}

		vector<int> sid(m+1); iota(AI(sid), 0);
		sort(AI(sid), [&](int a, int b) {
				return lst[r][a] > lst[r][b];
				});

		DE(r);
		int clen = 0;
    int QQ = 0;
		{
			// p1 == 0
			int &clen = QQ;
			ll sc = 1;
			for (int i = 1;i <= m;++i) 
				if (lst[r][i] != 0)
					clen += cnt[i]; 
				else
					mul(sc, w[i]);
					//mul(sc, p2[cnt[i]]-1);
			ll v = g(r) * p2[clen-r] % p * sc % p;
			//assert(v == cont(1, r));
			add(res, v);
		}
		for (int j = 0;;++j) {
			int p1 = lst[r][ sid[j] ];
			clen += cnt[ sid[j] ];
			if (p1 == 0) {
				// len = r
        
        assert(clen == QQ);
        break;
				ll sc = cur_v;
				ll v = cur_v * g(r) % p * p2[ clen - r] % p;
				assert(v == cont(1, r));
				add(res, v);
				break;
			}

			mul(cur_v, iw[ sid[j] ]);

			if (cnt[ sid[j] ] > 1) {
				clen -= cnt[ sid[j] ];
				ll sc = cur_v; 
				mul(sc, p2[ cnt[sid[j]]-1 ] - 1); 
				int O = r - p1;
				if (O > 0) {
					ll v = sc * g(O) % p * p2[clen - O] % p;
					add(res, v);
				}
				clen += cnt[ sid[j] ];
			}

			int p2 = lst[r][ sid[j+1] ];
			int L = r-p1+1, R = r-p2-1; 

			if (L <= R) {
				ll v = (pf[R] - pf[L-1] + p) % p
					* ::p2[clen - 1] % p; 
				add(res, v * cur_v);
			}
		}

		if (nxt_c) {
			++cnt[nxt_c];
			swap(w[nxt_c], wb[nxt_c]);
			swap(iw[nxt_c], iwb[nxt_c]);
		}
	}
	if (res < 0) res += p;
	cout << res << '\n'; 
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 22ms
memory: 8168kb

input:

3 2
1 2 2

output:

152

result:

ok answer is '152'

Test #2:

score: 0
Accepted
time: 20ms
memory: 8152kb

input:

4 3
1 2 1 2

output:

0

result:

ok answer is '0'

Test #3:

score: 0
Accepted
time: 16ms
memory: 8184kb

input:

6 3
1 2 3 3 2 1

output:

3627

result:

ok answer is '3627'

Test #4:

score: 0
Accepted
time: 20ms
memory: 8180kb

input:

5 5
1 4 5 3 2

output:

343

result:

ok answer is '343'

Test #5:

score: 0
Accepted
time: 20ms
memory: 8172kb

input:

5 5
1 5 4 3 2

output:

343

result:

ok answer is '343'

Test #6:

score: 0
Accepted
time: 13ms
memory: 8152kb

input:

5 5
3 1 5 4 2

output:

343

result:

ok answer is '343'

Test #7:

score: 0
Accepted
time: 20ms
memory: 8184kb

input:

5 5
4 1 2 3 5

output:

343

result:

ok answer is '343'

Test #8:

score: 0
Accepted
time: 20ms
memory: 8144kb

input:

5 5
2 3 2 2 2

output:

0

result:

ok answer is '0'

Test #9:

score: 0
Accepted
time: 16ms
memory: 8164kb

input:

5 5
1 2 2 2 5

output:

0

result:

ok answer is '0'

Test #10:

score: 0
Accepted
time: 16ms
memory: 8180kb

input:

5 5
4 2 1 3 5

output:

343

result:

ok answer is '343'

Test #11:

score: 0
Accepted
time: 20ms
memory: 8160kb

input:

5 5
2 3 4 5 1

output:

343

result:

ok answer is '343'

Test #12:

score: 0
Accepted
time: 20ms
memory: 8224kb

input:

5 5
4 3 5 2 1

output:

343

result:

ok answer is '343'

Test #13:

score: 0
Accepted
time: 20ms
memory: 8172kb

input:

5 5
3 4 5 2 1

output:

343

result:

ok answer is '343'

Test #14:

score: 0
Accepted
time: 16ms
memory: 8180kb

input:

5 5
4 3 3 5 2

output:

0

result:

ok answer is '0'

Test #15:

score: 0
Accepted
time: 20ms
memory: 8196kb

input:

5 5
1 4 4 1 1

output:

0

result:

ok answer is '0'

Test #16:

score: 0
Accepted
time: 20ms
memory: 8172kb

input:

5 5
1 5 2 4 3

output:

343

result:

ok answer is '343'

Test #17:

score: 0
Accepted
time: 13ms
memory: 8144kb

input:

5 5
4 2 5 3 1

output:

343

result:

ok answer is '343'

Test #18:

score: 0
Accepted
time: 19ms
memory: 8216kb

input:

5 5
3 1 4 5 2

output:

343

result:

ok answer is '343'

Test #19:

score: 0
Accepted
time: 20ms
memory: 8260kb

input:

5 5
5 1 3 4 2

output:

343

result:

ok answer is '343'

Test #20:

score: 0
Accepted
time: 12ms
memory: 8172kb

input:

5 5
4 5 3 5 5

output:

0

result:

ok answer is '0'

Test #21:

score: 0
Accepted
time: 18ms
memory: 8216kb

input:

5 5
2 2 3 4 2

output:

0

result:

ok answer is '0'

Test #22:

score: 0
Accepted
time: 21ms
memory: 8216kb

input:

5 5
4 5 1 2 3

output:

343

result:

ok answer is '343'

Test #23:

score: 0
Accepted
time: 16ms
memory: 8216kb

input:

5 5
3 5 1 2 4

output:

343

result:

ok answer is '343'

Test #24:

score: 0
Accepted
time: 16ms
memory: 8180kb

input:

5 5
5 4 1 2 3

output:

343

result:

ok answer is '343'

Test #25:

score: 0
Accepted
time: 20ms
memory: 8140kb

input:

5 5
5 3 4 1 2

output:

343

result:

ok answer is '343'

Test #26:

score: 0
Accepted
time: 20ms
memory: 8140kb

input:

5 5
3 1 2 1 5

output:

0

result:

ok answer is '0'

Test #27:

score: 0
Accepted
time: 19ms
memory: 8212kb

input:

5 5
3 1 4 2 5

output:

343

result:

ok answer is '343'

Test #28:

score: 0
Accepted
time: 16ms
memory: 8152kb

input:

5 5
1 2 4 5 3

output:

343

result:

ok answer is '343'

Test #29:

score: 0
Accepted
time: 16ms
memory: 8140kb

input:

5 5
4 3 1 5 2

output:

343

result:

ok answer is '343'

Test #30:

score: 0
Accepted
time: 12ms
memory: 8216kb

input:

5 5
2 1 3 4 5

output:

343

result:

ok answer is '343'

Test #31:

score: 0
Accepted
time: 20ms
memory: 8220kb

input:

5 5
4 2 1 3 5

output:

343

result:

ok answer is '343'

Test #32:

score: 0
Accepted
time: 16ms
memory: 8180kb

input:

5 5
4 3 1 4 3

output:

0

result:

ok answer is '0'

Test #33:

score: 0
Accepted
time: 16ms
memory: 8160kb

input:

5 5
3 4 1 1 3

output:

0

result:

ok answer is '0'

Test #34:

score: 0
Accepted
time: 20ms
memory: 8144kb

input:

20 5
5 2 5 1 5 5 2 4 5 5 2 5 5 5 5 4 2 5 3 4

output:

102882880

result:

ok answer is '102882880'

Test #35:

score: 0
Accepted
time: 16ms
memory: 8200kb

input:

20 5
3 2 1 2 2 2 2 2 4 3 2 2 3 3 5 2 2 1 2 5

output:

134653185

result:

ok answer is '134653185'

Test #36:

score: 0
Accepted
time: 20ms
memory: 8144kb

input:

20 5
1 2 3 2 1 3 5 1 2 4 5 2 3 4 5 1 4 3 4 5

output:

315505338

result:

ok answer is '315505338'

Test #37:

score: 0
Accepted
time: 14ms
memory: 8156kb

input:

20 5
5 2 2 4 2 3 5 1 1 3 1 5 2 4 4 3 1 4 3 5

output:

312062382

result:

ok answer is '312062382'

Test #38:

score: 0
Accepted
time: 20ms
memory: 8144kb

input:

20 5
3 4 2 5 4 5 5 4 1 4 3 3 4 3 4 2 3 2 5 3

output:

188515821

result:

ok answer is '188515821'

Test #39:

score: 0
Accepted
time: 17ms
memory: 8200kb

input:

20 5
3 5 1 3 3 4 5 2 1 1 3 1 2 5 2 1 1 2 5 2

output:

197857329

result:

ok answer is '197857329'

Test #40:

score: 0
Accepted
time: 16ms
memory: 8220kb

input:

20 10
3 8 6 8 9 2 1 5 8 6 7 8 4 8 6 8 10 8 8 8

output:

4905343

result:

ok answer is '4905343'

Test #41:

score: 0
Accepted
time: 20ms
memory: 8184kb

input:

20 10
10 5 1 8 7 2 7 2 6 2 2 2 2 2 4 2 3 7 9 7

output:

3724041

result:

ok answer is '3724041'

Test #42:

score: 0
Accepted
time: 13ms
memory: 8168kb

input:

20 10
5 1 9 6 10 4 5 3 2 4 8 3 7 1 8 6 2 9 10 7

output:

52978806

result:

ok answer is '52978806'

Test #43:

score: 0
Accepted
time: 16ms
memory: 8160kb

input:

20 10
5 8 6 2 1 10 3 8 9 7 6 5 10 9 1 7 3 4 4 2

output:

53309955

result:

ok answer is '53309955'

Test #44:

score: 0
Accepted
time: 20ms
memory: 8200kb

input:

20 10
1 8 1 7 9 7 9 9 7 4 1 6 2 7 8 6 6 9 6 7

output:

0

result:

ok answer is '0'

Test #45:

score: 0
Accepted
time: 16ms
memory: 8180kb

input:

20 10
1 10 10 10 2 9 1 1 7 2 3 9 5 10 8 4 1 4 2 5

output:

0

result:

ok answer is '0'

Test #46:

score: 0
Accepted
time: 16ms
memory: 8180kb

input:

20 20
9 16 3 18 8 19 6 4 2 17 1 15 10 11 5 13 12 7 14 20

output:

17263

result:

ok answer is '17263'

Test #47:

score: 0
Accepted
time: 19ms
memory: 8144kb

input:

20 20
2 17 18 12 15 20 11 9 10 5 6 16 7 8 4 13 3 1 19 14

output:

17263

result:

ok answer is '17263'

Test #48:

score: 0
Accepted
time: 21ms
memory: 8216kb

input:

20 20
14 15 19 8 3 20 9 12 18 7 5 11 4 2 16 6 1 17 10 13

output:

17263

result:

ok answer is '17263'

Test #49:

score: 0
Accepted
time: 20ms
memory: 8144kb

input:

20 20
18 9 3 4 13 12 15 11 2 16 19 7 10 20 17 8 6 1 14 5

output:

17263

result:

ok answer is '17263'

Test #50:

score: -100
Dangerous Syscalls

input:

20 20
7 6 4 14 20 13 1 15 5 18 16 10 1 16 12 14 5 13 1 3

output:


result: