You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
617 B
32 lines
617 B
#include "utilities.hpp"
|
|
|
|
#include <limits>
|
|
|
|
using namespace std;
|
|
|
|
int divi(int x, int y){
|
|
// clearly we don't want a division by zero
|
|
if(!y) {
|
|
if(x < 0) return numeric_limits<int>::min();
|
|
if(x == 0) return 1;
|
|
return numeric_limits<int>::max();
|
|
}
|
|
// in this situation there is a overflow which causes a SIGFPE
|
|
if(x == numeric_limits<int>::min() && y == -1) {
|
|
return numeric_limits<int>::max();
|
|
}
|
|
return x / y;
|
|
}
|
|
|
|
// exponentiation by squaring
|
|
int powi(int base, int exp){
|
|
if(exp < 0) return 0;
|
|
int res = 1;
|
|
while (exp) {
|
|
if (exp & 1)
|
|
res *= base;
|
|
exp >>= 1;
|
|
base *= base;
|
|
}
|
|
return res;
|
|
}
|
|
|