Tuesday, April 22, 2014

Volleyball Match

Tatyana is a big sports fan and she likes volleyball a lot! She writes down the final scores of the game after it has ended in her notebook.
If you are not familiar with the rules of volleyball, here’s a brief:
  • 2 teams play in total
  • During the course of the game, each team gets points, and thus increases its score by 1.
  • The initial score is 0 for both teams.
The game ends when
  • One of the teams gets 25 points and another team has < 24 points ( strictly less than 24).
  • If the score ties at 24:24, the teams continue to play until the absolute difference between the scores is 2.
Given the final score of a game in the format A:B i.e., the first team has scored Apoints and the second has scored B points, can you print the number of different sequences of getting points by teams that leads to this final score?
Input Format
The first line contains A and the second line contains B.
Constraints
0 ≤ A , B ≤ 109
Output Format
Output the number of different sequences of getting points by the teams that leads to the final score A : B. Final means that the game should be over after this score is reached. If the number is larger than 109+7, output number modulo 109 + 7. Print 0 if no such volleyball game ends with the given score.
Example input #00
3
25
Example output #00
2925
Example input #01
24
17
Example output #01
0
Explanation #01
There’s no game of volleyball that ends with a score of 24 : 17.
solution -->

Lets consider there are r1 blocks, so that, there will be r spaces to be filled (including the left of the left most block and right of the right most block). Now, n identical things should be filled in these spaces. There are n+r1 things in that line now, which can be arranged in (n+r1)!ways.
But, we should avoid the arrangements between the n things and r1 blocks, since they are identical.
So, the final answer is


(n+r1)!n!(r1)!=C(n+r1,r1).
code-->

#include<iostream>
using namespace std ;

long long fact[100];
int  p = 1000000007 ;

long long pow(long long a , int b){
    long long x=1 , y=a; 
     while(b){
         if(b%2==1){
         x=x*y;
         x=x%p;
     }
     y=y*y;
    y=y%p ;
    b=b/2;
     }
    return x ;
    
}
int max(int a , int b ){
    return a>b?a:b ;
}

int main(){
    
int a,b ;
    
    cin >> a >> b ;
    
    fact[0]=1 ;
    
    for(int i=1 ; i<100 ;i++)
        fact[i]=(fact[i-1]*(long long)i)%p;
    
    
    if( (a<25 && b<25) || abs(a-b)<2){
        cout <<"0";
        return  0 ;
    }
    
    if(a>=24 && b>=24){
        
        if(abs(a-b)!=2){
             cout <<"0";
        return  0 ;
        }
        
        int z =max(a,b)-2;
        z=z-24;
        long long q= (fact[24]*fact[24])%p ;
        long long ans =(fact[24+25-1]*pow(q,p-2))%p;
        
   long long zz=pow(2,z);        

            ans=(ans*zz)%p;
        
        cout << ans << endl ;
        return 0 ;
          }
    
    
        int y;
        if(a>b)
            y=b;
        else
           y=a; 
  
            long long z = (fact[y]*fact[25-1])%p  ;
        long long ans=(fact[25+y-1]*pow(z,p-2))%p;       
         
    cout << ans << endl ;
    
    return 0 ;
}

Monday, April 21, 2014

Manasa loves Maths

You are given an integer N. Is there a permutation of digits of integer that’s divisible by 8? A permutation of digits of integer N is defined as an integer formed by rearranging the digits of N. For example, if the number N = 123, then {123, 132, 213, 231, 312, 321} are the possible permutations.
Input Format
The first line contains an integer T i.e. number of test cases.
T lines follow, each containing the integer N.
Output Format
For each test case print YES if there exists one such re-arrangement of N such that it is divisible by 8 or NO if there isn’t.
Constraints
1 <= T <= 45
0 <= N <= 10110
Note
Re-arrangements of 10 are {10, 01} which boils down to {10, 1}.
Sample Input
2
61
75
Sample Output
YES
NO
Explanation
Test case #00: 16 is permutation of 61 which is divisible by 8.
Test case #01: None of permutation of 75, {57, 75}, are divisible by 8.

solution -----> we know that if last 3 digit of a number is divisible by 8 then the whole no is divisible by 8 .

code-->

import java.util.*;

class Solution{
   
    static boolean ok(int i ,int j ,int k , String s){
        
        if((s.charAt(i)*100 + s.charAt(j)*10 + s.charAt(k))%8==0)
          return true ;
        else
            return false ;
    }
    
    static void check(String z){
        
        int si = z.length();
         
        if(si==0){
                System.out.println("NO");
                return ;
            }
      
        if(si==1){
            if(Integer.parseInt(z)%8==0){
                System.out.println("YES");
                return ;
            }
            System.out.println("NO");
                return ;
        }
        
        if(si==2){
            int a = z.charAt(0)-48 ;
            int b = z.charAt(1)-48 ;
            
            if(((a*10+b)%8==0) || ((b*10+a)%8==0)){
                System.out.println("YES");
                return ;
            }
            System.out.println("NO");
                return ;
        }
        
         
        for(int i=0 ; i < si-2 ;i++){
                for(int j=i+1 ; j < si-1 ; j++){
                       for(int k = j+1 ; k < si ; k++ ){
                         
                     if(ok(i,j,k,z) || ok(i,k,j,z) || ok(j,i,k,z) || ok(j,k,i,z) || ok(k,j,i,z) || ok(k,i,j,z) ){
                         System.out.println("YES");
                            return ;    
                     }  
                  }
                }
        }
        
        System.out.println("NO");    
    }
    
    
    public static void main(String ss[]){
        Scanner s = new Scanner(System.in);
        String  a = s.nextLine();
        boolean answer =false;
        int t = Integer.parseInt(a);
        
        for(int i=0;i<t;i++){
            String  z = s.nextLine();
            check(z);
        }
    }
}