Vous êtes sur la page 1sur 11

Ba

Combination formula: (nCr)

long long dp[800][10];


const int mod = 1e9 + 7;
void solve() {
dp[0][0] = 1;
for(int i = 1; i <= 1000; i++)
for(int j = 0; j <= i; j++) {
dp[i][j] = dp[i - 1][j];
if (j) dp[i][j] += dp[i - 1][j - 1];
}
}
Binary search:
while(l <= r){
int mid = l + r >> 1;
if(calculate(mid) < *something*){
l = mid + 1;
}
else{
r = mid - 1;
}
}
return l;
Sqaure decomposition:
//first sort by (left position / block) then sort by right position
//assume we are on query i
while(l < i.left){
remove(l++);
}
while(l > i.left){
add(--l);
}
while(r < i.right + 1){
add(r++);
}
while(r > i.right + 1){
remove(--r);
}

Fast exponent calculate: (n ^ k % mod)

int modpow(int n,int k,int mod){


n %= mod;
int ans = 1;
while(k){
if (k & 1){
ans = (ans * n) % mod;
}
n *= n;
k >>= 1;
}
return ans % mod;
}
Advanced Sieve of Eratosthenes: (sng SNT)
bool Check(int N,int pos)
{
return (bool)(N & (1<<pos));
}
int Set(int N,int pos)
{
return N=N | (1<<pos);
}
const int N=100000001;
int status[N/32+2];
int nt[6000000];
void sieve()
{
int i,j,sqrtN;
sqrtN = (int)(sqrt(N));
for( i = 3; i <= sqrtN; i += 2)
if(Check(status[i>>5],i&31)==0)
for( j = i*i; j <= N; j += (i<<1))
status[j>>5]=Set(status[j>>5],j & 31);
int n=1;
nt[1]=2;
for(i=3; i<=N; i+=2)
if( Check(status[i>>5],i&31)==0)
nt[++n]=i;
}

GCD and LCM:

int gcd(int a,int b){


return b == 0 ? a : gcd(b,a % b);
}
int lcm(int a,int b){
return a * b / gcd(a,b);
}
KMP:
int b[123];
string p = "aa";
string t = "aaaaaaaaa";
int m = p.size();
int n = t.size();
void kmp() {
int i = 0,j = -1;
b[i] = j;
while(i < m){
while(j >= 0 and p[i] != p[j]){
j = b[j];
}
i++,j++;
b[i] = j;
}
}
void get() {
int i = 0,j = 0;
while(i < n){
while(j >= 0 and t[i] != p[j]){
j = b[j];
}
i++,j++;
if (j == m){
j = b[j];
}
}
}

Point and segment

struct p {
double x, y;
p(db a = 0, db b = 0) {
x = a;
y = b;
}
};
int orientation(p p1, p q, p r) {
int val = (q.y - p1.y) * (r.x - q.x) (q.x - p1.x) * (r.y - q.y);
if(val == 0) return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
// Given three colinear points p1, q, r, the function checks if
// point q lies on line segment 'pr'
bool onSegment(p p1, p q, p r) {
if(q.x <= max(p1.x, r.x) && q.x >= min(p1.x, r.x) &&
q.y <= max(p1.y, r.y) && q.y >= min(p1.y, r.y))
return true;
return false;
}
// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
bool doIntersect(p p1, p q1, p p2, p q2) {
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
if(o1 != o2 && o3 != o4)
return true;
//
p1, q1 and p2 are colinear and p2 lies on segment p1q1
if(o1 == 0 && onSegment(p1, p2, q1)) return true;
//
p1, q1 and p2 are colinear and q2 lies on segment p1q1
if(o2 == 0 && onSegment(p1, q2, q1)) return true;
//
p2, q2 and p1 are colinear and p1 lies on segment p2q2
if(o3 == 0 && onSegment(p2, p1, q2)) return true;
//
p2, q2 and q1 are colinear and q1 lies on segment p2q2
if(o4 == 0 && onSegment(p2, q1, q2)) return true;
return false;
}

Faster input(if needed):

inline int readInt () {


bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
inline void scan(int &x)
{
x=readInt();
}

Suffix array:

char A[MAXN];
struct entry {
int nr[2], p;
} L[MAXN];
int P[MAXLG][MAXN], N, i, stp, cnt;
int cmp(struct entry a, struct entry b) {
return a.nr[0] == b.nr[0] ? (a.nr[1] < b.nr[1] ? 1 : 0) : (a.nr[0] <
b.nr[0] ? 1 : 0);
}
int main(void) {
gets(A);
for(N = strlen(A), i = 0; i < N; i ++) {
P[0][i] = A[i] - 'a';
}
for(stp = 1, cnt = 1; cnt >> 1 < N; stp ++, cnt <<= 1)
{
for(i = 0; i < N; i ++)
{
L[i].nr[0] = P[stp - 1][i];
L[i].nr[1] = i + cnt < N ? P[stp - 1][i + cnt] : -1;
L[i].p = i;
}
sort(L, L + N, cmp);
for(i = 0; i < N; i ++) {
P[stp][L[i].p] = i > 0 && L[i].nr[0] == L[i - 1].nr[0] && L[i].nr[1] ==
L[i - 1].nr[1] ? P[stp][L[i - 1].p] : i;
}
}
return 0;
}
Big interger calculate:
struct Bigint {
// representations and structures
string a; // to store the digits
int sign; // sign = -1 for negative numbers, sign = 1 otherwise
// constructors
Bigint() {} // default constructor
Bigint( string b ) { (*this) = b; } // constructor for string
// some helpful methods
int size() { // returns number of digits
return a.size();
}
Bigint inverseSign() { // changes the sign
sign *= -1;

return (*this);
}
Bigint normalize( int newSign ) { // removes leading 0, fixes sign
for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
a.erase(a.begin() + i);
sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
return (*this);
}
// assignment operator
void operator = ( string b ) { // assigns a string to Bigint
a = b[0] == '-' ? b.substr(1) : b;
reverse( a.begin(), a.end() );
this->normalize( b[0] == '-' ? -1 : 1 );
}
// conditional operators
bool operator < ( const Bigint &b ) const { // less than operator
if( sign != b.sign ) return sign < b.sign;
if( a.size() != b.a.size() )
return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
return false;
}
bool operator == ( const Bigint &b ) const { // operator for equality
return a == b.a && sign == b.sign;
}

// mathematical operators
Bigint operator + ( Bigint b ) { // addition operator overloading
if( sign != b.sign ) return (*this) - b.inverseSign();
Bigint c;
for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
c.a += (carry % 10 + 48);
carry /= 10;
}
return c.normalize(sign);
}
Bigint operator - ( Bigint b ) { // subtraction operator overloading
if( sign != b.sign ) return (*this) + b.inverseSign();
int s = sign; sign = b.sign = 1;
if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
Bigint c;
for( int i = 0, borrow = 0; i < a.size(); i++ ) {
borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
borrow = borrow >= 0 ? 0 : 1;
}
return c.normalize(s);
}
Bigint operator * ( Bigint b ) { // multiplication operator overloading
Bigint c("0");

for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {


while(k--) c = c + b; // ith digit is k, so, we add k times
b.a.insert(b.a.begin(), '0'); // multiplied by 10
}
return c.normalize(sign * b.sign);
}
Bigint operator / ( Bigint b ) { // division operator overloading
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
Bigint c("0"), d;
for( int j = 0; j < a.size(); j++ ) d.a += "0";
int dSign = sign * b.sign; b.sign = 1;
for( int i = a.size() - 1; i >= 0; i-- ) {
c.a.insert( c.a.begin(), '0');
c = c + a.substr( i, 1 );
while( !( c < b ) ) c = c - b, d.a[i]++;
}
return d.normalize(dSign);
}
Bigint operator % ( Bigint b ) { // modulo operator overloading
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
Bigint c("0");
b.sign = 1;
for( int i = a.size() - 1; i >= 0; i-- ) {
c.a.insert( c.a.begin(), '0');
c = c + a.substr( i, 1 );
while( !( c < b ) ) c = c - b;
}
return c.normalize(sign);
}
// output method
void print() {
if( sign == -1 ) putchar('-');
for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
}

};

Other useful functions:


+ max_element(arr.begin(),arr.end()) -> return pointer to max element in
the array arr
+ binary_search(arr. begin (),arr.end(),val) -> return true if array arr has
value val using binary seach(array arr MUST be sorted)
+ getline(cin,s) : in case you wanna use read the whole line into string s
like nextLine() in java
+ lower_bound(arr.begin(),arr.end(),val) -> return pointer to the first
element in array arr which is not smaller than val
+ upper_bound(arr.begin(),arr.end(),val) -> return pointer to the first
element in array arr which is bigger than val
+ accumulate(arr.begin(),arr.end(),val) ->return sum of all element in
array arr and val