Codeforces Round #445 Div. 1 C Maximum Element (dp + 组合数学)
题目链接:
http://codeforces.com/contest/889/problem/C
题意:
给你 \(n\)和 \(k\)。
让你找一种全排列长度为\(n\)的 \(p\),满足存在下标 \(i\),\(p_i\)大于所有 \(p_j\),\(j\epsilon[1,i-1]\)同时大于所有\(p_i\),\(j\epsilon[i+1,i+k]\)。问你满足这样条件的排列有多少种?
题解:
设\(dp[i]\)表示以 \(i\) 结尾的,满足题目要求的\(1\) ~ \(i\)排列。
显然。
如果,\(i<=k+1\),则\(dp[i]=0\)。
因为我们考虑 \(i-1\) 在这个排列当中的位置。当 \(i-1\) 和 \(i\) 之间的数字超过 \(k\)个时,显然成立,此时共有 \((i-k-1)*(i-2)!\) 种序列。
否则,\(i-1\) 的下标\(j >= i-k\), 把排列的前 \(j\) 个数字离散化为都由\(1\) ~ \(j\) 组合之后,这些数字组成的排列一定是以 \(j\) 结尾,满足题目要求的排列,共有\(dp[j]\)个,因为后面的数字少于 \(k\)个,不可能满足题目要求。\(dp[j]\) 是离散化之后的结果,离散化之前的结果共有\(dp[j]*C(i-2,j-1)*(i-j-1)!=dp[j]*\frac{(i-2)!}{(j-1)!}\)个。可以理解为:先在剩下的 \(i-2\) 个数当中取 \(j-1\) 个排在下标为$ 1~j-1的$位置,下标 \(j\) 之后到最后一个元素之前的位置随意排列)。
所以,两种情况加起来就是:
\(dp[i]=(i-k-1)*(i-2)!+\sum_{j=i-k}^{i-1}dp[j]*\frac{(i-2)!}{(j-1)!}\)。
但是这样直接计算要 \(O(n^2)\)。
提取一下\((i-2)!\),变成:
\(dp[i]=(i-k-1)*(i-2)!+(i-2)!*\sum_{j=i-k}^{i-1}\frac{dp[j]}{(j-1)!}\)
\(= (i-2)!*[(i-k-1)+\sum_{j=i-k}^{i-1}\frac{dp[j]}{(j-1)!}]\)
后面一项 \(\frac{dp[j]}{(j-1)!}\) 就可以利用前缀和求出。阶乘的乘除可以利用逆元求出。直接算就是O(n)。
\(dp[n]\)是以 \(n\) 结尾的排列个数。我们把 \(n\) 排在不同的位置 \(h\),把\(n\)下标之前的数离散化到\(1\) ~ \(h-1\),跟上面的一样,所以最终答案为:
\(\sum_{h=1}^{n}dp[h]*\frac{(n-1)!}{(h-1)!}\)。
总复杂度:\(O(n)\)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000010;
ll inv[maxn],fac[maxn],dp[maxn],sum[maxn];
const int mod =1e9+7;
ll qpower(ll a,ll b){
ll res = 1;
while(b)
{
if(b&1)res = res*a%mod;
b>>=1;
a= a*a%mod;
}
return res;
}
int main(int argc, char const *argv[]) {
ll n,k;
ll ans = 0;
cin>>n>>k;
if(k+1>=n){
printf("0\n");
exit(0);
}
fac[0] = 1;
for (int i = 1; i <=n; i++) {
fac[i] = (fac[i-1] * i) %mod;
}
inv[n] = qpower(fac[n],mod-2);
for(int i=n-1;i>=0;i--){
inv[i] = inv[i+1] *(i+1);
inv[i] %= mod;
}
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
for(int i=k+2;i<=n;i++){
dp[i] = (i-k-1 + (sum[i-1] - sum[i-k-1] +mod)%mod)%mod;
dp[i] = (dp[i] * fac[i-2]) % mod;
sum[i] = sum[i-1] + (dp[i] * inv[i-1])%mod;
sum[i] %= mod;
ans += (((dp[i] * fac[n-1]) % mod) * inv[i-1])%mod;
ans %= mod;
}
cout<<ans<<endl;
return 0;
}
ADDITION:
当然也可以把不符合题目条件的先算出来,然后用 \(n!\)减去不符合条件的个数,即为答案。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
ll inv[maxn],fac[maxn],dp[maxn],sum[maxn];
const int mod =1e9+7;
ll qpower(ll a,ll b){
ll res = 1;
while(b)
{
if(b&1)res = res*a%mod;
b>>=1;
a= a*a%mod;
}
return res;
}
int main(int argc, char const *argv[]) {
ll n,k;
cin>>n>>k;
if(k+1>=n)
{
printf("0\n");
exit(0);
}
fac[0] = 1;
for(int i=1;i<=n;i++){
fac[i] = fac[i-1]*i%mod;
}
inv[n] = qpower(fac[n],mod-2);
for(int i=n-1;i>=0;i--){
inv[i] = inv[i+1] * (i+1) %mod;
}
dp[1] = sum[1] = 1;
ll ans = fac[n-1];
for(int i=2;i<=n;i++){
dp[i] = (sum[i-1] - sum[max(0LL,i-k-1)]) *fac[i-2] %mod;
sum[i] = (sum[i-1] + dp[i] * inv[i-1]) % mod;
ans = (ans + dp[i] * fac[n-1] %mod * inv[i-1])%mod;
}
cout<<(fac[n]-ans+mod)%mod<<endl;
return 0;
}
Codeforces Round #445 Div. 1 C Maximum Element (dp + 组合数学)的更多相关文章
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数
B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...
- Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...
- Codeforces Round #276 (Div. 1)B. Maximum Value 筛法
D. Maximum Value You are given a sequence a consisting of n integers. Find the maximum possible ...
- Codeforces Round #599 (Div. 2) A. Maximum Square 水题
A. Maximum Square Ujan decided to make a new wooden roof for the house. He has
- Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...
- Codeforces Round #568 (Div. 2) D. Extra Element
链接: https://codeforces.com/contest/1185/problem/D 题意: A sequence a1,a2,-,ak is called an arithmetic ...
随机推荐
- WP8 学习笔记(002_应用程序结构)
下图是微软官方给出的WP8应用程序执行顺序: 在App.XAML.CS中,有程序主要步骤的函数 // 应用程序启动(例如,从“开始”菜单启动)时执行的代码 // 此代码在重新激活应用程序时不执行 pr ...
- 彻底解决Linux索引节点(inode)占用率高的告警
今天邮箱里发现有一封某服务器inode使用率发生告警的邮件 登录到服务器上df -i查看,发现/路径下91%,磁盘使用率却不高,猜测可能是某个目录下的小文件过多,进而造成inode占用率过高,但不清楚 ...
- Hadoop for .NET Developers
Hadoop for .NET Developers(一):理解Hadoop 这些年来,大数据已经成为分析业界的兴奋源头.对于这个博客系列的目的,我将松散定义这个术语指的重点是从数据核心业务系统里数据 ...
- TaoCode-淘宝的SVN开源托管平台
无意中发现的..试用了一下,感觉还不错, 简单说一下怎样使用: 进入 http://code.taobao.org/project/explore/ 注冊完后依据提示新建项目,然后在本地随便新建一个文 ...
- HDU 2886 Lou 1 Zhuang
思维好重要.. 对于n+m == k , 当n == m || abs(n-m) == 1 时n*m取得最大值. 设 s = x*(l-x),s = lx-x^2.其导函数为 s' = -1/2x + ...
- channels
package main import ( "fmt" "time" "strconv") func pinger(c chan strin ...
- Codefroces Educational Round 27 (A,B,C,D)
A. Chess Tourney time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- vue 实例的生命周期
Vue把整个生命周期划分为创建.挂载.更新.销毁等阶段,每个阶段都会给一些"钩子"让我们来做一些我们想实现的动作. 分为以下几个阶段 1.beforeCreate 此阶段为 ...
- Python批量重命名指定目录下文件的两种方法
#法一 import os path = "C://Python34//" for file in os.listdir(path): if os.path.isfile(os.p ...
- [Angular] How to get Store state in ngrx Effect
For example, what you want to do is navgiate from current item to next or previous item. In your com ...