题面

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. Android学习笔记_66_图片处理专题

    1.图片缩放:不解析整个图片信息. public class DemoActivity extends Activity { @Override public void onCreate(Bundle ...

  2. Unity3d Gis 坐标转换

    最近在做unity3d与Gis结合的项目,最基本的就是坐标的转换问题,比如把经纬度为(166.23.9.27 , 39.55.15.74) 转换到unity里面成相应的位置点,废话不多说 上代码: u ...

  3. apache开启.htaccess及使用方法

    1 . 如何让的本地APACHE器.htaccess 如何让的本地APACHE呢?其实只要简朴修改一下apache的httpd.conf设置就让APACHE.htaccess开启了,来看看操作 打开h ...

  4. Java虚拟机垃圾回收(二) :垃圾回收算法(转载)

    1.标记-清除算法 标记-清除(Mark-Sweep)算法是一种基础的收集算法. 1.算法思路 "标记-清除"算法,分为两个阶段: (A).标记 首先标记出所有需要回收的对象: 标 ...

  5. the “inner class” idiom

    有些时候我们需要upcast为多种类型,这种情况下除了可以使用multiply inherits还可以inner class. 以下为例子: //: C10:InnerClassIdiom.cpp / ...

  6. Oracle数据库大量library cache: mutex X及latch: shared pool问题排查一例

    业务系统数据库夯住,数据库内大量的library cache: mutex X及latch: shared pool等待,alert日志信息如下 Tue Sep :: WARNING: inbound ...

  7. LeetCode 中级 - 翻转矩阵后的得分(861)

    有一个二维矩阵 A 其中每个元素的值为 0 或 1 . 移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0. 在做出任意次数的移动后,将该矩阵的每一 ...

  8. LeetCode 简单 - 路径总和(112)

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...

  9. 用python爬取小说章节内容

    在学爬虫之前, 最好有一些html基础, 才能更好的分析网页. 主要是五步: 1.  获取链接 2. 正则匹配 3. 获取内容 4. 处理内容 5. 写入文件 代码如下: #导入相关model fro ...

  10. ABAP术语-Business Components

    Business Components 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/26/1015254.html Group of re ...