题面

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. deep learning学习记录二

    接着我的微博继续八卦吧 微博里问了几个人,关于deep learning和cnn的区别,有不少热心网友给了回答,非常感谢.结合我听课和看文章的理解,我大胆大概总结一下: 在上世纪90年代,neural ...

  2. 书籍《深入理解Spring Cloud 与微服务构建》勘误、源码下载

    转载请标明出处: https://blog.csdn.net/forezp/article/details/79638403 本文出自方志朋的博客 文章勘误 错误在所难免,欢迎大家批评指正,在文章下方 ...

  3. SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot15-rabbitmq/ 本文出自方志朋的博客 这 ...

  4. 【转】RMAN删除过期备份或非过期备份

    (一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件.当使用RMAN执行备份操作时,会在RMAN资料库(RMAN Repository)中生成RMAN备份记录,默认情况下RMAN ...

  5. c#解析分析SQL语句

    最近总结了c#一般的功能,然后自己在博文中写了很多东西.主要是在用途上面.能够解决一些问题.现在分各个组件和方向写完了.主要的内容写了demo,也写了自己的项目组件和模型. 最后一个SQL分析.其实在 ...

  6. c#本地缓存实现

    用了一段时间java,java实现服务端程序很简单,有很多公共开源的组件或者软件.但是c#的很少. 现在准备自己写点东西,学习下新的东西,总结下c#的内容以及我们经常用的内容,抽离成类,组件,模型.方 ...

  7. flex 遇到white-space:nowrap

    背景,做一个前面图片宽度固定,后面宽度自适应,使用到了flex布局,但是想让后面div里文字不换行,超出以点点表示时,这时布局就乱了,查了下,原来flex布局与white-space:nowrap有影 ...

  8. CentOS7——网络配置

    ip addr #查看当前IP地址信息.(contos7以下的为ifconfig) /etc/sysconfig/network-scripts/ifcfg-*** #***代表不一定的,需要进入该设 ...

  9. 虚拟内存设置(解决linux内存不够情况)

    一.      虚拟内存介绍 背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于 ...

  10. 编程 - 前端 - JavaScript - 库 - ECharts (开源可视化)

    ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...