You are on page 1of 25

Team Reference Document

Sharif University of Technology - Mkay

Contents
1 Checklist 1.1 Common Bugs 1.2 IO . . . . . . . 1.3 Geometry . . . 1.4 Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 2 2 2 3 3 4 4 5 5 5 6 7 8 9 11 12 13 13 14 15 15 16 17 17 20 20 20 20 22 25 25 25

2 Geometry 2.1 Euclidian Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Convex Hull 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Convex Hull 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Graph Algorithms 3.1 Eulerian Tour . . . . . . . . . . . . . . . . 3.2 Fast Dijkstra . . . . . . . . . . . . . . . . 3.3 2-SAT & Strongly Connected Components 3.4 Two Connected Components . . . . . . . 3.5 Bipartite Matching . . . . . . . . . . . . . 3.6 Maximum Weighted Matching . . . . . . . 3.7 Maximum Flow with Negative Edges . . . 3.8 Minimum Cost Flow . . . . . . . . . . . . 4 Ad 4.1 4.2 4.3 4.4 4.5 4.6 4.7 Hoc Dice . . . . . . . . . . Hexagonal Grid . . . . Date Conversion . . . Roman Numbers . . . Permutation to Integer Interval Traversing . . Big Integer . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

5 Number Theory 5.1 Number Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Linear Algebra/Optimization 6.1 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Simplex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 String Matching 7.1 KMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1
1.1

Checklist
Common Bugs
READ THE STATEMENT AGAIN. TELL YOUR TEAMMATE IF NECESSARY Initialization: Use memset/clear(). Dont expect global variables to be zero. Care about multiple tests. Precision and Range: Use long long if necessary. Use BigInteger/BigDecimal Small cases (n=0,1,negative) 0-based 1-based Check MAXN Is your INF big enough? Is your Stack overow Innite loop? Check pasted codes! small enough Division by zero. Integer division a/(double)b

1.2

IO

Read from le! Write to standard output Use fscanf/printf instead of ifstream/cout if necessary. Remove debug info! Output format: Spaces at the end of line. Blank lines. View the output in VIM Be careful about -0.0

1.3

Geometry
for comparison

Use

Use multiple s if necessary Use sign( A B )sign( A C ) Angles are given in [, ]. Be careful about , .

1.4

Graph

Multiple components. Multiple edges. Loops.

2
2.1

Geometry
Euclidian Geometry

#define X r e a l ( ) #define Y imag ( ) const double e p s=1e 8; i n l i n e bool z e r o ( double t ) { return t<e p s && t>e p s ; } typedef complex<double> p o i n t ; bool e q u a l ( const p o i n t& a , const p o i n t& b ) { return z e r o ( b . Ya .Y)&&z e r o ( b . Xa .X) ; } double dot ( const p o i n t& p , const p o i n t& q ) { return p .Xq .X+p .Yq .Y; } double c r o s s ( const p o i n t& p , const p o i n t& q ) { return p .Xq . Yp .Yq .X; } struct c i r c l e { point o ; double r ; c i r c l e ( ) {} c i r c l e ( const p o i n t& o , double r ) : o ( o ) , r ( r ) {} }; struct l i n e { point v ; double c ; void n o r m a l i z e ( ) {double t=abs ( v ) ; i f ( z e r o ( t ) ) throw 1;v/=t ; c/=t ; } l i n e ( ) {} l i n e ( const p o i n t& v , double c ) : v ( v ) , c ( c ) { n o r m a l i z e ( ) ; } l i n e ( const p o i n t& p , const p o i n t& q ) {v=p o i n t ( 0 , 1 ) ( pq ) ; c=dot ( v , p ) ; normalize () ;} }; struct segment { point a , b ; segment ( ) {} segment ( const p o i n t& a , const p o i n t& b ) : a ( a ) , b ( b ) {} }; l i n e r e v ( const l i n e& l ) { return l i n e ( l . v, l . c ) ; } p o i n t i n t e r s e c t i o n ( const l i n e& l 1 , const l i n e& l 2 ) { p o i n t A( l 1 . v . X, l 2 . v .X) ,B( l 1 . v . Y, l 2 . v .Y) ,C( l 1 . c , l 2 . c ) ; i f ( z e r o ( c r o s s (A, B) ) ) throw 1; return p o i n t ( c r o s s (B, C) / c r o s s (B,A) , c r o s s (A, C) / c r o s s (A, B) ) ; } p o i n t i n t e r s e c t i o n ( const l i n e& l , const c i r c l e & c ) { double t=l . cdot ( l . v , c . o ) ; t=t ; t=c . r c . rt ; i f ( t<e p s ) throw 1; e l s e i f ( t<e p s ) t =0; t=s q r t ( t ) ; t+=c r o s s ( c . o , l . v ) ; return l . c l . vt l . v p o i n t ( 0 , 1 ) ; } p o i n t i n t e r s e c t i o n ( const c i r c l e & c1 , const c i r c l e & c2 ) { return i n t e r s e c t i o n ( l i n e ( 2 . 0 ( c2 . oc1 . o ) , norm ( c2 . o )norm ( c1 . o )+c1 . r c1 . r c2 . r c2 . r ) , c2 ) ; } p o i n t i n t e r s e c t i o n ( segment s1 , segment s 2 ) { p o i n t a=s 1 . a , b=s 1 . b ; p o i n t c=s 2 . a , d=s 2 . b ; p o i n t u=ba , v=dc ; i f ( c r o s s ( ac , v ) c r o s s ( bc , v )<=e p s && c r o s s ( ca , u ) c r o s s ( da , u )<=e p s ) { try { return i n t e r s e c t i o n ( l i n e ( a , b ) , l i n e ( c , d ) ) ; } catch ( i n t ) { i f ( dot ( u , c )>dot ( u , d ) ) swap ( c , d ) ; i f ( e q u a l ( a , d ) ) return a ; i f ( e q u a l ( b , c ) ) return b ; } }

2.2

Convex Hull 2D

throw }

1;

2.2

Convex Hull 2D

#define X r e a l ( ) #define Y imag ( ) const i n t maxn=1000000; const double e p s=1e 7; typedef complex<double> p o i n t ; p o i n t p [ maxn ] , cnvx [ maxn + 1 ] ; // be c a r e f u l a b o u t a l l t h e p o i n t s b e i n g i n one l i n e , and a b o u t d u p l i c a t e p o i n t s i n t n , cn ; // n must be b i g g e r than or e q u a l t o 3 i n l i n e bool i s l e s s ( const double& a , const double& b ) { return a+eps<b ; } i n l i n e bool arg comp ( const p o i n t& a , const p o i n t& b ) { return i s l e s s ( atan2 ( a . Y, a .X) , atan2 ( b . Y, b .X) ) ; } i n l i n e double c r o s s ( const p o i n t& a , const p o i n t& b ) { return a .Xb . Ya .Yb .X; } void c o n h u l l ( ) { p o i n t mid=p o i n t ( 0 . 0 , 0 . 0 ) ; f o r ( i n t i =0; i <n ; i ++) mid+=p [ i ] ; mid/=n ; f o r ( i n t i =0; i <n ; i ++) p [ i ]=mid ; // c h a n g i n g a r r a y p s o r t ( p , p+n , arg comp ) ; i n t f i r s t =0; f o r ( i n t i =1; i <n ; i ++) i f ( p [ i ] . Y<p [ f i r s t ] . Y | | ( p [ i ] . Y==p [ f i r s t ] . Y && p [ i ] . X<p [ f i r s t ] . X) ) f i r s t =i ; cnvx [ 0 ] = p [ f i r s t ] ; cnvx [ 1 ] = p [ ( f i r s t +1)%n ] ; cn =1; f o r ( i n t i =( f i r s t +2)%n ; i !=( f i r s t +1)%n ; i =( i +1)%n ) { while ( cn>0 && i s l e s s ( c r o s s ( cnvx [ cn ] cnvx [ cn 1] , p [ i ] cnvx [ cn 1]) , 0 . 0 ) ) cn; cnvx[++cn ]=p [ i ] ; } f o r ( i n t i =0; i <cn ; i ++) cnvx [ i ]+=mid ; // i f we want p [ ] back , we s h o u l d p [ i ]+=mid , b u t t h i e r o r d e r w i l l n o t preserved // now cnvx [ ] h o l d s a l l t h e p o i n t s on t h e cnvx h u l l , // i f we DONT want t h e p o i n t s n o t on g u s h e comment i t : i n t l a s t =1; // o n l y when we want o n l y g u s h e ha f o r ( i n t i =1; i <cn ; i ++) i f ( i s l e s s ( 0 . 0 , c r o s s ( cnvx [ i ] cnvx [ i 1] , cnvx [ ( i +1)%cn ] cnvx [ i 1]) ) ) cnvx [ l a s t ++]=cnvx [ i ] ; cn=l a s t ; }

