@gym - 100958J@ Hyperrectangle
@description@
给定一个大小为 \(l_1\times l_2 \dots l_d\) 的 d 维超矩形,将它的一个角放置在原点,使得它的第 i 维在范围 \([0, l_i]\)。
求出这个矩形中满足 \(x_1 + x_2 + \dots + x_d \leq s\) 的点形成的体积 × d!。
@solution@
首先,简单积分一下可以得到满足 \(\sum_{i=1}^{d}x_i \leq s\) 的点形成的体积为 \(\frac{s^d}{d!}\)。
因为数据都是整数,我们不妨去考虑每一个 \(1\times 1 \dots \times 1\) 的单位超矩形 \((x_1, x_2, \dots x_d) - (x_1 + 1, x_2 + 1, \dots x_d + 1)\) 的贡献。
除去完全包含(\(s - \sum_{i=1}^{d}x_i \geq d\))与完全不包含(\(s - \sum_{i=1}^{d}x_i \leq 0\)),还有 d - 1 种不完全包含的情况 (\(0 < s - \sum_{i=1}^{d}x_i < d\))。
可以作个背包 + 简单前缀和优化算出每一种情况有多少个单位超矩形。
然而不完全包含的情况并不能直接通过积分算体积。不妨仍考虑组合方法,记 \(f_j\) 表示满足 \(s - \sum_{i=1}^{d}x_i = j\) 的体积。
对于 \(\sum_{i=1}^{d}x_i \leq s\),积分出来的体积是 \(\frac{s^d}{d!}\);然而我们也可以采用单位超矩形的贡献算出体积。
不妨只考虑 s < d 的情况,则可以列出等式 \(\frac{s^d}{d!} = \sum_{i=0}^{s}{s - i + d - 1\choose d - 1}f_i\),由此就可以在 \(f_s\) 与 \(f_{0...s-1}\) 之间建立递推关系。
时间复杂度瓶颈在背包的部分,为 O(l^3)。
@accepted code@
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 300;
const int MAXS = MAXN*MAXN;
const int MOD = int(1E9) + 7;
inline int add(int x, int y) {return (x + y) % MOD;}
inline int mul(int x, int y) {return 1LL*x*y % MOD;}
inline int sub(int x, int y) {return add(x, MOD-y);}
int pow_mod(int b, int p) {
int ret = 1;
for(int i=p;i;i>>=1,b=mul(b,b))
if( i & 1 ) ret = mul(ret, b);
return ret;
}
int l[MAXN + 5], d, s, S;
int fct[2*MAXN + 5], ifct[2*MAXN + 5];
int comb(int n, int m) {
return mul(fct[n], mul(ifct[m], ifct[n-m]));
}
int f[MAXN + 5];
void init() {
fct[0] = 1;
for(int i=1;i<=2*d;i++)
fct[i] = mul(fct[i-1], i);
ifct[2*d] = pow_mod(fct[2*d], MOD-2);
for(int i=2*d-1;i>=0;i--)
ifct[i] = mul(ifct[i+1], i+1);
for(int i=1;i<=d;i++) {
// f[i] = mul(pow_mod(i, d), ifct[d]);
f[i] = pow_mod(i, d);
for(int j=0;j<i;j++)
f[i] = sub(f[i], mul(comb(i-j+d-1, d-1), f[j]));
}
}
int g[MAXS + 5];
int main() {
scanf("%d", &d), init();
for(int i=1;i<=d;i++)
scanf("%d", &l[i]), l[i]--;
scanf("%d", &s);
g[0] = 1;
for(int i=1;i<=d;i++) {
S += l[i];
for(int j=1;j<=S;j++) g[j] = add(g[j], g[j-1]);
for(int j=S;j>l[i];j--) g[j] = sub(g[j], g[j-l[i]-1]);
}
int ans = 0;
for(int i=0;i<=s;i++)
ans = add(ans, mul(f[min(s-i,d)], g[i]));
printf("%d\n", ans);
}
@details@
一开始总以为它是一道积分题,结果发现怎么积分都不对劲。
然后尝试不从积分走而从组合数学走,发现还真能做出来。
@gym - 100958J@ Hyperrectangle的更多相关文章
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
随机推荐
- Apache 慢连接dos
http://neue.v2ex.com/t/108717------不实用 http://www.blogjava.net/bukebushuo/articles/293776.html http: ...
- AVL树的创建--C语言实现
AVL树是一种自平衡(Self-balancing)二叉查找树(Binary Search Tree),要求任何一个节点的左子树和右子树的高度之差不能超过1. AVL树的插入操作首先会按照普通二叉查找 ...
- BZOJ 1028 BZOJ 1029 //贪心
1028: [JSOI2007]麻将 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2197 Solved: 989[Submit][Status][ ...
- 【面试考】【入门】决策树算法ID3,C4.5和CART
关于决策树的purity的计算方法可以参考: 决策树purity/基尼系数/信息增益 Decision Trees 如果有不懂得可以私信我,我给你讲. ID3 用下面的例子来理解这个算法: 下图为我们 ...
- 学习ASP.NET Core(08)-过滤搜索与分页排序
上一篇我们介绍了AOP的基本概览,并使用动态代理的方式添加了服务日志:本章我们将介绍过滤与搜索.分页与排序并添加对应的功能 注:本章内容大多是基于solenovex的使用 ASP.NET Core 3 ...
- Siemens PLC分类和基本性能指标
PLC分类 整体式plc也成为单元式,特点是电源,中央处理器单元以及I/O借口都集成在一个机壳内. 标准摸板试结构化,也成为组合式,特点是电源,中央处理器单元模板以及I/O模板在结构上都是相互独立的, ...
- Layui 改变数据表格样式覆盖
改变表格行高.layui-table-cell{ height:40px; line-height: 36px; } 改变复选框高宽和定位等等.layui-table-view .layui-form ...
- C#中操作JSON
引入支持JSON操作的库,比如LitJSON: 引入之后就可以解析JSON了. 写一个JSON文本 读取这个JSON文本: 解析JSON数据最佳实践--使用泛型,将泛型类型指定成自己定义的类型,直接获 ...
- 安装superset遇到的坑
实验环境:ubuntu16.04 python环境: 3.6.7 安装参考:https://superset.incubator.apache.org/installation.html 特别提醒: ...
- Java中的集合(十三) 实现Map接口的Hashtable
Java中的集合(十三) 实现Map接口的Hashtable 一.Hashtable简介 和HashMap一样,Hashtable采用“拉链法”实现一个哈希表,它存储的内容是键值对(key-value ...