1059 - Jacquard Circuits

The eccentric sculptor Albrecht Caravaggio Mondrian has been inspired by the history of the computer to create works of art that he calls ``Jacquard circuits." Each of his works of art consists of a series of polygonal circuit boards (defined below), all having the same shape but at different scales, joined together with wire or string into a three-dimensional tiered structure. Figure 1 below shows an example with two levels and a pentagonal shape.

\epsfbox{p2395a.eps}

A.C.M. (as he is known to his friends) bases his art upon the punched hole cards of the Jacquard loom (that were later adapted by Charles Babbage for his analytical engine) and upon the regular grid layout approach often used in circuit interfacing (for instance, Pin Grid Arrays).

The circuit boards used in the sculptures are in the shapes of lattice polygons. A lattice polygon is defined as any closed, non-self-intersecting cycle of straight line segments that join points with integer coordinates, with no two consecutive line segments parallel. For any given lattice polygon P , there is a smallest lattice polygon with the same shape and orientation as P -- call this the origin. Smaller polygons of the same shape and orientation as P are called its predecessors and polygons larger than P are its successors. (See Figure 2 on the following page.)

To build one of his sculptures, A.C.M. begins by randomly selecting N lattice points and then drawing a pattern by connecting these points. (Note that not all of the N points are necessarily vertices of a lattice polygon. This may happen, for example, if three or more consecutive points are collinear.) Let P be the lattice polygon determined by the pattern. Then he determines the origin corresponding to P , selects the number of levels, M , he wishes to use, and constructs the first M polygonal circuit boards in the series (that is, the origin and its first M - 1 successors). Each lattice point lying strictly within the boundary of any of these polygons is a hole where strings or wires meet.

The hard part of creating the sculpture is tying together all the strings or wires that meet at a given hole. Furthermore, some of A.C.M.'s famous miniaturized sculptures, built using nano-engineering techniques, involve hundreds of thousands of levels. Mondrian would like a way to determine, given a polygonal shape and the number of levels, how many holes there will be in the final sculpture. You must write a program to help him. (For example, the sculpture in Figure 1 above has 15 holes.) Assume holes have zero diameter.

\epsfbox{p2395b.eps}

Input 

The input consists of one or more test cases. Each case begins with a line containing two positive integersN (3$ \le$N$ \le$1000) and M (1$ \le$M$ \le$1000000) . N is the number of lattice points Mondrian selected and M is the number of levels in the completed sculpture. Each of the next N lines contains two integers x , y (| x|,| y|$ \le$1000000) which denote the coordinates of one of Mondrian's points. Points are listed in either clockwise or counterclockwise order.

The last test case is followed by a line containing two zeroes.

Output 

For each test case, print a line containing the test case number (beginning with 1) followed by the number of holes in an M -level sculpture starting from the origin polygon of the given pattern. In no case will this value exceed the maximum possible value of a 64-bit signed integer.

Sample Input 

5 2 
0 0 
8 0 
12 4 
8 8 
0 8 
3 2 
-1 -1 
3 1 
5 -1 
0 0

Sample Output 

Case 1: 15 
Case 2: 2






#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
int n,m,i,t,d,l,r,B,x[1009],y[1009],mark[1009];

long long S,u,v;
unsigned long long g;

void gcd(int &d,int x)
{
	int z;
	d=labs(d);
	x=labs(x);
	while(x%d)
	{
		z=x%d;
		x=d;
		d=z;
	}
}

int main()
{
	while(scanf("%d%d",&n,&m)&&n)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&x[i],&y[i]);
			mark[i]=1;
		}
		for(i=0;i<n;i++)
		{
			l=(i+n-1)%n;
			r=(i+1)%n;
			u=x[l]-x[i];
			u*=y[r]-y[i];
			v=y[l]-y[i];
			v*=x[r]-x[i];
			if(u==v)
				mark[i]=0;
		}
		l=0;
		for(i=0;i<n;i++)
			if(mark[i])
			{
				x[l]=x[i];
				y[l++]=y[i];
			}
		n=l;
		for(i=1;i<n;i++)
		{
			x[i]-=x[0];
			y[i]-=y[0];
		}
		x[0]=0;
		y[0]=0;
		x[n]=0;
		y[n]=0;
		for(i=1;i<n;i++)
		{
			if(x[i])
				d=x[i];
			if(y[i])
				d=y[i];
		}
		
		for(i=1;i<n;i++)
		{
			gcd(d,x[i]);
			gcd(d,y[i]);
		}
		B=0;
		for(i=1;i<=n;i++)
		{
			x[i]/=d;
			y[i]/=d;
			r=x[i]-x[i-1];
			if(r!=0)
				gcd(r,y[i]-y[i-1]);
			else
				r=labs(y[i]-y[i-1]);
			B+=r;
		}
		S=0;
		for(i=0;i<n;i++)
		{
			u=x[i];
			u*=y[i+1];
			v=x[i+1];
			v*=y[i];
			S+=u-v;
		}
		if(S<0)
			S=-S;
		u=m;
		u*=m+1;
		u/=2;
		u*=m+m+1;
		u/=3;
		g=u;
		g*=S;
		v=m;
		v*=m+1;
		v/=2;
		v*=B;
		g=g-v+m+m;
		printf("Case %d: %llu\n",++t,g/2);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值