Points in a Plane


Problem Statement :


There are N points on an XY plane. In one turn, you can select a set of collinear points on the plane and remove them. Your goal is to remove all the points in the least number of turns. Given the coordinates of the points, calculate two things:

The minimum number of turns (T) needed to remove all the points.
The number of ways to to remove them in T turns. Two ways are considered different if any point is removed in a different turn.
Input Format

The first line contains the number of test cases T. T test cases follow. Each test case contains N on the first line, followed by N lines giving the coordinates of the points.

Constraints

1 <= T <= 50
1 <= N <= 16
0 <= xi,yi <= 100
No two points will have the same coordinates.

Output Format

Output T lines, one for each test case, containing the least number of turns needed to remove all points and the number of ways to do so. As the answers can be large, output them modulo 1000000007.



Solution :



title-img


                            Solution in C :

In C++ :





#include<iostream>
#include<set>
#include<map>
#include<string>
#include<stdio.h>
#include<sstream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<string.h>
using namespace std ;
#define MOD 1000000007
#define INF (int)1e9
#define MAXN 20
typedef pair<int,int> P ;

int n,pre[MAXN],fac[MAXN],x[MAXN],y[MAXN] ;
int col[MAXN][MAXN] ;

char bit[1 << MAXN] ;
char good[1 << MAXN] ;
char best[1 << MAXN] ;
char valid[1 << MAXN] ;

char vid,id[1 << MAXN] ;
int memo[1 << MAXN] ;
int solve(int mask)
{
 if(bit[mask] <= 2) return 1 ;
 if(id[mask] == vid) return memo[mask] ;
 if(good[mask]) return pre[bit[mask]] ;
 id[mask] = vid ;

 int j,nmask = mask ;
 for(j = 0;j < n;j++) if(mask & 1 << j)
 {
  nmask ^= 1 << j ;
  break ;
 }

 int ways = 0,can = best[mask] ;
 if(best[nmask] == can - 1) ways = solve(nmask) ;
 for(int i = nmask;i > 0;i = ((i - 1) & nmask))
 {
  int k = i | 1 << j ;
  if(valid[k] && best[mask ^ k] == can - 1)
  {
   ways += solve(mask ^ k) ;
   if(ways >= MOD) ways -= MOD ;
  }
 }
 return memo[mask] = ways ;
}

void generate()
{
 for(int tt = 0;tt < 10;tt++)
 {
  char in[] = "in .txt" ;
  in[2] = tt + '0' ;
  FILE * fout = fopen(in,"w") ;
  
  int runs = 50 ;
  fprintf(fout,"%d\n",runs) ;
  for(int j = 0;j < runs;j++)
  {
   n = rand() % 18 ;
   if(tt == 8) n = 18 ;
   
   char vis[102][102] ;
   memset(vis,0,sizeof vis) ;
   for(int i = 0;i < n;i++)
   {
    if(tt < 3 && j < 15)
    {
     x[i] = rand() % 10 ;
     y[i] = rand() % 10 ;     
    }
    else if(tt < 6 && j < 15)
    {
     x[i] = rand() % 5 ;
     y[i] = rand() % 5 ;
    }
    else if(tt < 10 && j < 15)
    {
     x[i] = i ;
     y[i] = i + (rand() % 5 - 2) ;
     if(y[i] < 0) y[i] = i ;
    }
    else
    {
     x[i] = rand() % 100 + 1 ;
     y[i] = rand() % 100 + 1 ;
    }
    
    if(vis[x[i]][y[i]]) { i-- ; continue ; }
    vis[x[i]][y[i]] = 1 ;
   }


   fprintf(fout,"%d\n",n) ;
   for(int i = 0;i < n;i++) fprintf(fout,"%d %d\n",x[i],y[i]) ;
  }
  fclose(fout) ;
 }
}

