Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Friday, September 6, 2013

Solution to spoj problem Joey is Confused with girls...

Question :
So many girls are in love with Joey. He wants to make all of them happy by taking them out for a movie tonight, but unfortunately he has only "K" movie tickets. So he wants to consider all the possible combinations of "K" girls. He just asked you how many different combinations of "K" girls he can make out of "N" girls, can you tell this to him? You know that sometimes this number can be huge, so you should tell him answer modulo 1,000,000,007.

Inputs :
First Line contains an integer "T", the no. of test cases. Each of the next "T" lines will contain only 2 integers "N" and "K".

Outputs :
For each test case print a single number modulo 1,000,000,007 that is the number of possible combinations of "K" girls out of "N" girls.


Constraints :
1 <= T <= 10^5, 1 <= N <= 1000, 0 <= K <= N.



Level : Medium

Solution to the Problem :


#include<stdio.h>
int min(int i,int j)
{
    if(i<j)
    return i;
    return j;
}

int mod=1000000007;

int arr[1001][1001];

void preprocess()
{
     int i,j;
     for(i=0;i<1001;i++)
     {
                        arr[i][0]=1;
                        arr[i][i]=1;
     }
     for(i=2;i<1001;i++)
     {
                        for(j=1;j<=i;j++)
                        arr[i][j]=(arr[i-1][j]+arr[i-1][j-1])%mod;
     }
}

main()
{
      preprocess();
      int t;
      scanf("%d",&t);
      while(t--)
      {
                int n,k;
                scanf("%d%d",&n,&k);
                printf("%d\n",arr[n][k]);
      }
}





No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.