[Variants] An interesting counting problem related to square product

Правка en3, от SPyofcode, 2021-11-01 06:42:35

The statement:

Given three integers $$$n, k, p$$$, $$$(1 \leq k \leq n < p)$$$.

Count the number of array $$$a[]$$$ of size $$$k$$$ that satisfied

  • $$$1 \leq a_1 < a_2 < \dots < a_k \leq n$$$
  • $$$a_i \times a_j$$$ is perfect square $$$\forall 1 \leq i < j \leq n$$$

Since the number can be big, output it under modulo $$$p$$$.

For convenient, you can assume $$$p$$$ is a large constant prime $$$10^9 + 7$$$

Yet you can submit the problem for $$$k = 3$$$ here.

Notice that in this blog, we will solve for generalization harder variants

For original problem you can see in this blog

Extra Tasks

These are harder variants, and generalization from the original problem. You can see more detail here

*Marked as solved only if tested with atleast $$$10^6$$$ queries

Solved A: Can we also use phi function or something similar to solve for $$$k = 2$$$ in $$$O(\sqrt{n})$$$ or faster ?

Solved B: Can we also use phi function or something similar to solve for general $$$k$$$ in $$$O(\sqrt{n})$$$ or faster ?

Solved C: Can we also solve the problem where there can be duplicate: $$$a_i \leq a_j\ (\forall\ i < j)$$$ and no longer $$$a_i < a_j (\forall\ i < j)$$$ ?

Solved D: Can we solve the problem where there is no restriction between $$$k, n, p$$$ ?

Solved E: Can we solve for negative integers, whereas $$$-n \leq a_1 < a_2 < \dots < a_k \leq n$$$ ?

Solved F: Can we solve for a specific range, whereas $$$L \leq a_1 < a_2 < \dots < a_k \leq R$$$ ?

G: Can we solve for cube product $$$a_i \times a_j \times a_k$$$ effectively ?

H: Can we solve if it is given $$$n$$$ and queries for $$$k$$$ ?

I: Can we solve if it is given $$$k$$$ and queries for $$$n$$$ ?

J: Can we also solve the problem where there are no order: Just simply $$$1 \leq a_i \leq n$$$ ?

K: Can we also solve the problem where there are no order: Just simply $$$0 \leq a_i \leq n$$$ ?

M: Can we solve for $$$q$$$-product $$$a_{i_1} \times a_{i_2} \times \dots \times a_{i_q} = x^q$$$ (for given constant $$$q$$$) ?

N: Given $$$0 \leq \delta \leq n$$$, can we also solve the problem when $$$1 \leq a_1 \leq a_1 + \delta + \leq a_2 \leq a_2 + \delta \leq \dots \leq a_k \leq n$$$ ?

A better solution for k = 2

Idea

In the above approach, we fix $$$u$$$ as a squarefree and count $$$p^2$$$.

But what if I fix $$$p^2$$$ to count $$$u$$$ instead ?

Yet you can see that the first loop now is $$$O(\sqrt{n})$$$, but it will still $$$O(n)$$$ total because of the second loop

Swap for loop implementation

Approach

Let $$$f(n)$$$ is the number of pair $$$(a, b)$$$ that $$$1 \leq a < b \leq n$$$ and $$$(a, b, n)$$$ is a three-term geometric progression.

Let $$$g(n)$$$ is the number of pair $$$(a, b)$$$ that $$$1 \leq a \leq b \leq n$$$ and $$$(a, b, n)$$$ is a three-term geometric progression.

Let $$$F(n) = \overset{n}{\underset{p=1}{\Large \Sigma}} f(p)$$$.

But why do we need these functions anyway

So it is no hard to prove that $$$g(n) = f(n) + 1$$$.

This interesting sequence $$$g(n)$$$ is A000188, having many properties, such as

  • Number of solutions to $$$x^2 \equiv 0 \pmod n$$$.
  • Square root of largest square dividing $$$n$$$.
  • Max $$$gcd \left(d, \frac{n}{d}\right)$$$ for all divisor $$$d$$$.

Well, to make the problem whole easier, I gonna skip all the proofs to use this property (still, you can use the link in the sequence for references).

$$$g(n) = \underset{d^2 | n}{\Large \Sigma} \phi(d)$$$.

From this property, we can solve the problem in $$$O(\sqrt{n})$$$.

Hint 1
Hint 2
Hint 3
Hint 4
Solution

Yet this paper also takes you to something similar.

Implementation

O(sqrt n log log sqrt n) solution
O(sqrt) solution

A better solution for general k

Extra task A, B

Algorithm

As what clyring decribed here

Let $$$f_k(n)$$$ is the number of set $$$(a_1, a_2, \dots, a_k, n)$$$ that $$$1 \leq a_1 < a_2 < \dots < a_k \leq n$$$ and $$$(a_1, a_2, \dots, a_k, n)$$$ is a $$$(k+1)$$$-term geometric progression.

Let $$$g_k(n)$$$ is the number of set $$$(a_1, a_2, \dots, a_k, n)$$$ that $$$1 \leq a_1 \leq a_2 \leq \dots \leq a_k \leq n$$$ and $$$(a_1, a_2, \dots, a_k, n)$$$ is a $$$(k+1)$$$-term geometric progression.

Let $$$F_k(n) = \overset{n}{\underset{p=1}{\Large \Sigma}} f_k(p)$$$.

Let $$$s_k(n)$$$ is the number of way to choose $$$p^2$$$ among those $$$k$$$ numbers when you fix squarefree $$$u$$$ (though we are doing in reverse).

The formula

Implementation

O(sqrt n log sqrt n)
O(sqrt log log sqrt n)

Complexity

The complexity of the first implementation is $$$O(\sqrt{n} \log \sqrt{n})$$$

Hint 1
Hint 2
Hint 3
Proof

The complexity of the second implementation is $$$O(\sqrt{n} \log \log \sqrt{n})$$$

Hint 1
Proof

Solution for duplicates elements in array

Extra task C

Idea

It is no hard to proove that we can use the same algorithm as described in task A, B or in original task.

Hint
Proof

Using the same algorithm, the core of calculating is to find out the number of non-decreasing integer sequence of size $$$k$$$ where numbers are in $$$[1, n]$$$.

The formula is

Can you proove it ?

Hint 1
Hint 2
Hint 3
Proof

Now it is done, just that it

The idea is the same as what clyring described here but represented in the other way

Implementation

O(n) solution
O(sqrt n log sqrt n + k) solution
O(sqrt n log log sqrt n + k) solution

Complexity

In the first implementation it is obviously linear.

Hint 1
Hint 2

The second and third implementation is also easy to show its complexity

Hint 1
Hint 2

Sadly, since $$$k \leq n$$$. We also conclude that the complexity is $$$O(n)$$$, and even worse it also contains large constant factor compared to that in the first implementation.

But it is still effecient enough to solve problem where $$$k$$$ is small.

Bonus

Solution when there are no restriction between k, n, p

Extra task D

Idea

So first of all, the result do depend on how you calculate binomial coefficient but they are calculated independently even if you can somehow manage to use the for loop of binomial coeffient go first.

Therefore even if there is no restriction between $$$k, n, p$$$, the counting part and the algorithm doesnt change.

You just need to change how you calculate binomial coefficient, and that is all for this task.

Let just ignore the fact that though this need more detail, but as the blog is not about nck problem I will just make it quick

For large prime $$$p > max(n, k)$$$

  • Just using normal combinatorics related to factorial (since $$$p > max(n, k)$$$ nothing will affect the result)
  • For taking divides under modulo you can just take modular inversion (as a prime always exist such number)
  • Yet this is standard problem, just becareful of the overflow part
  • You can also optimize by precalculating factorial, inversion number and inversion factorial in linear too

For general prime $$$p$$$

  • We can just ignore factors $$$p$$$ in calculating $$$n!$$$.
  • You also need to know how many times factor $$$p$$$ appears in $$$1 \dots n$$$
  • Then combining it back when calculating for the answer.
  • If we dont do this $$$n!$$$ become might divides some factors of $$$p$$$.
  • By precalculation you can answer queries in $$$O(1)$$$

For squarefree $$$p$$$

  • Factorize $$$p = p_1 \times p_2 \times p_q$$$ that all $$$p_i$$$ is prime.
  • Ignore all factors $$$p_i$$$ when calculate $$$n!$$$.
  • Remember to calculate how many times factors $$$p_i$$$ appear in $$$1 \dots n$$$.
  • When query for the answer we just combine all those part back.
  • Remember you can just take modulo upto $$$\phi(p)$$$ which you can also calculate while factorizing $$$p$$$.
  • Remember that $$$n!$$$ must not divides any factor $$$p_i$$$ otherwise you will get wrong answer.
  • By precalculation you can answer queries in $$$O(\log p)$$$

For general positive modulo $$$p$$$

  • Factorize $$$p = p_1^{f_1} \times p_2^{f_2} \times p_q^{f_q}$$$ that all $$$p_i$$$ is unique prime.
  • We calculate $$$C(n, k)$$$ modulo $$$p_i^{f_i}$$$ for each $$$i = 1 \dots q$$$.
  • To do that, we need to calculate $$$n!$$$ modulo $$$p_i^{f_i}$$$ which is described here.
  • To get the final answer we can use CRT.
  • Yet this is kinda hard to code and debug also easy to make mistake so you must becareful
  • I will let the implementation for you lovely readers.
  • Yet depends on how you calculate stuffs that might increase your query complexity
  • There are few (effective or atleast fully correct) papers about this but you can read the one written here

Implementation

O(n) for prime p > max(n, k)
O(n log mod + sqrt(mod)) for prime p or squarefree p

Complexity

In the first implementation it is obviously linear.

Hint

And for the second implementation.

Hint 1
Hint 2

So you got $$$O(n \times \log p + \sqrt{p})$$$ in final.

Bonus

Though you can still optimize this but by doing that why dont you just go straight up to solve for non squarefree $$$p$$$ too ?

Solution when numbers are also bounded by negative number

Extra task E

Idea

Yet this is the same as extra task C where only the counting part should be changed.

As we only care about integer therefore let not use complex math into this problem.

If there exist a negative number and a positive number, the product will be negative thus the sequence will not satisfied.

Becareful, there are the zeros too.

When the numbers are all unique, or $$$-n \leq a_1 < a_2 < \dots < a_k \leq n$$$

There are 4 cases:

Thus give us the formula of $$$task_E(n, k) = 2 \times task_B(n, k) + 2 \times task_B(n, k - 1)$$$.

Hint 1
Hint 2
Hint 3
Hint 4
Proof

Remember that when $$$k = 0$$$ the answer is $$$0$$$ otherwise you might somewhat having wrong result for negative number in binomial coefficients formula

So what if I mix the problem with task C too ?

When the numbers can have duplicates, or $$$-n \leq a_1 \leq a_2 \leq \dots \leq a_k \leq n$$$

There are 5 cases:

Yet once again you can simplified it with less cases for easier calculation.

There are 2 main cases:

Thus give us the formula of $$$task_E(n, k) = 1 + 2 \times \overset{k}{\underset{t = 1}{\Large \Sigma}} task_B(n, t)$$$.

Why the formula is 2 * ...?
No I mean why there is no binomial coefficients for selecting the number of zeros ?
So where is the part 1 come frome ? - Why isnt it 2 instead ?

But this give you a $$$O(k)$$$ solution.

You can do better with math

Hint 1
Hint 2
Solution

Implementation

O(sqrt n log log sqrt n) when the numbers are unique

And for duplicates (mixed with task C), we have:

O(kn) = O(n^2)
O(k sqrt n log sqrt n) = O(n sqrt n log n)
O(k sqrt n log log sqrt n) = O(n sqrt n log log n)
O(k sqrt n + sqrt n log log sqrt n) = O(n sqrt n)
O(k + sqrt n log log sqrt n) = O(n)

Complexity

So.. you might be tired of calculating complexity again and again for those are too familar to you.

So I gonna skip this as the proof is as the same as what you can read above.

But as a bonus, you optimize the problem using this

Then you can solve the problem in $$$O(\sqrt{n} \log \log \sqrt{n} + \sqrt{p} \log^q p)$$$ ($$$q$$$ is depended on how you implement it).

Solution when numbers are also bounded by a specific range

Extra task F

Algorithm

We split into 4 cases

The cases

You can easilier solve for each cases in linear

Case 1
Case 2
Case 3
Case 4

Implementation

O(max(|l|, |r|))
O(|r-l| sqrt(x) / log(x) for x = max(|l|, |r|)

Complexity

For the first implemenetation it is obviously linear time in $$$max(|L|, |R|)$$$ and linear space in $$$|L - R|$$$

For the second implementation it is harder to proove. Yet it is bounded by the complexity of calculating segment erastothenes sieve .

Contribution

Теги combinatorics, generalization, mobius, phi, euler totient

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en58 Английский SPyofcode 2022-08-11 06:44:58 1 Fixing Latex Notation
en57 Английский SPyofcode 2022-08-11 06:43:05 47
en56 Английский SPyofcode 2022-08-11 06:40:20 80 Fix latex notations.
en55 Английский SPyofcode 2021-11-10 19:53:18 1215
en54 Английский SPyofcode 2021-11-09 16:59:56 533
en53 Английский SPyofcode 2021-11-09 12:12:28 1853 Tiny change: '^9 + 7$.\nFor $n \' -> '^9 + 7$.\n\nFor $n \'
en52 Английский SPyofcode 2021-11-08 13:43:39 1194
en51 Английский SPyofcode 2021-11-08 06:13:01 13
en50 Английский SPyofcode 2021-11-07 16:25:16 6097 Tiny change: 'es of $k$ (1 \leq k ' -> 'es of $k$ $(1 \leq k '
en49 Английский SPyofcode 2021-11-07 14:18:44 12
en48 Английский SPyofcode 2021-11-07 12:45:40 155 Reverted to en46
en47 Английский SPyofcode 2021-11-07 12:28:35 155
en46 Английский SPyofcode 2021-11-06 16:50:00 285 Tiny change: 't \rfloor \right + \unders' -> 't \rfloor + \unders'
en45 Английский SPyofcode 2021-11-06 14:33:23 12
en44 Английский SPyofcode 2021-11-06 13:46:02 153
en43 Английский SPyofcode 2021-11-06 07:13:11 57
en42 Английский SPyofcode 2021-11-06 07:06:43 3009
en41 Английский SPyofcode 2021-11-06 04:59:50 35
en40 Английский SPyofcode 2021-11-06 04:11:51 7
en39 Английский SPyofcode 2021-11-06 04:11:11 556
en38 Английский SPyofcode 2021-11-06 03:56:31 933
en37 Английский SPyofcode 2021-11-06 02:55:34 920 Tiny change: '*\n\n#### Algorithm\n\n<spoil' -> '*\n\n#### Idea\n\n<spoil'
en36 Английский SPyofcode 2021-11-05 17:25:43 37 Tiny change: 'squarefree as **it j' -> 'squarefree, it is not how we use in the formula as **it j'
en35 Английский SPyofcode 2021-11-05 17:11:33 8990
en34 Английский SPyofcode 2021-11-05 17:03:34 14732
en33 Английский SPyofcode 2021-11-05 16:50:02 4141
en32 Английский SPyofcode 2021-11-05 10:36:09 617
en31 Английский SPyofcode 2021-11-05 07:21:43 631
en30 Английский SPyofcode 2021-11-05 06:45:33 246
en29 Английский SPyofcode 2021-11-05 06:39:36 2084
en28 Английский SPyofcode 2021-11-03 05:22:22 537
en27 Английский SPyofcode 2021-11-03 05:04:31 1713
en26 Английский SPyofcode 2021-11-03 03:04:41 58
en25 Английский SPyofcode 2021-11-02 19:52:48 5 Tiny change: 'log/entry/edit/96379)**\' -> 'log/entry/96379)**\'
en24 Английский SPyofcode 2021-11-02 13:30:46 44
en23 Английский SPyofcode 2021-11-02 12:45:42 90
en22 Английский SPyofcode 2021-11-02 12:43:07 181
en21 Английский SPyofcode 2021-11-02 12:30:16 107
en20 Английский SPyofcode 2021-11-02 12:28:56 50
en19 Английский SPyofcode 2021-11-02 12:27:13 5785
en18 Английский SPyofcode 2021-11-02 04:29:23 46 Tiny change: 'g,2021-7-11] for fixi' -> 'g,2021-7-12] for fixi'
en17 Английский SPyofcode 2021-11-01 20:05:24 74
en16 Английский SPyofcode 2021-11-01 14:07:51 12
en15 Английский SPyofcode 2021-11-01 13:56:28 7 Tiny change: ' generalization harder va' -> ' generalized harder va'
en14 Английский SPyofcode 2021-11-01 13:50:48 92
en13 Английский SPyofcode 2021-11-01 13:35:43 24 Tiny change: 'qrt{R - L} {\Large )' -> 'qrt{R - L}) {\Large )'
en12 Английский SPyofcode 2021-11-01 13:33:59 299
en11 Английский SPyofcode 2021-11-01 13:31:23 188
en10 Английский SPyofcode 2021-11-01 13:27:51 9686 Tiny change: 'ask **F, J**, better' -> 'ask **F, J, M, O**, better'
en9 Английский SPyofcode 2021-11-01 08:44:03 156
en8 Английский SPyofcode 2021-11-01 06:59:09 39
en7 Английский SPyofcode 2021-11-01 06:51:52 29
en6 Английский SPyofcode 2021-11-01 06:50:49 77
en5 Английский SPyofcode 2021-11-01 06:49:01 21
en4 Английский SPyofcode 2021-11-01 06:44:37 71
en3 Английский SPyofcode 2021-11-01 06:42:35 605
en2 Английский SPyofcode 2021-11-01 06:40:17 15718 (published)
en1 Английский SPyofcode 2021-11-01 06:22:35 32737 Initial revision (saved to drafts)