Submission #73804

# Submission time Handle Problem Language Result Execution time Memory
73804 2018-08-29T04:32:59 Z cki86201 Dstorv (FXCUP3_dstorv) C++11
100 / 100
199 ms 100992 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ldouble;

const int MOD = 1e9 + 7;
int N, r, h, A, B;
char X[5050];

int pw(int x, int y = MOD - 2) {
	int res = 1;
	while(y) {
		if(y & 1) res = (ll) res * x % MOD;
		x = (ll) x * x % MOD;
		y >>= 1;
	}
	return res;
}

char Y[5050];
int D[5050][5050], dl[5050], dr[5050];

void add(int &x, int y) { x = (x + y) % MOD; }

void get_dp(char X[], int N, int dp[], int S) {
	if(S == 0) { dp[0] = 1; return; }
	int height[5050] = {}, hc = 0, c = 0, idxs[5050];
	for(int i=1;i<=N;i++) {
		if(X[i] == 'H') {
			height[++hc] = c;
			idxs[hc] = i;
		}
		else ++c;
	}
	memset(D, 0, sizeof D);
	D[S][1] = 1;
	for(int i=S;i<=hc;i++) {
		for(int j=1;j<=height[i];j++) { add(D[i][j], D[i][j-1]); add(D[i][j], D[i-1][j]); }
	}
	for(int i=S;i<=hc;i++) {
		dp[idxs[i]] = D[i][height[i]];
	}
}

int main() {
	scanf("%d%d%d", &N, &r, &h);
	int tr = r * (ll) pw(r + h) % MOD;
	int th = h * (ll) pw(r + h) % MOD;
	r = tr; h = th;
	scanf("%s", X);
	scanf("%d%d", &A, &B);
	int lv = 0, rv = N - 1;
	while(lv < N && X[lv] == 'H') ++lv, --B;
	while(rv >= 0 && X[rv] == 'R') --rv, --A;
	if(lv == N) {
		printf("%d\n", (A == 0 && B == 0) ? 1 : 0);
	}
	else if(rv == -1) {
		printf("%d\n", (A == 0 && B == 0) ? 1 : 0);
	}
	else {
		if(A < 0 || B < 0) { puts("0"); return 0; }
		for(int i=lv;i<=rv;i++) Y[i-lv+1] = X[i];
		N = rv - lv + 1;
		get_dp(Y, N, dl, B);
		reverse(Y+1, Y+1+N);
		for(int i=1;i<=N;i++) Y[i] = (Y[i] == 'R' ? 'H' : 'R');
		get_dp(Y, N, dr, A);
		reverse(dr, dr+2+N);
		int ans = 0;
		for(int i=0;i<=N;i++) add(ans, (ll)dl[i] * dr[i + 1] % MOD);
		int ch = 0, cr = 0;
		for(int i=1;i<=N;i++) if(Y[i] == 'R') ++ch;
		cr = N - ch;
		rep(i, cr - A) ans = (ll)ans * r % MOD;
		rep(i, ch - B) ans = (ll)ans * h % MOD;
		printf("%d\n", ans);
	}
	return 0;
}

Compilation message

dstorv.cpp: In function 'int main()':
dstorv.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &N, &r, &h);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
dstorv.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", X);
  ~~~~~^~~~~~~~~
dstorv.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &A, &B);
  ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 83 ms 100324 KB Output is correct
3 Correct 2 ms 100324 KB Output is correct
4 Correct 84 ms 100324 KB Output is correct
5 Correct 104 ms 100324 KB Output is correct
6 Correct 106 ms 100360 KB Output is correct
7 Correct 128 ms 100368 KB Output is correct
8 Correct 86 ms 100420 KB Output is correct
9 Correct 83 ms 100540 KB Output is correct
10 Correct 116 ms 100548 KB Output is correct
11 Correct 109 ms 100548 KB Output is correct
12 Correct 3 ms 100548 KB Output is correct
13 Correct 3 ms 100548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 100548 KB Output is correct
2 Correct 108 ms 100548 KB Output is correct
3 Correct 107 ms 100548 KB Output is correct
4 Correct 103 ms 100548 KB Output is correct
5 Correct 166 ms 100548 KB Output is correct
6 Correct 3 ms 100548 KB Output is correct
7 Correct 3 ms 100548 KB Output is correct
8 Correct 3 ms 100548 KB Output is correct
9 Correct 117 ms 100652 KB Output is correct
10 Correct 97 ms 100876 KB Output is correct
11 Correct 114 ms 100876 KB Output is correct
12 Correct 123 ms 100876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 83 ms 100324 KB Output is correct
3 Correct 2 ms 100324 KB Output is correct
4 Correct 84 ms 100324 KB Output is correct
5 Correct 104 ms 100324 KB Output is correct
6 Correct 106 ms 100360 KB Output is correct
7 Correct 128 ms 100368 KB Output is correct
8 Correct 86 ms 100420 KB Output is correct
9 Correct 83 ms 100540 KB Output is correct
10 Correct 116 ms 100548 KB Output is correct
11 Correct 109 ms 100548 KB Output is correct
12 Correct 3 ms 100548 KB Output is correct
13 Correct 3 ms 100548 KB Output is correct
14 Correct 84 ms 100548 KB Output is correct
15 Correct 108 ms 100548 KB Output is correct
16 Correct 107 ms 100548 KB Output is correct
17 Correct 103 ms 100548 KB Output is correct
18 Correct 166 ms 100548 KB Output is correct
19 Correct 3 ms 100548 KB Output is correct
20 Correct 3 ms 100548 KB Output is correct
21 Correct 3 ms 100548 KB Output is correct
22 Correct 117 ms 100652 KB Output is correct
23 Correct 97 ms 100876 KB Output is correct
24 Correct 114 ms 100876 KB Output is correct
25 Correct 123 ms 100876 KB Output is correct
26 Correct 112 ms 100876 KB Output is correct
27 Correct 142 ms 100876 KB Output is correct
28 Correct 147 ms 100876 KB Output is correct
29 Correct 183 ms 100876 KB Output is correct
30 Correct 181 ms 100876 KB Output is correct
31 Correct 181 ms 100876 KB Output is correct
32 Correct 105 ms 100876 KB Output is correct
33 Correct 173 ms 100892 KB Output is correct
34 Correct 166 ms 100900 KB Output is correct
35 Correct 154 ms 100928 KB Output is correct
36 Correct 159 ms 100928 KB Output is correct
37 Correct 3 ms 100928 KB Output is correct
38 Correct 3 ms 100928 KB Output is correct
39 Correct 3 ms 100928 KB Output is correct
40 Correct 132 ms 100928 KB Output is correct
41 Correct 196 ms 100992 KB Output is correct
42 Correct 199 ms 100992 KB Output is correct
43 Correct 142 ms 100992 KB Output is correct
44 Correct 124 ms 100992 KB Output is correct
45 Correct 198 ms 100992 KB Output is correct