int main()
{
 fac[0] = 1 ;
 for(int i = 1;i < MAXN;i++) fac[i] = 1LL * i * fac[i - 1] % MOD ;
 for(int i = 1;i < 1 << MAXN;i++) bit[i] = bit[i >> 1] + (i & 1) ;
 pre[0] = pre[1] = 1 ;
 for(int i = 2;i < MAXN;i++)
 {
  pre[i] = 1LL * pre[i - 2] * (i - 1) % MOD ;
  if(i % 2 == 1) pre[i] += pre[i - 1] ;
  pre[i] %= MOD ;
 }

// generate() ; return 0 ;

 int runs ; 
 scanf("%d",&runs) ;
 while(runs--)
 {
  scanf("%d",&n) ;
  for(int i = 0;i < n;i++) scanf("%d%d",&x[i],&y[i]) ;
  
  memset(col,0,sizeof col) ;
  for(int k1 = 0;k1 < n;k1++)
   for(int k2 = 0;k2 < n;k2++)
   {
    for(int j = 0;j < n;j++)
    {
     int area = x[j] * (y[k1] - y[k2]) + x[k1] * (y[k2] - y[j]) + x[k2] * (y[j] - y[k1]) ;
     if(area == 0) col[k1][k2] |= 1 << j ;
    }
   }
  
  for(int i = 0;i < 1 << n;i++)
  {
   if(bit[i] <= 2) { valid[i] = true ; continue ; }
   for(int j = 0;j < n;j++) if(i & 1 << j)
   {
    int k1 = -1 ;
    for(int k = j + 1;k < n;k++) if(i & 1 << k) { k1 = k ; break ; }
    if((col[j][k1] | i) == col[j][k1]) valid[i] = true ;
    else valid[i] = false ;
    break ;
   }
  }
  
  best[0] = 0 ;
  for(int i = 1;i < 1 << n;i++)
  {
   if(bit[i] == 1) { best[i] = 1 ; continue ; }
   int j;
   for(j = 0;j < n;j++) if(i & 1 << j) break ;
   
   int cret = n ;
   for(int k = j + 1;k < n;k++)
    if(i & 1 << k)
     cret = min(cret,1 + best[i & ~col[j][k]]) ;
   best[i] = cret ;
  }
  
  for(int i = 0;i < 1 << n;i++)
  {
   good[i] = 1 ;
   if(bit[i] <= 2) continue ;
   int j;
   for(j = 0;j < n;j++) if(i & 1 << j) break ;
   if(!good[i ^ 1 << j]) { good[i] = 0 ; continue ; }
   
   for(int k = j + 1;k < n;k++)
    if(i & 1 << k)
     if(bit[i & col[j][k]] > 2)
      good[i] = 0 ;
  }
  
  int tot = best[(1 << n) - 1] ;
  vid++ ;
  int ret = solve((1 << n) - 1) ;
  ret = 1LL * ret * fac[tot] % MOD ;
  printf("%d %d\n",tot,ret) ;
 }
 
 return 0 ;
}








In Java :






import java.util.*;
import java.io.*;

class Solution
{
	BufferedReader input;
	BufferedWriter out;
	StringTokenizer token;

	int N;
	int[] x,y;
	int[] dp,dp3;
	boolean[] ok;
	int[] member;
	int mod = 1000000007;

	int BitCount(int x)
	{
		int ret = 0;
		while(x > 0)
		{
			if( (x&1) != 0 ) ret++;
			x >>= 1;
		}
		return ret;
	}

	boolean collinear(int set)
	{
		int ctr = 0;
		for(int i = 0; set > 0; i++)
		{
			if( (set&1) != 0 )
				member[ctr++] = i;
			set >>= 1;
		}
		if(ctr <= 2)return true;
		int a = x[member[0]]-x[member[1]];
		int b = y[member[0]]-y[member[1]];
		for(int i = 2; i < ctr; i++)
		{
			int aa = x[member[0]]-x[member[i]];
			int bb = y[member[0]]-y[member[i]];
			if(aa*b != a*bb)return false;
		}
		return true;
	}

	String binary(int x)
	{
		String ret = "";
		for(int i = 0; i < N; i++)
		{
			if( ((x>>i)&1) == 0) ret = "0"+ret;
				else ret = "1"+ret;
		}
		return ret;
	}

	void solve() throws IOException
	{
		long qq = System.currentTimeMillis();
		input = new BufferedReader(new InputStreamReader(System.in));
		out = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = nextInt();
		int twoMax = (1<<16);
		dp = new int[twoMax];
		x = new int[16];
		y = new int[16];
		ok = new boolean[twoMax];
		dp3 = new int[twoMax];
		member = new int[16];
		ArrayList<Integer> o;
		for(int t = 0; t < T; t++)
		{
			N = nextInt();
			int twoN = (1<<N);
			for(int i = 0; i < N; i++)
			{
				x[i] = nextInt();
				y[i] = nextInt();
			}
			o = new ArrayList<Integer>();
			for(int i = twoN-1; i > 0; i--)
			{
				ok[i] = false;
				if(collinear(i))
				{
					ok[i] = true;
					o.add(i);
				}
			}
			Arrays.fill(dp,-1);
			dp[0] = 0;
			dp3[0] = 1;
			int m = 0;
			for(int i = 0; i < o.size(); i++)
			{
				int ii = o.get(i);
				for(int j = m; j >= 0; j--)
				{
					if((ii&j) == 0 && dp[j] != -1)
					{
						m = Math.max(m,j|ii);
						if(dp[j|ii] == -1 || dp[j|ii] > 1+dp[j])
						{
							dp[j|ii] = 1+dp[j];
							dp3[j|ii] = (int)(((long)(dp[j]+1)*dp3[j])%mod);
						}
						else if(dp[j|ii] == 1+dp[j])
						{
							dp3[j|ii] += ((long)(dp[j]+1)*dp3[j])%mod;
							dp3[j|ii] %= mod;
						}
					}
				}
			}
			out.write(""+ dp[(twoN)-1] + " " + dp3[(twoN)-1]);
			out.newLine();
		}
		out.flush();
		out.close();
		input.close();

	}

