ATcoder 1983 BBQ Hard
E - BBQ Hard
Time limit : 2sec / Memory limit : 256MB
Score : 1400 points
Problem Statement
Snuke is having another barbeque party.
This time, he will make one serving of Skewer Meal.
He has a stock of N Skewer Meal Packs. The i-th Skewer Meal Pack contains one skewer, Ai pieces of beef and Bi pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.
To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.
(See the image in the Sample section for better understanding.)
In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo 109+7.
Constraints
- 2≦N≦200,000
- 1≦Ai≦2000,1≦Bi≦2000
Input
The input is given from Standard Input in the following format:
N
A1 B1
A2 B2
:
AN BN
Output
Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo 109+7.
Sample Input 1
3
1 1
1 1
2 1
Sample Output 1
26
The 26 ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.

我们可以先强行把 有序对转化成无序对并可以自己加自己的形式,也就是题目要求的 Σ(i=1 to n)Σ(j=i+1 to n) C(a[i]+a[j]+b[i]+b[j] , a[i]+a[j])=[ Σ(i=1 to n)Σ(j=1 to n) C(a[i]+a[j]+b[i]+b[j] , a[i]+a[j]) - Σ(i= 1 to n) C(a[i]*2+b[i]*2 , a[i]*2) ]/2。
后面那个Σ好求,直接带一遍组合数就行了,所以现在问题的关键是Σ(i=1 to n)Σ(j=1 to n) C(a[i]+a[j]+b[i]+b[j] , a[i]+a[j]) 怎么求。
考虑到a[],b[]都是<=2000的,可以从这里入手。我们发现,设点X[i] = {-a[i] , -b[i]} ,Y[i] = {a[i] , b[i]} ,那么上式的含义就是在二维平面上,每次只能向右或者向上走,从每个X[i] 走到每个Y[j]的方案数的和,而这个玩意又很好dp,嗯。。。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=4005;
const int N=200005,inv=ha/2+1;
int n,px[N],py[N],jc[maxn<<1];
int ans=0,f[maxn][maxn],ni[maxn<<1]; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} inline void init(){
jc[0]=1;
for(int i=1;i<=8000;i++) jc[i]=jc[i-1]*(ll)i%ha;
ni[8000]=ksm(jc[8000],ha-2);
for(int i=8000;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
} inline void dp(){
for(int i=0;i<=4000;i++)
for(int j=0;j<=4000;j++){
if(i) f[i][j]=add(f[i][j],f[i-1][j]);
if(j) f[i][j]=add(f[i][j],f[i][j-1]);
}
} inline int C(int x,int y){
return jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;
} inline void calc(){
for(int i=1;i<=n;i++) ans=add(ans,f[2000+px[i]][2000+py[i]]);
for(int i=1;i<=n;i++) ans=add(ans,ha-C((px[i]+py[i])<<1,py[i]<<1));
ans=ans*(ll)inv%ha;
} int main(){
init();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",px+i,py+i);
f[2000-px[i]][2000-py[i]]++;
}
dp();
calc();
printf("%d\n",ans);
return 0;
}
ATcoder 1983 BBQ Hard的更多相关文章
- AtCoder AGC001E BBQ Hard (DP、组合计数)
题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_e 题解: 求\(\sum^n_{i=1}\sum^n_{j=i+1} {A_i+A_j+B ...
- AtCoder练习
1. 3721 Smuggling Marbles 大意: 给定$n+1$节点树, $0$为根节点, 初始在一些节点放一个石子, 然后按顺序进行如下操作. 若$0$节点有石子, 则移入盒子 所有石子移 ...
- Atcoder Grand Contest 001E - BBQ Hard(组合意义转化,思维题)
Atcoder 题面传送门 & 洛谷题面传送门 Yet another 思维题-- 注意到此题 \(n\) 数据范围很大,但是 \(a_i,b_i\) 数据范围很小,这能给我们什么启发呢? 观 ...
- 【agc001e】BBQ HARD(动态规划)
[agc001e]BBQ HARD(动态规划) 题面 atcoder 洛谷 题解 这些agc都是写的整场的题解,现在还是把其中一些题目单独拿出来发 这题可以说非常妙了. 我们可以把这个值看做在网格图上 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- org.eclipse.jdi.TimeoutException:Timeout occurred while waiting for packet 1983.occurred
eclipse 增大junit内存配置 现象:eclipse在junit测试的时候出现如下错误 org.eclipse.jdi.TimeoutException:Timeout occurred wh ...
- AtCoder Regular Contest 082
我都出了F了……结果并没有出E……atcoder让我差4分上橙是啥意思啊…… C - Together 题意:把每个数加1或减1或不变求最大众数. #include<cstdio> #in ...
- AtCoder Regular Contest 069 D
D - Menagerie Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Snuke, w ...
随机推荐
- Mybatis的Service循环调用错误
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'z ...
- Eclipse Code Recommenders 自动补全(联想)神器
Eclipse Code Recommenders 可以在eclipse市场中下载.自动补全.模糊匹配.非常有用!
- metasploitable2更改root密码
metasploitable2这个系统众所周知,一个用户名和密码是msfadmin.但是这个账号权限不全,我们想要改root密码来登陆为所欲为.也没试过破解,咱们索性就改了吧. 就简单几行代码.. ...
- c++_方格分割
标题:方格分割 6x6的方格,沿着格子的边线剪开成两部分.要求这两部分的形状完全相同. 如图:p1.png, p2.png, p3.png 就是可行的分割法. 试计算:包括这3种分法在内,一共有多少种 ...
- uwsgs loading shared libraries: libicui18n.so.58 异常处理
背景 想使用 ningx + uwsgi + flask 搭建 python 应用环境 Python使用的是anaconda3(pyhton 3.6) 依赖包安装完毕,但是执行 uwsgi 的时候出现 ...
- 【HIHOCODER 1513】 小Hi的烦恼 (BitSet)
描述 小Hi从小的一大兴趣爱好就是学习,但是他发现尽管他认真学习,依旧有学神考的比他好. 小Hi在高中期间参加了市里的期末考试,一共五门:语文.数学.英语.物理.化学. 成绩出来之后,小Hi发现有些同 ...
- asp网页无法打开
环境:Window 2003.IIS6.Framework1.1 .VS2003 一个WebForm项目里面包含一些asp网页 运行后发现asp页面无法访问 提示:无法找到该页 解决方案: 1. [ ...
- javascript:与获取鼠标位置有关的属性
javascript并没有mouse对象,获取鼠标坐标要靠强大的event对象。 我们通过监听document的mousemove,就可以实时获得鼠标位置。 但是!!event中和鼠标相关的属性太多了 ...
- HDU2069-Coin Change
Coin Change 这题题意和UVA674很像,但加了一个限制条件不能超过100个硬币.于是我们可以用d[i][j]来表示硬币数量为i时金钱总数为j时的方法总数,总钱不能超过250. const ...
- [Go]指针操作
指针类型比较常见 type Dog struct { name string } func (dog *Dog) SetName (name string){ dog.name = name } 对于 ...