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.
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:
- For $$$n \le 10 ^ 4,\max \operatorname{d}(n) <= 64$$$;
- For $$$n \le 5 \times 10 ^ 4,\max \operatorname{d}(n) <= 100$$$;
- For $$$n \le 10 ^ 5,\max \operatorname{d}(n) <= 128$$$;
- For $$$n \le 2 \times 10 ^ 5,\max \operatorname{d}(n) <= 160$$$;
- For $$$n \le 3 \times 10 ^ 5,\max \operatorname{d}(n) <= 180$$$;
- For $$$n \le 5 \times 10 ^ 5,\max \operatorname{d}(n) <= 200$$$;
- For $$$n \le 10 ^ 6,\max \operatorname{d}(n) <= 240$$$;
- For $$$n \le 5 \times 10 ^ 6,\max \operatorname{d}(n) <= 384$$$;
- For $$$n \le 10 ^ 7,\max \operatorname{d}(n) <= 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 > 1$$$.So if you use operation $$$x = \phi(x)$$$ for $$$x = n$$$ initially,it will become $$$1$$$ in $$$\operatorname{O}(\log_2 n)$$$ times.
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:
Last:written in the end:
I would like to thank:
- zhengqingyuan who edit the blog;
- AkiLotus,PeruvianCartel,peltorator for correcting the mistake;
- Ghulam_Junaid to remind me about a feature about great common diverse;
- Timosh to remind me about a feature about operator
and
andor
. - AkiLotus to remind me something about prime factors.
- Timosh,The-Winner,GUNNER19_2.0 to provide some examples.
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
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.
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 :/
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:
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.
.oh wow, I totally forgot about references lol. I took a look at it, and it makes way more sense now. Thanks!
Sorry in my country wikipedia is banned and please send here in detail.
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.
OK,I'll add it.
what does distinct prefix gcds mean?
let's say you are computing prefix gcd of an array (similar to prefix sum but you are doing gcd instead of sum), distinct values in this prefix gcd array is "distinct prefix gcds".
Man, I don't know. Maybe it's just preference, but I don't think "intelliger" is a word.
fixed
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Very interesting. This may be helpful for those who are interested in math.
I also saw a problem involving prefix
OR
s, which has at most $$$log_2(r)$$$ different prefixes. Same goes forAND
.Thanks.
Could you give me the URL?
1669H - Maximal AND and XOR and OR (from kep.uz)
OK,but I've senn 1669H and found the solution didn't use the feature.
Yeah, there are several ways to solve a problem
OK.
thanks.
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
if i am not mistaken, for $$$n \le 10^4$$$, we have $$$\max d(n) \le 64$$$, not $$$68$$$.
I'll check that.
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
For number 3, another problem example is 1797E - Li Hua and Array, although I am not sure if you could consider it as an application of the trick
OK,I'll add it.
+
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.
I use the estimation $$$d(N)=o(n^\varepsilon)$$$ because it works very well for the values that I deal with.
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
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.
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
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.
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.
This post made my way back to CM, and 9th place in the last Div. 2, as 2013E - Prefix GCD was literally named "Prefix GCD". I guess I was lucky.
Congradulations to you and thank you to remind me the problem.
But there's nothing in this post that's helpful in proving the greedy solution for 2013E, or am I missing something?
I've come out of a solution using conclution $$$2$$$ and I'll try if it works.
When doing greedy problems, I usually don't prove my solution. Instead, I try to come up with a counter-test, and if I fail to, I'll assume it works. That's how CP works
Then what's the point of your comment?
In the blog it's written that there are at most $$$log(A_{max})$$$ different prefix gcds. And when I looked at 2013E, I assumed it has the same idea, as each time it's better to change(decrease) the gcd, which we don't have to do more than $$$log(A_{max})$$$ times. You can check my code, where I used $$$r = 100$$$, where I check best element each time, first is the smallest element, second is the smallest resulting GCD and so on. You don't have to do it more than ~40 times.
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
https://cses.fi/problemset/task/1082
Can include this classic problem as well for at most 2*(sqrt(N)) distinct integers (point 5).
OK
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Thanks for this. Please do more of these if possible!
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
thanks for the blog!!would be really helpful by more blogs on number theory and math!!
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).
Auto comment: topic has been updated by zhengqingyuan (previous revision, new revision, compare).