2.3

Convex Hull 3D

const double e p s=1e 6; struct p o i n t { double x , y , z ; p o i n t ( ) {} p o i n t ( double x , double y , double z ) : x ( x ) , y ( y ) , z ( z ) {} }; p o i n t operator (const p o i n t& p , const p o i n t& q ) { return p o i n t ( p . xq . x , p . yq . y , p . zq . z ) ;}

bool i s Z e r o ( const p o i n t& p ) { return abs ( p . x )<=e p s && abs ( p . y )<=e p s && abs ( p . z )<=e p s ;} double dot ( const p o i n t& p , const p o i n t& q ) { return p . xq . x+p . yq . y+p . z q . z ; } p o i n t c r o s s ( const p o i n t& p , const p o i n t& q ) { return p o i n t ( p . yq . zp . z q . y , p . z q . xp . xq . z , p . xq . yp . yq . x ) ; } int n ; point p [ 1 0 0 ] ; struct comparator { point a , v ; comparator ( p o i n t a , p o i n t v ) : a ( a ) , v ( v ) {} bool operator ( ) ( const p o i n t& p , const p o i n t& q ) { return dot ( c r o s s ( pa , qa ) , v )<e p s ; } }; bool i s E d g e ( i n t i , i n t j ) { comparator comp ( p [ i ] , p [ j ]p [ i ] ) ; i n t minOne=1; f o r ( i n t t =0; t<n;++t ) i f ( ! i s Z e r o ( c r o s s ( p [ t ]p [ i ] , p [ j ]p [ i ] ) ) && ( minOne==1 | | comp ( p [ t ] , p [ minOne ] ) ) ) minOne=t ; i f ( minOne==1) return true ; bool a l l S m a l l=true , a l l B i g=true ; f o r ( i n t t =0; t<n;++t ) i f ( t != i && t != j && comp ( p [ t ] , p [ minOne ] ) ) a l l B i g=f a l s e ; f o r ( i n t t =0; t<n;++t ) i f ( t != i && t != j && comp ( p [ minOne ] , p [ t ] ) ) a l l S m a l l=f a l s e ; return a l l S m a l l | | a l l B i g ; }

3
3.1

Graph Algorithms
Eulerian Tour

const i n t maxn=1000+110; i n t ednum [ maxn ] [ maxn ] ; v e c t o r <int> vec [ maxn ] ; // I n d i r e c t S u p p o r t M u l t i p l e Edges and Loops void d f s e u l e r i a n ( i n t v , v e c t o r <int> & l i s t ) { while ( ! vec [ v ] . empty ( ) ) { i n t u=vec [ v ] . back ( ) ; vec [ v ] . pop back ( ) ; i f ( ednum [ v ] [ u ] ) { ednum [ v ] [ u]; ednum [ u ] [ v]; d f s e u l e r i a n (u , l i s t ) ; } } l i s t . push back ( v ) ; }

3.2

Fast Dijkstra

const i n t maxn=1000+110; int n , e ; v e c t o r <int> vec [ maxn ] , c o s t [ maxn ] ; i n t d i s [ maxn ] ; struct comp { bool operator ( ) ( i n t a , i n t b ) { i f ( d i s [ a ] ! = d i s [ b ] ) return d i s [ a]< d i s [ b ] ; return a<b ;

3.3

2-SAT & Strongly Connected Components

} }; s e t <int , comp> s i t ; void d i j f a s t ( i n t v ) { memset ( d i s , 6 3 , s i z e o f d i s ) ; dis [ v ]=0; f o r ( i n t i =0; i <n ; ++i ) s i t . i n s e r t ( i ) ; while ( ! s i t . empty ( ) ) { i n t u=( s i t . b e g i n ( ) ) ; s i t . e r a s e ( u ) ; f o r ( i n t i =0; i <i n t ( vec [ u ] . s i z e ( ) ) ; ++i ) { i f ( d i s [ u]+ c o s t [ u ] [ i ] < d i s [ vec [ u ] [ i ] ] ) { s i t . e r a s e ( vec [ u ] [ i ] ) ; d i s [ vec [ u ] [ i ] ]= d i s [ u]+ c o s t [ u ] [ i ] ; s i t . i n s e r t ( vec [ u ] [ i ] ) ; } } } }

3.3

2-SAT & Strongly Connected Components

//2 Sat // V e r t i c e s a r e numbered 0 . . n1 f o r t r u e s t a t e s . // F a l s e s t a t e o f t h e v a r i a b l e i i s i+n , which i s e x a c t l y o t h e r ( i ) const i n t maxn=600; int n ; // For 2SAT Only i n l i n e i n t o t h e r ( i n t v ) { return v<n? v+n : vn ; } i n l i n e i n t var ( i n t v ) { return v<n? v : vn ; } i n l i n e i n t type ( i n t v ) { return v<n ? 1 : 0 ; } i n t v a l [ maxn ] ; // v e c t o r <int> ngb [ maxn+maxn ] ; v e c t o r <int> n g b r e v [ maxn+maxn ] ; i n t marker , d f s t , d f s t i m e [ maxn+maxn ] , d f s o r d e r [ maxn+maxn ] ; i n t group [ maxn+maxn ] ; void s a t c l e a r ( ) { f o r ( i n t i =0; i <maxn+maxn;++ i ) { ngb [ i ] . r e s i z e ( 0 ) ; ngb rev [ i ] . r e s i z e (0) ; } } void d f s ( i n t v ) { i f ( d f s t i m e [ v ]!= 1) return ; d f s t i m e [ v]= 2; i n t deg=n g b r e v [ v ] . s i z e ( ) ; f o r ( i n t i =0; i <deg;++ i ) dfs ( ngb rev [ v ] [ i ] ) ; d f s t i m e [ v]= d f s t ++; } void d f s n ( i n t v ) { i f ( group [ v ]!= 1) return ; group [ v]= marker ; i n t deg=ngb [ v ] . s i z e ( ) ; f o r ( i n t i =0; i <deg;++ i ) d f s n ( ngb [ v ] [ i ] ) ; } // For 2SAT Only void a d d e d g e ( i n t a , i n t b ) { // Add a c l a u s e t o 2SAT ngb [ o t h e r ( a ) ] . push back ( b ) ;

3.4

Two Connected Components

n g b r e v [ a ] . push back ( o t h e r ( b ) ) ; ngb [ o t h e r ( b ) ] . push back ( a ) ; n g b r e v [ b ] . push back ( o t h e r ( a ) ) ; } // void s o l v e ( ) { // Throws 1 i f no answer i s found d f s t =0; memset ( d f s t i m e , 1 , s i z e o f ( d f s t i m e ) ) ; f o r ( i n t i =0; i <n+n;++ i ) dfs ( i ) ; memset ( v a l , 1 , s i z e o f ( v a l ) ) ; f o r ( i n t i =0; i <n+n;++ i ) d f s o r d e r [ n+nd f s t i m e [ i ] 1]= i ; memset ( group , 1 , s i z e o f ( group ) ) ; f o r ( i n t i =0; i <n+n;++ i ) { marker=i ; dfsn ( dfs order [ i ] ) ; } // For 2SAT Only f o r ( i n t i =0; i <n;++ i ) { i f ( group [ i ]==group [ i+n ] ) throw 1; v a l [ i ]=( group [ i ]> group [ i+n ] ) ? 0 : 1 ; } // }

