题面

Solution:

这是一道很好的dp题。

一开始看不懂题面没有一点思路,看了好久题解才看懂题目...

\(y[i]\) 为第 \(i\) 个词结尾,\(l[i]\) 为第 \(i\) 个词长度。

设状态 \(f[i][j]\) 为长度为 \(i\) 的,以 \(j\) 结尾的一句诗的方案数,那么

\[f[i][Y] = \sum_{y[i]=Y}\sum_{x=1}^{n}f[i-l[j]][x]
\]

发现后面那一坨可以预处理,设

\[g[i]=\sum_{x=1}^nf[i][x]
\]

\(g[i]\) 的意义是长度为 \(i\) 一句诗的方案数,显然可以无限背包 \(O(nk)\) 求。

\[g[i]=\sum_{j=1}^ng[i-l[j]]
\]

那么

\[f[i][Y]=\sum_{y[j]=Y}g[i-l[j]]
\]

对于每一个需要押的韵('A','B'...)答案就等于:

\[Ans[i] = \sum_{j=1}^nf[k][j]^{cnt[i]}
\]

然后输出 \(\prod_\limits{i=A}^ZAns[i]\)。

\(Source:\)

#include <set>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm> using namespace std; #define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define LL long long
#define INF (0x3f3f3f3f)
#define mem(a, b) memset(a, b, sizeof (a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(x) cout << #x << " = " << x << endl
#define travle(i, x) for (register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for (register int (i) = (a); (i) <= (b); ++ (i))
#define Forr(i, a, b) for (register int (i) = (a); (i) >= (b); -- (i))
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define ____ debug("go\n") namespace io {
static char buf[1<<21], *pos = buf, *end = buf;
inline char getc()
{ return pos == end && (end = (pos = buf) + fread(buf, 1, 1<<21, stdin), pos == end) ? EOF : *pos ++; }
inline int rint() {
register int x = 0, f = 1;register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline LL rLL() {
register LL x = 0, f = 1; register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1ll) + (x << 3ll) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline void rstr(char *str) {
while (isspace(*str = getc()));
while (!isspace(*++str = getc()))
if (*str == EOF) break;
*str = '\0';
}
template<typename T>
inline bool chkmin(T &x, T y) { return x > y ? (x = y, 1) : 0; }
template<typename T>
inline bool chkmax(T &x, T y) { return x < y ? (x = y, 1) : 0; }
}
using namespace io; const int N = 5e3 + 1, mod = 1e9 + 7; int f[N][N], g[N], cnt[N], y[N], l[N], n, m, k; LL qpow(LL a, LL b) {
LL res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
} int main() {
#ifndef ONLINE_JUDGE
file("Cow_Poetry");
#endif
n = rint(), m = rint(), k = rint();
for (register int i = 1; i <= n; ++ i)
l[i] = rint(), y[i] = rint();
g[0] = 1;
for (register int i = 1; i <= k; ++ i)
for (register int j = 1; j <= n; ++ j) if (i - l[j] >= 0)
g[i] = (g[i] + g[i - l[j]]) % mod;
for (register int i = 1; i <= k; ++ i)
for (register int j = 1; j <= n; ++ j) if (i - l[j] >= 0)
f[i][y[j]] = (f[i][y[j]] + g[i - l[j]]) % mod;
char op[3];
for (register int i = 1; i <= m; ++ i) {
rstr(op);
cnt[op[0] - 'A'] ++;
}
LL ans = 1;
for (register int i = 0; i < 26; ++ i) if (cnt[i]) {
LL res = 0;
for (register int j = 1; j <= n; ++ j) if (f[k][j]) {
res = (res + qpow(f[k][j], cnt[i]) % mod) % mod;
}
ans = ans * res % mod;
}
cout << ans << endl;
return 0;
}

[USACO19JAN]Cow Poetry的更多相关文章

  1. LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理

    \(\mathrm{Cow Poetry}\) 问题描述 LG5196 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表到第\(i\)位时,结尾为\(j ...

  2. USACO19JAN Gold题解

    噩梦的回忆.. 上周日在机房打的模拟赛,结果十分惨烈,就最后一题yy出了正解结果玄学的只拿了80 考试结果:0+0+80=80 订正时对着T3打了2hours结果还是90 订正结果:100+100+9 ...

  3. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

  4. 20190922 「HZOJ NOIP2019 Round #7」20190922模拟

    综述 这次是USACO2019JAN Gold的题目. \(\mathrm{Cow Poetry}\) 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表 ...

  5. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  6. P5200 [USACO19JAN]Sleepy Cow Sorting

    P5200 [USACO19JAN]Sleepy Cow Sorting 题目描述 Farmer John正在尝试将他的N头奶牛(1≤N≤10^5),方便起见编号为1…N,在她们前往牧草地吃早餐之前排 ...

  7. P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)

    链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  8. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  9. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

随机推荐

  1. oracle 基础语法(二)

    一.实现分页 说明以下tablename是同一表.这些操作是对同一表(tablename)的操作 ======================================= 如何实现分页提取记录 ...

  2. gcc扩展语法一:在表达式中的语句和声明

    在GNU C中包含在括号中的复合语句可以作为一个表达式.这就允许你在表达式中使用循环,switch和局部变量. 以前复合语句是包含在大括号中语句序列.在这种构造中,圆括号包围在大括号中.如下面的例子: ...

  3. android中cursor对象的使用

    cursor对象是使用行来存储数据的,你要使用它获得数据,就必须知道每一列的数据名称以及他的数据类型才能获得对象数据 常见的方法: .close()关闭资源:记住,所有的资源对象使用完成后都要主动关闭 ...

  4. mysql if...else 的使用

    select case when tca.id = '3' then 'vw' else epc_code end as epccode,tfp.product_id, tfp.vender, tfp ...

  5. Sass 基础(五)

    @if @if 指令是一个SassScript,它可以根据条件处理样式块,如果条件为true返回一个样式块,反之 false 返回另一个样式块,在Sass 中除了@if之,还可以配合@else if和 ...

  6. P3818 小A和uim之大逃离 II(bfs,有条件的广搜)

    题目背景 话说上回……还是参见 https://www.luogu.org/problem/show?pid=1373 吧 小a和uim再次来到雨林中探险.突然一阵南风吹来,一片乌云从南部天边急涌过来 ...

  7. Linux外在设备的使用

    参考高峻峰 著 循序渐进Linux(第二版) 软盘在linux下对应的设备文件为/dev/fdx.主设备号fd是软盘驱动器(floppydisk)的缩写,次设备号x是软盘驱动器的相应编号.例如,/de ...

  8. Linux环境中配置环境变量无效

    1.在Linux系统中的[ ~/.baserc ]文件与[ /etc/profile ]配置环境变量后(可以使任意环境变量)无效的现象,如下为解决办法: 使用命令: vim ~/.zshrc 在 [# ...

  9. angular、angular2、vue的生命周期

    angular生命周期是什么 1.Angular每个组件都存在一个生命周期,从创建,变更到销毁.Angular提供组件生命周期钩子,把这些关键时刻暴露出来,赋予在这些关键结点和组件进行交互的能力,掌握 ...

  10. CentOS7.2中安装MongoDB

    MongoDB是由C++编写的NoSQL的分布式文件数据库,用的json格式的k-value存储方式. MongoDB官网 https://www.mongodb.com 一.下载和安装 下载完后文件 ...