finding the number of ways to generate a number - math

I am new to permutations and combinations. I am provided with the number n and I can make a number with the help of n+1 digits which are 0,1,...n. I need to find in how many ways I can make the sum n by putting these n+1 digits in n places.
Like i am having number n=2
then
(a) 0 and 2 (b)1 and 1 (c)2 and 0 .
for a number n=3
then
(a)0,0,3 (b)0,3,0 (c)3,0,0 (d)0,1,2 (e)0,2,1 (f)1,0,2 (g)1,2,0 (h)2,0,1 (i)2,1,0 (j)1,1,1
So in total i have 10 ways to generate sum=3 by using the digits 0,1,2,3.
And also consider i can put these n+1 digits in n places only.

The number of compositions of n into k nonnegative summands is (n+k-1) choose n by the stars-and-bars method. You have k=n, so the count is 2n-1 choose n. Your examples were 3C2=3 and 5C3=10.

Yes, the number of total ways for making the sum=n using n+1 digits is equal to (n+n-1)C(n-1) or you can say (n+n-1)C(n).

Related

Check whether a number can be expressed as sum of x powers of two

Is there a bit trick to check whether a number can be expressed as sum of x powers of 2?
Example: For x=3 n=21, the numbers are 16, 4, and 1. If n=30, it should be false, because there are no 3 powers of two to represent 30.
For a number n …
… the minimum x is the number of 1-bits in n. This number is called popcount(n).
Example: The binary number 0b1011 needs at least popcount(0b1011)=3 powers of two to be summed up (0b1000+0b0010+0b0001).
… the maximum x is n. Because 1 is a power of two you can add 1 n times to get n.
Now comes the hard question. What if x is between popcount(n) and n?
As it turns out, all of these x are possible. To build a sum of x powers of two …
start at the shortest sum (the binary representation of n)
If you have less than x addends, split any addend that is bigger than 1 into two addends, increasing the number of addends by one. This can be done until you arrive at x=n.
Example: Can 11=0b1011 be expressed as a sum of x=7 powers of two?
Yes, because popcount(n)=3 <= x=7 <= n=11.
To build a sum with x=7 powers of two we use
11 = 0b1011 = 0b1000+0b10+0b1 | only 3 addends, so split one
= (0b100+0b100)+0b10+0b1 | only 4 addends, so split another one
= ((0b10+0b10)+0b100)+0b10+0b1 | only 5 addends, so split another one
= (((0b1+0b1)+0b10)+0b100)+0b10+0b1 | only 6 addends, so split another one
= (((0b1+0b1)+(0b1+0b1))+0b100)+0b10+0b1 | 7 addends, done
Implementation
To implement the check »can n can be expressed as sum of x powers of two?« use
isSumOfXPowersOfTwo(n, x) {
return x<=n && x>=popcount(n).
}
For efficient bit-twiddling implementations of popcount see this question. Some processors even have an instruction for that.

how to solve ALICESIE on spoj. How it has common pattern for its answer

What is the logic behind pattern i.e.(ans=(n+1)/2) in question ALICESIE on spoj.
Algorithm_given:
1.Create a list of consecutive integers from N to 2 (N, N-1, N-2, ..., 3, 2). All of those N-1numbers are initially unmarked.
2.Initially, let P equal N, and leave this number unmarked.
3.Mark all the proper divisors of P (i.e. P remains unmarked).
4.Find the largest unmarked number from 2 to P – 1, and now let P equal this number.
5.If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.
Find total number of unmarked numbers.
i know its O(sqrt(n)) solution but answer is expected in O(1),it can found by seeing the common pattern i.e.(N+1)/2
But how to prove it Mathematically
link: ALICESIE

What's the probability that the distinct number appear in a size-k subset of n distinct number?

What's the probability that the distinct number appear in a size-k subset of n distinct numbers?
Let A be our target number, S be some the size-k subset of [1,2,3....n].
What's the probability that A is the one of k numbers in S? Many thanks.
PS: I can draw a condition tree diagram, and find the answer maybe the k/n.
But how can I think about it? Thanks again.
The probability is indeed, as you mentioned, k/n. Think about is this way: Let x be an element of [1,2,...,n]. There are binom(n,k) subsets of size k in total, and there are binom(n-1,k-1) subsets of size k that contain x (because x is chosen and we need to choose another k-1 elements). Hence the probability of x to be contained in S is binom(n-1,k-1)/binom(n,k)=k/n.

if two n bit numbers are multiplied then the result will be maximum how bit long

I was wondering if there is any formula or way to find out much maximum bits will be required if two n bits binary number are multiplied.
I searched a lot for this but was unable to find its answer anywhere.
Number of digits in base B required for representing a number N is floor(log_B(N)+1). Logarithm has this nice property that log(X*Y)=log(X)+log(Y), which hints that the number of digits for X*Y is roughly the sum of the number of digits representing X and Y.
It can be simply concluded using examples:
11*11(since 11 is the maximum 2 bit number)=1001(4 bit)
111*111=110001(6 bit)
1111*1111=11100001(8 bit)
11111*11111=1111000001(10 bit)
and so from above we can see your answer is 2*n
The easiest way to think about this is to consider the maximum of the product, which is attained when we use the maximum of the two multiplicands.
If value x is an n-bit number, it is at most 2^n - 1. Think about this, that 2^n requires a one followed by n zeroes.
Thus the largest possible product of two n-bit numbers will be:
(2^n - 1)^2 = 2^(2n) - 2^(n+1) + 1
Now n=1 is something of a special case, since 1*1 = 1 is again a one-bit number. But in general we see that the maximum product is a 2n-bit number, whenever n > 1. E.g. if n=3, the maximum multiplicand is x=7 and the square 49 is a six-bit number.
It's worth noting that the base of the positional system doesn't matter. Whatever formula you come up with for the decimal multiplication will work for the binary multiplication.
Let's apply a bit of deduction and multiply two numbers that have relatively prime numbers of digits: 2 and 3 digits, respectively.
Smallest possible numbers:
10 * 100 = 1000 has 4 digits
Largest possible numbers:
99 * 999 = 98901 has 5 digits
So, for a multiplication of n-digit by m-digit number, we deduce that the upper and lower limits are n+m and n+m-1 digits, respectively. Let's make sure it holds for binary as well:
10 * 100 = 1000 has 4 digits
11 * 111 = 10101 has 5 digits
So, it does hold for binary, and we can expect it to hold for any base.
x has n binary digits means that 2^(n-1) <= x < 2^n, also assume that y has m binary digits. That means:
2^(m+n-2)<=x*y<2^(m+n)
So x*y can have m+n-1 or m+n digits. It easy to construct examples where both cases are possible:
m+n-1: 2*2 has 3 binary digits (m = n = 2)
m+n: 7*7=49=(binary)110001 has 6 binary digits and m = n = 3

probabilities combinations

I faced a problem while writing a code in Matlab that calculates sum of products of all possible combinations of n numbers taken from a vector with length m. It is similar to the task that you will drag exactly n different balls out of the bag with m balls (order doesn't matter).
example:
m = 5, n = 3 then i need to calculate sum of 10 summands
thanks for your time
You should use nchoosek.
m=5;
n=3;
s=sum(nchoosek(1:m,n), 2);

Resources