The question of buying lottery tickets

Source: Internet
Author: User
Tags int size require valid ticket

Problem Statement

In most states, gamblers can choose from a wide variety of different lottery. The rules of a lottery are defined by two integers (choices and blanks) and two Boolean variables (sorted and unique). Choices represents the highest valid number that you would use on your lottery. (All integers between 1 and choices, inclusive, are valid and can appear on your ticket.) Blanks represents the number of spots on your ticket where numbers can be written.

The sorted and unique variables indicate restrictions on the tickets can create. If sorted is set to true, then the numbers on your ticket must are written in non-descending order. The If sorted is set to false, then the numbers could be written in any order. Likewise, if unique is set to True, then each number you write in your ticket must be distinct. If unique is set to False, then repeats are allowed.

Here are some example lottery tickets, where choices = and blanks = 4:

{3, 7, ticket--this is unconditionally valid.

{4, 1, 9}--Because the numbers are not in nondescending order, this ticket is valid only if sorted = False.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

{8, 8, 8,}--because there are repeated numbers, this ticket are valid only if Unique = False.

{One, 6, 2, 6}--This ticket was valid only if Sorted = false and unique = False.

Given a list of lotteries and their corresponding rules, return a list, lottery names sorted from how easy they are to win . The probability that you'll win a lottery is equal to (1/(number of valid lottery tickets for that game)). The easiest lottery to win should appear at the front of the list. Ties should be broken alphabetically (= Example 1).

In short, the topic is to ask n an optional number, M white space, in some limited case (whether to allow duplication, whether ordered) there are how many combination of ways?

Allow repeat, do not require ordered: n^m

Cannot repeat, does not require ordered: N (N-1) (N-2) ... (n-m+1)

Cannot repeat, but must be ordered: N (N-1) (N-2) ... (n-m+1)/m!

Allow duplicates, but must be ordered:?

This seems to be not so simple, I did not think how the combination of mathematical methods to get the answer directly, but can be solved by recursion. Because it must be ordered, the number of I digits must be greater than or equal to the i-1 bit. Define the recursive function f (m,n) for the number of possible combinations of number n that can be filled in the first m, which equals the sum of the numbers 1 to n at the m-1 bit. That is, F (m,n) =f (m-1,1) +f (m-1,2) +...+f (m-1,n). What I didn't ask for is F (m,n)

public class Lottery {//cannot repeat, does not require ordered private long factorial (int n, int size) {long sum  
        = 1;  
        for (int i=0;i<size;i++) {sum*= (n-i);  
    return sum;  
        }//Allow duplicates, do not require ordered private long pow (int n, int size) {long sum=1;  
        for (int i=0;i<size;i++) {sum*=n;  
    return sum;  
    }//cannot be duplicated, but must be ordered private long combination (int n, int m) {return factorial (n,m)/factorial (m,m);  
    Long Table[][]=new long[100][10];  
            Allow duplicates, but must be ordered private long combinationnounique (int n, int m) {for (int i=0;i<n;i++)  
        Arrays.fill (table[i],-1);  
    return f (m,n);  
            The possible combination number of the number n can be filled in by M//At the number of combinations of numbers 1 to n at the m-1 bit = private long f (int m,int n) {if (m==0)  
        return 1; if (table[n-1][m]!=-1) RETUrn Table[n-1][m];  
        Long sum=0;  
        for (int i=1;i<=n;i++) {sum+=f (m-1,i);  
        } table[n-1][m]=sum;  
    return sum;  
        public static void Main (String args[]) {lottery lot=new lottery ();  
    System.out.println (Lot.combinationnounique (93, 8)); }  
      
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.