General tips in C++

  1. mod 10^9+7—> const int MOD = 1e9 + 7; or static constexpr int MOD = 1000000007;!!!not 10e9
  2. Faster operation to select larger/smaller: min(a,b); max(a,b);
  3. Faster operation to swap: swap(vec[a], vec[b]);
  4. Sort: sort(vec.begin(), vec.end()); in descending: sort(vec.begin(), vec.end(), greater<int>());
  5. int to str: int n = to_string(str);
  6. To pass pointer:
    // 1. by reference argument
    void fun(int& a){
       a = 1;
    }
    int main(){
       int a;
       fun(a);
    }
    // 2. by pointer argument
    void fun(int* a){
       *a = 1;
    }
    int main(){
       int a;
       fun(&a);
    }
  7. int range: -INT_MAX-1 ~ INT_MAX
  8. case statement:
    switch(n){
        case '1':
            //something here
            //continue
            break; //essential!
        case '2':
            //something here
            //continue
            break; 
        default:
            //sth
            break;
    }
  9. data type:
    int // 32 bits, -2^31 ~ 2^31 -1, ~10^10
    long // 32~64 bits
    long long // 64 bits, ~10^19
  10. compare with pair: two factors compare:
    pair<int, int> mx{};
    for (auto& d : dimensions) {
        int x = d[0], y = d[1];
        mx = max(mx, pair(x * x + y * y, x * y));
        }