Блог пользователя OtterZ

Автор OtterZ, 20 месяцев назад, По-английски

Here are some useful conclutions for naive algorithms to solve number theory problem,I hope you can know something about it and solve number theory problems more easily.

1.The number of prime factors of an integer

It's sure that the number of prime factors of an integer is very small,and an integer $$$v$$$ can be the product of at most $$$\log_2(v)$$$ primes ($$$2 ^ k$$$ the worst).This can be used for bruteforce and State compression.

Thanks AkiLotus to remind me that for the number of distinct prime factors of a integer $$$\operatorname{w}(n)$$$,$$$\sum_{i = 1}^n \operatorname{w}(n)$$$ is $$$\operatorname{O}(n \log \log n)$$$.

example:510D,1422F - Boring Queries.

2.The number of factors of an integer

First of all,$$$\sum_{i = 1} ^ n \operatorname{d}(n) = \sum_{i = 1} ^ n [\frac{n}{i}] \approx n \ln n$$$.

Then I've found out that the number of factors of an integer($$$\operatorname{d}(n)$$$) is usually small,and to make sure,I made a code to get the maxinum number of the number of factors,and get:

  1. For $$$n \le 10 ^ 4,\operatorname{d}(n) \lt = 64$$$;
  2. For $$$n \le 5 \times 10 ^ 4,\operatorname{d}(n) \lt = 100$$$;
  3. For $$$n \le 10 ^ 5,\operatorname{d}(n) \lt = 128$$$;
  4. For $$$n \le 2 \times 10 ^ 5,\operatorname{d}(n) \lt = 160$$$;
  5. For $$$n \le 3 \times 10 ^ 5,\operatorname{d}(n) \lt = 180$$$;
  6. For $$$n \le 5 \times 10 ^ 5,\operatorname{d}(n) \lt = 200$$$;
  7. For $$$n \le 10 ^ 6,\operatorname{d}(n) \lt = 240$$$;
  8. For $$$n \le 5 \times 10 ^ 6,\operatorname{d}(n) \lt = 384$$$;
  9. For $$$n \le 10 ^ 7,\operatorname{d}(n) \lt = 448$$$;

So if your solution of a problem is $$$\operatorname{O}(n\max \operatorname{d}(a_i))$$$ or $$$\operatorname{O}(\sum \operatorname{d}(a_i))$$$,it might be correct because for $$$a_i \le 10 ^ 7$$$,it's sure that $$$\operatorname{d}(a_i) \le 500$$$.

examples:

3.Euler's Function: $$$\operatorname{O}(\log_2 n)$$$ times to $$$1$$$.

It's sure that $$$\phi(n) \le \frac{n}{2}$$$ for $$$2 | n$$$,and $$$2 | \phi(n)$$$ for $$$n \gt 1$$$.So if you use operation $$$x = \phi(x)$$$ for $$$x = n$$$ initially,it will become $$$1$$$ in $$$\operatorname{O}(\log_2 n)$$$ times.

In fact,it can be related to problems about power,when we try to solve the problem using Euler's Theorem or Extend Euler's Theorem.

example:

4.Prefixes: $$$\operatorname{O}(\log_2 n)$$$ distinct prefix great common diverse/and/or

Thanks Ghulam_Junaid and Timosh to remind me about the feature.

For $$$\gcd(a_1,a_2,...,a_k)$$$,We can add a new integer $$$a_{k + 1}$$$ and found:

  • If $$$\gcd(a_1,a_2,...,a_k) | a_{k + 1}$$$,it's sure that $$$\gcd(a_1,a_2,...,a_k,a_{k + 1}) = \gcd(a_1,a_2,...,a_k)$$$.
  • Otherwise,$$$\gcd(a_1,a_2,...,a_k,a_{k + 1}) \le [\frac{\gcd(a_1,a_2,...,a_k)}{2}]$$$.

So there are at most $$$\log_2 n$$$ distinct prefix great common diverse.

For operator and or or,every integers can be written by $$$\log_2 n$$$ digits,and:

  • For operator and,the number of "1" in the digits decreases;
  • And for operator or,the numbr of "1" increases;

So there are at most $$$\log_2 n$$$ prefixes and suffixes.

example:

5.At most $$$[2\sqrt{n}]$$$ distinct integers of $$$[\frac{n}{i}],1 \le i \le n$$$.

Known as number theory chunking in public,we can proof that $$$[\frac{n}{i}] = [\frac{n}{[\frac{n}{i}]}]$$$,and then split $$$[1,n]$$$ to $$$\operatorname{O}(\sqrt n)$$$ sections like $$$[l,r = [\frac{n}{l}]]$$$,it's really useful while calculating $$$\sum_{i = 1}^n \operatorname{f}([\frac{n}{i}])$$$ or it's easy to consider several integer $$$v_1,v_2,...,v_k$$$ together when $$$[\frac{n}{v_i}],1 \le i \le k$$$ is the same.

If we use Möbius inversion first,we might found that we have to calculate $$$\operatorname{f}(\frac{x}{d})$$$,using the feature we can found out that we can calculate the function just $$$[2\sqrt{n}]$$$ times for distinct numbers.

example:

6.Special fraction sums

Thanks hydra_cody.

  • $$$\sum_{i = 1}^n \frac{1}{i} \approx \ln n$$$
  • $$$\lim_{n \to \inf}\sum_{i = 1} ^ n \frac{1}{i ^ 2} = \frac{\pi ^ 2}{6}$$$
  • $$$\lim_{n \to \inf}\sum_{i = 0} ^ n \frac{1}{2 ^ i} = 2$$$

Last:written in the end:

I would like to thank:

  • Проголосовать: нравится
  • +147
  • Проголосовать: не нравится

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится +17 Проголосовать: не нравится

Question: why "intelliger" instead of "integer"? Is it an implication that integers are intelligent and sentient beings?

A slight correction on 1: the growth is in fact just $$$\mathcal{O}(\log \log n)$$$ on average.

  • »
    »
    20 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I think he was talking about the worst case relating to (not necessarily distinct) prime factors, since oftentimes the worst case is of greater importance in competitive programming.

    I'm curious about the worst case when we count distinct prime factors though — the wikipedia page you linked says that $$$\omega(n) \sim \frac{\log n}{\log \log n}$$$ when $$$n$$$ is a primorial, but I can't see how one would come up with this approximation :/

    • »
      »
      »
      20 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится +3 Проголосовать: не нравится

      Ah, I see. I tend to only consider distinct ones (non-distinct can be compressed in map form), so was kinda misreading that part.

      For your concern, I looked at the citation which wrote:

      For references to each of these average order estimates see equations (3) and (18) of the MathWorld reference and Section 22.10-22.11 of Hardy and Wright.

      Probably the document they meant to mention was G. H. Hardy and E. M. Wright (2006). An Introduction to the Theory of Numbers (6th ed.). Oxford University Press..

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

gcd(x, y) = min(x, y) or gcd(x, y) <= min(x, y) / 2. so we can say there are O(lg(n)) distinct prefix gcds.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Man, I don't know. Maybe it's just preference, but I don't think "intelliger" is a word.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Very interesting. This may be helpful for those who are interested in math.

I also saw a problem involving prefix ORs, which has at most $$$log_2(r)$$$ different prefixes. Same goes for AND.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

thanks.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

if i am not mistaken, for $$$n \le 10^4$$$, we have $$$\max d(n) \le 64$$$, not $$$68$$$.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

For number 3, another problem example is 1797E - Li Hua и массив, although I am not sure if you could consider it as an application of the trick

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I always use the following estimation even though it's incorrect:

The number of divisors of $$$N$$$ is $$$O(\sqrt[3]{N})$$$.

It just works very well for $$$N$$$ values that we deal with in CP.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For the number of prefix gcd, it's worth mention that, if we think the values of array as factorized form and map the frequency table of prime factors to a vector(ex. $$$50 = 2^1 \cdot 3^0 \cdot 5^2$$$ so we use vector $$$[1, 0, 2, 0, 0, 0, \ldots]$$$ to represent it), then taking gcd of two number is essentially taking element-wise min of their respective vector, and the sum of element in a vector is $$$O(\lg n)$$$ so we can decrease it $$$O(\lg n)$$$ times.

You can also think it as an extended result of prefix bitwise and have $$$O(\lg n)$$$ different value.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I don't quite undertand what you are trying to calculate with $$$w(n), \sum_{i=1}^{n} w(n) \text{ is } O(n \log \log n)$$$

Let see the case when $$$n = 16$$$ then $$$16 log(log 16) = 16.3165$$$ but the prime factorization of $$$16$$$ is $$$2 * 2 * 2 * 2$$$ and clearly has 1 distinct prime factor, and from $$$1, 2, 3, 4, .... 16$$$ there are 6 distinct prime factors

EDIT: nvm i didn't realize it was complexity for some reason.

  • »
    »
    20 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    For $$$\operatorname{w}(n)$$$,$$$\sum_{i = 1}^n \operatorname{w}(n)$$$,you should count the distinct prime factors seperately for each of the integers,and add the numbers together.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This post made my way back to CM, and 9th place in the last Div. 2, as 2013E - Префиксные НОД was literally named "Prefix GCD". I guess I was lucky.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

https://cses.fi/problemset/task/1082

Can include this classic problem as well for at most 2*(sqrt(N)) distinct integers (point 5).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

thanks for the blog!!would be really helpful by more blogs on number theory and math!!

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OtterZ (previous revision, new revision, compare).

»
16 месяцев назад, скрыть # |
Rev. 5  
Проголосовать: нравится -10 Проголосовать: не нравится
spoiler