QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#290636#2020. 微信步数MoRanSky100 ✓60ms9000kbC++233.0kb2023-12-25 06:13:362023-12-25 06:13:36

Judging History

This is the latest submission verdict.

  • [2023-12-25 06:13:36]
  • Judged
  • Verdict: 100
  • Time: 60ms
  • Memory: 9000kb
  • [2023-12-25 06:13:36]
  • Submitted

answer

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long LL;

const int N = 5e5 + 5, S = 11, INF = 2e9, P = 1e9 + 7;

int n, K, w[N], C[N], D[N], x[S], mn[S], mx[S], len[S], ans, v[S];
int l[S], s[S][S];

int a[S], ta, b[S], tb, c[S];

void inline mul() {
	memset(c, 0, sizeof c);
	for (int i = 0; i <= ta; i++) {
		for (int j = 0; j <= tb; j++) {
			c[i + j] = (c[i + j] + (LL)a[i] * b[j]) % P;
		}
	}
	memcpy(a, c, sizeof a);
	ta += tb;
}

bool inline check(int x) {
	for (int k = 1; k <= K; k++)
		if (len[k] - (LL)x * abs(v[k]) <= 0) return false;
	return true;
}

int inline power(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = (LL)res * a % P;
		a = (LL)a * a % P;
		b >>= 1;
	}
	return res;
}


int inline query(int a, int b) {
	int res = 1;
	for (int i = 0; i < b; i++)
		res = (LL)res * (a - i) % P * power(i + 1, P - 2) % P;
	return res;
}

int inline work(int x, int k) {
	if (k == 0) return x;
	int sum = 0, t = x + 1, z = 1;
	for (int j = 1; j <= min(x, k); j++) {
		z = (LL)z * j % P;
		t = (LL)t * power(j + 1, P - 2) % P * (x - j + 1) % P;
		sum = (sum + (LL)s[k][j] * t % P * z) % P;
	}
	return sum;
}

int main() {
	scanf("%d%d", &n, &K);
	for (int i = 1; i <= K; i++) 
		scanf("%d", w + i), len[i] = mx[i] = w[i], mn[i] = 1;
	bool ok = true;
	
	s[0][0] = 1;
	for (int i = 1; i <= K; i++) {
		for (int j = 1; j <= i; j++) {
			s[i][j] = (s[i - 1][j - 1] + (LL)s[i - 1][j] * j) % P;
		}
	}
	
	for (int i = 1; i <= n; i++) {
		scanf("%d%d", C + i, D + i);
		int c = C[i];
		x[c] += D[i];
		if (w[c] - x[c] < mx[c]) mx[c] = w[c] - x[c];
		if (1 - x[c] > mn[c]) mn[c] = 1 - x[c];
		int t = max(mx[c] - mn[c] + 1, 0);
		if (t < len[c]) {
			int u = i;
			for (int j = 1; j <= K; j++)
				if (j != c) {
					if (len[j] <= 0) { u = 0; break; }
					u = (LL)u * len[j] % P;
				}
			ans = (ans + u) % P;
			len[c] = t;
		}
		
	}
	int cnt = 0;
	for (int i = 1; i <= K; i++) cnt += !x[i], v[i] = x[i];
	
	
	if (ok && cnt == K) { puts("-1"); return 0; }
	

	
	for (int i = 1; i <= n; i++) {
		int c = C[i];
		x[c] += D[i];
		if (w[c] - x[c] < mx[c]) mx[c] = w[c] - x[c];
		if (1 - x[c] > mn[c]) mn[c] = 1 - x[c];
		int t = max(mx[c] - mn[c] + 1, 0);
		
		if (t < len[c]) {
			int u = n + i;
			for (int j = 1; j <= K; j++)
				if (j != c) {
					if (len[j] <= 0) { u = 0; break; }
					u = (LL)u * len[j] % P;
				}
			ans = (ans + u) % P;
			memset(a, 0, sizeof a); 
			
			ta = 1;
			a[0] = n + i, a[1] = n;
		
			for (int k = 1; k <= K; k++) {
				if (k != c) {
					tb = 1;
					b[0] = len[k] % P;
					b[1] = (P - abs(v[k])) % P;
					mul();
				}
			
			}
			
			int l = 0, r = 1e9;
			
			while (l < r) {
				int mid = (l + r + 1) >> 1;
				if (check(mid)) l = mid;
				else r = mid - 1;
			}
			
			if (check(r)) {
				for (int j = 0; j <= ta; j++) {
					
					ans = (ans + (LL)a[j] * work(r, j)) % P;
				}
			}
			
			
			
			len[c] = t;
		}
	}
	printf("%d\n", ans);
	return 0;
}

詳細信息

