alexzhou / CP-Template

A useful template for competitive programming

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CP-Template

A useful template for competitive programming containing commonly used functions with optimal time complexity as well as nifty templates to make implementation a lot faster.

Here's a brief description of the functions and templates used :

  • powa() : Calculates the value of an using Binary Exponentiation. Takes m = mod (109 + 7) as an argument in case Modular Exponentiation is to be implemented. Do note that this function carries out Binary Exponentiation by default, in order to implement Modular Exponentation just uncomment x %= m at both of its occurences.
  • dectobin() : Takes an integer as its only argument and return its binary representation in the form of a string. For example, if I give 5 as the argument to this function, the string "101" is returned. Really useful for problems involving bitwise operators.
  • The template pbds is used to implement a Policy Based Data Structure, more about which you can check out here. There are a few additional header files to be included in order to implement this, you can have a look at this video for a well detailed explanation on the same.
  • The ostream and istream templates are used to input/output the contents of an vector in just a single line - cin>>(vector_name) / cout<<(vector_name). Please make sure that you have initialized the size of the vector if you are inputting it using this template.

There are a few more templates present but they hardly ever get used, so it would be better to just omit them for now.

Some of the hash defines used :

  • all(x) : Super useful when using the STL sort() function since it saves the effort of writing x.begin(),x.end().
  • fastio : A necessity especially in the field of Competitive Programming. The time saved using this is astronomical. Here's an article on the same
  • sz(q) : Used to find the size of a data structure q, mainly used when iterating through the elements of a data structure you don't explicitly know the size of. -fo(a,b) : There are four such hash defines with similar function, which is to condense a for loop into as little characters as possible.

Hope this summarizes the template well enough, you can always reach out to me if you'd like to suggest any changes or make any contributions of your own - that would be much appreciated. Many thanks to Aneesh for sharing his template on his profile, this is essentially a modification of that. Without his contribution I wouldn't even be typing this!

Thanks a lot for reading through everything I've typed, hope you have a great day ahead! : )

About

A useful template for competitive programming


Languages

Language:C++ 100.0%