codeforces 559D Randomizer
来源:互联网 发布:混合横截面数据 编辑:程序博客网 时间:2024/06/05 07:36
Gerald got tired of playing board games with the usual six-sided die, and he bought a toy called Randomizer. It functions as follows.
A Randomizer has its own coordinate plane on which a strictly convex polygon is painted, the polygon is called a basic polygon. If you shake a Randomizer, it draws some nondegenerate (i.e. having a non-zero area) convex polygon with vertices at some vertices of thebasic polygon. The result of the roll (more precisely, the result of the shaking) is considered to be the number of points with integer coordinates, which were strictly inside (the points on the border are not considered) the selected polygon. Now Gerald is wondering: what is the expected result of shaking the Randomizer?
During the shaking the Randomizer considers all the possible non-degenerate convex polygons with vertices at the vertices of the basic polygon. Let's assume that there are k versions of the polygons. Then the Randomizer chooses each of them with probability .
The first line of the input contains a single integer n (3 ≤ n ≤ 100 000) — the number of vertices of the basic polygon.
Next n lines contain the coordinates of the vertices of the basic polygon. The i-th of these lines contain two integers xi and yi( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th vertex of the polygon. The vertices are given in the counter-clockwise order.
Print the sought expected value with absolute or relative error at most 10 - 9.
40 02 02 20 2
0.2
50 02 02 21 30 2
0.8125
A polygon is called strictly convex if it is convex and no its vertices lie on the same line.
Let's assume that a random variable takes values x1, ..., xn with probabilities p1, ..., pn, correspondingly. Then the expected value of this variable equals to .
计算几何+期望+神奇的思路~
前置技能:皮克定理S=内部格点数+边上格点数/2-1.
n个点,可以构成的多边形数为:2^n-1-n-n*(n-1)/2,为所有选择方式-一个点都没选上-单点-两点。
枚举一条边a[i]a[i+k],只考虑它的一边,点数的概率是(2^(n-k-1)-1)/构成的多边形数。我们枚举这条边,顺便求出点的个数乘上概率。
如果看不懂上面的,可以看看这个:http://blog.leanote.com/post/dataisland/CF559D,非常棒!
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define pick(u,v) (u+1.0-0.5*v)#define cal(u) (n>100 ? 1/inc[u+1]:(inc[n-k-1]-1.0)/kk) #define ne(u) ((u+1)%n)int n;long long cnt1,cnt2,kkz;double ans,inc[100001],kk,tot,now;struct node{double x,y;node operator - (const node&u){return (node){x-u.x,y-u.y};}double operator * (const node&u){return x*u.y-y*u.x;}}a[100001],tmp,l,r;int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}int GCD(int u,int v){return v ? GCD(v,u%v):u;}int gcd(int u,int v){return GCD(abs(u),abs(v));}int main(){n=read();inc[0]=1;for(int i=0;i<n;i++) a[i]=(node){read(),read()},inc[i+1]=inc[i]*2.0;kk=inc[n]-1-n-(double)n*(n-1)*0.5;tmp=a[n-1]-a[0];cnt1+=gcd(tmp.x,tmp.y);r=a[1]-a[0];cnt1+=gcd(r.x,r.y);for(int i=2;i<n;i++){l=a[i]-a[0];tmp=a[i]-a[i-1];tot+=r*l*0.5;cnt1+=gcd(tmp.x,tmp.y);r=l;}for(int i=0;i<n;i++){r=a[ne(i)]-a[i];cnt2=gcd(r.x,r.y);now=0;for(int j=ne(ne(i)),k=2;k<=35 && ne(j)!=i;j=ne(j),k++){l=a[j]-a[i];tmp=l-r;cnt2+=(kkz=gcd(l.x,l.y))+gcd(tmp.x,tmp.y);now+=r*l*0.5;ans+=(pick(now,cnt2)+(ne(j)==i ? 0:kkz-1))*cal(k);r=l;cnt2-=kkz;}}printf("%.7lf\n",pick(tot,cnt1)-ans);return 0;}
- codeforces 559D Randomizer
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- Codeforces 275D
- Codeforces 126D
- Codeforces 337d
- Codeforces D. Points
- codeforces 96D Volleyball
- Codeforces 165D
- codeforces 180D
- 世界如此美好,我却如此暴躁,这样不好,不好
- 不用加减乘除做加法的一些方法
- linux Redis安装
- es6的扩展运算符
- 数据结构基础一之----预备知识
- codeforces 559D Randomizer
- 算法系列—— Reverse Linked List II
- JavaFX——懵懂
- 多个Activity之间共用一个Socket实例
- 放假啦
- win7 安装Redis
- Java web 开发中eclipse的数据库驱动程序加载问题
- string和stringstream用法总结
- 【面经笔记】TCP/IP