Test #1:

score: 5
Accepted
time: 1ms
memory: 8024kb

input:

5 5
2 2 1 3 3
1 1
2 -1
1 1
5 -1
5 1

output:

63

result:

ok 1 number(s): "63"

Test #2:

score: 5
Accepted
time: 0ms
memory: 7984kb

input:

5 5
2 2 2 3 2
5 -1
5 1
2 1
2 1
5 1

output:

108

result:

ok 1 number(s): "108"

Test #3:

score: 5
Accepted
time: 1ms
memory: 7964kb

input:

5 5
3 3 1 3 2
4 1
4 -1
1 -1
3 -1
2 1

output:

150

result:

ok 1 number(s): "150"

Test #4:

score: 5
Accepted
time: 0ms
memory: 7896kb

input:

100 3
8 7 6
1 1
1 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
1 1
1 -1
1 1
1 -1
2 1
2 -1
1 1
1 -1
2 1
2 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
2 1
2 -1
3 1
3 -1
3 1
3 -1
1 1
1 -1
1 1
1 -1
3 1
3 -1
3 1
3 -1
1 1
1 -1
1 1
1 -1
2 1
2 -1
1 1
1 -1
2 1
2 -1
2 1
2 -1
2 1
2 -1
3 1
3 -1
1 1
1 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
...

output:

-1

result:

ok 1 number(s): "-1"

Test #5:

score: 5
Accepted
time: 1ms
memory: 5948kb

input:

100 3
6 7 5
1 1
2 1
3 1
2 -1
2 -1
3 -1
3 -1
2 -1
3 1
2 -1
2 -1
2 1
1 -1
1 1
2 -1
2 -1
2 1
3 -1
3 -1
1 -1
2 1
3 -1
3 -1
1 -1
3 1
3 1
1 1
3 -1
1 1
1 1
1 -1
1 1
3 1
2 -1
2 1
2 1
1 -1
2 1
1 -1
2 1
3 -1
2 -1
1 -1
3 1
1 -1
2 1
2 -1
1 -1
2 1
2 -1
3 -1
1 1
3 -1
1 1
1 -1
3 1
1 1
2 1
3 1
2 1
2 1
2 -1
2 1
3 1
...

output:

1445

result:

ok 1 number(s): "1445"

Test #6:

score: 5
Accepted
time: 1ms
memory: 7840kb

input:

100 3
5 7 6
3 1
1 1
2 -1
2 -1
2 1
2 1
1 1
1 -1
2 1
2 -1
3 1
2 1
3 -1
2 -1
1 1
1 -1
1 -1
3 1
2 1
1 1
1 1
2 1
1 1
1 1
2 1
1 1
3 -1
3 1
3 1
3 1
2 1
3 1
3 -1
3 -1
3 1
2 1
1 -1
1 1
1 1
3 -1
1 -1
3 -1
3 -1
2 1
2 1
3 -1
2 -1
2 -1
3 -1
1 1
1 -1
1 -1
3 1
2 -1
2 1
3 -1
1 -1
2 1
2 -1
1 -1
1 -1
2 1
3 -1
1 1
3 1...

output:

1823

result:

ok 1 number(s): "1823"

Test #7:

score: 5
Accepted
time: 9ms
memory: 8420kb

input:

100000 1
84020
1 -1
1 -1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 -1
1 1
1 1
1 1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 -1
1 -1
1 -1
1 1
1 -1
1 -1
1 1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 1
1...

output:

333173136

result:

ok 1 number(s): "333173136"

Test #8:

score: 5
Accepted
time: 10ms
memory: 8312kb

input:

100000 1
74078
1 -1
1 1
1 1
1 -1
1 -1
1 -1
1 1
1 1
1 1
1 1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 -1
1 -1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 1
1 -1
1 1
1 -1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 1
1 -1
1 1
1 -1
1 -1
1 1
...

output:

453472675

result:

ok 1 number(s): "453472675"

Test #9:

score: 5
Accepted
time: 9ms
memory: 8284kb

input:

100000 2
788727 548098
1 -1
2 1
2 -1
2 1
1 -1
1 1
2 -1
2 -1
1 -1
2 1
2 -1
2 1
2 -1
1 -1
2 -1
2 1
1 -1
2 1
1 -1
1 -1
1 1
1 -1
1 -1
2 -1
1 1
1 -1
2 1
2 1
1 -1
1 1
1 -1
1 1
2 -1
2 -1
1 -1
2 -1
2 1
2 -1
2 -1
2 -1
1 -1
2 -1
2 1
2 1
1 1
1 1
2 1
1 -1
2 -1
2 1
1 -1
1 -1
1 -1
2 -1
1 1
1 -1
1 1
2 1
2 1
1 1
1 ...