	int nextInt() throws IOException
	{
		if(token == null || !token.hasMoreTokens())
			token = new StringTokenizer(input.readLine());
		return Integer.parseInt(token.nextToken());
	}

	Long nextLong() throws IOException
	{
		if(token == null || !token.hasMoreTokens())
			token = new StringTokenizer(input.readLine());
		return Long.parseLong(token.nextToken());
	}

	String next() throws IOException
	{
		if(token == null || !token.hasMoreTokens())
			token = new StringTokenizer(input.readLine());
		return token.nextToken();
	}

	public static void main(String[] args) throws Exception
	{
		new Solution().solve();
	}
}








In C :







#include <stdio.h>

#define P 1000000007

long long g=1,p[20][2],t,tt,v,kon,a[20][70000][2],b[70000];
long long i,j,k,l,m,n,maz[70000],mmaz[70000];


void uloz(long long mam, long long ind, long long vv)
{
if(ind ==n) {mmaz[mam]=1;return;}

uloz(mam, ind+1,vv);

if(vv&(1<<ind)) uloz(mam+(1<<ind),ind+1,vv);

return;
}

void priamka(long long xx, long long yy)
{
long long vv=0,ii;

for(ii=0;ii<n;ii++)
  {
  vv*=2;

  if((p[ii][0]-p[xx][0])*(p[yy][1]-p[xx][1]) == (p[ii][1]-p[xx][1])*(p[yy][0]-p[xx][0]))
     vv++;
  }


if(maz[vv]==0) uloz(0,0,vv);

maz[vv]=1;

return;
}


void pocitaj(long long ind)
{
long long ii,jj,kk,min;

for(ii=0;ii<(1<<n);ii++) {a[ind][ii][0]=0;a[ind][ii][1]=0;}

for(ii=0;ii<(1<<n);ii++)
  if(a[ind-1][ii][1] && mmaz[ii])
    {
    a[ind][0][1] = 1;
    a[ind][0][0] = (a[ind][0][0] + a[ind-1][ii][0])%P;
    kon=1;
   }

//printf("%lld=kon\n",kon);

if(kon) return;

a[ind][0][0]=0;
a[ind][0][1]=0;



for(ii=0;ii<(1<<n);ii++)
  if(a[ind-1][ii][1])
    {
    g++;
    while(((1<<min)&ii) == 0) min++;

    for(jj=0;jj<l;jj++)
       // if(kk=(maz[jj]&ii)) makaj1(ind,ii,kk,0,0);

        if(b[kk=(ii&maz[jj])]!=g && (kk&(1<<min)))
           {
             b[kk]=g;
             a[ind][ii^kk][1] = 1;
             a[ind][ii^kk][0] = (a[ind][ii^kk][0] + a[ind-1][ii][0])%P;
           }


    }


//kon=1;

return;
}


int main()
{

scanf("%lld",&t);
for(tt=0;tt<t;tt++)
 {
 scanf("%lld",&n);
 for(i=0;i<n;i++) scanf("%lld %lld",&p[i][0],&p[i][1]);

 for(i=0;i<(1<<n);i++) {maz[i]=0;mmaz[i]=0;}


//  for(i=0;i<n;i++) {mmaz[(1<<i)]=1;maz[(1<<i)]=1;}
 if(n==1) {mmaz[1]=1;maz[1]=1;}



 for(i=0;i<n;i++)
  for(j=i+1;j<n;j++)
    {
    priamka(i,j);
 //   printf("%lld %lld -> %lld\n",i,j,priamka(i,j));
    }

for(i=0;i<(1<<n);i++) maz[i]=mmaz[i];

l=0;
  for(i=1;i<(1<<n);i++)
     if(maz[i]) maz[l++] = i;

//printf("%lld\n",l);

//for(i=0;i<l;i++) printf("%lld..\n",maz[i]);


 k=0;
 for(i=0;i<(1<<n);i++) a[0][i][1]=0;
 a[0][(1<<n)-1][1] = 1;
 a[0][(1<<n)-1][0] = 1;


 kon=0;
 i=0;

 while(kon==0)
   {
   i++;
   pocitaj(i);

//  for(j=0;j<(1<<n);j++) printf("%lld %lld---> %lld\n",i,j,a[i][j][0]);

   }

 v = a[i][0][0];
 for(j=2;j<=i;j++) v= (v*j)%P;

  printf("%lld %lld\n",i,v);
 }


return 0;
}
                        








View More Similar Problems

Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

View Solution →

Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

View Solution →

Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

View Solution →

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →