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

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →