@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 ...
随机推荐
- 利用js实现 禁用浏览器后退| 去除上一个历史记录链接
也是查找了好多资料才找到的,这种方式,可以消除 后退的所有动作.包括 键盘.鼠标手势等产生的后退动作. <script language="javascript"> / ...
- LaunchScreen作为启动图设置,修改无效的解决方案
原有的推流APP用launchScreen做的启动图,现在要修改一张,发现修改无效. 当前测试的方法有 1,重启Xcode 卸载app 清楚xcode缓存 2,修改launchScreen.stor ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- .Net基础之4——流程控制
(1)异常捕获 我们在程序中经常会出现各种各样的异常,你如果想要你的程序变得坚强一点. 在你的代码中应该经常性的使用try-catch来进行异常捕获. 语法: try { 可能会出现异常的代码: } ...
- A + B Problem(hdu1000)
注意,认真读题目的Input要求,看看是输入一组测试数据还是输入多组测试数据.输入多组数据,不要忘记while(). #include<iostream> using namespace ...
- python基础:list与string互转
数据清洗必备技能 https://www.cnblogs.com/yaner2018/p/11162630.html
- Java——动态生成POJO类
package com.java.test.dynamicpojo; import java.io.ByteArrayOutputStream; import java.io.IOException; ...
- 集合概述及Collection接口的常用方法
java集合像是一种容器,可以动态的把多个对象的引用放到容器中 java的集合类可以用于存储数量不等的多个对象,还可以用于保存具有映射关系的关联数组 package com.aff.coll; imp ...
- vnc安装 VNC的安装以及使用[转]
VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UNIX 和 ...
- winxp无法访问win10教育版共享资源的问题处理
一.问题来源: dell5460笔记本上一个winxp系统虚拟机,访问一台lenovoT470笔记本win10教育版系统上的共享资源(使用命令行方式:net use s: \\172.18.45.10 ...