[HDU] [1466] Number of Intersections of Lines

Problem Description

Loading...

Problem Analysis

Since not every pair of lines intersects, we need to determine the number of parallel lines
For each n, if there are j parallel lines, we can loop over and check
Obviously, the intersection points contributed by parallel lines are
number of parallel lines * number of non-parallel lines
The non-parallel lines can also have various combinations of intersection point counts
So we can add them recursively, adding the possible counts for the non-parallel lines
That is, dp[i]=(i-j)*j+dp[i-j]
It is clear that this method also includes cases where the non-parallel lines contain parallel lines
But this does not affect the result; it only causes duplicate output
So we just need to check whether this point-count scheme has been used before

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int a[21][200];
int main() {
	for(int i=0; i<=20; i++)
		for(int j=0; j<i; j++)
			if(j==0)
				a[i][j]=1;
			else
				for(int k=0; k<=i*(i-1)/2; k++)
					if(a[i-j][k]==1)
						a[i][(i-j)*j+k]=1;
	int n;
	while(scanf("%d",&n)!=EOF) {
		for(int i=0; i<=n*(n-1)/2; i++) {
			if(a[n][i]==1)
				printf("%d%c",i,i==n*(n-1)/2?'\n':' ');
		}
	}
	return 0;
}

Loading...