【51nod】2589 快速讨伐
51nod 2589 快速讨伐
又是一道倒着推改变世界的题。。。
从后往前考虑,设\(dp[i][j]\)表示还有\(i\)个1和\(j\)个\(2\)没有填,那么填一个1的话直接转移过来
\(dp[i][j] \rightarrow dp[i - 1][j]\)
如果填一个\(2\)要把\(A[j]\)的那些敌人都扔在这个2的后面
方案是
\(\binom{N - i + N - j + sum[N] - sum[j - 1]}{A[j]} A[j]! dp[i][j] \rightarrow dp[i][j - 1]\)
然后把\(i < j\)的状态都标成0就好了
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 2005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353,MAXV = 4000000;
int N;
int A[MAXN],s[MAXN];
int fac[MAXV + 5],invfac[MAXV + 5];
int dp[MAXN][MAXN];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int C(int n,int m) {
if(n < m) return 0;
else return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(A[i]);
s[0] = 1;
for(int i = 1 ; i <= N ; ++i) s[i] = s[i - 1] + A[i];
fac[0] = 1;
for(int i = 1 ; i <= MAXV ; ++i) fac[i] = mul(fac[i - 1],i);
invfac[MAXV] = fpow(fac[MAXV],MOD - 2);
for(int i = MAXV - 1 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
dp[N][N] = 1;
for(int i = N ; i >= 0 ; --i) {
for(int j = N ; j >= 0 ; --j) {
if(i < j) {dp[i][j] = 0;continue;}
if(i && i - 1 >= j) {
update(dp[i - 1][j],dp[i][j]);
}
if(j) {
update(dp[i][j - 1],mul(mul(dp[i][j],fac[A[j]]),C(N - i + N - j + s[N] - s[j - 1],A[j])));
}
}
}
out(dp[0][0]);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
【51nod】2589 快速讨伐的更多相关文章
- 51nod 2589 快速讨伐
51nod 如果不考虑升级操作,只有买装备操作和打怪操作,那么首先一定要先买装备,然后可以打死1级的怪,这些怪被打死的时间只要在第一次买装备后面好了,因为现在总操作是\(n+\sum a_i\)个,所 ...
- 51nod 1013快速幂 + 费马小定理
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 这是一个等比数列,所以先用求和公式,然后和3^(n+1)有关,有n ...
- 51nod 矩阵快速幂(模板题)
1113 矩阵快速幂 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计算结果太大 ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 51Nod 1004 n^n的末位数字(日常复习快速幂,莫名的有毒,卡mod值)
1004 n^n的末位数字 题目来源: Author Ignatius.L (Hdu 1061) 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数N,输出 ...
- 51Nod 1046 A^B Mod C(日常复习快速幂)
1046 A^B Mod C 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = ...
- 51nod 1835 - 完全图 - [dp][组合数公式][快速幂]
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1835 基准时间限制:1 秒 空间限制:131072 KB ...
- 51Nod 快速傅里叶变换题集选刷
打开51Nod全部问题页面,在右边题目分类中找到快速傅里叶变换,然后按分值排序,就是本文的题目顺序. 1.大数乘法问题 这个……板子就算了吧. 2.美妙的序列问题 长度为n的排列,且满足从中间任意位置 ...
- 51nod 1013 3的幂的和 - 快速幂&除法取模
题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...
随机推荐
- Vue-es6基础语法
什么是ES6 ECMAScript 6 简称ES6, 在2015年6月正式发布~ ECMAScript 是JavaScript语言的国际标准. 我们本着二八原则,掌握好常用的,有用的~能让我们更快的 ...
- Smali基础知识
Smali是用于Dalvik(Android虚拟机)的反汇编程序实现 汇编工具(将Smali代码汇编为dex文件)为smali.jar baksmali.jar则是反汇编程序 地址:https://b ...
- springboot的jar在linux运行
springboot项目使用maven打包成jar包,如何在linux优雅部署?平时启动项目使用java -jar命令,关闭程序需要查询pid再查杀进程,这样都太麻烦了,今天发现一个博客已经写好的脚本 ...
- postgresql could not connect to server
问题: postgresql部署在linux上,在自己电脑上使用pgadmin连接出现could not connect to server错误 问题分析: 出现上述原因有3种情况 1.linux上的 ...
- Python进行Redis数据迁移
Python进行Redis数据迁移 由于开发时的误操作,导致redis数据损坏,所以需要进行redis的数据迁移,网上大佬的教程基本都是需要下载附加工具,亦或是需要一些复杂的操作,个人觉得麻烦还不如写 ...
- VScode优化记录
主题: One Dark Pro VS Code 官网页面 这是为 VS Code准备的 One Dark 主题,他是 Atom 标志性的主题.需要我说更多吗?我喜欢这个主题. Mon ...
- SpringMVC之ajax与表单 Post 数据提交差异小结
最近在写一个富文本框的后台数据服务的时候遇到一些关于 ajax 提交与 表单提交的比较特殊的案例,这里拿来跟大家分享,希望能让大家有所启发. 1. 首先是常见表单提交在SpringMVC的控制器中的代 ...
- maven打包遇到错误,Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
对Pom文件进行配置(亲自尝试,已成功解决) <build> <plugins> <plugin> <groupId>org.apache.maven. ...
- export default {} 和new Vue()区别?
export default 的用法:相当于提供一个接口给外界,让其他文件通过 import 来引入使用. 而对于 new Vue({})部分, 只是创建一个Vue的实例 就是相当于创建一个根组件 h ...
- Android开发final的用法
Android开发final的用法 final如果修饰类,该类不能被继承: final如果修饰变量,该变量不能被修改,不能再重新赋值,即变为常量: final如果修饰方法,该方法不能被重写: 此外 ...