From Marc Garneau C.I., Olympiads School, Codeforces, Foxen
About
C++ main
Purple when :(
EDT Toronto, Canada
RIP TOPS, you had a good run.
Ever wanted a DMOJ for math? Say hi to TopsOJ
Blog
2022-03-06: Joined DMOJ.
2022-04-04: Solved this question in C (very proud of this one).
2022-06-28: Started coding in Java! (Also solved this problem again).
2022-07-18: Started coding in C++! (Solved this problem again) (new main)
2022-10-01: First contest - got gray :D.
2022-10-25: Third contest - got green :D
2023-03-06: 1st Year Anniversary! (Submitted)
2023-07-23: Top 1000 in points!
2023-08-17: Ninth Contest - got blue :D
Points Milestones (starts at 200)
2022-12-14: 200 points.
2023-07-15: 300 points.
2023-08-20: 350 points.
Problems Milestones (starts at 150)
2022-11-24: 150 problems solved.
2023-01-05: 200 problems solved.
2023-01-25: 250 problems solved.
2023-03-13: 300 problems solved.
2023-05-14: 350 problems solved.
2023-08-03: 400 problems solved.
2023-09-30: 450 problems solved.
2024-01-02: 500 problems solved. (around this time)
First of Each Point
2022-03-06: First 5-pointer.
2022-03-07: First 3-pointer.
2022-03-26: First 1-pointer
2022-04-01: First 7-pointer.
2022-11-07: First 10-pointer. Submission.
2023-01-21: First 12-pointer.
2023-02-04: First 15-pointer. Submission.
2023-07-15: First 20-pointer (cheesed).
2023-07-20: First 17-pointer.
Fastest Submissions
2023-02-28: Fastest submission to Mock CCC '22 Contest 2 J2 - Snowboarding Championship.
2023-03-09: Fastest submission to PEG Test '11 - Cyclopian Puzzle.
2023-04-08: Fastest submission to DMOPC '21 Contest 7 P1 - Chika Grids.
2023-05-20: Fastest submission to MMM '14 A - Distinct Prime Factors.
2023-07-16: Fastest submission to DMOPC '21 Contest 7 P2 - Knitting Scarves.
2023-09-18: Fastest submission to DMOPC '18 Contest 4 P4 - Dr. Henri and Lab Data.
2023-10-09: Fastest submission to Mock CCO '18 Contest 3 Problem 4 - Roger Solves A Classic Rage Tree Problem.
Contest Results
CCC - 2022-02-15: CCC 2023 Junior 60/75.
Comments: choked way too hard on J5.
USACO Bronze - January 2024: 1000/1000.
Comments: choked silver :(
Interesting Day (same problems as points)
Cool Coding Stuff
(BELOW) Shortest Hello World code in C, test it here.
main(){puts("Hello World");}
(BELOW) Weirdest Hello World in C, test it here.
#include "stdio.h"
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };
main(m1,s) char *s; {
int a,b,c,d,o[k],n=(int)s;
if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
else switch(m1-=h){
case f:
a=(b=(c=(d=g)<<g)<<g)<<g;
return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
case h:
for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a);
case g:
if(n<h)return(g);
if(n<j){n-=g;c='D';o[f]=h;o[g]=f;}
else{c='\r'-'\b';n-=j-g;o[f]=o[g]=g;}
if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
return(o[b-g]%n+k-h);
default:
if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);
}
}
Fun Fact: Putting a URL in C/C++ code does not generate an error. Test it here
Fun Fact: C++ allows the use of and and or in if-else statements. if (x == 1 && y == 1)
is the same as if (x == 1 and y == 1)
. if (x == 1 || y == 1)
is the same as if (x == 1 or y == 1)
.
Fun Fact: If you type import this
in a python shell something very interesting happens! Try it here.
Fun Fact: Try typing from __future__ import braces
in a python shell.
Tips and Tricks
++i
is SOMETIMES faster thani++
, and never slower- C++ has an inbuilt GCD Function -
__gcd(x, y)
Checking Parity - Bitwise AND Operator, use this for larger numbers, modulo is faster for smaller numbers. Or you can use the __builtin_parity(n)
function that returns true if odd and false if even but its a lot to type.
if (x & 1) {
cout << "Odd";
} else
cout << "Even";
}
Fast Multiplication/Division by 2 - Binary Shift Operators
x <<= 1; // Multiplication with 2
x >>= 1; // Divide by 2
Swap Numbers - XOR Operator
int a = 1;
int b = 2;
a ^= b ^= a ^= b;
Set a Number To 0 - XOR Operator - x ^= x
Number of Digits - Faster than Traditional Method
// Assume variable N is an arbitrary number
numDigits = floor(log10(N))+1;
Useful C++ STL Algorithms
Test if all elements fulfill a condition: std::all_of(list.begin(), list.end(), foo());
Test if any element fulfill a condition: std::any_of(list.begin(), list.end(), foo());
Test if no elements fulfill a condition: std::none_of(list.begin(), list.end(), foo());
Ascending Order Sort: std::sort(list.begin(), list.end());
Descending Order Sort: std::sort(list.begin(), list.end(), greater<int>());
Find specific element: Not String - std::find(list.begin(), list.end(), key)
, returns list.end()
if not found. String - s.find(ch)
, returns std::string::npos if not found.
Left rotate ("abcde" --> "bcdea"): std::rotate(list.begin(), list.begin()+rotationAmount, list.end());
Right rotate ("abcde" --> "eabcd"): std::rotate(list.begin(), list.begin()+list.size()-rotationAmount, list.end());
Binary Form Initialization
auto num = 0b1011 // Binary number for 1011, decimal form would be 11
Useful C++ Macros
emplace_back
is faster than push_back
.
#define eb(elements...) emplace_back(elements)
Prints the name of the variable and the value of that variable when debugging
#define debug(args...) { string _s=#args;replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it, args); }
// You need this function
inline void err(istream_iterator<string> it) {}
template<typename T, typename... Args> inline void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); }
Prints the elements of any container that supports iterators neatly.
#define print(_dt) do{cerr<<"Contents of "<<#_dt<<": ";trav(_el, _dt){cerr<<fixed<<left<<setw(size(to_string(*(max_element(all(_dt)))))+1)<<_el<<" ";}cerr<<endl;}while(0)
Fast C++ IO
template<typename T> inline void scan(T&x){ x=0;bool neg=0;T c=getchar();if(c=='-')neg=1,c=getchar();while((c<48)|| (c>57))c=getchar();for(;c<48||c>57;c=getchar());for(;c>47&&c<58;c=getchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x*=-1; } // Scan single data value
inline void scan(){} // This function is needed for below
template<typename _Ft, typename... _Rint> inline void scan(_Ft& arg, _Rint&... rest) { scan(arg);scan(rest...); } // Scans arbitrary amount of data values
template<typename T> inline void put(T n) { bool neg=0;if(n<0)n*=-1,neg=1;char snum[65];int i=0;do{snum[i++]=n%10+'0';n/=10;}while(n);--i;if(neg)putchar('-');while(i>=0)putchar(snum[i--]);putchar(10); } // Put single data value
inline void put(){} // Needed for below function
template<typename _Ft, typename..._Rint> inline void put(_Ft& arg, _Rint&... rest) { put(arg);put(rest...); } // Puts an arbitrary number of data values, seperated by newlines
C++ Fast IO Continued:
getchar_unlocked()
is much faster than getchar()
, if you're using windows, simply put:
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#else
#define getchar _getchar_nolock // windows equivalent
#define putchar _putchar_nolock
#endif
DEMO of Fast C++ IO
int x, y, z;
scan(x, y, z); // Reads three ints
put(x, y, z); // Outputs three ints
bruce best teacher.
d2d5a7a1e3d03527dd0ecba8e008ee6ff63544b520104f98869e273449b814f6