2013
11-10

# Fourier’s Lines

Joseph Fourier was a great mathematician and physicist and is well known for his mathematic series. Among all the nineteen children in his family, Joseph was the youngest and the smartest. He began to show his interest in mathematics when he was very young. After he grew up, he often corresponded with C. Bonard (a professor of mathematics at Auxerre) by exchanging letters.

In one letter written to Bonard, Fourier asked a question: how to draw 17 lines on a plane to make exactly 101 crossings, where each crossing belongs to exactly two lines. Obviously, this is an easy problem, and Figure-1 is a solution that satisfies his requirement. Now the problem for you is a universal one. Can we draw N lines on a plane to make exactly M crossings, where each crossing belongs to exactly two lines? If we can, how many pieces, at most, can these lines cut the plane into?

The input may have several sets of test data. Each set is one line containing two integers N and M (1 <= N <= 100, 0 <= M <= 10000), separated by a space. The test data is followed by a line containing two zeros, which indicates the end of input and should not be processed as a set of data.

Output one line for each set of input in the following format:

Case i: N lines cannot make exactly M crossings.

if the drawing of these lines is impossible;

or:

Case i: N lines with exactly M crossings can cut the plane into K pieces at most.

Note: Even if N or M equals to one, you should use the words “lines” and “crossings” in your output.

4 3
4 6
4 2
5 11
17 101
0 0


Case 1: 4 lines with exactly 3 crossings can cut the plane into 8 pieces at most.
Case 2: 4 lines with exactly 6 crossings can cut the plane into 11 pieces at most.
Case 3: 4 lines cannot make exactly 2 crossings.
Case 4: 5 lines cannot make exactly 11 crossings.
Case 5: 17 lines with exactly 101 crossings can cut the plane into 119 pieces at most.


/* @author: */
import java.util.Scanner;
public class Main {

static int com(int a)
{
return a*(a-1)/2;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

long line[][]=new long[101][10001];
int i,j,k,p,q,cs=0;
long  max,tmp;
for(i=1;i<=100;i++)
line[i][0]=i+1;
for(i=2;i<=100;i++)
for(j=1;j<=com(i);j++)
{
max=0;
for(k=1;k<=i-1;k++)
{
p=i-k;q=j-k*p;
if(q>=0)
{
if(line[p][q]!=0)
{
tmp=line[p][q]+k*(p+1);
if(tmp>max)
max=tmp;
}
}
}
line[i][j]=max;
}
while (sc.hasNext())
{
i=sc.nextInt();
j=sc.nextInt();
if(i==0&&j==0)
break;
cs++;
if(line[i][j]>0)
System.out.printf("Case %d: %d lines with exactly %d crossings can cut the plane into %d
pieces at most.\n",cs,i,j,line[i][j]);
else System.out.printf("Case %d: %d lines cannot make exactly %d crossings.\n",cs,i,j);
}
}
}

1. a是根先忽略掉，递归子树。剩下前缀bejkcfghid和后缀jkebfghicd，分拆的原则的是每个子树前缀和后缀的节点个数是一样的，根节点出现在前缀的第一个，后缀的最后一个。根节点b出现后缀的第四个位置，则第一部分为四个节点，前缀bejk，后缀jkeb，剩下的c出现在后缀的倒数第2个，就划分为cfghi和 fghic，第3部分就为c、c

2. 如果两个序列的最后字符不匹配（即X [M-1]！= Y [N-1]）
L（X [0 .. M-1]，Y [0 .. N-1]）= MAX（L（X [0 .. M-2]，Y [0 .. N-1]），L（X [0 .. M-1]，Y [0 .. N-1]）
这里写错了吧。

3. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;