output:

433871022

result:

ok 1 number(s): "433871022"

Test #10:

score: 5
Accepted
time: 4ms
memory: 8000kb

input:

100000 2
851838 526074
1 1
1 1
1 1
1 -1
2 -1
2 -1
1 1
2 1
2 1
2 1
2 1
1 1
1 -1
2 1
1 1
2 1
1 -1
2 -1
1 -1
2 1
2 1
2 -1
1 -1
2 -1
2 -1
2 -1
1 -1
2 -1
2 1
2 -1
2 1
2 -1
2 -1
2 1
2 1
2 -1
2 -1
1 -1
2 -1
1 -1
1 -1
1 -1
2 1
1 -1
1 1
2 -1
2 -1
1 1
2 1
2 -1
2 1
1 -1
2 -1
1 1
2 1
2 -1
1 -1
1 -1
1 1
2 -1
2 1...

output:

635878930

result:

ok 1 number(s): "635878930"

Test #11:

score: 5
Accepted
time: 9ms
memory: 8388kb

input:

100000 2
936902 869936
1 -1
1 -1
1 -1
2 -1
2 1
1 -1
2 1
2 1
2 -1
1 1
2 -1
2 -1
1 -1
2 -1
1 -1
1 -1
1 -1
2 -1
2 1
2 1
2 -1
2 -1
2 1
1 1
1 -1
1 -1
2 -1
1 -1
2 -1
1 -1
2 -1
1 -1
2 1
2 -1
1 -1
2 1
2 1
1 1
1 -1
1 1
2 -1
2 1
2 -1
2 -1
1 -1
2 1
1 1
1 1
1 1
1 -1
2 1
2 1
1 -1
1 -1
2 -1
2 -1
2 1
1 1
2 -1
1 -1...

output:

442317474

result:

ok 1 number(s): "442317474"

Test #12:

score: 5
Accepted
time: 3ms
memory: 8228kb

input:

100000 2
696105 696736
2 1
1 -1
1 -1
1 1
1 1
1 -1
1 -1
2 1
2 -1
2 -1
1 -1
1 1
1 -1
2 1
2 -1
1 1
2 -1
1 -1
1 -1
1 1
1 -1
2 -1
1 1
2 1
1 1
1 -1
1 1
1 -1
2 1
1 -1
2 1
1 1
1 1
2 -1
1 -1
2 1
1 1
1 -1
2 1
1 -1
2 1
2 1
1 -1
1 -1
1 -1
2 1
2 1
2 1
1 -1
1 1
2 1
2 -1
2 -1
2 -1
2 -1
2 -1
2 1
1 1
2 1
2 -1
1 -1
1...

output:

564885404

result:

ok 1 number(s): "564885404"

Test #13:

score: 5
Accepted
time: 32ms
memory: 8788kb

input:

500000 10
755576 503368 607237 931815 889701 742191 594456 676526 704905 569952
9 1
9 -1
8 1
8 -1
4 1
4 -1
7 1
7 -1
3 1
3 -1
1 1
1 -1
6 1
6 -1
1 1
1 -1
10 1
10 -1
2 1
2 -1
1 1
1 -1
2 1
2 -1
5 1
5 -1
4 1
4 -1
3 1
3 -1
7 1
7 -1
3 1
3 -1
4 1
4 -1
6 1
6 -1
7 1
7 -1
4 1
4 -1
1 1
1 -1
2 1
2 -1
5 1
5 -1
6 ...

output:

-1

result:

ok 1 number(s): "-1"

Test #14:

score: 5
Accepted
time: 60ms
memory: 8260kb

input:

500000 10
843479 559917 806296 766435 965493 781368 889933 632099 689781 934269
3 1
10 1
3 1
3 -1
6 1
7 -1
8 1
9 1
4 1
2 -1
5 1
2 -1
5 1
8 -1
4 -1
10 -1
8 -1
7 -1
1 -1
3 -1
5 1
6 1
3 -1
6 -1
3 1
10 -1
5 1
4 1
2 1
10 -1
5 -1
4 1
5 -1
4 -1
5 -1
4 1
3 -1
8 1
5 1
3 -1
1 -1
5 1
2 -1
8 1
5 1
9 -1
4 -1
10 ...

output:

212475448

result:

ok 1 number(s): "212475448"

Test #15:

score: 5
Accepted
time: 59ms
memory: 8900kb

input:

500000 10
564550 662731 907937 653446 887637 552698 501487 502890 574491 689451
8 -1
2 1
2 -1
10 -1
5 -1
10 1
9 1
8 1
6 1
6 1
2 -1
2 -1
9 -1
1 -1
9 -1
7 -1
8 1
9 -1
9 1
9 -1
6 1
10 1
1 -1
8 -1
10 -1
10 1
7 1
4 1
6 1
1 -1
2 -1
2 1
5 -1
2 -1
7 1
5 -1
8 -1
5 1
7 -1
2 -1
3 1
9 1
3 -1
7 1
8 1
5 -1
1 1
6 ...

output:

27023740

result:

ok 1 number(s): "27023740"

Test #16:

score: 5
Accepted
time: 55ms
memory: 7860kb

input:

500000 10
918488 957499 964399 900665 976079 674192 501308 980114 958902 545155
6 1
1 -1
1 1
9 1
1 1
5 -1
4 1
1 -1
9 -1
6 1
1 -1
6 1
10 1
5 -1
8 1
10 1
2 1
7 1
5 -1
6 -1
10 -1
1 -1
4 -1
6 -1
4 1
10 -1
10 -1
6 -1
4 -1
7 -1
4 1
6 1
6 -1
10 1
9 1
4 -1
4 1
3 -1
7 -1
1 -1
5 1
8 1
10 1
2 1
2 -1
2 1
3 1
6 ...

output:

128050940

result:

ok 1 number(s): "128050940"

Test #17:

score: 5
Accepted
time: 40ms
memory: 8444kb

input:

500000 3
826619243 796070724 841056572
3 1
2 1
3 -1
1 -1
2 1
2 -1
1 -1
1 1
1 -1
3 1
1 -1
2 -1
3 -1
3 1
1 -1
1 1
3 -1
2 1
2 1
3 -1
2 1
2 1
1 -1
1 1
1 1
1 -1
2 1
3 -1
2 -1
2 1
3 1
1 1
1 -1
3 1
2 -1
1 -1
2 1
2 1
2 -1
3 -1
1 -1
1 -1
2 -1
3 -1
1 -1
3 -1
2 1
2 -1
3 1
1 -1
1 1
1 1
1 1
1 1
3 -1
1 1
3 1
2 1
...

output:

953829771

result:

ok 1 number(s): "953829771"

Test #18:

score: 5
Accepted
time: 41ms
memory: 9000kb

input:

500000 3
956491428 866109891 631435277
2 1
1 -1
1 -1
3 -1
3 1
2 -1
2 1
1 -1
2 1
2 -1
1 -1
2 1
2 1
2 -1
1 1
3 1
3 -1
1 -1
1 1
1 1
3 -1
2 -1
1 1
1 1
2 -1
2 1
2 -1
2 -1
2 1
3 1
1 -1
2 -1
1 -1
2 1
3 1
1 1
3 -1
1 -1
2 -1
3 1
2 1
3 1
3 1
2 1
3 -1
1 -1
3 -1
2 1
1 1
2 1
3 -1
3 1
1 -1
3 -1
1 1
2 -1
2 -1
2 1
...

output:

286394049

result:

ok 1 number(s): "286394049"

Test #19:

score: 5
Accepted
time: 40ms
memory: 8540kb

input:

500000 3
816766322 779617142 794227651
3 1
2 1
1 1
3 -1
2 1
2 -1
1 1
1 1
1 1
3 -1
1 -1
2 1
3 1
1 -1
1 -1
1 1
3 1
3 1
1 -1
3 -1
2 1
1 -1
2 1
3 1
1 1
2 -1
3 -1
2 -1
1 -1
3 -1
1 1
3 -1
3 1
1 -1
3 1
2 -1
2 -1
1 -1
3 1
3 1
2 -1
3 1
3 -1
1 1
1 -1
3 1
1 1
2 1
2 1
1 1
2 1
1 -1
2 1
2 -1
2 1
3 -1
3 1
1 1
3 -1...

output:

950925221

result:

ok 1 number(s): "950925221"

Test #20:

score: 5
Accepted
time: 40ms
memory: 8840kb

input:

500000 3
600925621 781710332 540983402
1 -1
1 1
2 1
3 1
1 1
1 1
3 1
3 1
2 -1
3 -1
3 1
1 -1
1 1
2 -1
1 -1
1 -1
3 -1
2 1
1 1
1 -1
1 -1
1 1
3 -1
2 -1
2 -1
3 1
1 1
1 -1
3 1
2 1
1 -1
2 1
1 -1
3 -1
3 -1
2 -1
3 -1
2 1
3 -1
1 1
2 1
2 1
3 -1
2 1
1 1
1 1
3 1
2 -1
1 1
3 1
2 -1
3 -1
3 1
2 -1
3 -1
1 1
1 1
1 -1
3...

output:

370329204

result:

ok 1 number(s): "370329204"

Extra Test:

score: 0
Extra Test Passed