E. ABBA
题目描述
Bobo has a string of length
2
(
n
+
m
)
2(n + m)
2(n+m) which consists of characters A
and B
. The string also has a fascinating property: it can be decomposed into
(
n
+
m
)
(n + m)
(n+m) sub-sequences of length
2
2
2, and among the
(
n
+
m
)
(n + m)
(n+m) subsequences
n
n
n of them are AB
while other
m
m
m of them are BA
.
Given n and m, find the number of possible strings modulo ( 1 0 9 + 7 ) (10^{9}+7) (109+7).
题解
首先研究合法串 s 的性质。
s由
(
n
+
m
)
(n+m)
(n+m)个A
和
(
n
+
m
)
(n+m)
(n+m)个B
组成,因此s最多能分解为n+m个AB
或BA
。
由于s能分解为n个AB
和m个BA
,故考虑一种分解方式:
s中前n个
A
用于AB
,之后m个A
用于BA
,B
同理。
考虑到s的任意合法分解方式都能转化为上述分解方式,可以以该分解方式为基础对s计数:
f [ i ] [ j ] f[i][j] f[i][j] 表示s的前缀的方案数,这些前缀都由 i i i 个
A
和 j j j 个B
组成。
考虑第 ( i + j + 1 ) (i+j+1) (i+j+1) 位 (以第1位为始) 为A
时:
- 若 i < n i<n i<n ,则该位的
A
与之后的B
组成AB
。- 若 i > = n i>=n i>=n ,则该位的
A
与之前的B
组成BA
。
此时必须保证该位之前还有剩余的B
相匹配,即: j > i − n j > i-n j>i−n 。故 i < n ∣ ∣ j > i − n i < n || j > i-n i<n∣∣j>i−n 时, f [ i + 1 ] [ j ] + = f [ i ] [ j ] f[i+1][j] += f[i][j] f[i+1][j]+=f[i][j]
第 ( i + j + 1 ) (i+j+1) (i+j+1) 位 (以第1位为始) 为
B
时,同理有:
当 j < m ∣ ∣ i > j − m j < m || i > j-m j<m∣∣i>j−m 时, f [ i ] [ j + 1 ] + = f [ i ] [ j ] f[i][j+1] += f[i][j] f[i][j+1]+=f[i][j]
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 2, mod = 1e9 + 7;
int n, m;
int f[N][N];
int main()
{
while(scanf("%d%d", &n, &m) == 2)
{
int len = n + m;
for(int i = 0; i <= len; ++i)
for(int j = 0; j <= len; ++j)
f[i][j] = 0;
f[0][0] = 1;
for(int i = 0; i <= len; ++i)
for(int j = 0; j <= len; ++j) {
if((i < n || j > i - n) && ((f[i+1][j] += f[i][j]) >= mod)) f[i+1][j] -= mod;
if((j < m || i > j - m) && ((f[i][j+1] += f[i][j]) >= mod)) f[i][j+1] -= mod;
}
printf("%d\n", f[len][len]);
}
return 0;
}
F. Random Point in Triangle
题目描述
Bobo has a triangle ABC with A ( x 1 , y 1 ) A(x_1, y_1) A(x1,y1), B ( x 2 , y 2 ) B(x_2, y_2) B(x2,y2) and C ( x 3 , y 3 ) C(x_3, y_3) C(x3,y3). Picking a point P P P uniformly in triangle A B C ABC ABC, he wants to know the expectation value E ( m a x { S P A B , S P B C , S P C A } ) E(max\{S_{PAB},S_{PBC},S_{PCA}\}) E(max{SPAB,SPBC,SPCA}) where S X Y Z S_{XYZ} SXYZ denotes the area of triangle X Y Z XYZ XYZ.
Print the value of 36 × E 36\times E 36×E . It can be proved that it is always an integer.
题解
重心、两条中线、顶点及两边围成的区域对应的底边为同一条。
记
S
A
B
C
S_{ABC}
SABC 为
S
S
S ,积分得
E
=
22
×
S
E=22 \times S
E=22×S 。已知三顶点坐标,用叉乘法计算三角形面即可。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read()
{
ll x = 0; char ch = getchar(); bool f = 1;
for (; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') f = 0;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
return f ? x : -x;
}
int main()
{
int x1, y1, x2, y2, x3, y3;
while(scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3) == 6) {
ll a = x2 - x1, b = y2 - y1;
ll c = x3 - x1, d = y3 - y1;
ll ans = abs(a * d - b * c);
printf("%lld\n", ans * 11);
}
return 0;
}
J.
题目描述
Bobo has two fractions x a \frac{x}{a} ax and y b \frac{y}{b} by . He wants to compare them. Find the result.
题解
签到题。唯一的难点是爆long long 会python。
代码
while True:
try:
arr = input().split(' ')
x = int(arr[0])
a = int(arr[1])
y = int(arr[2])
b = int(arr[3])
if x * b > y * a :
print('>')
elif x * b == y * a :
print('=')
else :
print('<')
except EOFError:
break