codeforces 559D Randomizer

26 篇文章 0 订阅
10 篇文章 0 订阅

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 .

Input

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.

Output

Print the sought expected value with absolute or relative error at most 10 - 9.

Examples
input
4
0 0
2 0
2 2
0 2
output
0.2
input
5
0 0
2 0
2 2
1 3
0 2
output
0.8125
Note

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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值