답안 #483612

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483612 2021-10-31T06:25:46 Z ntabc05101 Ljetopica (COI19_ljetopica) C++14
100 / 100
81 ms 23260 KB
#include<bits/stdc++.h>
using namespace std;

#define taskname ""

const int mod = 1e9 + 7;
const int mxN = 1005;

int n, k;
string str;
//int pw2[mxN];

template<class U, class T>
void add(U &a, T b) {
	a += b;
	if (a >= mod) {
		a -= mod;
	}
}

int sol(string &A) {
	if (!A[0]) {
		return 0;
	}
	int dp[n][k + 1][2];
	long long dp2[n][k + 1][2];

	int res = 0;
	for (bool e: {0, 1}) {
		memset(dp, 0, sizeof(dp));
		memset(dp2, 0, sizeof(dp2));
		dp[0][0][1] = 1;
		dp2[0][0][1] = 1;
		for (int i = 0; i < n - 1; i++) {
			for (int j = 0; j <= k; j++) {
				for (int f: {0, 1}) {
					if (j + f > k) {
						continue;
					}
					int g = str[i] ^ ((j + f) & 1);
					add(dp[i + 1][j + f][0], dp[i][j][0]);
					add(dp2[i + 1][j + f][0], (dp2[i][j][0] * 2 + dp[i][j][0] * g) % mod);
					if (g < A[i + 1]) {
						add(dp[i + 1][j + f][0], dp[i][j][1]);
						add(dp2[i + 1][j + f][0], (dp2[i][j][1] * 2 + dp[i][j][1] * g) % mod);
					}
					else {
						if (g == A[i + 1]) {
							add(dp[i + 1][j + f][1], dp[i][j][1]);
							add(dp2[i + 1][j + f][1], (dp2[i][j][1] * 2 + dp[i][j][1] * g) % mod);
						}
					}
					//cout << i + 1 << " " << j + f << " " << dp[i + 1][j + f][0] << " " << dp[i + 1][j + f][1] << "\n";
				}
			}
		}
		add(res, dp2[n - 1][k][0]);
		add(res, dp2[n - 1][k][1]);
		for (auto& c: str) {
			c ^= 1;
		}
	}

	//cout << res << "\n";
	return res;
}

int main() {
  if (fopen(taskname".inp", "r")) {
    freopen(taskname".inp", "r", stdin);
    freopen(taskname".out", "w", stdout);
  }
  else {
    if (fopen(taskname".in", "r")) {
      freopen(taskname".in", "r", stdin);
      freopen(taskname".out", "w", stdout);
    }
  }

  cin.tie(0)->sync_with_stdio(0);
	
	cin >> n >> k;
	/*pw2[0] = 1;
	for (int i = 1; i <= n; i++) {
		pw2[i] = pw2[i - 1] * 2 % mod;
	}*/
	cin >> str;
	string A, B; cin >> A >> B;
	for (auto &c: A) {
		c -= '0';
	}
	for (auto &c: B) {
		c -= '0';
	}

	int i;
	for (i = A.size() - 1; (~i) && !A[i]; i--) {
		A[i] = 1;
	}
	A[i] = 0;

	//for (auto &c: A) cout << char(c + '0'); cout << "\n";

	for (auto &c: str) {
		c = (c == 'R');
	}

	cout << (sol(B) - sol(A) + mod) % mod << "\n";

  return 0;
}

Compilation message

ljetopica.cpp: In function 'int sol(std::string&)':
ljetopica.cpp:29:12: warning: unused variable 'e' [-Wunused-variable]
   29 |  for (bool e: {0, 1}) {
      |            ^
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen(taskname".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
ljetopica.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen(taskname".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ljetopica.cpp:75:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |       freopen(taskname".in", "r", stdin);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
ljetopica.cpp:76:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |       freopen(taskname".out", "w", stdout);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 316 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 220 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 13772 KB Output is correct
2 Correct 39 ms 9548 KB Output is correct
3 Correct 42 ms 10956 KB Output is correct
4 Correct 79 ms 22336 KB Output is correct
5 Correct 31 ms 9036 KB Output is correct
6 Correct 81 ms 23260 KB Output is correct
7 Correct 19 ms 5668 KB Output is correct
8 Correct 39 ms 10700 KB Output is correct
9 Correct 3 ms 716 KB Output is correct
10 Correct 42 ms 9908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 316 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 220 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 68 ms 13772 KB Output is correct
25 Correct 39 ms 9548 KB Output is correct
26 Correct 42 ms 10956 KB Output is correct
27 Correct 79 ms 22336 KB Output is correct
28 Correct 31 ms 9036 KB Output is correct
29 Correct 81 ms 23260 KB Output is correct
30 Correct 19 ms 5668 KB Output is correct
31 Correct 39 ms 10700 KB Output is correct
32 Correct 3 ms 716 KB Output is correct
33 Correct 42 ms 9908 KB Output is correct
34 Correct 63 ms 16932 KB Output is correct
35 Correct 19 ms 5964 KB Output is correct
36 Correct 35 ms 9660 KB Output is correct
37 Correct 76 ms 21452 KB Output is correct
38 Correct 10 ms 2976 KB Output is correct
39 Correct 67 ms 18764 KB Output is correct
40 Correct 10 ms 2508 KB Output is correct
41 Correct 44 ms 12384 KB Output is correct
42 Correct 78 ms 15812 KB Output is correct
43 Correct 62 ms 15904 KB Output is correct
44 Correct 66 ms 18716 KB Output is correct
45 Correct 18 ms 5452 KB Output is correct
46 Correct 55 ms 14540 KB Output is correct
47 Correct 57 ms 15436 KB Output is correct
48 Correct 34 ms 8968 KB Output is correct
49 Correct 2 ms 844 KB Output is correct
50 Correct 64 ms 17816 KB Output is correct
51 Correct 27 ms 8140 KB Output is correct
52 Correct 29 ms 8632 KB Output is correct
53 Correct 78 ms 21708 KB Output is correct
54 Correct 19 ms 5928 KB Output is correct
55 Correct 63 ms 17340 KB Output is correct
56 Correct 71 ms 19740 KB Output is correct
57 Correct 7 ms 2252 KB Output is correct
58 Correct 62 ms 15376 KB Output is correct