βΉοΈ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.5 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://codeforces.com/blog/entry/52697 |
| Last Crawled | 2026-04-02 06:45:09 (14 days ago) |
| First Indexed | 2018-12-19 21:53:19 (7 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Hashing: How to pick base and mod? - Codeforces |
| Meta Description | Codeforces. Programming competitions and contests, programming community |
| Meta Canonical | null |
| Boilerpipe Text | β Pay attention
β Top rated
#
User
Rating
1
B
enq
3792
2
t
ourist
3755
3
e
cnerwala
3696
4
V
ivaciousAubergine
3647
5
k
sun48
3629
6
j
iangly
3616
7
t
urmax
3559
8
s
trapple
3486
9
d
Xqwq
3436
10
K
evin114514
3427
β Top contributors
#
User
Contrib.
1
Q
ingyu
158
2
adamant
153
3
U
m_nik
152
4
D
ominater069
147
5
Proof_by_QED
145
6
e
rrorgorn
144
7
cry
140
8
YuukiS
135
8
TheScrasse
135
10
awoo
134
β Find user
β Recent actions
szawinis
Blog
Teams
Submissions
Groups
Contests
szawinis's blog
Picking the base and mod for string hashing is very important in decreasing the probability of hash collisions. How much are the guidelines affected by the problem itself? So far, I've read that the base should be larger than the alphabet, and the mod should be really large (but not overflow). But is there anything else? |
| Markdown | [](https://codeforces.com/)
[](https://codeforces.com/blog/entry/52697?locale=en) [](https://codeforces.com/blog/entry/52697?locale=ru)
[Enter](https://codeforces.com/enter?back=%2Fblog%2Fentry%2F52697) \| [Register](https://codeforces.com/register)
- [Home](https://codeforces.com/)
- [Top](https://codeforces.com/top)
- [Catalog](https://codeforces.com/catalog)
- [Contests](https://codeforces.com/contests)
- [Gym](https://codeforces.com/gyms)
- [Problemset](https://codeforces.com/problemset)
- [Groups](https://codeforces.com/groups)
- [Rating](https://codeforces.com/ratings)
- [Edu](https://codeforces.com/edu/courses)
- [API](https://codeforces.com/apiHelp)
- [Calendar](https://codeforces.com/calendar)
- [Help](https://codeforces.com/help)
β Pay attention
Before contest
[Codeforces Round (Div. 2)](https://codeforces.com/contests/2217)
5 days
[Register now Β»](https://codeforces.com/contestRegistration/2217)
\*has extra registration
β Top rated
| \# | User | Rating |
|---|---|---|
| 1 | [Benq](https://codeforces.com/profile/Benq "Legendary Grandmaster Benq") | 3792 |
| 2 | [tourist](https://codeforces.com/profile/tourist "Legendary Grandmaster tourist") | 3755 |
| 3 | [ecnerwala](https://codeforces.com/profile/ecnerwala "Legendary Grandmaster ecnerwala") | 3696 |
| 4 | [VivaciousAubergine](https://codeforces.com/profile/VivaciousAubergine "Legendary Grandmaster VivaciousAubergine") | 3647 |
| 5 | [ksun48](https://codeforces.com/profile/ksun48 "Legendary Grandmaster ksun48") | 3629 |
| 6 | [jiangly](https://codeforces.com/profile/jiangly "Legendary Grandmaster jiangly") | 3616 |
| 7 | [turmax](https://codeforces.com/profile/turmax "Legendary Grandmaster turmax") | 3559 |
| 8 | [strapple](https://codeforces.com/profile/strapple "Legendary Grandmaster strapple") | 3486 |
| 9 | [dXqwq](https://codeforces.com/profile/dXqwq "Legendary Grandmaster dXqwq") | 3436 |
| 10 | [Kevin114514](https://codeforces.com/profile/Kevin114514 "Legendary Grandmaster Kevin114514") | 3427 |
| | |
|---|---|
| [Countries](https://codeforces.com/ratings/countries) \| [Cities](https://codeforces.com/ratings/cities) \| [Organizations](https://codeforces.com/ratings/organizations) | [View all β](https://codeforces.com/ratings) |
β Top contributors
| \# | User | Contrib. |
|---|---|---|
| 1 | [Qingyu](https://codeforces.com/profile/Qingyu "Legendary Grandmaster Qingyu") | 158 |
| 2 | [adamant](https://codeforces.com/profile/adamant "Master adamant") | 153 |
| 3 | [Um\_nik](https://codeforces.com/profile/Um_nik "Legendary Grandmaster Um_nik") | 152 |
| 4 | [Dominater069](https://codeforces.com/profile/Dominater069 "Legendary Grandmaster Dominater069") | 147 |
| 5 | [Proof\_by\_QED](https://codeforces.com/profile/Proof_by_QED "International Master Proof_by_QED") | 145 |
| 6 | [errorgorn](https://codeforces.com/profile/errorgorn "Legendary Grandmaster errorgorn") | 144 |
| 7 | [cry](https://codeforces.com/profile/cry "International Master cry") | 140 |
| 8 | [YuukiS](https://codeforces.com/profile/YuukiS "International Grandmaster YuukiS") | 135 |
| 8 | [TheScrasse](https://codeforces.com/profile/TheScrasse "Grandmaster TheScrasse") | 135 |
| 10 | [awoo](https://codeforces.com/profile/awoo "Grandmaster awoo") | 134 |
| | |
|---|---|
| | [View all β](https://codeforces.com/top-contributed) |
β Find user
β Recent actions
- [Um\_nik](https://codeforces.com/profile/Um_nik "Legendary Grandmaster Um_nik") β [How to read problem statements](https://codeforces.com/blog/entry/62730)  
- [nifeshe](https://codeforces.com/profile/nifeshe "Grandmaster nifeshe") β [Codeforces Round 997 (Div. 2)](https://codeforces.com/blog/entry/138420)  
- [joelgun14](https://codeforces.com/profile/joelgun14 "Master joelgun14") β [The ICPC NAC 2026 Experience](https://codeforces.com/blog/entry/152257) 
- [Argentum47](https://codeforces.com/profile/Argentum47 "Expert Argentum47") β [Codeforces Round 1089 (Div. 2)](https://codeforces.com/blog/entry/152376) 
- [m7a1g5i8k8a1r4p0](https://codeforces.com/profile/m7a1g5i8k8a1r4p0 "Master m7a1g5i8k8a1r4p0") β [UTPC April Fool's Contest 2026 Editorial](https://codeforces.com/blog/entry/152595) 
- [programmercat](https://codeforces.com/profile/programmercat "Specialist programmercat") β [Happy Autism Awareness Day](https://codeforces.com/blog/entry/152598) 
- [jerrywang0916](https://codeforces.com/profile/jerrywang0916 "Expert jerrywang0916") β [List of Suspected Cheaters in Nebius Round 2](https://codeforces.com/blog/entry/152532) 
- [ToxicPie9](https://codeforces.com/profile/ToxicPie9 "Candidate Master ToxicPie9") β [April Fools Day Contest 2026 Editorial](https://codeforces.com/blog/entry/152573) 
- [CarViz](https://codeforces.com/profile/CarViz "Expert CarViz") β [fraud in the philipines\!](https://codeforces.com/blog/entry/152594) 
- [AlperenT](https://codeforces.com/profile/AlperenT "Master AlperenT") β [April Fools Day Contest 2026](https://codeforces.com/blog/entry/152445) 
- [AlexandruINV](https://codeforces.com/profile/AlexandruINV "Expert AlexandruINV") β [A Friendly Competitive Programming Discord Server\!](https://codeforces.com/blog/entry/151914) 
- [macaquedev](https://codeforces.com/profile/macaquedev "Candidate Master macaquedev") β [April Fools Day Contest 2026 Definitely Official Editorial](https://codeforces.com/blog/entry/152580) 
- [abhinav4710](https://codeforces.com/profile/abhinav4710 "Expert abhinav4710") β [Issues with the latest plagiarism check issued](https://codeforces.com/blog/entry/152589) 
- [\_\_AB\_\_](https://codeforces.com/profile/__AB__ "Expert __AB__") β [High Expectations from Expectations](https://codeforces.com/blog/entry/152586) 
- [uranaitnazarov](https://codeforces.com/profile/uranaitnazarov "Newbie uranaitnazarov") β [why would yall dislike my comment its peak bruh \#haters dawg](https://codeforces.com/blog/entry/152596) 
- [hasan2](https://codeforces.com/profile/hasan2 "Newbie hasan2") β [HASAN](https://codeforces.com/blog/entry/152584) 
- [holl0w](https://codeforces.com/profile/holl0w "Master holl0w") β [FlandeOJ, yet another competitive programming roadmap...](https://codeforces.com/blog/entry/152552) 
- [macaquedev](https://codeforces.com/profile/macaquedev "Candidate Master macaquedev") β [Blatant Thievery](https://codeforces.com/blog/entry/152588) 
- [yanb0](https://codeforces.com/profile/yanb0 "Master yanb0") β [Codeforces Round 1075 (Div. 2) Editorial](https://codeforces.com/blog/entry/150452) 
- [Ritwin](https://codeforces.com/profile/Ritwin "Master Ritwin") β [Stanford Local Programming Contest 2026](https://codeforces.com/blog/entry/152392) 
- [krakar](https://codeforces.com/profile/krakar "Expert krakar") β [Falsely Suspected of Cheating](https://codeforces.com/blog/entry/152590) 
- [tinyfold](https://codeforces.com/profile/tinyfold "Expert tinyfold") β [Guys I have a CONFESSION](https://codeforces.com/blog/entry/152562) 
- [Saad\_aa](https://codeforces.com/profile/Saad_aa "Newbie Saad_aa") β [Help needed For a more optimal solution](https://codeforces.com/blog/entry/152583) 
- [MikeMirzayanov](https://codeforces.com/profile/MikeMirzayanov "Headquarters, MikeMirzayanov") β [Rule about third-party code is changing](https://codeforces.com/blog/entry/8790)  
- [Anadi](https://codeforces.com/profile/Anadi "International Grandmaster Anadi") β [Codeforces Round \#519 Editorial](https://codeforces.com/blog/entry/62797)  
| | |
|---|---|
| | [Detailed β](https://codeforces.com/recent-actions) |
- [szawinis](https://codeforces.com/profile/szawinis)
- [Blog](https://codeforces.com/blog/szawinis)
- [Teams](https://codeforces.com/teams/with/szawinis)
- [Submissions](https://codeforces.com/submissions/szawinis)
- [Groups](https://codeforces.com/groups/with/szawinis)
- [Contests](https://codeforces.com/contests/with/szawinis)
### [szawinis's blog](https://codeforces.com/blog/szawinis)
[Hashing: How to pick base and mod?](https://codeforces.com/blog/entry/52697)
By [szawinis](https://codeforces.com/profile/szawinis "Candidate Master szawinis"), [history](https://codeforces.com/topic/52953/en1), 9 years ago, 
Picking the base and mod for string hashing is very important in decreasing the probability of hash collisions. How much are the guidelines affected by the problem itself? So far, I've read that the base should be larger than the alphabet, and the mod should be really large (but not overflow). But is there anything else?
- [](https://codeforces.com/blog/entry/52697)
- \+14
- [](https://codeforces.com/blog/entry/52697)
- [](https://codeforces.com/profile/szawinis) [szawinis](https://codeforces.com/profile/szawinis)
-  9 years ago
- [](https://codeforces.com/blog/entry/52697#comments) [5](https://codeforces.com/blog/entry/52697#comments)
 [Comments (4)]()
Show archived
\| [Write comment?](https://codeforces.com/blog/entry/52697)
| | |
|---|---|
| Β» [gongy](https://codeforces.com/profile/gongy "Grandmaster gongy") | 9 years ago, [show (+1)](https://codeforces.com/blog/entry/52697) [\#]() |
| | |
|---|---|
| Β» [](https://codeforces.com/profile/gongy) [gongy](https://codeforces.com/profile/gongy "Grandmaster gongy") | 9 years ago, [hide](https://codeforces.com/blog/entry/52697) [\#](https://codeforces.com/blog/entry/52697#comment-367252 "Link to comment") \| [](https://codeforces.com/blog/entry/52697) \+18 [](https://codeforces.com/blog/entry/52697) You sound like you are on the right track. The base and mod must be coprime β usually, you just choose a prime base and prime mod anyway. Furthermore, you usually need hash two or more mods to stay safe. This is because of the birthday paradox. Normally people use base 137 and hash in 1e9+7 and 1e9+9. [β](https://codeforces.com/blog/entry/52697) [Reply](https://codeforces.com/blog/entry/52697) |
- | | |
|---|---|
| Β» Β» [yeputons](https://codeforces.com/profile/yeputons "Grandmaster yeputons") | 9 years ago, [show](https://codeforces.com/blog/entry/52697) [\#]() |
| | |
|---|---|
| Β» Β» [](https://codeforces.com/profile/yeputons) [yeputons](https://codeforces.com/profile/yeputons "Grandmaster yeputons") | 9 years ago, [hide](https://codeforces.com/blog/entry/52697) [\#](https://codeforces.com/blog/entry/52697#comment-367302 "Link to comment") [^](https://codeforces.com/blog/entry/52697#comment-367252 "Parent comment") \| [](https://codeforces.com/blog/entry/52697) \+13 [](https://codeforces.com/blog/entry/52697) Base should also be strictly greater than the number of different values an individual element can be. E.g. 137 is enough for English latters, but not enough for arbitrary `char`s. Also, it's good to ensure that all codes of individual elements are between 0 and `base-1` so they do not collide modulo `base` for some random reason. [β](https://codeforces.com/blog/entry/52697) [Reply](https://codeforces.com/blog/entry/52697) |
- | | |
|---|---|
| Β» Β» [\_Dormammu\_](https://codeforces.com/profile/_Dormammu_ "Expert _Dormammu_") | 6 years ago, [show](https://codeforces.com/blog/entry/52697) [\#]() |
| | |
|---|---|
| Β» Β» [](https://codeforces.com/profile/_Dormammu_) [\_Dormammu\_](https://codeforces.com/profile/_Dormammu_ "Expert _Dormammu_") | 6 years ago, [hide](https://codeforces.com/blog/entry/52697) [\#](https://codeforces.com/blog/entry/52697#comment-589514 "Link to comment") [^](https://codeforces.com/blog/entry/52697#comment-367252 "Parent comment") \| [β](https://codeforces.com/blog/entry/52697) Rev. 2 [β](https://codeforces.com/blog/entry/52697) [](https://codeforces.com/blog/entry/52697) \-24 [](https://codeforces.com/blog/entry/52697) :) [β](https://codeforces.com/blog/entry/52697) [Reply](https://codeforces.com/blog/entry/52697) |
| | |
|---|---|
| Β» [bhishma](https://codeforces.com/profile/bhishma "Expert bhishma") | 9 years ago, [show](https://codeforces.com/blog/entry/52697) [\#]() |
| | |
|---|---|
| Β» [](https://codeforces.com/profile/bhishma) [bhishma](https://codeforces.com/profile/bhishma "Expert bhishma") | 9 years ago, [hide](https://codeforces.com/blog/entry/52697) [\#](https://codeforces.com/blog/entry/52697#comment-367357 "Link to comment") \| [](https://codeforces.com/blog/entry/52697) \+21 [](https://codeforces.com/blog/entry/52697) This topics has been discussed a lot of times. [Where to Use and How not to Use Polynomial String Hashing (PACHOCK,RADOSZEWSKI)](https://www.mii.lt/olympiads_in_informatics/pdf/INFOL119.pdf) [Anti-hash test](https://codeforces.com/blog/entry/4898) [β](https://codeforces.com/blog/entry/52697) [Reply](https://codeforces.com/blog/entry/52697) |
In English
In Russian
β
***
***
β
[Codeforces](https://codeforces.com/) (c) Copyright 2010-2026 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: Apr/02/2026 09:45:09 (l2).
Desktop version, switch to [mobile version](https://codeforces.com/blog/entry/52697?mobile=true).
[Privacy Policy](https://codeforces.com/privacy) \| [Terms and Conditions](https://codeforces.com/terms)
Supported by
[](https://ton.org/)
User lists


| Name |
|---| |
| Readable Markdown | [](https://codeforces.com/)
[](https://codeforces.com/blog/entry/52697?locale=en) [](https://codeforces.com/blog/entry/52697?locale=ru)
β Pay attention
β Top rated
| \# | User | Rating |
|---|---|---|
| 1 | [Benq](https://codeforces.com/profile/Benq "Legendary Grandmaster Benq") | 3792 |
| 2 | [tourist](https://codeforces.com/profile/tourist "Legendary Grandmaster tourist") | 3755 |
| 3 | [ecnerwala](https://codeforces.com/profile/ecnerwala "Legendary Grandmaster ecnerwala") | 3696 |
| 4 | [VivaciousAubergine](https://codeforces.com/profile/VivaciousAubergine "Legendary Grandmaster VivaciousAubergine") | 3647 |
| 5 | [ksun48](https://codeforces.com/profile/ksun48 "Legendary Grandmaster ksun48") | 3629 |
| 6 | [jiangly](https://codeforces.com/profile/jiangly "Legendary Grandmaster jiangly") | 3616 |
| 7 | [turmax](https://codeforces.com/profile/turmax "Legendary Grandmaster turmax") | 3559 |
| 8 | [strapple](https://codeforces.com/profile/strapple "Legendary Grandmaster strapple") | 3486 |
| 9 | [dXqwq](https://codeforces.com/profile/dXqwq "Legendary Grandmaster dXqwq") | 3436 |
| 10 | [Kevin114514](https://codeforces.com/profile/Kevin114514 "Legendary Grandmaster Kevin114514") | 3427 |
β Top contributors
| \# | User | Contrib. |
|---|---|---|
| 1 | [Qingyu](https://codeforces.com/profile/Qingyu "Legendary Grandmaster Qingyu") | 158 |
| 2 | [adamant](https://codeforces.com/profile/adamant "Master adamant") | 153 |
| 3 | [Um\_nik](https://codeforces.com/profile/Um_nik "Legendary Grandmaster Um_nik") | 152 |
| 4 | [Dominater069](https://codeforces.com/profile/Dominater069 "Legendary Grandmaster Dominater069") | 147 |
| 5 | [Proof\_by\_QED](https://codeforces.com/profile/Proof_by_QED "International Master Proof_by_QED") | 145 |
| 6 | [errorgorn](https://codeforces.com/profile/errorgorn "Legendary Grandmaster errorgorn") | 144 |
| 7 | [cry](https://codeforces.com/profile/cry "International Master cry") | 140 |
| 8 | [YuukiS](https://codeforces.com/profile/YuukiS "International Grandmaster YuukiS") | 135 |
| 8 | [TheScrasse](https://codeforces.com/profile/TheScrasse "Grandmaster TheScrasse") | 135 |
| 10 | [awoo](https://codeforces.com/profile/awoo "Grandmaster awoo") | 134 |
β Find user
β Recent actions
- [szawinis](https://codeforces.com/profile/szawinis)
- [Blog](https://codeforces.com/blog/szawinis)
- [Teams](https://codeforces.com/teams/with/szawinis)
- [Submissions](https://codeforces.com/submissions/szawinis)
- [Groups](https://codeforces.com/groups/with/szawinis)
- [Contests](https://codeforces.com/contests/with/szawinis)
[szawinis's blog](https://codeforces.com/blog/szawinis)
Picking the base and mod for string hashing is very important in decreasing the probability of hash collisions. How much are the guidelines affected by the problem itself? So far, I've read that the base should be larger than the alphabet, and the mod should be really large (but not overflow). But is there anything else? |
| Shard | 70 (laksa) |
| Root Hash | 17282934374156394870 |
| Unparsed URL | com,codeforces!/blog/entry/52697 s443 |