hdu 5448 Marisa’s Cake(计算几何加推公式)
来源:互联网 发布:淘宝返积分 编辑:程序博客网 时间:2024/06/04 19:18
Marisa’s Cake
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 210 Accepted Submission(s): 123
Problem Description
Today is Marisa’s birthday and she is given a cake the shape of a convex polygon of n vertices. Furthermore, all the n vertices are all going to be on integer points of the Cartesian coordinate system. Marisa asks her friend Reimu to randomly cut off some vertices of the cake. The set of vertices chosen to be cut off has size at most n−3 , and all possible choices have the same probability to be picked up. In order to remove all chosen vertices, Reimu might cut the cake several times. Since Reimu is a perfectionist, she will always cut the cake from one vertex to another. Hence, Reimu’s cutting will not generate vertices which are not in the original polygon.
Marisa wants to know the expected size of the cake she might get, and because you are madly in love with her, you decided to do anything she wants for her! You take out your laptop and are ready to calculate the expected size for Marisa. However, Marisa is bad with fractions. She wants to make sure the answer she gets is always an integer. To do that, she would like you to multiply the answer with the total number of possible cakes there are. Unfortunately, that still doesn’t guarantee the answer be an integer. An additional 2 must also be multiplied into the answer as well. For example, letA=(0,0),B=(1,0),C=(1,1),D=(0,2) and ABCD is the shape of Marisa’s birthday cake. There are 5 possible pieces ABCD,ABC,BCD,CDA,DAB that Marisa might get, and the corresponding area of these convex polygons are 32,12,12,1,1 respectively. The expected size of the cake that Marisa might get is (32+12+12+1+1)÷5=910 , and what you should tell Marisa 910×5×2=9 . Calculate the answer for Marisa and who knows, maybe she would love you back!
Marisa wants to know the expected size of the cake she might get, and because you are madly in love with her, you decided to do anything she wants for her! You take out your laptop and are ready to calculate the expected size for Marisa. However, Marisa is bad with fractions. She wants to make sure the answer she gets is always an integer. To do that, she would like you to multiply the answer with the total number of possible cakes there are. Unfortunately, that still doesn’t guarantee the answer be an integer. An additional 2 must also be multiplied into the answer as well. For example, let
Input
The first line on the input contains an integer T(T≤10) . There will be T test cases. The first line of each test case contains an integer n(3≤n≤100000) indicating the number of vertices of the convex polygon. The i−th of the following n lines contains two integers xi and yi(0≤x,y≤109) separated by a blank. (xi,yi) is the i−th vertex of the convex polygon, and (x1,y1),...,(xn,yn) will be in counterclockwise order.
Output
For each test case, output a number as the answer. In case the answer is greater than 1000000006 , please modulo the answer with 1000000007 . (You might figure out by now that Marisa is not good at dealing with large numbers either)
Sample Input
240 01 01 10 251 13 13 22 31 2
Sample Output
950solution:给定一个多边形的顶点位置,问你由这些顶点构成的多边形的面积是多少面积乘
2 的话直接算叉积就好了,例如三个点A,B,C 构成的三角形,设从原点到他们的向量为fA,fB,fC ,那么三角形面积的2 倍就是fA×fB+fB×fC+fC×fA ,同理k 个点(对应向量按逆时针排序为f1,f2,...,fk )构成的多边形面积的2 倍等于∑k−1i=1fi×fi+1+fk×f1 。
直接算每种情况的多边形求面积是O(n2n) 的,但是我们可以发现计算面积的过程中有很多的重复计算。
对于两个在原多边形上的点i,j ,我们可以算出它们产生的贡献fi×fj 在多少个需要算的多边形M={a0,a1,...,ap−1} 的式子里出现。
首先如果at=i ,那么必须有a(t+1)modp=j ,也就是说按逆时针顺序在i 和j 之间的点都不能选,而且为了保证p≥3 ,所以按逆时针顺序在j 到i 这一段的点至少要有一个在多边形M 上,设j 到i 之间有c 个点,那么对应的方案数就是2c−1 ,于是我们得到了任意两个点之间向量叉积的系数。
于是答案可以被表示为∑i=1n∑j=1i−1(2i−j−1−1)(fi×fj)+(2n−i+j−1−1)(fj×fi)=∑i=1n(2i−1fi)×(∑j=1i−12−jfj)+(∑j=1i−12jfj)×(2n−i−1fi)
如果令g(x)=∑i=1x2ifih(x)=∑i=1x2−ifi
答案就是∑i=1n(2i−1fi)×h(i−1)+g(i−1)×(2n−i−1fi)
于是在模意义下预处理2i 和2−i ,边利用前缀和的特点维护g(i),h(i) ,边统计答案就行了,时间复杂度O(n) 。#include<cstdio>using namespace std;const int mod = 1e9 + 7;const int maxn = 100050;int n ;long long mul[maxn], inv[maxn];struct Point{ int x, y; Point(int x = 0, int y = 0) :x(x), y(y){} int operator *(const Point &p){ return (1ll * x*p.y%mod - 1ll * y*p.x%mod + mod) % mod; } Point operator *(const long long p){ return Point(p*x%mod, y*p%mod); } Point operator + (const Point &p){ return Point((x + p.x) % mod, (y + p.y) % mod); }}e,mul1,inv1;int main(){ int t; scanf("%d", &t); int inv2 = 500000004;//由费马小定理求出 mul[0] = inv[0] = 1; for (int i = 1; i < maxn; i++) { mul[i] = (2 * mul[i - 1]) % mod; inv[i] = (inv2*inv[i - 1]) % mod; }//求2^i 和2^(-i)的前缀 while (t--) { long long ans = 0; scanf("%d", &n); mul1 = inv1 = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &e.x, &e.y); ans = (ans + (e*mul[i - 1])* inv1) % mod; if (i == n)ans = (ans + mul1*(e*inv2)) % mod;//此时n-i-1为-1 因此要用2的逆元inv2 else ans = (ans + mul1*(e*mul[n - i - 1])) % mod; mul1 = mul1 + e*mul[i]; inv1 = inv1 + e*inv[i]; } printf("%I64d\n", ans); } return 0;}
0 0
- hdu 5448 Marisa’s Cake(计算几何加推公式)
- HDU 5448 Marisa's Cake(计算几何)
- hdu 5448 Marisa’s Cake(几何+凸包)
- HDU 5448 Marisa’s Cake
- 【HDU 5448】Marisa’s Cake
- HDOJ 5448 Marisa’s Cake
- HDU 5448 Marisa’s Cake(n个点中任意点组成的多边形面积之和)
- HDU 5640:King's Cake【几何】
- HDu 4762 Cut the Cake 概率(推公式,积分)
- HDU 4798 Skycity (计算几何+推公式) 2013 Asia Changsha Regional Contest
- HDU 4454 Stealing a Cake (计算几何+三分)
- HDU 4454 Stealing a Cake(暴力计算几何)
- POJ1905 Expanding Rods(计算几何推公式+二分)
- hdu4454Stealing a Cake+计算几何
- HDU 3117 Fibonacci Numbers(Fibonacci矩阵加速递推+公式)
- HDU 1756 Cupid's Arrow 计算几何
- hdu 1756 Cupid's Arrow 计算几何
- hdu 1756 Cupid's Arrow 计算几何
- cat, more, less命令
- Android 2D绘图解析之 Canvas,Paint
- 将具有关联关系的两个表通过hibernate从数据库中查出来后转成Json对象时报错解决:采用过滤器
- JavaSE入门开发常见问题--已更待删
- Android学习之RecylerView完美实现瀑布流效果
- hdu 5448 Marisa’s Cake(计算几何加推公式)
- 归途与征程
- iOS一个全屏滑动返回手势
- CentOS 6 安装 APC(Alternative PHP Cache)
- 显示操控方法和装置、用户终端及显示操控系统
- 深入理解C++中的mutable关键字
- 微信分享的问题
- 小牛学堂
- myeclipse集成egit图文教程