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

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →