[Tutorial] An interesting counting problem related to square product

Revision en57, by SPyofcode, 2021-11-09 12:12:22

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 k$$$

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 in $$$O(n)$$$ for general $$$k$$$ from the original task

For harder variants you can see in this blog [Variants] An interesting counting problem related to square product

Solution for k = 1

The answer just simply be $$$n$$$

Solution for k = 2

Problem

Given $$$n (1 \leq n \leq 10^9)$$$, count the number of pair $$$(a, b)$$$ that satisfied

  • $$$1 \leq a < b \leq n$$$
  • $$$a \times b$$$ is a perfect square.

Since the number can be big, output it under modulo $$$10^9 + 7$$$.

You can submit here: https://lqdoj.edu.vn/problem/aicprtsp2.

Examples

Example 1
Example 2
Example 3

Algorithm

We need to count the number of pair $$$(a, b)$$$ that $$$1 \leq a < b \leq n$$$ and $$$a \times b$$$ is perfect square.

Every positive integer $$$x$$$ can be represent uniquely as $$$x = u \times p^2$$$ for some positive integer $$$u, p$$$ and $$$u$$$ as small as possible ($$$u$$$ is squarefree number).

Let represent $$$x = u \times p^2$$$ and $$$y = v \times q^2$$$ (still, minimum $$$u$$$, $$$v$$$ ofcourse).

We can easily proove that $$$x \times y$$$ is a perfect square if and if only $$$u = v$$$.

So for a fixed squarefree number $$$u$$$. You just need to count the number of ways to choose $$$p^2$$$.

The answer will be the sum of such ways for each fixed $$$u$$$.

Illustration

Squarefree Sieve 10x10 Table | Iteration 0
Squarefree Sieve 10x10 Table | Iteration 1
Squarefree Sieve 10x10 Table | Iteration 2
Squarefree Sieve 10x10 Table | Iteration 3
Squarefree Sieve 10x10 Table | Iteration 5
Squarefree Sieve 10x10 Table | Iteration 6
Squarefree Sieve 10x10 Table | Iteration 7
Squarefree Sieve 10x10 Table | Iteration 10
Squarefree Sieve 10x10 Table | Iteration 11
Squarefree Sieve 10x10 Table | Iteration 13
Squarefree Sieve 10x10 Table | Iteration 14
Squarefree Sieve 10x10 Table | Iteration 15
Squarefree Sieve 10x10 Table | Iteration 17
Squarefree Sieve 10x10 Table | Iteration 19
Squarefree Sieve 10x10 Table | Iteration 21
Squarefree Sieve 10x10 Table | Iteration 22
Squarefree Sieve 10x10 Table | Iteration 23
Squarefree Sieve 10x10 Table | Iteration 100

Implementation

Implementation using factorization
Implementation 1
Implementation 2
Implementation related to Möbius function

Complexity

So about the complexity....

For the implementation using factorization, it is $$$O(n \log n)$$$.

Hint 1
Hint 2
Proof
Bonus

For the 2 implementations below, the complexity is linear.

Hint 1
Hint 2
Hint 3
Hint 4
Proof

For the last implementation, the complexity is Linear

Hint 1
Hint 2
Proof

Solution for general k

Problem

Given $$$k, n (1 \leq k \leq n \leq 10^9)$$$, 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 k$$$

Since the number can be big, output it under modulo $$$10^9 + 7$$$.

You can submit here: https://lqdoj.edu.vn/problem/aicprtspk.

Examples

Example 1
Example 2
Example 3

Using the same logic above, we can easily solve the problem.

Now you face up with familliar binomial coefficient problem

This implementation here is using the assumption of $$$p$$$ prime and $$$p > max(n, k)$$$

You can still solve the problem for squarefree $$$p$$$ using lucas and CRT

Yet just let things simple as we only focus on the counting problem, we will assume $$$p$$$ is a large constant prime.

O(n) solution

Extra Tasks

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

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

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

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)$$$ ?

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

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

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$$$ ?

O: What if the condition is just two nearby elements and not all pairs. Or you can say $$$a_i \times a_{i+1} \forall 1 \leq i < n$$$ is a perfect square ?

Tags combinatorics

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en60 English SPyofcode 2021-11-09 12:21:19 4
en59 English SPyofcode 2021-11-09 12:20:38 6 Tiny change: 'n subimit [https://' -> 'n subimit here: [https://'
en58 English SPyofcode 2021-11-09 12:19:51 265
en57 English SPyofcode 2021-11-09 12:12:22 3178
en56 English SPyofcode 2021-11-05 19:03:46 3619
en55 English SPyofcode 2021-11-01 08:53:10 64
en54 English SPyofcode 2021-11-01 08:43:50 158
en53 English SPyofcode 2021-11-01 06:55:47 2 Tiny change: ' for $k = 3$ in $O(\s' -> ' for $k = 2$ in $O(\s'
en52 English SPyofcode 2021-11-01 06:44:45 71
en51 English SPyofcode 2021-11-01 06:40:09 16954 (published)
en50 English SPyofcode 2021-11-01 06:22:49 49881 (saved to drafts)
en49 English SPyofcode 2021-11-01 05:27:15 49
en48 English SPyofcode 2021-11-01 05:18:00 184
en47 English SPyofcode 2021-10-31 19:17:30 40
en46 English SPyofcode 2021-10-31 19:10:27 5305
en45 English SPyofcode 2021-10-31 12:29:39 196
en44 English SPyofcode 2021-10-31 12:15:21 229
en43 English SPyofcode 2021-10-31 12:12:51 5175
en42 English SPyofcode 2021-10-31 12:04:20 4012 (published)
en41 English SPyofcode 2021-10-31 07:41:25 0 (saved to drafts)
en40 English SPyofcode 2021-10-31 07:10:39 916 Tiny change: ') solution">\n\n```c' -> ') solution for k = 3">\n\n```c'
en39 English SPyofcode 2021-10-31 06:59:31 6996
en38 English SPyofcode 2021-10-30 20:35:28 436
en37 English SPyofcode 2021-10-30 19:51:26 8
en36 English SPyofcode 2021-10-30 19:50:57 168
en35 English SPyofcode 2021-10-30 19:49:17 38
en34 English SPyofcode 2021-10-30 19:48:18 710
en33 English SPyofcode 2021-10-30 19:43:42 6
en32 English SPyofcode 2021-10-30 19:42:58 714
en31 English SPyofcode 2021-10-30 19:36:20 3825
en30 English SPyofcode 2021-10-30 16:20:57 8
en29 English SPyofcode 2021-10-30 16:19:47 2877
en28 English SPyofcode 2021-10-30 16:18:30 56
en27 English SPyofcode 2021-10-30 16:16:24 6543
en26 English SPyofcode 2021-10-30 11:38:14 769
en25 English SPyofcode 2021-10-30 06:48:18 76
en24 English SPyofcode 2021-10-30 06:45:41 23 Reverted to en22
en23 English SPyofcode 2021-10-30 06:41:21 23
en22 English SPyofcode 2021-10-30 05:34:13 2034 Tiny change: ' summary="Hint 2">\n\nThe ' -> ' summary="Proof">\n\nThe '
en21 English SPyofcode 2021-10-30 04:47:01 229
en20 English SPyofcode 2021-10-29 20:05:05 68 Tiny change: '---\n\n## Tasks\n\n' -> '---\n\n## Extra Tasks\n\n'
en19 English SPyofcode 2021-10-29 19:57:01 603
en18 English SPyofcode 2021-10-29 19:50:27 5436 Tiny change: '------\n\n' -> '------\n\n-------------------------\n\n-------------------------\n\n'
en17 English SPyofcode 2021-10-29 17:43:10 26
en16 English SPyofcode 2021-10-29 12:50:12 3892
en15 English SPyofcode 2021-10-29 12:16:06 1739
en14 English SPyofcode 2021-10-29 03:52:02 2
en13 English SPyofcode 2021-10-28 18:40:23 452 Tiny change: '## Statement:\' -> '## The statement:\'
en12 English SPyofcode 2021-10-28 18:31:38 1487
en11 English SPyofcode 2021-10-28 18:09:33 748
en10 English SPyofcode 2021-10-28 13:42:36 278
en9 English SPyofcode 2021-10-28 13:31:37 99
en8 English SPyofcode 2021-10-28 13:23:42 78
en7 English SPyofcode 2021-10-28 13:21:55 1383
en6 English SPyofcode 2021-10-28 13:08:47 885 Tiny change: 'e product effective' -> 'e product $a_i \times a_j \times a_k$ effective'
en5 English SPyofcode 2021-10-28 13:01:32 1 Tiny change: 'n $O\left(sqrt{n} \l' -> 'n $O\left(\sqrt{n} \l'
en4 English SPyofcode 2021-10-28 13:00:39 478
en3 English SPyofcode 2021-10-28 12:53:55 748 (published)
en2 English SPyofcode 2021-10-28 12:42:55 7656
en1 English SPyofcode 2021-10-28 12:05:23 3517 Initial revision (saved to drafts)