CF_GoodBye2017简单题解

题目传送门

A.New Year and Counting Cards

签到题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char s[maxn];
int main(){
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
scanf("%s",s);
int n = strlen(s);
int t = 0;
for(int i = 0;i < n; i++) {
if(s[i] >= 'a' && s[i] <= 'z') {
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') t++;
} else {
if(s[i] == '1' || s[i] == '3' || s[i] == '5' || s[i] == '7' || s[i] == '9') t++;
}
}
printf("%d\n",t);
return 0;
}

B.New Year and Buggy Bot

签到题……手残打错一个小于号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
char p[55][55];
char s[maxn];
int n,m,f[4],d[maxn];
bool check(int w) {
int i,j,k;
for(i = 0;i < n; i++) {
for(j = 0;j < m; j++) {
if(p[i][j] == 'S') break;
}
if(j < m) break;
}
for(k = 0;k < w; k++) {
if(d[k] == 0) i++;
else if(d[k] == 1) i--;
else if(d[k] == 2) j++;
else j--;
if(i < 0 || i == n || j < 0 || j == m) return false;
if(p[i][j] == '#') return false;
if(p[i][j] == 'E') return true;
}
return false;
}
int main(){
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int w,i,ans = 0;
scanf("%d%d",&n,&m);
for(i = 0;i < n; i++) scanf("%s",p[i]);
scanf("%s",s);
w = strlen(s);
for(i = 0;i < 4; i++) f[i] = i;
do {
for(i = 0;i < w; i++) d[i] = f[s[i] - '0'];
if(check(w)) ans++;
} while(next_permutation(f,f + 4));
printf("%d\n",ans);
return 0;
}

C.New Year and Curling

水题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
double fy[1005];
int dx[1005];
int main(){
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n,r,i,j;
double y,yy;
scanf("%d%d",&n,&r);
for(i = 0;i < n; i++) {
scanf("%d",&dx[i]);
y = r;
for(j = 0;j < i; j++) {
if(abs(dx[i] - dx[j]) <= 2 * r) {
yy = sqrt(4.0 * r * r - 1.0 * (dx[i] - dx[j]) * (dx[i] - dx[j]));
y = max(y,fy[j] + yy);
}
}
fy[i] = y;
}
for(i = 0;i < n; i++) printf("%.10f ",fy[i]);
return 0;
}

D.New Year and Arbitrary Arrangement

$f[i][j]$表示有$i$个$a$字符并且当前已经有$j$个$ab$序列的概率
当$i+j >= k$的时候,再加一个$b$字符就肯定结束,可以直接推出加一个$b$字符产生的期望贡献
所以$dp$总复杂度也就$k^{2}$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//created by missever
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int add(int x,int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
int mul(int x,int y) {
LL z = 1LL * x * y;
return z - z / mod * mod;
}
int powt(int a,int b) {
int r = 1;
while(b) {
if(b & 1) r= mul(r,a);
a = mul(a,a);
b >>= 1;
}
return r;
}
int f[2005][2005];
int main(){
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int i,j,k,pa,pb,p,inv,w,ans = 0;
scanf("%d%d%d",&k,&pa,&pb);
p = pa + pb;
inv = powt(p,mod - 2);
w = add(mul(p,powt(pb,mod - 2)),mod - 1);
f[1][0] = 1;
for(i = 1;i <= 2000; i++) {
for(j = 0;j <= 2000; j++) {
if(f[i][j]) {
if(i + j >= k) ans = add(ans,mul(f[i][j],add(i + j,w)));
else {
f[i + 1][j] = add(f[i + 1][j],mul(f[i][j],mul(pa,inv)));
f[i][i + j] = add(f[i][i + j],mul(f[i][j],mul(pb,inv)));
}
}
}
}
printf("%d\n",ans);
return 0;
}

E.New Year and Entity Enumeration

把状态相同的列放到一组,每组的答案就是非空集合的划分方案数,即贝尔数
最后乘起来就好了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//created by missever
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
int mul(int x, int y) {
LL z = 1LL * x * y;
return z - z / mod * mod;
}
int powt(int a, int b) {
int r = 1;
while(b) {
if(b & 1) r = mul(r, a);
a = mul(a, a);
b >>= 1;
}
return r;
}
char s[1005];
char f[1005][55];
int g[1005];
int c[1005],b[1005];
int C(int x,int y) {
return mul(c[x],powt(mul(c[y],c[x - y]),mod - 2));
}
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int m, n, i, j;
scanf("%d%d", &m, &n);
c[0] = 1;
for(i = 1;i <= m; i++) c[i] = mul(c[i - 1],i);
b[0] = 1;
for(i = 1;i <= m; i++) {
for(j = 0;j < i; j++) b[i] = add(b[i],mul(b[j],C(i - 1,j)));
}
for(i = 0; i < n; i++) {
scanf(" %s", s);
for(j = 0; j < m; j++) f[j][i] = s[j];
}
for(i = 0; i < m; i++) f[i][n] = 0;
for(i = 0; i < m; i++) {
for(j = 0; j < i; j++) {
if(strcmp(f[i], f[j]) == 0) break;
}
g[j]++;
}
int ans = 1;
for(i = 0;i < m; i++) {
ans = mul(ans,b[g[i]]);
}
printf("%d\n", ans);
return 0;
}

F.New Year and Rainbow Roads

