Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
We got two integers x and y, and we call an array is a perfect array if and only if it satisfies the following conditions:
The elements in the array are integers.
The length of the array is exactly y.
The product of all elements is exactly x.
So how to find the number of beautiful arrays for different x and y?
For example, if x is 3 and y is 2, there are four beautiful arrays:
[1,3],[3,1],[-1,-3],[-3,-1]
Let's do few examples first:
x = 30, y = 3, x's prime factors are 2, 3, 5. Possible combinations are 2 * 3 * 5, 6 * 5, 2 * 15, 3 * 10, 30. For every possible combination of length L we need to count the number of possible arrangement of its elements in 3 slots, that is y!/(y-L)!. We obtain 6 + 6 + 6 + 6 + 3 = 27.
x = 36, y = 5, x's prime factors are 2, 2, 3, 3. Writing in the same way as the previous example would be tedious, so let's think about y bins in which we want to put 2, 2 and 3, 3. The number of ways we can distribute 2, 2 into 5 bins can be computed with the help of stars and bars, we need to put 4 bars among 2, 2. There is 6!/(4!2!) = 15. The same for 3, 3. So, the answer is 225.
Factor x into prime divisors. Such factorization is unique. And takes up to sqrt(x).
Count the number of ways we can distribute every pi into y bins. Multiply.
Account for positive/negative multiplying by
First of all, you need to factor x. This can be done in O(√x) (googl it).
Let x=Πp_i^k_i (p_i is a prime number)
Count the number of distributing ∑k_i for y cells.
This is (∑k_i+x-1)!/(x-1)!/∑k_i
There is also the degree of freedom for positive and negative.
This is the number of way to make pairs.
Related
I have a problem with coming up with an algorithm for the "graph" :(
Maybe one of you would be so kind and direct me somehow <3
The task is as follows:
We have a board of at least 3x3 (it doesn't have to be a square, it can be 4x5 for example). The user specifies a sequence of moves (as in Android lock pattern). The task is to check how many points he has given are adjacent to each other horizontally or vertically.
Here is an example:
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
The user entered the code: 10,6,7,3
The algorithm should return the number 3 because:
10 is a neighbor of 6
6 is a neighbor of 7
7 is a neighbor of 3
Eventually return 3
Second example:
Matrix:
1 2 3
4 5 6
7 8 9
The user entered the code: 7,8,6,3
The algorithm should return 2 because:
7 is a neighbor of 8
8 is not a neighbor of 6
6 is a neighbor of 3
Eventually return 2
Ofc number of operations equal length of array - 1
Sorry for "ile" and "tutaj", i'm polish
If all the codes are unique, use them as keys to a dictionary (with (row/col) pairs as values). Loop thru the 2nd item in user input to the end, check if math.Abs(cur.row-prev.row)+math.Abs(cur.col-prev.col)==1. This is not space efficient but deal with user input in linear complexity.
The idea is you have 4 conditions, one for each direction. Given any matrix of the shape n,m which is made of a sequence of integers AND given any element:
The element left or right will always be + or - 1 to the given element.
The element up or down will always be + or - m to the given element.
So, if abs(x-y) is 1 or m, then x and y are neighbors.
I demonstrate this in python.
def get_neighbors(seq,matrix):
#Conditions
check = lambda x,y,m: np.abs(x-y)==1 or np.abs(x-y)==m
#Pairs of sequences appended with m
params = zip(seq, seq[1:], [matrix.shape[1]]*(len(seq)-1))
neighbours = [check(*i) for i in params]
count = sum(neighbours)
return neighbours, count
seq = [7,8,6,3]
matrix = np.arange(1,10).reshape((3,3))
neighbours, count = get_neighbors(seq, matrix)
print('Matrix:')
print(matrix)
print('')
print('Sequence:', seq)
print('')
print('Count of neighbors:',count)
Matrix:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Sequence: [10, 6, 7, 3]
Count of neighbors: 3
Another example -
seq = [7,8,6,3]
matrix = np.arange(1,10).reshape((3,3))
neighbours, count = get_neighbors(seq, matrix)
Matrix:
[[1 2 3]
[4 5 6]
[7 8 9]]
Sequence: [7, 8, 6, 3]
Count of neighbors: 2
So your input is the width of a table, the height of a table, and a list of numbers.
W = 4, H = 3, list = [10,6,7,3]
There are two steps:
Convert the list of numbers into a list of row/column coordinates (1 to [1,1], 5 to [2,1], 12 to [3,4]).
In the new list of coordinates, find consequent pairs, which have one coordinate identical, and the other one has a difference of 1.
Both steps are quite simple ("for" loops). Do you have problems with 1 or 2?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Given an integer n between 0 and 10,0000,0000, count the number of integers smaller than n which contain the digits [2,0,1,8] in order.
So e.g. the number 9,230,414,587 should be counted, because removing the digits [9,3,4,4,5,7] leaves us with [2,0,1,8].
Example input and output:
n = 2018 -> count = 1
n = 20182018 -> count = 92237
My general thought is that: the maximum length of n is 10 and the worst situation is that we have to insert 6 digits into [2,0,1,8] and remove the duplicates and the numbers greater than n.
I don't see any own attempts to solve, so I'll give only clue:
You have 9-digits number (small numbers might be represented as 000002018) containing digit sequence 2,0,1,8.
Name them 'good' ones.
Let denote digit places from 1 to 9 right to left:
number 532705183
digits 5 3 2 7 0 5 1 8 3
index 9 8 7 6 5 4 3 2 1
The most left '2' digit can occupy places from 4 to 9. How many good numbers contain the first 2 at k-th place? Let make function F2(l, k) for quantity of good numbers where 2 refers to digit 2, l is number length, k is place for the most left digit.
. . . . 2 . . . .
^
|
left part k right part should contain 0 1 8 sequence
without 2's
F2(9, k) = 9^(9-k) * Sum(F0(k-1, j) for j=1..k-1)
Overall quantity of good numbers is sum of F2(9, k) for all possible k.
GoodCount = Sum(F2(9, k) for k=4..9)
Explanation:
There are 9-k places at the left. We can put any digit but 2 there, so there are 9^(9-k) possible left parts.
Now we can place 0 at the right part and count possible variants for 018 subsequences. F0(...) will of course depend on F1(...) and F1 will depend on F8(...) for shorter numbers.
So fill tables for values for F8, F0, F1 step-by-step and finally calculate result for digit 2.
Hand-made example for 4-digit numbers containing subsequence 1 8 and k = position of the first '1':
k=2: there are 81 numbers of kind xx18
k=3: there are numbers of kind x1x8 and x18x
there are 9 subnumbers like x8, 10 subnumbers 8x, so (10+9)*9=171
k=4: there are numbers of kind
1xx8 (9*9=81 such numbers),
1x8x (9*10=90 numbers),
18xx (100 numbers),
so 81+90+100=271
Overall: 81+171+271=523
This is actually a relatively small problem set. If the numbers were much bigger, I'd opt to use optimised techniques to just generate all numbers that meet your criteria (those containing the digits in that order) rather than generating all possible numbers and checking each to ensure it meets the criteria.
However, the brute force method does your 20182018 variant in about ten seconds and the full 1,000,000,000 range in a little under eight minutes.
So, unless you need it faster than that, you may find the brute-force method more than adequate:
import re
num = 1000000000 # or 20182018 or something else.
lookfor = re.compile("2.*0.*1.*8")
count = 0
for i in range(num + 1):
if lookfor.search(str(i)) is not None:
count += 1
#print(count, i) # For checking.
print(count)
I've found a code to find number of possibilities to make change using given coins: How to count possible combination for coin problem. But how to count it, if we think about different permutations of the same sequence? I mean that, e.g. amount is 12, and "4 4 2 2" and "4 2 4 2" should be counted as 2, not 1.
As you've mentioned inside your question you can count the possible combinations as stated in How to count possible combination for coin problem. But in order to include the permutations into your answer:
If you distinguish the permutation of the same numbers [1 7 7] and [1 7 7] e.g. just count each sequence([1 7 7] here) as n! (n = # of elements in the sequence) [instead of 1]
Otherwise : multiply each sequence by n!/(m!l!...) where m = number of equal elements of type 1, l is number of equal elements of type 2 and so on... . For example for sequence like [a b b c c c] you should count this 6!/(2!*3!) [instead of 1]
So use the algorithm inside that link, that I don't repeat again, but just instead of counting each combination as 1 use the formula that I said (depending on the case you desire).
(! is factorial.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I check if a number is sum of multiples of 3 and 5 given that the number could be as big as 100,000 . I need an optimized way to break a number into two parts such that the two parts are multiple of 3 and 5 only and the part which is multiple of 3 is greater than the part which is multiple of 5 and if that kind of splitting is not possible then I need to reject that number .
Eg:
1 => cant be split so rejected ,
35 => 30 + 5 ,
65 => 60 + 5 (Though 30 + 35 could be a split but since part which is multiple of 3 has to be greater than the part which is multiple of 5),
11 => 6+5
Every (integer) number modulo 3 yields 0, 1 or 2.
So let's examine all cases (n > 3 must yield for obvious reasons):
n % 3 == 0. Easy: We just take 0 == 0 * 5 and n / 3 as splitting.
n % 3 == 2. Easy again: One number will be 5 and the other (n-5) / 3. When subtracting 5 from n, we will create a second number (n-5), which falls under the first case.
n % 3 == 1. Same as in case 2, but this time we substract 10 == 2*5.
A small problem is the property that the multiple of 3 has to be larger than the one of 5. For this to hold true, n has to be at least 22. ( 22 == 2 * 5 + 3 * 4).
So all numbers smaller than 22 with the property n % 3 == 1 have to be rejected: 4, 7, 10, 13, 16 and 19. (As long as the factor for the multiples have to be non-negative).
If you mean to find a way to split a number to two parts, where the first part is a multiple of 3 and the second is a multiple of 5, with the extra requirement that the first (multiple of 3) part is greater than than the second (multiple of 5) part, then it's rather trivial:
Every number from 20 and above can be split that way.
Proof: For given number N, exactly one of the three numbers, N, N-5, N-10 will be a multiple of 3 (consider modulo 3 arithmetic.) So, one of these three splits satisfy the requirements:
N 0
N-5 5
N-10 10
and since N >= 20, the 1st part is greater (or equal) than the 2nd.
Off the top of my head --
Make Q = N / 3, integer division, rounding down. Make R the remainder.
If R = 0 you're done.
If R == 2, decrement Q.
Else R must be 1, subtract 2 from Q.
Your answer is Q * 3 and N - (Q * 3). Check that all results are positive and that the 3s multiple > 5s multiple restriction is satisfied.
(Note that this is essentially the same as Sirko's answer, but I felt it worthwhile to think it through separately, so I didn't attempt to analyze his first.)
max divisor of 3 and 5 is 1.
so when N = 3, or N >= 5, it can be sum of multiple of 3 and 5.
Just use this code:-
Enjoy :)
$num = 0; // Large Number
$arr = array();
if(isset($_POST['number']) $num = $_POST['number'];
// Assuming you post the number to be checked.
$j=0;
for($i=0;$i<$num;$i++)
{
if(($num-$i)%3==0 || ($num-$i)%5==0) { $arr[j] = $num - $i; $j++; }
}
//This creates an array of all possible numbers.
$keepLooping = true;
while($keepLooping)
{
$rand = array_rand($arr,2);
if(($rand[0] + $rand[1]) == $num)
{
//Do whatever you like with them. :)
}
}
I haven't tested it though but just for your idea. Instead of the for loop to select the possibilities, you can choose some other way whichever suits you.
I tried to solve this TopCoder problem: http://community.topcoder.com/stat?c=problem_statement&pm=10863&rd=14150
But, my solution is not good, and I don't understand why.
I understood the solution given there (down page: look for LotteryPyaterochka): http://apps.topcoder.com/wiki/display/tc/SRM+466
So, to sum up my problem:
We are playing a special kind of lottery:
Each ticket in this lottery is a rectangular grid with N rows and 5 columns, where each cell contains an integer between 1 and 5*N, inclusive. All integers within a single ticket are distinct.
The lottery organizers randomly choose 5 distinct integers, each between 1 and 5*N, inclusive. Each possible subset of 5 integers has the same probability of being chosen. These integers are called the winning numbers. A ticket is considered a winner if and only if it has a row which contains at least 3 winning numbers.
We want to know the number of winning ticket (thus, having at least 3 winning number in the same row)
So, I stuck in the following step:
number of ways of choosing the 5 numbers which appear in the 'winning row'.
The topCoder solution says:
(#ways of choosing the 5 numbers which appear in the 'winning row') =
(#ways of choosing the x winning numbers which appear in the 'winning row') * (#ways of choosing 5-x 'non-winning numbers') =
(5 choose x) * ((5N-5) choose (5-x))
Since the number of winning numbers in this row is at least 3, x can be 3 or 4 or 5. So, we have
(#ways of choosing the 5 numbers which appear in the 'winning row') =
(5 choose 3) * ((5N-5) choose 2) + (5 choose 4) * ((5N-5) choose 1) + (5 choose 5) * ((5N-5) choose 0))
And what I say:
(#ways of choosing the 5 numbers which appear in the 'winning row') =
(3 number among the 5 winning number) * (2 numbers to complete the row to choose among the 5N-5 non winning number + 2 winning number non chosen before) =
(5N choose 3) * ((5N-3)choose 2)
For N = 10 my method give: (5 choose 3)*(47 choose 2) = 10810
And the topcoder method give: ((5 choose 3)(45 choose 2) + (5 choose 4)(45 choose 1) + (5 choose 5)*(45 choose 0)) = 10126
Why is my method wrong ?
Thanks
Let's say the winning numbers are 1, 2, 3, 4 and 5. Now let's look at the ticket that contains all five numbers in the winning row.
Your method counts that ticket many times, since it's included in the following counts:
1 2 3 + two other numbers
1 2 4 + two other numbers
1 2 5 + two other numbers
1 3 4 + two other numbers
...
The same thing happens to tickets with four winning numbers.
This is the reason why these cases need to be counted separately.