#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#include <sstream>
#include <iterator>
#define MOD ((i64)(1e9+7))
using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
// table[r][h] = R이 r개, H가 h개 있을 때 둘다 0개가 될 확률
ii64 table[5001][5001];
ii64 flowerp, handp;
int a, b;
int n, r, h;
string s;
i64 ipow(i64 x, i64 k)
{
if (k == 0)
return 1;
i64 half = ipow(x, k / 2);
half = (half * half) % MOD;
if (k % 2 == 0)
return half;
else
return (half * x) % MOD;
}
ii64 calcLeft(int i)
{
if (i == -1)
{
if (b == 0)
return ii64(1, 1);
return ii64(0, 1);
}
if (s[i] != 'H')
return ii64(0, 1);
int leftmost = -1;
for (int j = 0; j <= i; j++)
{
if (s[j] == 'R')
{
leftmost = j;
break;
}
}
ii64 leftp = ii64(1, 1);
int hl = 0, hr = 0;
int remainr = 0;
for (int j = 0; j <= i; j++)
{
if (s[j] != 'H')
{
remainr++;
continue;
}
if (j <= leftmost)
hl++;
else
hr++;
}
if (leftmost == -1)
{
if (hr != b)
return ii64(0, 1);
}
else
{
int remain = b - hl;
if (remain < 0)
return ii64(0, 1);
int need = hr - remain;
if (need == hr || need < 0)
return ii64(0, 1);
leftp = table[remainr][need];
}
return leftp;
}
ii64 calcRight(int i)
{
if (i == s.size())
{
if (a == 0)
return ii64(1, 1);
return ii64(0, 1);
}
if (s[i] != 'R')
return ii64(0, 1);
ii64 rightp(1, 1);
int rightmost = -1;
for (int j = n - 1; j >= i; j--)
{
if (s[j] == 'H')
{
rightmost = j;
break;
}
}
int rl = 0, rr = 0;
int remainh = 0;
for (int j = n - 1; j >= i; j--)
{
if (s[j] != 'R')
{
remainh++;
continue;
}
if (j >= rightmost)
rr++;
else
rl++;
}
if (rightmost == -1)
{
if (rr != a)
return ii64(0, 1);
}
else
{
int remain = a - rr;
if (remain < 0)
return ii64(0, 1);
int need = rl - remain;
if (need == rl || need < 0)
return ii64(0, 1);
rightp = table[need][remainh];
}
return rightp;
}
int main()
{
scanf("%d %d %d", &n, &r, &h);
cin >> s;
table[0][0].first = table[0][0].second = 1;
flowerp.first = h;
flowerp.second = r + h;
handp.first = r;
handp.second = r + h;
for(int flower = 0; flower <= n; flower ++)
{
for(int hand = 0; hand <= n; hand++)
{
if (flower == 0 && hand == 0)
continue;
table[flower][hand].first = 0;
table[flower][hand].second = 1;
if (flower > 0)
{
table[flower][hand].first = (handp.first * table[flower - 1][hand].first) % MOD;
table[flower][hand].second = (handp.second * table[flower - 1][hand].second) % MOD;
}
if (hand > 0)
{
ii64 f(flowerp.first * table[flower][hand - 1].first % MOD, flowerp.second * table[flower][hand - 1].second % MOD);
table[flower][hand].first =
(table[flower][hand].first * f.second + table[flower][hand].second * f.first) % MOD;
table[flower][hand].second = (table[flower][hand].second * f.second) % MOD;
}
}
}
scanf("%d %d", &a, &b);
ii64 ans(0, 1);
for (int i = 0; i <= s.size(); i++)
{
auto leftp = calcLeft(i - 1);
auto rightp = calcRight(i);
ii64 p(leftp.first * rightp.first % MOD, leftp.second * rightp.second % MOD);
ans.first = ((ans.first * p.second) %MOD + (ans.second * p.first) % MOD) %MOD;
ans.second = (ans.second * p.second) % MOD;
}
printf("%lld\n", (ans.first * ipow(ans.second, MOD - 2) % MOD));
return 0;
}
Compilation message
dstorv.cpp: In function 'ii64 calcRight(int)':
dstorv.cpp:117:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i == s.size())
~~^~~~~~~~~~~
dstorv.cpp: In function 'int main()':
dstorv.cpp:225:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i <= s.size(); i++)
~~^~~~~~~~~~~
dstorv.cpp:182: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:222:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Incorrect |
2 ms |
408 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Incorrect |
2 ms |
408 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |