[whu1568]dp优化
http://acm.whu.edu.cn/land/problem/detail?problem_id=1568
思路:先将所有数分解,得到2,3,5,7的个数,转化为用这些2,3,5,7"构成"的不同序列的个数。一般思路,令dp[a][b][c][d]表示2有a个,3有b个,5有c个,7有d个时的答案,那么有如下转移方程:dp[a][b][c][d] = sigma(i:2->9)(a', b', c', d'),a'为a减去i包含2的因子个数的结果,b',c',d'同理。由于空间消耗太大,必须另外考虑方法。注意到5和7是不可能组成其它的数的,可以单独处理,于是可以先用2和3构造,但需要把长度加进去作为状态的一部分,最后再把5和7插到构造成的序列里面。
//#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << (a) << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl typedef double db;
typedef long long LL;
typedef pair<int, int> pii;
typedef multiset<int> msi;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e6 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } template<int mod>
struct ModInt {
const static int MD = mod;
int x;
ModInt(int x = ): x(x) {}
int get() { return x; } ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
ModInt operator /= (const ModInt &that) { *this = *this / that; } ModInt inverse() const {
int a = x, b = MD, u = , v = ;
while(b) {
int t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < ) u += MD;
return u;
} };
typedef ModInt<> mint; const int c[][] = {{, }, {, }, {, }, {, }, {, }, {, }, {, }, {, }, {, }, {, }};
const int d[] = {, };
int cnt[];
mint dp[][][];
bool vis[][][];
mint fact[], fact_inv[]; mint dfs(int a, int b, int p) {
if (vis[a][b][p]) return dp[a][b][p];
if (p == ) return ;
vis[a][b][p] = true;
dp[a][b][p] = ;
for (int i = ; i < ; i++) {
if (i == || i == ) continue;
int aa = c[i][], bb = c[i][];
if (aa <= a && bb <= b) dp[a][b][p] += dfs(a - aa, b - bb, p - );
}
return dp[a][b][p];
} void Init() {
fact[] = ;
fact_inv[] = ;
for (int i = ; i <= ; i++) {
fact[i] = fact[i - ] * i;
fact_inv[i] = fact[i].inverse();
}
} int n;
char s[maxn]; int main() {
//freopen("in.txt", "r", stdin);
dp[][][] = ;
vis[][][] = true;
Init();
while (cin >> n) {
scanf("%s", s);
mem0(cnt);
rep_up0(i, n) {
if (s[i] == '' || s[i] == '') {
cnt[s[i] - '']++;
continue;
}
rep_up0(j, ) {
cnt[d[j]] += c[s[i] - ''][j];
}
}
mint ans = ;
int c23 = cnt[] + cnt[];
rep_up1(i, c23) ans += dfs(cnt[], cnt[], i) * fact[i + cnt[] + cnt[]] * fact_inv[cnt[]] * fact_inv[cnt[]] * fact_inv[i];
printf("%d\n", ans.get());
}
return ;
}
[whu1568]dp优化的更多相关文章
- NOIP2015 子串 (DP+优化)
子串 (substring.cpp/c/pas) [问题描述] 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个 互不重 叠 的非空子串,然后把这 k 个子串按照其在字 ...
- LCIS tyvj1071 DP优化
思路: f[i][j]表示n1串第i个与n2串第j个且以j结尾的LCIS长度. 很好想的一个DP. 然后难点是优化.这道题也算是用到了DP优化的一个经典类型吧. 可以这样说,这类DP优化的起因是发现重 ...
- 取数字(dp优化)
取数字(dp优化) 给定n个整数\(a_i\),你需要从中选取若干个数,使得它们的和是m的倍数.问有多少种方案.有多个询问,每次询问一个的m对应的答案. \(1\le n\le 200000,1\le ...
- dp优化1——sgq(单调队列)
该文是对dp的提高(并非是dp入门,dp入门者请先参考其他文章) 有时候dp的复杂度也有点大...会被卡. 这几次blog大多数会讲dp优化. 回归noip2017PJT4.(题目可以自己去百度).就 ...
- loj6171/bzoj4899 记忆的轮廊(期望dp+优化)
题目: https://loj.ac/problem/6171 分析: 设dp[i][j]表示从第i个点出发(正确节点),还可以有j个存档点(在i点使用一个存档机会),走到终点n的期望步数 那么 a[ ...
- 常见的DP优化类型
常见的DP优化类型 1单调队列直接优化 如果a[i]单调增的话,显然可以用减单调队列直接存f[j]进行优化. 2斜率不等式 即实现转移方程中的i,j分离.b单调减,a单调增(可选). 令: 在队首,如 ...
- 【学习笔记】动态规划—各种 DP 优化
[学习笔记]动态规划-各种 DP 优化 [大前言] 个人认为贪心,\(dp\) 是最难的,每次遇到题完全不知道该怎么办,看了题解后又瞬间恍然大悟(TAT).这篇文章也是花了我差不多一个月时间才全部完成 ...
- Codevs 1305 Freda的道路(矩阵乘法 DP优化)
1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description Freda要到Rainbow的城堡去玩了.我们可以认 ...
- [总结]一些 DP 优化方法
目录 注意本文未完结 写在前面 矩阵快速幂优化 前缀和优化 two-pointer 优化 决策单调性对一类 1D/1D DP 的优化 \(w(i,j)\) 只含 \(i\) 和 \(j\) 的项--单 ...
随机推荐
- Xss Game挑战
前言 最新学习了下xss的更深入的东西,学习了一波浏览器解析机制和XSS向量编码的知识. 这里就些xss的练习题巩固知识 学习的话结合如下两篇文章看,从例子和基础原理层面都有: http://boba ...
- SUCTF 2019 Upload labs 2 踩坑记录
SUCTF 2019 Upload labs 2 踩坑记录 题目地址 : https://github.com/team-su/SUCTF-2019/tree/master/Web/Upload La ...
- pytorch Dataset数据集和Dataloader迭代数据集
import torch from torch.utils.data import Dataset,DataLoader class SmsDataset(Dataset): def __init__ ...
- 将jar包发布到maven中央仓库
将jar包发布到maven中央仓库 最近做了一个swagger-ui的开源项目,因为是采用vue进行解析swagger-json,需要前端支持,为了后端也能方便的使用此功能,所以将vue项目编译后的结 ...
- WebApi参数检查验证FluentValidation的使用方法
右键打开NuGet程序包管理,进入浏览,搜索 FluentValidation,点击下载 在Model文件夹添加一个Person类进行校验 校验前,using需要引入相应的命名空间方可使用,Abstr ...
- sql注入notebook
内容来自: https://ca0y1h.top/ 联合查询注入 使用场景 页面上有显示位 什么是显示位:在一个在一个网站的正常页面,服务端执行SQL语句查询数据库中的数据,客户端将数据展示在页面中, ...
- Linux 如何通过 iscsi target name 获取 ip
by Mike Andrews # lsscsi -t [:::] disk iqn.-.com.blockbridge:t-pjxfzufjkp-illoghjk,t,0x1 /dev/sda [: ...
- 闲置安卓设备搭建Linux服务器实现外网访问
title: 闲置安卓设备搭建Linux服务器实现外网访问 这是我搭过的第一个博客系统,写贴纪念一下 待博主整理好思路,将今天所用到的全部分享! 好吧,我就是穷.富人靠科技,穷人靠变异.我这种穷人是真 ...
- Spring5参考指南:Bean的生命周期管理
文章目录 Spring Bean 的生命周期回调 总结生命周期机制 startup和Shutdown回调 优雅的关闭Spring IoC容器 Spring Bean 的生命周期回调 Spring中的B ...
- 已有项目接入git远程仓库
1.项目根目录初始化git仓库 git init 2.将本地项目与远程仓库关联(首先得在远程创建一个代码仓库) git remote add origin 远程仓库地址 诺,仓库地址就是这个玩意 3. ...