def nbinomial(n, x, p):
    """
    Negative binomial distribution.
    
    Probability of xth successful observation on the nth trial, with 
    probability of success for each trial of p.
    """
    return F(n - 1) / (F(n - 1 - (x - 1)) * F(x - 1)) * p**x * q**(n - x)
def binomial(n, x, p):
    """
    Binomial distribution.
    
    Probability of x successes observed over n trials, with probability of
    success for each trial of p.
    """
    return F(n) / (F(n - x) * F(x)) * p**x * q**(n - x)
def solution(n, k):
    arr = list(range(1, n + 1))

    answer = []

    while n != 0:
        answer.append(arr.pop((k - 1) // F(n - 1)))
        k %= F(n - 1)
        n -= 1

    return answer
def poisson(k, lam):
    """
    Poisson distribution.

    Probability of k successful observations given a rate of success lam.
    """
    return lam**k * exp(-lam) / F(k)
def calculate_multiplication_coeff(p,n):
    # Coeff for nth term is (p(p+1)(p+2)....(p+n-1)) / n! 

    temp = p
    for i in range(1,n-1):
        temp = temp * (p+i)
    return temp / F(n-1)
Exemple #6
0
    def getP(self, N, K):
        #Base condition
        if len(N) == 0: return ''
        if K <= 0 and N != []: return ''.join(map(str, sorted(N)[::-1]))

        N, l = sorted(N), len(N)
        f = F(l - 1)

        #getting first digit

        if K % f == 0:
            pos = (K // f) - 1
            Num = str(N.pop(pos)) + self.getP(N, K - (pos + 1) * F(l - 1))
        else:
            pos = K // f
            Num = str(N.pop(pos)) + self.getP(N, K - pos * F(l - 1))

        return Num
Exemple #7
0
 def calculateSunTime(longitude: float,
                      latitude: float,
                      date: datetime = datetime.now(),
                      zenith: float = 90.8):
     fr = lambda v, m: v + (m if v < 0 else (-m if v >= m else 0))
     day, month, year = (date.day, date.month, date.year)
     R = pi / 180
     N = F(275 * month / 9) - (F((month + 9) / 12) * (1 + F(
         (year - 4 * F(year / 4) + 2) / 3))) + day - 30
     lh = longitude / 15
     t = (N + ((6 - lh) / 24)), (N + ((18 - lh) / 24))
     M = ((0.9856 * t) - 3.289 for t in t)
     l = tuple(
         fr(M + (1.916 * S(R * M)) + (0.020 * S(R * 2 * M)) + 282.634, 360)
         for M in M)
     ra = tuple(fr(1 / R * AT(0.91764 * T(R * L)), 360) for L in l)
     raQ = tuple((F(RA / 90)) * 90 for RA in ra)
     ra = tuple((ra[i] + (lQ - raQ[i])) / 15
                for i, lQ in enumerate((F(L / 90)) * 90 for L in l))
     sd = (0.39782 * S(R * L) for L in l)
     deg = tuple((1 / R) * AC((C(R * zenith) - (sd * S(R * latitude))) /
                              (C(AS(sd)) * C(R * latitude))) for sd in sd)
     gt = tuple(
         datetime(year, month, day, *hm).replace(
             tzinfo=timezone.utc).astimezone(
                 tz=datetime.now().astimezone().tzinfo)
         for hm in ((fr(int(UT), 24), int(round((UT - int(UT)) * 60, 0)))
                    for UT in (fr(T - lh, 24) for T in (
                        H + ra[i] - (0.06571 * t[i]) - 6.622
                        for i, H in enumerate(((360 - deg[0]) / 15,
                                               deg[1] / 15))))))
     return gt[0] - timedelta(days=1), gt[1]
    def trailingZeroes(self, n: int) -> int:
        from math import factorial as F

        if n == 0:
            return 0

        string = str(F(n))

        cnt = 0

        for i in range(len(string) - 1, -1, -1):
            if string[i] == '0':
                cnt += 1
            else:
                break

        return cnt
Exemple #9
0
 def getPermutation_1(self, n, k):
     """
     :type n: int
     :type k: int
     :rtype: str
     """
     ind = [str(i) for i in range(1, n + 1)]
     num = ''
     new_n = n - 1
     for i in range(n):
         mi = F(new_n)
         index = k // mi
         if k % mi == 0:  # part of the rep
             index -= 1
         num += ind.pop(index)
         k %= mi
         new_n -= 1
     return num
def nbinomial(n, x, p):
    """Negative binomial distribution."""
    return F(n - 1) / (F(n - 1 - (x - 1)) * F(x - 1)) * p**x * q**(n - x)
def binomial(n, x, p):
    """Binomial distribution."""
    return F(n) / (F(n - x) * F(x)) * p**x * q**(n - x)
from math import factorial as F
if __name__ == '__main__':
    n = int(input())
    ans = 1
    for i in range(3, 6, 2):
        t = n + i - 1
        b = n - 1
        ans *= int(F(t) / (F(t - b) * F(b)))
    print(ans)
'''

G. Challenge Pennants
time limit per test
0.5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Because of budget cuts one IT company established new non-financial reward system instead of bonuses.

Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets "I fixed a critical bug" pennant on his table. A man who suggested a new interesting feature gets "I suggested a new feature" pennant on his table.

Because of the limited budget of the new reward system only 5 "I fixed a critical bug" pennants and 3 "I suggested a new feature" pennants were bought.

In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded "I fixed a critical bug" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded "I suggested a new feature" pennants is passed on to his table.

One man can have several pennants of one type and of course he can have pennants of both types on his table. There are n tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants.
Input
from math import factorial as F 
if __name__=='__main__':
    n = int(input())
    ans = 0
    for i in range(5,8):
        ans += int(F(n)/(F(n-i)*F(i)))
    print (ans)

'''

F. Selection of Personnel
time limit per test
0.5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate.
Input

The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes.
Output

Output one integer — the number of different variants of group composition.
Examples
Input

7
Exemple #14
0
def binomial(n, x, p):
    return F(n) / (F(n - x) * F(x)) * p**x * q**(n - x)
'''  Program 57  '''
print "Program --> Square of the given number"
print input(
    "Enter the number..")**2, "is the Squared value of the given number"
print "########################################"
'''  Program 58  '''
print "Program 58--> Power of the given number"
print pow(input("Enter the number.."),
          input("Enter the value to be raised..")), "is the powered value"
print "########################################"
'''  Program 59  '''
print "Program 59--> Krishnamoorthy number"
from math import factorial as F
num = input("Enter the number..")
print "Krishnamoorthy Number" if num == sum(
    map(lambda n: F(n), map(int, str(num)))) else "Not a Krishnamoorthy Number"
print "########################################"
'''  Program 60  '''
print "Program 60--> Generation of Krishnamoorthy numbers"
from math import factorial as F
A = lambda num: num == sum(map(lambda n: F(n), map(int, str(num))))
print[i for i in xrange(input("Start.."), input("End.."))
      if A(i)], "is(are) the Krishnamoorthy numbers within the given range"
print "########################################"
'''  Program 61  '''
print "Program 61--> Patterns"
x = xrange
n = input("Enter the size (common for all patterns)..")

print "Right Angled Triangle-Left Side"
for i in x(n):
def nck(n, k):
    """N Chose K function to get the number of K combinations of N elements given by the binomial.
    ARGS: n, k where n is the number of elements and k is the number of combinations we need to get from n
    """
    return int(F(n) / (F(k) * F(n - k)))