考虑每两个$G$相互独立,它们之间要么通过$B,R$相连,要么直接相连,两种情况讨论一下就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int n, x, z, lr, lg, lb, mr, mb;
char c;
int main() {
#ifdef CX_TEST
//freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %c", &x, &c);
if (c == 'R' || c == 'G') {
if (lr > 0) {
z += x - lr;
mr = max(mr, x - lr);
}
lr = x;
}
if (c == 'B' || c == 'G') {
if (lb > 0) {
z += x - lb;
mb = max(mb, x - lb);
}
lb = x;
}
if (c == 'G') {
if (lg > 0 && mr + mb > x - lg) {
z += x - lg - mr - mb;
}
lg = x;
mr = mb = 0;
}
}
printf("%d\n", z);
return 0;
}

G.New Year and Original Order

考虑求出比$k$小的数字的个数有$j$个的数字的个数为$f[j][k]$
把数字$k$的贡献拆成$k$个$1$
那么$f[j][k]$对答案的贡献就是$f[j][k] * g[n - j],g[i]$是$i$个$1$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//created by missever
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int maxn = 705;
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
int mul(int x, int y) {
LL z = 1LL * x * y;
return z - z / mod * mod;
}
void update(int &a, int b) {
a += b;
if(a >= mod) a -= mod;
}
char s[maxn];
int a[maxn],p[maxn],g[maxn];
int f[maxn][maxn][10][2];
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, i, j, k, l, ans = 0;
scanf(" %s", s);
n = strlen(s);
for(i = p[0] = 1; i < maxn; i++) p[i] = mul(p[i - 1], 10);
for(i = 1; i < maxn; i++) g[i] = add(g[i - 1], p[i - 1]);
for(i = 0; i < n; i++) a[i] = s[i] - '0';
for(i = 1; i < 10; i++) f[0][0][i][0] = 1;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
for(k = 1; k < 10; k++) {
if(f[i][j][k][0]) {
for(l = 0; l < a[i]; l++) update(f[i + 1][j + (l < k)][k][1], f[i][j][k][0]);
update(f[i + 1][j + (a[i] < k)][k][0], f[i][j][k][0]);
}
if(f[i][j][k][1]) {
for(l = 0; l < 10; l++) update(f[i + 1][j + (l < k)][k][1], f[i][j][k][1]);
}
}
}
}
for(i = 0;i < n; i++) {
for(k = 1;k < 10; k++) ans = add(ans,mul(g[n - i],add(f[n][i][k][0],f[n][i][k][1])));
}
printf("%d\n", ans);
return 0;
}

H.New Year and Boolean Bridges

先连$A$,并查集维护,对于一个联通块,我们肯定要把它连成一个环
如果一个联通块中存在两点的关系为$X$,肯定无解
否则考虑把这些联通块合并一下,两个联通块之间的所有点对的关系都是$O$的话可以合并
转化成$n/2$个点的图求最小的完全子图划分
可以$fwt$维护
但是由于我们只需要关注一个值的情况
所以可以省去逆变换
不如……直接上容斥吧……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//created by missever
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int maxn = 1 << 23;
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
int mul(int x, int y) {
LL z = 1LL * x * y;
return z - z / mod * mod;
}
char s[55][55];
int p[55], r[55], d[55], h[55], ans = 0;
int f[maxn],g[maxn],bit[maxn];
int ff(int x) {
if(p[x] != x) p[x] = ff(p[x]);
return p[x];
}
void funion(int x, int y) {
x = ff(x);
y = ff(y);
if(x != y) p[y] = x;
}
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, m, i, j;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf(" %s", s[i]);
for(i = 0; i < n; i++) p[i] = i;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(i == j) continue;
if(s[i][j] == 'A') funion(i, j);
}
}
for(i = 0; i < n; i++) d[ff(i)]++;
memset(h, -1, sizeof(h));
for(i = m = 0; i < n; i++) {
if(d[i] > 1) h[i] = m++;
}
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(i == j) continue;
if(s[i][j] == 'X') {
if(ff(i) == ff(j)) {
return puts("-1"), 0;
}
if(d[ff(i)] > 1 && d[ff(j)] > 1) {
r[h[ff(i)]] |= 1 << h[ff(j)];
r[h[ff(j)]] |= 1 << h[ff(i)];
}
}
}
}
if(!m) {
printf("%d\n",n - 1);
return 0;
}
for(i = 0;i < m; i++) r[i] ^= (1 << m) - 1;
for(i = 0; i < (1 << m); i++) {
if((i & (-i)) == i) f[i] = 1;
else {
j = i ^ (1 << (__builtin_ffs(i) - 1));
if(f[j] && (r[i] & j) == j) f[i] = 1;
}
g[i] = 1;
bit[i] = ((m - __builtin_popcount(i)) & 1) ? mod - 1 : 1;
}
for (i = 0; i < m; i++) {
for (j = 0; j < (1 << m); j++) {
if ((j >> i) & 1) f[j] += f[j ^ (1 << i)];
}
}
for(i = 0;i < m; i++) {
for(j = 0;j < (1 << m); j++) g[j] = mul(g[j],f[j]);
int x = 0;
for(j = 0;j < (1 << m); j++) x = add(x,mul(bit[j],g[j]));
if(x) break;
}
printf("%d\n", n + i);
return 0;
}