The famous Glenbow Museum in Calgary isWestern Canada’s largest museum, with exhibits ranging from art to culturalhistory to mineralogy. A brand new section is being planned, devoted tobrilliant computer programmers just like you. Unfortunately, due to lack ofspace, the museum is going to have to build a brand new building and relocateinto it. The size and capacity of the new building differ from those of theoriginal building. But the floor plans of both buildings are orthogonalpolygons. An orthogonal polygon is a polygon whose internal angles are either90 or 270. If 90 angles are denoted as R (Right) and 270 angles are denoted asO (Obtuse) then a string containing only R and O can roughly describe anorthogonal polygon. For example, a rectangle (Figure 1) is the simplestorthogonal polygon and it can be described as RRRR (the angles are listed incounter-clockwise order, starting from any corner). Similarly, a cross-shapedorthogonal polygon (Figure 2) can be described by the sequence RRORRORRORRO,RORRORRORROR, or ORRORRORRORR. These sequences are called angle strings.
Of course, anangle string does not completely specify the shape of a polygon — it saysnothing about the length of the sides. And some angle strings cannot possiblydescribe a valid orthogonal polygon (RRROR, for example). To complicate thingsfurther, not all orthogonal polygons are acceptable floor plans for the museum.A museum contains many valuable objects, and these objects must be guarded. Dueto cost considerations, no floor can have more than one guard. So a floor planis acceptable only if there is a place within the floor from which one guardcan see the entire floor. Similarly, an angle string is acceptable only if itdescribes at least one acceptable polygon. Note that the cross-shaped polygonin Figure 2 can be guarded by someone standing in the center, so it is acceptable.Thus the angle string RRORRORRORRO is acceptable, even though it also describesother polygons that cannot be properly guarded by a single guard. Help thedesigners of the new building determine how many acceptable angle strings thereare of a given length.
Input
The input file contains several test cases.Each test case consists of a line containing a positive integer L (1 ≤ L ≤1000), which is the desired length of an angle string. The input will end witha line containing a single zero.
Output
For each test case, print a line containingthe test case number (beginning with 1) followed by the number of acceptableangle strings of the given length. Follow the format of the sample output.
Sample Input
4
6
0
Sample Output
Case 1: 1
Case 2: 6
【题意】
对于一个边平行于坐标轴的多边形,我们可以用一个由R和O组成的序列描述。从某个顶点开始按照逆时针走,碰到一个90°内角记R,270°内角记O,形成一个角度序列。给定正整数L,有多少个长为L的角度序列对应一个星型多边形,即多边形中存在一点可以看到多边形边界上的每一个点。多边形每条边的长度任意。
【思路】
多边形的长度l就是顶点数n,当n为奇数或者n<4时无解。设有x个O,y个R,则由内角和公式(n-2)*180=270x+90y,和x+y=n联立可得x=(n-4)/2,y=(n+4)/2。通过画图可以看出构成星型多边形必须由4个阶梯序列OROROR…ORO和4个RR构成。也就是说序列中的O不能相邻。问题转换成了,x=(n-4)/2个O,y=(n+4)/2个R构成一个序列,且O不相邻(首尾也不能同时为O)。用组合数学推公式,假设现在序列的首部和尾部都是R,那么用隔板法将O插入,有y-1个位置可以插入,即C(x,y-1);假设序列首部是R,尾部是O,那么有y-1个位置可以插入,即C(x-1,y-1),同理首部是O,尾部是R也是C(x-1,y-1),求和即可得到ans = n*n*(n+2)*(n-2)/192
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main() {
int kase = 1;
while (scanf("%lld", &n) == 1 && n) {
printf("Case %d: ", kase++);
if (n < 4 || n & 1) {
printf("0\n");
continue;
}
printf("%lld\n", n*n*(n+2)*(n-2)/192);
}
return 0;
}