3.4

Two Connected Components

const i n t maxn=10000; v e c t o r <int> d [ maxn ] , dname [ maxn ] ; v e c t o r <int> VC gr [ 2 maxn ] , EC gr [ 2 maxn ] , edgeCut ; // edgeCut i s z e r o b a s e bool verCut [ maxn ] ; bool c h e ck [ maxn ] ; i n t n , VC sz , EC sz , numVC, numEC ; void r e a d i n i t ( ) { i n t e ; c i n >>n>>e ; memset ( check , 0 , s i z e o f ( check ) ) ; memset ( verCut , 0 , s i z e o f ( verCut ) ) ; edgeCut . r e s i z e ( 0 ) ; f o r ( i n t i =0; i <n ; i ++){ d [ i ] . r e s i z e (0) ; dname [ i ] . r e s i z e ( 0 ) ; // f o r e d g e } f o r ( i n t i =0; i <n ; i ++){ VC gr [ i ] . r e s i z e ( 0 ) ; EC gr [ i ] . r e s i z e ( 0 ) ; } f o r ( i n t i =0; i <e ; i ++){ i n t a , b ; c i n >>a>>b ; a; b; // change t o z e r o b a s e d [ a ] . push back ( b ) ; dname [ a ] . push back ( i ) ; d [ b ] . push back ( a ) ; dname [ b ] . push back ( i ) ; } VC sz=EC sz=numVC=numEC=0; } i n t v s t a c k [ maxn ] , v s z =0; // f o r v e r t e x i n t e s t a c k [ maxn ] , e s z =0; // f o r e d g e i n t l e v e l [ maxn ] ; i n t d f s ( i n t x , i n t l , i n t edge name ) { c h eck [ x]= true ; l e v e l [ x]= l ; v s t a c k [ v s z++]=x ; // f o r v e r t e x

3.5

Bipartite Matching

e s t a c k [ e s z++]=x ; // f o r e d g e i n t mine=l , o t h e r=n , chnum=0; f o r ( i n t i =0; i <( i n t ) d [ x ] . s i z e ( ) ; i ++) i f ( ! check [ d [ x ] [ i ] ] ) { i n t i n d=v s z ; chnum++; i n t a=d f s ( d [ x ] [ i ] , l +1,dname [ x ] [ i ] ) ; i f ( a<o t h e r ) o t h e r=a ; i f ( a==l ) { // j u s t f o r v e r t e x VC gr [ VC sz ] . push back ( x ) ; i f ( ! verCut [ x ] ) { verCut [ x]= true ; numVC++; } while ( vsz>i n d ) VC gr [ VC sz ] . push back ( v s t a c k [ v s z ] ) ; VC sz++; } } e l s e i f ( l e v e l [ d [ x ] [ i ]] < mine ) mine=l e v e l [ d [ x ] [ i ] ] ; i f ( l==0 && chnum>1 && ! verCut [ x ] ) { // j u s t f o r v e r t e x verCut [ x]= true ; numVC++; } i f ( l==0 | | ( o t h e r==l && mine>=l 1) ) { // j u s t f o r e d g e i f ( l >0) edgeCut . push back ( edge name ) ; int a ; while ( ( a=e s t a c k [ e s z ] ) !=x ) EC gr [ EC sz ] . push back ( a ) ; EC gr [ EC sz ] . push back ( x ) ; EC sz++; } return mine<o t h e r ? mine : o t h e r ; } i n t main ( ) { read init () ; f o r ( i n t i =0; i <n ; i ++) i f ( ! check [ i ] ) dfs ( i ,0 ,0) ; numEC=edgeCut . s i z e ( ) ; // f o r e d g e cout<<number o f VC components=<<VC sz<<e n d l ; f o r ( i n t i =0; i <VC sz ; i ++,cout<<e n d l ) f o r ( i n t j =0; j <( i n t ) VC gr [ i ] . s i z e ( ) ; j ++) cout<<VC gr [ i ] [ j ]+1<< ; cout<<number o f EC components=<<EC sz<<e n d l ; f o r ( i n t i =0; i <EC sz ; i ++,cout<<e n d l ) f o r ( i n t j =0; j <( i n t ) EC gr [ i ] . s i z e ( ) ; j ++) cout<<EC gr [ i ] [ j ]+1<< ; cout<<number o f cutv e r t e x s=<<numVC <<e n d l ; f o r ( i n t i =0; i <n ; i ++) i f ( verCut [ i ] ) cout<<i +1<< ; cout<<e n d l ; cout<<number o f cute d g e s=<<numEC<<e n d l ; f o r ( i n t i =0; i <( i n t ) edgeCut . s i z e ( ) ; i ++) cout<<edgeCut [ i ]+1<< ; cout<<e n d l ; return 0 ; }

3.5

Bipartite Matching

const i n t maxn=1000+110; v e c t o r <int> vec [ maxn ] ; i n t matched [ maxn ] , mate [ maxn ] ; i n t mark [ maxn ] , mark2 [ maxn ] ;

3.6

Maximum Weighted Matching

int r , c , e ; int d f s ( int v ) { mark [ v ] = 1 ; f o r ( i n t i =0; i <i n t ( vec [ v ] . s i z e ( ) ) ; ++i ) { i n t j=vec [ v ] [ i ] ; mark2 [ j ] = 1 ; i f ( mate [ j ]==1 | | ( ! mark [ mate [ j ] ] && d f s ( mate [ j ] ) ) ) { matched [ v ] = 1 ; mate [ j ]=v ; return 1 ; } } return 0 ; } i n t matching ( ) { memset ( matched , 0 , s i z e o f matched ) ; memset ( mate , 1, s i z e o f mate ) ; i n t sum=0; // Greedy f o r ( i n t i =0; i <r ; ++i ) { f o r ( i n t j =0; j <i n t ( vec [ i ] . s i z e ( ) ) ; ++j ) { i f ( mate [ vec [ i ] [ j ] ]==1) { matched [ i ] = 1 ; mate [ vec [ i ] [ j ] ]= i ; ++sum ; break ; } } } // Matching Mark2 i s f o r V e r t e x Cover f o r ( i n t i =0; i <r ; ++i ) i f ( ! matched [ i ] ) { memset ( mark , 0 , s i z e o f mark ) ; memset ( mark2 , 0 , s i z e o f mark2 ) ; sum+=d f s ( i ) ; } // V e r t e x Cover memset ( mark , 0 , s i z e o f mark ) ; memset ( mark2 , 0 , s i z e o f mark2 ) ; f o r ( i n t i =0; i <r ; ++i ) i f ( ! mark [ i ] && ! matched [ i ] ) dfs ( i ) ; //Now v e r t i c e s w i t h mark [ i ]==0 or mark2 [ j ]=1 a r e minimum v e r t e x c o v e r return sum ; }

3.6

Maximum Weighted Matching

const i n t maxn=500; i n t m, n , match ; i n t mat [ maxn ] [ maxn ] , u [ maxn ] , v [ maxn ] ; i n t g r l [ maxn ] , boy [ maxn ] , s a f [ maxn ] [ 2 ] , i n s ; i n t mark [ maxn ] , mrk ; bool SWAP; void i n i t ( ) { memset ( mat , 0 , s i z e o f ( mat ) ) ; memset ( boy , 1 , s i z e o f ( boy ) ) ; memset ( g r l , 1 , s i z e o f ( g r l ) ) ; memset ( u , 0 , s i z e o f ( u ) ) ; memset ( v , 0 , s i z e o f ( v ) ) ; } void r e a d ( ) { c i n >>m >>n ; i n t e ; c i n >>e ;

3.6

Maximum Weighted Matching

10

f o r ( i n t i =0; i <e ; i ++){ i n t v1 , v2 , w ; c i n >>v1>>v2>>w ; mat [ v1 1 ] [ v2 1]=w ; } SWAP=(m ) ; >n i f (SWAP) { swap (m, n ) ; f o r ( i n t i =0; i <n ; i ++) f o r ( i n t j=i +1; j <n ; j ++) swap ( mat [ i ] [ j ] , mat [ j ] [ i ] ) ; } f o r ( i n t i =0; i < m; i ++){ u [ i ]= 1; f o r ( i n t j =0; j <n ; j ++) i f ( mat [ i ] [ j ]>u [ i ] ) u [ i ]=mat [ i ] [ j ] ; } } bool b f s ( ) { f o r ( i n t mah=0; mah<i n s ; mah++){ i n t a=s a f [ mah ] [ 0 ] ; f o r ( i n t i =0; i <n ; i ++) i f ( mark [ i ] ! = mrk && mat [ a ] [ i ]==u [ a ]+v [ i ] ) { i f ( g r l [ i ]==1){ i n t x=i , t ; match++; while ( x>1){ g r l [ x]= s a f [ mah ] [ 0 ] ; t=x ; x=boy [ s a f [ mah ] [ 0 ] ] ; boy [ s a f [ mah ][0]]= t ; mah=s a f [ mah ] [ 1 ] ; } return true ; } mark [ i ]=mrk ; s a f [ i n s ] [ 0 ] = g r l [ i ] ; s a f [ i n s ++][1]=mah ; } } return f a l s e ; } i n t main ( ) { init () ; read ( ) ; match =0; mrk=0; while ( match< { m) i n s =0; mrk++; f o r ( i n t i =0; i < m; i ++) i f ( boy [ i ]==1) { s a f [ i n s ] [ 0 ] = i ; s a f [ i n s ++][1]= 1; } while ( ! b f s ( ) ) { i n t e p s =1<<30; f o r ( i n t j =0; j <n ; j ++) i f ( mark [ j ] ! = mrk ) f o r ( i n t i =0; i <i n s ; i ++) i f ( u [ s a f [ i ] [ 0 ] ] + v [ j ]mat [ s a f [ i ] [ 0 ] ] [ j ]< e p s ) e p s=u [ s a f [ i ] [ 0 ] ] + v [ j ]mat [ saf [ i ] [ 0 ] ] [ j ] ; f o r ( i n t i =0; i <i n s ; i ++) u [ s a f [ i ][0]] = eps ; f o r ( i n t i =0; i <n ; i ++) i f ( mark [ i ]==mrk ) v [ i ]+=e p s ; } } i f (SWAP) {

3.7

Maximum Flow with Negative Edges

11

f o r ( i n t i =0; i <n ; i ++) f o r ( i n t j=i +1; j <n ; j ++) swap ( mat [ i ] [ j ] , mat [ j ] [ i ] ) ; f o r ( i n t i =0; i <n ; i ++){ swap ( u [ i ] , v [ i ] ) ; swap ( boy [ i ] , g r l [ i ] ) ; } swap (m, n ) ; } f o r ( i n t i =0; i < m; i ++) c e r r <<i +1<< > <<boy [ i ]+1<< e n d l ; return 0 ; }

3.7

Maximum Flow with Negative Edges

const i n t maxn=102; i n t c [ maxn ] [ maxn ] ; queue<int> q ; i n t par [ maxn ] ; i n t i n c r e a s e f l o w ( i n t n , i n t s , i n t t , i n t theMax=1){ memset ( par , 1 , s i z e o f par ) ; while ( ! q . empty ( ) ) q . pop ( ) ; q . push ( s ) ; par [ s ]= s ; while ( ! q . empty ( ) ) { i n t v=q . f r o n t ( ) ; q . pop ( ) ; f o r ( i n t i =0; i <n;++ i ) i f ( c [ v ] [ i ] >0 && par [ i ]==1){ par [ i ]=v ; q . push ( i ) ; } } i f ( par [ t ]==1) return 0 ; i n t neck=theMax ; f o r ( i n t v=t ; v!= s ; v=par [ v ] ) i f ( neck==1 | | neck>c [ par [ v ] ] [ v ] ) neck=c [ par [ v ] ] [ v ] ; f o r ( i n t v=t ; v!= s ; v=par [ v ] ) { c [ par [ v ] ] [ v]=neck ; c [ v ] [ par [ v]]+= neck ; } return neck ; } i n t C [ maxn ] [ maxn ] ; bool i n i t i a l i z e ( i n t n ) { memset ( c , 0 , s i z e o f c ) ; f o r ( i n t i =0; i <n;++ i ) { f o r ( i n t j =0; j <n;++ j ) { i f (C [ j ] [ i ] <0) { c [ i ] [ j ]=C [ i ] [ j ]+C [ j ] [ i ] ; c [ j ] [ i ]=0; i f ( c [ i ] [ j ] <0) return f a l s e ; c [ n ] [ j ]=C [ j ] [ i ] ; c [ i ] [ n+1]=C [ j ] [ i ] ; } e l s e i f (C [ i ] [ j ]>=0) c [ i ] [ j ]=C [ i ] [ j ] ; } } while ( i n c r e a s e f l o w ( n+2,n , n+1) ) ; f o r ( i n t i =0; i <n;++ i )

3.8

Minimum Cost Flow

12

i f ( c [ n ] [ i ]!=0) return f a l s e ; return true ; }

3.8

Minimum Cost Flow

const i n t maxn=500; const i n t maxe =100000; const i n t i n f =1000000000; int n , s , t , cap [ maxn ] [ maxn ] , c o s t [ maxn ] [ maxn ] , f [ maxn ] [ maxn ] , maxFlow , minCost , par [ maxn ] , d [ maxn ] ; bool mark [ maxn ] ; inline int s l a c k ( int i , int j ) { return cap [ i ] [ j ] f [ i ] [ j ] ; } i n l i n e void push ( i n t i , i n t j , i n t f l ) { f [ i ] [ j ]+= f l ; f [ j ] [ i ]= f l ; } i n t d f s ( i n t x , i n t neck ) { mark [ x]= f a l s e ; i f ( x==t ) return neck ; int y ; f o r ( i n t i =0; i <n ; i ++) i f ( s l a c k ( x , i )>0 && mark [ i ] && 0<(y=d f s ( i , neck <? s l a c k ( x , i ) ) ) ) { push ( x , i , y ) ; return y ; } return 0 ; } i n l i n e void findMaxFlow ( ) { // f i n d maximum f l o w and f i l l f [ i ] [ j ] maxFlow=0; int y ; do { f o r ( i n t i =0; i <n ; i ++) mark [ i ]= true ; y=d f s ( s , i n f ) ; maxFlow+=y ; } while ( y>0) ; } i n l i n e bool r e l a x ( i n t i , i n t j ) { i f ( d [ j ]<=d [ i ]+ c o s t [ i ] [ j ] ) return f a l s e ; d [ j ]=d [ i ]+ c o s t [ i ] [ j ] ; par [ j ]= i ; return true ; } i n t e , e1 [ maxe ] , e2 [ maxe ] ; i n l i n e i n t bellman ( ) { f o r ( i n t i =0; i <n ; i ++) d [ i ]=0; f o r ( i n t i =0; i <n ; i ++) f o r ( i n t j =0; j <e ; j ++)

13

i f ( s l a c k ( e1 [ j ] , e2 [ j ] ) >0) r e l a x ( e1 [ j ] , e2 [ j ] ) ; f o r ( i n t j =0; j <e ; j ++) i f ( s l a c k ( e1 [ j ] , e2 [ j ] ) >0 && r e l a x ( e1 [ j ] , e2 [ j ] ) ) return e1 [ j ] ; return 1; } i n l i n e void findMinCost ( ) { e =0; f o r ( i n t i =0; i <n ; i ++) f o r ( i n t j =0; j <n ; j ++) i f ( cap [ i ] [ j ] >0 | | cap [ j ] [ i ] >0) { e1 [ e ]= i ; e2 [ e ]= j ; e++; } int x ; while (0<=(x=bellman ( ) ) ) { f o r ( i n t i =0; i <n ; i ++) x=par [ x ] ; // t o f a l l i n a neg c y c l e i n t neck=s l a c k ( par [ x ] , x ) ; f o r ( i n t i=par [ x ] ; i !=x ; i=par [ i ] ) neck<?=s l a c k ( par [ i ] , i ) ; push ( par [ x ] , x , neck ) ; f o r ( i n t i=par [ x ] ; i !=x ; i=par [ i ] ) push ( par [ i ] , i , neck ) ; } minCost =0; f o r ( i n t i =0; i <n ; i ++) f o r ( i n t j =0; j <i ; j ++) minCost+=f [ i ] [ j ] c o s t [ i ] [ j ] ; }

4
4.1

Ad Hoc
Dice

typedef p a i r <int , int> j o f t ; const i n t MTSZ=1000; // must be l a r g e r than 36 const i n t maxs=MTSZMTSZ 3 6 ; const i n t FR=0 ,RT=1,BK=2 ,LT=3 ,TP=4 ,DN=5; i n t Top [ 6 ] [ 6 ] ; // i f a i s i n FRONT and b i s RIGHT t h e n Top [ a ] [ b ] i s on TOP :D const i n t Front [ 6 ] = { 2 , 3 , 0 , 1 , 5 , 4 } ; void i n i t ( ) { int l i s t [6][4]={{1 ,5 ,3 ,4} ,{2 ,5 ,0 ,4} ,{3 ,5 ,1 ,4} ,{2 ,4 ,0 ,5} ,{1 ,0 ,3 ,2} ,{1 ,2 ,3 ,0}}; memset ( Top, 1 , s i z e o f ( Top ) ) ; f o r ( i n t i =0; i <6; i ++) f o r ( i n t j =0; j <4; j ++) Top [ i ] [ l i s t [ i ] [ j ] ] = l i s t [ i ] [ ( j +3) % 4 ] ; } struct s t a t e { int s t ; s t a t e ( i n t d=5 , i n t r =1, i n t x=0, i n t y=0){ s t =(d6+ r ) MTSZMTSZ+xMTSZ+y ; // be c a r e f u l a b o u t MAXINT } i n l i n e i n t getX ( ) const { return ( s t /MTSZ)% MTSZ; }

4.2

Hexagonal Grid

14

i n l i n e i n t getY ( ) const { return s t% MTSZ; } i n l i n e i n t g e t d i r ( i n t d i r ) const { i n t a=s t / (MTSZMTSZ) ; i n t r=a%6,d=a / 6 ; switch ( d i r ) { case 0 : return Top [ d ] [ r ] ; case 1 : return r ; case 2 : return Front [ Top [ d ] [ r ] ] ; case 3 : return Front [ r ] ; case 4 : return Front [ d ] ; case 5 : return d ; } return 1; // s h o u l d n o t happen } s t a t e go ( i n t d i r ) const { i n t d=g e t d i r (DN) , r=g e t d i r (RT) ; switch ( d i r ) { case 0 : return s t a t e ( Top [ d ] [ r ] , r , getX ( ) 1, getY ( ) ) ; case 1 : return s t a t e ( r , Front [ d ] , getX ( ) , getY ( ) +1) ; case 2 : return s t a t e ( Front [ Top [ d ] [ r ] ] , r , getX ( ) +1 , getY ( ) ) ; case 3 : return s t a t e ( Front [ r ] , d , getX ( ) , getY ( ) 1) ; } return s t a t e ( ) ; // s h o u l d n o t happen } i n l i n e friend ostream& operator<<(ostream& out , const s t a t e& s ) { return out<< ( <<s . getX ( )<< , <<s . getY ( )<< ) : <<s . g e t d i r (DN)<< i s down & <<s . g e t d i r (RT)<< i s r i g h t ; } i n l i n e bool operator <(const s t a t e& a ) const { return s t <a . s t ; } };

4.2

Hexagonal Grid

// // e v e r y u s e o f j o f t i s l i k e t h i s : // 0 // // x+2 5 / \ 1 y+2 // x+1 4 \ / 2 y+1 // x y // 3 // b u t f o r I /O, u s e s i m p l e a x i s // x x+1 // y+1 | | // /\ /\ // y | | | // \/ \/ // typedef p a i r <int , int> j o f t ; const i n t go [ 6 ] [ 2 ] = { { 1 , 1 } , { 0 , 1 } , { 1 , 0 } , { 1 , 1 } , { 0 , 1 } , { 1 , 0 } } ; const i n t simp go [ 6 ] [ 2 ] = { { 1 , 0 } , { 1 , 1 } , { 2 , 0 } , { 1 , 1 } , { 1 , 1 } , { 2 , 0 } } ; i n l i n e i n t myabs ( i n t x ) { return ( x<0 ? x : x ) ; } i n t g e t d i s ( const j o f t& a , const j o f t& b ) { i n t x=a . f i r s t b . f i r s t , y=a . secondb . s e c o n d ; i n t d i s =0;

4.3

Date Conversion

15

i f ( xy<0) d i s=myabs ( x )+myabs ( y ) ; e l s e i f ( myabs ( x )>myabs ( y ) ) d i s=myabs ( y )+myabs ( xy ) ; e l s e d i s=myabs ( x )+myabs ( xy ) ; return d i s ; } j o f t t o s i m p l e ( const j o f t& hex ) { return j o f t (2 hex . f i r s t +1, hex . s e c o n d ) ; } j o f t to h e x ( const j o f t& simp ) { return j o f t ( ( simp . f i r s t 1)/ 2 , simp . s e c o n d ) ; }

4.3

Date Conversion

const i n t miny =1700; const i n t maxy=2299; const i n t minm=1; const i n t maxm=12; const i n t mind=1; i n l i n e bool l e a p ( i n t y ) { i f ( y%400==0) return true ; i f ( y%100==0) return f a l s e ; i f ( y%4==0) return true ; return f a l s e ; } i n l i n e i n t ydays ( i n t y ) { return l e a p ( y ) ? 3 6 6 : 3 6 5 ; } i n l i n e i n t mdays ( i n t y , i n t m) { i f (m==2)// f e b return l e a p ( y ) ? 2 9 : 2 8 ; i f (m==9 | | m==4 | | m==6 | | m==11) return 3 0 ; return 3 1 ; } i n l i n e bool i n ( i n t x , i n t y , i n t z ) { return x<=y && y<=z ; } i n l i n e bool v a l i d ( i n t y , i n t m, i n t d ) { return i n ( miny , y , maxy ) && i n (minm , m, maxm) && i n ( mind , d , mdays ( y ,m) ) ; } i n l i n e i n t num( i n t y , i n t m, i n t d ) { i n t r e s =0; f o r ( i n t i=miny ; i <y ; i ++) r e s+=ydays ( i ) ; f o r ( i n t i=minm ; i < m; i ++) r e s+=mdays ( y , i ) ; r e s+=dmind ; return r e s ; } i n l i n e void num to date ( i n t x , i n t &y , i n t &m, i n t &d ) { f o r ( y=miny ; ydays ( y ) <= x ; x=ydays ( y++)) ; f o r (m =minm ; mdays ( y , m) <= x ; x=mdays ( y , m++)) ; d=mind+x ; }

4.4

Roman Numbers

typedef p a i r <s t r i n g , int> rnum ; v e c t o r <rnum> nums ; map<char , int> v a l s ;

4.5

Permutation to Integer

16

void i n i t ( ) { nums . push nums . push nums . push nums . push nums . push nums . push nums . push nums . push nums . push nums . push nums . push nums . push nums . push vals vals vals vals vals vals vals [ [ [ [ [ [ [

back ( rnum ( M , 1 0 0 0 ) ) ; back ( rnum ( CM , 9 0 0 ) ) ; back ( rnum ( D , 5 0 0 ) ) ; back ( rnum ( CD , 4 0 0 ) ) ; back ( rnum ( C , 1 0 0 ) ) ; back ( rnum ( XC , 9 0 ) ) ; back ( rnum ( L , 5 0 ) ) ; back ( rnum ( XL , 4 0 ) ) ; back ( rnum ( X , 1 0 ) ) ; back ( rnum ( IX , 9 ) ) ; back ( rnum ( V , 5 ) ) ; back ( rnum ( IV , 4 ) ) ; back ( rnum ( I , 1 ) ) ;

I ]=1; V ] = 5 ; X ] = 1 0 ; L ] = 5 0 ; C ] = 1 0 0 ; D ] = 5 0 0 ; M ] = 1 0 0 0 ;

} s t r i n g num2rom ( i n t n ) { s t r i n g ans ; f o r ( i n t i =0; i <i n t ( nums . s i z e ( ) ) ; ++i ) { while ( n>=nums [ i ] . s e c o n d ) { ans+=nums [ i ] . f i r s t ; n=nums [ i ] . s e c o n d ; } } return ans ; } i n t rom2num ( s t r i n g s ) { i n t ans =0; f o r ( i n t i =0; i <i n t ( s . s i z e ( ) ) ; ++i ) { i n t sgn =+1; i f ( i +1<i n t ( s . s i z e ( ) ) && v a l s [ s [ i ]] < v a l s [ s [ i + 1 ] ] ) sgn =1; ans+=sgn v a l s [ s [ i ] ] ; } return ans ; }

4.5

Permutation to Integer

// p e r m u a t a t i o n and i n t e g e r s a r e z e r o b a s e d const i n t maxn=50; i n t f a c t [ maxn ] ; i n t perm2int ( i n t a , i n t n ) { // Keep i n mind t h a t a i s m o d i f i e d i n t b [ maxn ] ; f o r ( i n t i =0; i <n ; i ++) b [ a [ i ] ] = i ; i n t ans =0; f o r ( i n t i=n1; i >=0; i ) { i n t j=b [ i ] ; ans+=f a c t [ i ] j ; swap ( a [ j ] , a [ i ] ) ; b [ a [ j ]]= j ; } return ans ; } void int2perm ( i n t x , i n t a , i n t n ) { f o r ( i n t i =0; i <n ; i ++) a [ i ]= i ;

4.6

Interval Traversing

17

i n t b [ maxn ] ; f o r ( i n t i=n1; i >=0; i ) { b [ i ]=x/ f a c t [ i ] ; x%=f a c t [ i ] ; } f o r ( i n t i =0; i <n ; i ++) swap ( a [ i ] , a [ b [ i ] ] ) ; } void i n i t ( ) { fact [0]=1; f o r ( i n t i =1; i <maxn ; i ++) f a c t [ i ]= f a c t [ i 1] i ; }

4.6

Interval Traversing

const double e p s=1e 6; bool i s l e s s ( double a , double b ) { return a<be p s ; } const i n t maxn=100; struct e v e n t { double x ; char type ; int ind ; e v e n t ( ) {} e v e n t ( double x , char t y p e , i n t i n d ) : x ( x ) , type ( t y p e ) , i n d ( i n d ) {} }; bool operator <(const e v e n t& e1 , const e v e n t& e2 ) { i f ( ! i s l e s s ( e1 . x , e2 . x ) && ! i s l e s s ( e2 . x , e1 . x ) ) return e1 . type<e2 . type ; return i s l e s s ( e1 . x , e2 . x ) ; } int n ; i n t i n s i d e [ maxn ] ; double s [ maxn ] , t [ maxn ] ; v e c t o r <event> e ; void t r a v e r s e ( ) { f o r ( i n t i =0; i <n;++ i ) { e . push back ( e v e n t ( s [ i ] , S , i ) ) ; e . push back ( e v e n t ( t [ i ] , T , i ) ) ; } s o r t ( e . b e g i n ( ) , e . end ( ) ) ; memset ( i n s i d e , 0 , s i z e o f i n s i d e ) ; i n t c u r I n =0; f o r ( i n t i t =0; i t <2;++ i t ) { f o r ( i n t i =0; i <n;++ i ) { i f ( e [ i ] . type== S ) { c u r I n+=1i n s i d e [ e [ i ] . i n d ] ; i n s i d e [ e [ i ] . ind ]=1; } e l s e i f ( e [ i ] . type==T ) { c u r I n=i n s i d e [ e [ i ] . i n d ] ; i n s i d e [ e [ i ] . ind ]=0; } } } }

4.7

Big Integer

struct Bint { s t a t i c const in t b a s e =10000;

4.7

Big Integer

18

s t a t i c const in t b l 1 0 =4; s t a t i c const in t maxn=1000; i n t n , a [ maxn ] ; Bint ( i n t x=0) { n=1; a [0]= x ; normalize () ; } Bint ( const s t r i n g &s ) { n=0; f o r ( i n t i=s . s i z e ( )b l 1 0 ; ; i=b l 1 0 ) { i n t r=i+b l 1 0 ; i f ( i <0) i =0; a [ n++]= a t o i ( s . s u b s t r ( i , ri ) . c s t r ( ) ) ; i f ( ! i ) break ; } } void n o r m a l i z e ( ) { a [ n]=0; f o r ( i n t i =0; i <n ; i ++) { a [ i +1]+=a [ i ] / b a s e ; a [ i ]%=b a s e ; i f ( a [ n ] >0) a[++n ] = 0 ; } } void s h r i n k ( ) { while ( n>1 && ! a [ n 1]) n; } Bint& operator+=(const Bint&x ) { f o r ( i n t i =0; i <x . n ; i ++) { i f ( i>=n ) a[++n ] = 0 ; a [ i ]+=x . a [ i ] ; } normalize () ; return t h i s ; } Bint operator+(const Bint &o ) const { Bint sum=t h i s ; sum+=o ; return sum ; } Bint& operator=(const Bint&x ) { // Assuming t h e r e s u l t i s nonn e g a t i v e f o r ( i n t i =0; i <n1; i ++) a [ i ]+=base 1; a [0]+=1; a [ n1]=1; f o r ( i n t i =0; i <x . n ; i ++) { i f ( i>=n ) a[++n ] = 0 ; a [ i ]=x . a [ i ] ; } normalize () ; shrink () ; return t h i s ; } Bint operator (const Bint &o ) const { // Assuming t h e r e s u l t i s nonn e g a t i v e Bint sum=t h i s ; sum=o ; return sum ; } operator s t r i n g ( ) { char fmt [ 1 0 ] ;

4.7

Big Integer

19

s p r i n t f ( fmt , %%0%i i , b l 1 0 ) ; s t r i n g s= ; f o r ( i n t i=n1; i >=0; i ) { char p a r t [ b l 1 0 + 2 ] ; s p r i n t f ( part , ( ( i==n1)? %i : fmt ) , a [ i ] ) ; s+=p a r t ; } return s ; } Bint& operator=(const i n t x ) { //Be c a r e f u l ! x base <MAX INT f o r ( i n t i =0; i <n ; i ++) a [ i ]=x ; normalize () ; return t h i s ; } Bint operator ( const i n t x ) const { //Be c a r e f u l ! x base <MAX INT Bint r e s =t h i s ; r e s =x ; return r e s ; } Bint operator ( const Bint&x ) const { Bint r e s ; f o r ( i n t i =0; i<=n+x . n ; i ++) res . a [ i ]=0; r e s . n=n+x . n ; f o r ( i n t i =0; i <n ; i ++, r e s . n o r m a l i z e ( ) ) f o r ( i n t j =0; j <x . n ; j ++) r e s . a [ i+j ]+=a [ i ] x . a [ j ] ; res . shrink () ; return r e s ; } Bint& operator=(const Bint&x ) { t h i s=t h i s x ; return t h i s ; } bool operator <(const Bint&x ) const { i f ( n!=x . n ) return n<x . n ; f o r ( i n t i=n1; i >=0; i ) i f ( a [ i ]!= x . a [ i ] ) return a [ i ]<x . a [ i ] ; return f a l s e ; } Bint& operator/=(const i n t x ) { i n t rem=0; f o r ( i n t i=n1; i >=0; i ) { a [ i ]+=rem b a s e ; rem=a [ i ]%x ; a [ i ]/=x ; } shrink () ; // //// Now , amount o f rem i s ( t h i s % x ) . return t h i s ; } Bint operator / ( const i n t x ) const { Bint r e s =t h i s ; r e s /=x ; return r e s ; } Bint power ( const i n t k ) const { i n t l a s t =0; f o r ( i n t i =0; k>>i ; i ++)

20

i f ((1<< i ) & k ) l a s t=i ; Bint ans ( 1 ) ; f o r ( i n t i=l a s t ; i >=0; i ) { i f ( ( k>>i ) &1) ans= t h i s ; i f ( i >0) ans=ans ; } return ans ; } };

5
5.1

Number Theory
Number Theory

i n l i n e i n t r e d u c e ( i n t a , i n t m) { a%= i f ( a <0) a+= return a ; } m; m; struct t r i p l e { i n t x , y , d ; t r i p l e ( i n t x =0 , i n t y =0 , i n t d =0) : x ( x ) , y ( y ) , d ( d ) {} }; t r i p l e gcd ( i n t a , i n t b ) { i f ( a <0){ t r i p l e t=gcd(a , b ) ; t . x=t . x ; return t ; } i f ( b<0){ t r i p l e t=gcd ( a ,b ) ; t . y=t . y ; return t ; } i f ( a==0) return t r i p l e ( 0 , 1 , b ) ; t r i p l e t=gcd ( b%a , a ) ; return t r i p l e ( t . yt . x ( b/ a ) , t . x , t . d ) ; } i n l i n e i n t i n v ( i n t a , i n t m) { return r e d u c e ( gcd ( a ,m) . x ,m) ; } i n t s o l v e ( i n t m1, i n t b1 , i n t m2, i n t b2 ) { return r e d u c e ( b1 i n v (m2, m1) m2+b2 i n v (m1, m2) m1, m1m2) ; }

6
6.1

Linear Algebra/Optimization
Linear Algebra

const double e p s=1e 6; i n l i n e double i s z e r o ( double t ) { return t>e p s && t<e p s ; } template<c l a s s T> struct m a t r i c e { i n t m, n ; T a ; m a t r i c e ( i n t m , i n t n ) :m( m ) , n ( n ) { a=new T [m ] ; f o r ( i n t i =0; i < m;++ i ) a [ i ]=new T [ n ] ; } void makeI ( ) { f o r ( i n t i =0; i < m;++ i ) f o r ( i n t j =0; j <n;++ j ) a [ i ] [ j ]=( i==j ) ? 1 : 0 ; } void c l e a r ( ) { f o r ( i n t i =0; i < m;++ i ) delete [ ] a [ i ] ; delete [ ] a ; m =n=0;

6.1

Linear Algebra

21

} T& operator [ ] ( const i n t& t ) const { return a [ t ] ; } m a t r i c e <T> operator+(const m a t r i c e& b ) { m a t r i c e r e s (m, n ) ; f o r ( i n t i =0; i < m;++ i ) f o r ( i n t j =0; j <n;++ j ) r e s [ i ] [ j ]= a [ i ] [ j ]+b [ i ] [ j ] ; return r e s ; } m a t r i c e <T> operator (const m a t r i c e& b ) { m a t r i c e r e s (m, n ) ; f o r ( i n t i =0; i < m;++ i ) f o r ( i n t j =0; j <n;++ j ) r e s [ i ] [ j ]= a [ i ] [ j ]b [ i ] [ j ] ; return r e s ; } m a t r i c e <T> operator ( const m a t r i c e& b ) { m a t r i c e r e s (m, b . n ) ; f o r ( i n t i =0; i < m;++ i ) f o r ( i n t j =0; j <b . n;++ j ) { res [ i ] [ j ]=0; f o r ( i n t k=0;k<n;++k ) r e s [ i ] [ j ]+=a [ i ] [ k ] b [ k ] [ j ] ; } return r e s ; } m a t r i c e <T> operator+=(const m a t r i c e& b ) { f o r ( i n t i =0; i < m;++ i ) f o r ( i n t j =0; j <n;++ j ) a [ i ] [ j ]+=b [ i ] [ j ] ; } m a t r i c e <T> operator=(const m a t r i c e& b ) { f o r ( i n t i =0; i < m;++ i ) f o r ( i n t j =0; j <n;++ j ) a [ i ] [ j ]=b [ i ] [ j ] ; } void operator=(const m a t r i c e& b ) { clear () ; n=b . n ; m .m; =b a=b . a ; } }; template<c l a s s T> m a t r i c e <T> c l o n e ( const m a t r i c e <T >& a ) { m a t r i c e <T> r e s ( a .m, a . n ) ; f o r ( i n t i =0; i <a .m;++ i ) f o r ( i n t j =0; j <a . n;++ j ) r e s [ i ] [ j ]= a [ i ] [ j ] ; return r e s ; } template<c l a s s T> T RowEch ( m a t r i c e <T >& a , m a t r i c e <T >& b ) { T d e t =1; i n t r =0; f o r ( i n t j =0; j <a . n;++ j ) { bool f l a g=f a l s e ; f o r ( i n t i=r ; i <a .m;++ i ) if (! iszero (a [ i ] [ j ]) ){ swap ( a [ i ] , a [ r ] ) ; swap ( b [ i ] , b [ r ] ) ; i f ( i != r )

6.2

Simplex

22

d e t=d e t ; f l a g=true ; break ; } if (! flag ) d e t =0; else { d e t=a [ r ] [ j ] ; T u=1/a [ r ] [ j ] ; f o r ( i n t t =0; t<a . n;++t ) a [ r ] [ t ]=u ; f o r ( i n t t =0; t<b . n;++t ) b [ r ] [ t ]=u ; f o r ( i n t i =0; i <a .m;++ i ) { i f ( i==r ) continue ; u=a [ i ] [ j ] ; f o r ( i n t t =0; t<a . n;++t ) a [ i ] [ t ]=u a [ r ] [ t ] ; f o r ( i n t t =0; t<b . n;++t ) b [ i ] [ t ]=ub [ r ] [ t ] ; } ++r ; } } return d e t ; } template<c l a s s T> ostream& operator<<(ostream& out , m a t r i c e <T> a ) { f o r ( i n t i =0; i <a .m;++ i , out<<e n d l ) f o r ( i n t j =0; j <a . n;++ j ) out<<a [ i ] [ j ]<< ; return out ; }

6.2

Simplex

// Does NOT h a n d l e c a s e s where b [ i ] = 0 ! const i n t maxsize =200 + 2 0 ; const double e p s=1e 7; i n l i n e double i s 0 ( double x ) { return eps<x && x<e p s ; } struct Matrix { i n t m, n ; double a [ maxsize ] [ maxsize 2 ] ; i n t r o w v a r [ maxsize ] ; bool b a s i c [ maxsize 2 ] ; double &b ( i n t x ) { return a [ x ] [ m + 1 ] ; } +n void p i v o t ( i n t row , i n t c o l ) ; void s i m p l e x ( ) ; bool i n i t s i m p l e x ( ) ; void g e t s o l ( double x ) { // Get f i n a l s o l u t i o n . Remember , x i s 1b a s e d f o r ( i n t i =1; i<= +n ; i ++) x [ i ] = 0 ; m f o r ( i n t i =1; i<= i ++) x [ r o w v a r [ i ] ] = b ( i ) ; m; } }; void Matrix : : p i v o t ( i n t row , i n t c o l ) { double d=a [ row ] [ c o l ] ;

6.2

Simplex

23

f o r ( i n t j =1; j<= +n+1; j ++) m a [ row ] [ j ]/=d ; f o r ( i n t i =0; i<= i ++) { m; i f ( i==row ) continue ; double e=a [ i ] [ c o l ] ; f o r ( i n t j =1; j<= +n+1; j ++) { m a [ i ] [ j ] = a [ row ] [ j ] e ; } } b a s i c [ r o w v a r [ row ] ] = 0 ; basic [ col ]=1; r o w v a r [ row]= c o l ; } bool Matrix : : i n i t s i m p l e x ( ) { // Find an i n i t i a l s o l u t i o n , you don t need t h i s i f you a l r e a d y have one s t a t i c Matrix o ; i n t mini =1; f o r ( i n t i =2; i<= i ++) m; i f ( b ( i )<b ( mini ) ) mini=i ; i f ( b ( mini )>=0) return 1 ; o .m m; = o . n=n+1; // B u i l d mxn e q u a t i o n m a t r i x : f o r ( i n t i =1; i<= i ++) m; f o r ( i n t j =0; j<=n ; j ++) o . a [ i ] [ j ]=a [ i ] [ j ] ; f o r ( i n t i =1; i<= i ++) m; o . a [ i ] [ n+1]=1; // O b j e c t i v e f u n c t i o n : //Z = x ( n+1) i s t h e o b j e c t i v e f u n c t i o n o.a[0][0]=1; f o r ( i n t j =1; j<= +n+2; j ++) o . a [ 0 ] [ j ] = 0 ; m o . a [ 0 ] [ n+1]=1; // a u x i l i a r y v a r i a b l e s : f o r ( i n t i =1; i<= i ++) m; f o r ( i n t j =1; j<= j ++) m; o . a [ i ] [ j+o . n ] = 0 ; f o r ( i n t i =1; i<= i ++) m; o . a [ i ] [ i+o . n ] = 1 ; //B: f o r ( i n t i =1; i<= i ++) m; o . b ( i )=b ( i ) ; // r o w v a r and b a s i c : f o r ( i n t i =0; i<=o .m . n+1; i ++) o . b a s i c [ i ] = 0 ; +o f o r ( i n t i=1+o . n ; i<= +o . n ; i ++) o . b a s i c [ i ] = 1 ; m f o r ( i n t i =1; i<= i ++) o . r o w v a r [ i ]= o . n+i ; m; o . p i v o t ( mini , n+1) ; o . simplex () ; i f ( is0 (o . b(0) ) ) { i f ( o . b a s i c [ n +1]) { c e r r << E r r o r i n i n i t s i m p l e x : x0 i s b a s i c <<e n d l ; exit (2) ; } //mxn e q u a t i o n m a t r i x :

6.2

Simplex

24

f o r ( i n t i =1; i<= i ++) m; f o r ( i n t j =0; j<=n ; j ++) a [ i ] [ j ]= o . a [ i ] [ j ] ; // don t t o u c h o b j e c t i v e f u n c t i o n y e t . . f o r ( i n t i =1; i<= i ++) m; f o r ( i n t j =1; j<= j ++) m; a [ i ] [ n+j ]=o . a [ i ] [ n+j + 1 ] ; //B: f o r ( i n t i =1; i<= i ++) m; b ( i )=o . b ( i ) ; // r o w v a r and b a s i c : f o r ( i n t i =1; i<= i ++) { m; r o w v a r [ i ]= o . r o w v a r [ i ] ; i f ( o . r o w v a r [ i ]>o . n ) r o w v a r [ i ]; } f o r ( i n t i =0; i<= +n+1; i ++) b a s i c [ i ] = 0 ; m f o r ( i n t i =1; i<= i ++) b a s i c [ r o w v a r [ i ] ] = 1 ; m; // Fix o b j e c t i v e f u n c t i o n : ( so t h a t i t o n l y has n o n b a s i c v a r s ) f o r ( i n t i =1; i<= i ++) { m; i n t c o l=r o w v a r [ i ] ; double e=a [ 0 ] [ c o l ] ; f o r ( i n t j =1; j<= +n+1; j ++) m a [ 0 ] [ j ] = a [ i ] [ j ] e ; } return 1 ; } else return 0 ; } void Matrix : : s i m p l e x ( ) { if (! init simplex () ) { cout<<No F e a s i b l e S o l u t i o n s ! <<e n d l ; exit (1) ; } f o r ( i n t s t e p = 1 ; ; s t e p++) { i n t c o l =0; double min=e p s ; f o r ( i n t i =1; i<= +n ; i ++) m i f ( ! b a s i c [ i ] && a [ 0 ] [ i ]<min ) { c o l=i ; min=a [ 0 ] [ i ] ; } i f ( c o l ==0) break ; i n t row=1; f o r ( i n t i =1; i<= i ++) { m; i f ( i s 0 ( a [ i ] [ c o l ] ) ) continue ; +n+1]/ a [ i ] [ c o l ] ; double b i a=a [ i ] [ m if ( is0 ( bi a ) ) { cout<<b ( <<i << ) i s z e r o ! E x i t i n g t o a v o i d l o o p c o n d i t i o n . <<e n d l ; exit (1) ; } e l s e i f ( b i a >0 && ( row==1 | | b i a <min ) ) { row=i ; min=b i a ; } } i f ( row==1) { cout<<No S o l u t i o n , Area i s Unbounded ! <<e n d l ; exit (1) ; }

25

p i v o t ( row , c o l ) ; } }

7
7.1

String Matching
KMP

const i n t maxm=1000000; i n t ne xt [ maxm ] ; i n t kmp( s t r i n g& s , s t r i n g& t x t ) { // r e t u r n s t h e number o f h i t s next [0]= 1; f o r ( i n t i =1; i<=s . l e n g t h ( ) ; i ++){ i n t i n d=next [ i 1 ] ; while ( ind >1 && s [ i n d ] ! = s [ i 1]) i n d=next [ i n d ] ; next [ i ]= i n d +1; } i n t s t =0; i n t r e s =0; f o r ( i n t i =0; i <t x t . l e n g t h ( ) ; i ++){ while ( s t >1 && t x t [ i ] ! = s [ s t ] ) s t=next [ s t ] ; s t ++; i f ( s t==s . l e n g t h ( ) ) { // one sample has been found // ( i s . l e n g t h ( ) +2) s t=next [ s t ] ; r e s ++; } } return r e s ; }

7.2

Hash

typedef unsigned long long l l ; const i n t b a s e =127; // or 131 J u s t An Odd Number void hash ( const char a [ ] , i n t n , i n t k , l l ans [ ] ) { // c h a r a [ ] OR i n t a [ ] OR . . . l l pw=1, hsh =0; f o r ( i n t i =0; i <k ; ++i ) pw=b a s e ; f o r ( i n t i =0; i <n ; ++i ) { hsh=hsh b a s e+a [ i ] ; i f ( i>=k ) hsh=pw a [ i k ] ; i f ( i+1>=k ) ans [ i k+1]=hsh ; } }

You might also like