Google code jam exercise -- Your Rank Is Pure

Source: Internet
Author: User

Continue to the Round 1B 2010. The second question does not understand the question. The third question can be understood, but the algorithm has been tossing for a long time. Let's take a look at the third question first.

Given a set S = {2, 3 ,... n}, its subset s' satisfies the nature: n is the k element in s', then k should also be in s', similarly, k is the k' element, so k' should also be in s'. After a finite step, k = 1, not in s', it is called n for s' as a Pure Rank. Given an n, calculate the number of S.

In Content Analysis, the dynamic planning algorithm is excluded. How do I think that many questions are about dynamic planning? I don't know much about state planning. Take a look at the specific algorithm as follows,

If n is a set of S' pure rank, s' = S' ^ {1, 2,... k}, then k is the pure rank of s. The dynamic planning algorithm is to find the method from s' to s.

S 'has k elements, and S ''has k' elements. From S' to S'', it needs to be from {k + 1, k + 2 ,... n-1} select k-k' elements.

Use count [n] [k] to indicate the number of the Set S with k elements and n is pure rank, then there is count [n] [k] = sum count [k] [k'] * C [n-k-1] [k-k'-1] for k' = 1... k-1

Since the final number may be relatively large, we need to take the remainder of 100003 in the process of accumulation. The remainder cannot be replaced by a method equal to or greater than 100003.

The final code is as follows:

#!/usr/bin/python#encoding:UTF-8#Filename:FileFixIt.pyimport sysdef solveN(n):    comb = [[0 for j in xrange(n+1)] for i in xrange(n+1)]    j = 0    for i in xrange(n+1):        comb[i][j] = 1        comb[i][i] = 1    for i in xrange(1,n+1):        for j in xrange(1,i):            comb[i][j] = comb[i-1][j]+comb[i-1][j-1]            if comb[i][j]>=100003:                comb[i][j] = comb[i][j]%100003    count = [[0 for j in xrange(n+1)] for i in xrange(n+1)]    for i in xrange(2,n+1):        count[i][1] = 1    for i in xrange(3,n+1):        for j in xrange(2,i):            count[i][j] = 0            for k in xrange(1,j):                count[i][j] += count[j][k]* comb[i-j-1][j-k-1]                 if count[i][j]>=100003:                    count[i][j] = count[i][j]%100003#    print "count:"            #    for i in xrange(2,n+1):#        for j in xrange(1,i):#            print "count[%d][%d] %d" %(i,j,count[i][j])#    cnt = 0#    for j in xrange(1,n):#        cnt += count[n][j]#    print "cnt:",cnt#    return cnt    return count    inname = "input.txt"outname = "output.txt"if len(sys.argv)>1:    inname = sys.argv[1]    outname = inname.rstrip(".in")    outname = outname + ".out"fin = open(inname,"r")fout = open(outname,"w")testCaseNum = int(fin.readline().rstrip("\n"))caseNum = 0lines = fin.readlines()allCase = [int(line.rstrip("\n")) for line in lines]maxN = max(allCase)count = solveN(maxN)for caseNum,n in enumerate(allCase):    cnt = 0    for j in xrange(1,n):        cnt += count[n][j]        if cnt>=100003:            cnt = cnt%100003    answer = "Case #%d: %d\n" %(caseNum+1,cnt)    fout.write(answer)fin.close()fout.close()

Finally, both the small and large case Tests passed, and large case takes a long time.

Is there a repeat?

 

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.