├── Data structures ├── 0-1_trie.cpp ├── BIT.cpp ├── Lazy_propagation_range_sum_update.cpp ├── Persistent segment tree.cpp ├── Priority_queue_STL.cpp ├── Sparse_table.cpp ├── Trie.cpp ├── Union_find.cpp └── lazy_propagation.cpp ├── Graph theory ├── Detect cycle in a directed graph.cpp ├── Dijkstra's algorithm.cpp └── LCA_binary_lifting.cpp ├── Number theory ├── Birthday paradox.cpp └── Sieve of eratosthenes.cpp ├── Online judge solutions ├── Codechef │ ├── ADACRA.cpp │ ├── BAADSHAH.cpp │ ├── CHEFCODE.cpp │ ├── CHEFEXQ.cpp │ ├── CHEFSUBA.cpp │ ├── CLIQUED.cpp │ ├── DCE03.cpp │ ├── DEVSTR.cpp │ ├── DIGITSEP.cpp │ ├── DIVSUBS.cpp │ ├── FENWITTER.cpp │ ├── GCDQ.cpp │ ├── KIRLAB.cpp │ ├── MAKETRI.cpp │ ├── MAXSEGM.cpp │ ├── NEO01.cpp │ ├── QHOUSE.cpp │ ├── RRPLAYER.cpp │ ├── SCHEDULE.cpp │ ├── SEAINCR.cpp │ ├── SNACKUP.cpp │ ├── SSTORY.cpp │ ├── STDYTAB.cpp │ ├── SUMQ.cpp │ ├── UNIONSET.cpp │ └── WSITES01.cpp ├── Codeforces │ ├── 1092A.cpp │ ├── 1092B.cpp │ ├── 1092C.cpp │ ├── 1102A.cpp │ ├── 1102B.cpp │ ├── 1102C.cpp │ ├── 1106A.cpp │ ├── 1106B.cpp │ ├── 1106C.cpp │ ├── 1106D.cpp │ ├── 1110A.cpp │ ├── 1110C.cpp │ ├── 1114A.cpp │ ├── 1114B.cpp │ ├── 1114C.cpp │ ├── 1130A.cpp │ ├── 1130B.cpp │ ├── 1130C.cpp │ ├── 1130D.cpp │ ├── 1130E.cpp │ ├── 1133A.cpp │ ├── 1133B.cpp │ ├── 1133C.cpp │ ├── 1133D.cpp │ ├── 1133E.cpp │ ├── 1139C.cpp │ ├── 1141F2.cpp │ ├── 189A topdown.cpp │ ├── 189A.cpp │ ├── 282E.cpp │ ├── 313B.cpp │ ├── 327A.cpp │ ├── 339D.cpp │ ├── 415C.cpp │ ├── 431C.cpp │ ├── 455A.cpp │ ├── 456D.cpp │ ├── 466C.cpp │ ├── 489B.cpp │ ├── 489C.cpp │ ├── 500A.cpp │ ├── 522A.cpp │ ├── 577B.cpp │ ├── 639B.cpp │ ├── 696A.cpp │ ├── 735C.cpp │ ├── 742B.cpp │ ├── 742C.cpp │ ├── 746C.cpp │ ├── 749C.cpp │ ├── 762B.cpp │ ├── 764C.cpp │ ├── 782B using ternary search.cpp │ ├── 782B.cpp │ ├── 782C.cpp │ ├── 785C.cpp │ ├── 785D.cpp │ ├── 787A.cpp │ ├── 787B.cpp │ ├── 791B.cpp │ ├── 794C.cpp │ ├── 799B.cpp │ ├── 799C.cpp │ ├── 810B.cpp │ ├── 812C.cpp │ ├── 816B.cpp │ ├── 822C.cpp │ ├── 822D.cpp │ ├── 842C.cpp │ └── 982C.cpp ├── Csacademy │ ├── 35B.cpp │ ├── 35C.cpp │ └── 35D.cpp ├── Hackerearth │ ├── Finding mino.cpp │ ├── Little monk's interviews.cpp │ ├── Rjit need leaves.cpp │ └── The exam.cpp ├── Hackerrank │ ├── The coin change problem.cpp │ ├── The longest common subsequence.cpp │ ├── anagrams.cpp │ ├── boat trips.cpp │ ├── candies.cpp │ ├── chocolate feast.cpp │ ├── counting sort 1.cpp │ ├── counting sort 2.cpp │ ├── cut the sticks.cpp │ ├── cycle detection.cpp │ ├── encryption.cpp │ ├── equal stacks.cpp │ ├── find digit.cpp │ ├── flatland space stations.cpp │ ├── gears of war.cpp │ ├── greedy florist.cpp │ ├── ice cream parlour.cpp │ ├── insertion sort advanced analysis.cpp │ ├── jumping on the clouds revisited.cpp │ ├── largest rectangle.cpp │ ├── library fine.cpp │ ├── liza's workbook.cpp │ ├── longest common subsequence.cpp │ ├── luck balance.cpp │ ├── manasa and the stones.cpp │ ├── mandragora forest.cpp │ ├── mark and his toys using sorting.cpp │ ├── mark and his toys.cpp │ ├── maximum element.cpp │ ├── maximum subarray.cpp │ ├── minimum distances.cpp │ ├── modified kaprekar numbers.cpp │ ├── new year chaos.cpp │ ├── nikita and the game.cpp │ ├── quicksort 1 partion.cpp │ ├── quicksort 2.cpp │ ├── quicksort in-place.cpp │ ├── red john is black.cpp │ ├── running time of quicksort.cpp │ ├── sherlock and array.cpp │ ├── sum of all distances.cpp │ └── time conversion.cpp ├── Spoj │ ├── ABA12D.cpp │ ├── ABCDEF.cpp │ ├── ABSYS.cpp │ ├── ACODE.cpp │ ├── ACPC10A.cpp │ ├── ACPC10D.cpp │ ├── ACPC11B.cpp │ ├── ADAINDEX.cpp │ ├── ADDREV.cpp │ ├── AGGRCOW.cpp │ ├── AIBOHP.cpp │ ├── ALICESIE.cpp │ ├── ALTSEQ.cpp │ ├── AMR10G.cpp │ ├── AMR12D.cpp │ ├── ANARC05B.cpp │ ├── ANARC09A.cpp │ ├── AP2-AP.cpp │ ├── ARMY.cpp │ ├── ATOMS.cpp │ ├── BAT3.cpp │ ├── BEENUMS.cpp │ ├── BITMAP.cpp │ ├── BLACKOUT.cpp │ ├── BLINNET.cpp │ ├── BRDGHRD.cpp │ ├── BRIDGE.cpp │ ├── BUBBLESORT.cpp │ ├── BUGLIFE.cpp │ ├── BYTESM2.cpp │ ├── CADYDIST.cpp │ ├── CANDY.cpp │ ├── CANTON.cpp │ ├── CEQU.cpp │ ├── CHOCOLA.cpp │ ├── CIRCLE_E.cpp │ ├── CNTDO.cpp │ ├── CODESPTB.cpp │ ├── COINS.cpp │ ├── COMDIV.cpp │ ├── CPRIME.cpp │ ├── CPRMT.cpp │ ├── CRZYSMKR.cpp │ ├── CSTREET.cpp │ ├── DICT.cpp │ ├── DIEHARD.cpp │ ├── DIVFACT.cpp │ ├── DQUERY.cpp │ ├── DSUBSEQ.cpp │ ├── EASYPROB.cpp │ ├── EDIST top down.cpp │ ├── EDIST.cpp │ ├── EDIT.cpp │ ├── EGYPIZZA.cpp │ ├── EIGHT.cpp │ ├── ENIGMATH.cpp │ ├── ETF using prime sieve and totient sieve.cpp │ ├── ETF using totient sieve.cpp │ ├── ETF.cpp │ ├── ETFD.cpp │ ├── FACEFRND.cpp │ ├── FACT0.cpp │ ├── FACTCG2.cpp │ ├── FARIDA.cpp │ ├── FASHION.cpp │ ├── FAVDICE.cpp │ ├── FIBOSUM.cpp │ ├── FOXLINGS.cpp │ ├── FREQUENT.cpp │ ├── FRNDCIRC.cpp │ ├── GIRLSNBS.cpp │ ├── GLJIVE.cpp │ ├── GSS1.cpp │ ├── GSS3.cpp │ ├── GSS4.cpp │ ├── HACKRNDM.cpp │ ├── HANGOVER.cpp │ ├── HAYBALE.cpp │ ├── HIGHWAYS.cpp │ ├── HISTOGRA.cpp │ ├── HOLI.cpp │ ├── HORRIBLE.cpp │ ├── INVCNT.cpp │ ├── IOIPALIN.cpp │ ├── JAVAC.cpp │ ├── KGSS.cpp │ ├── KNAPSACK.cpp │ ├── LABYR1.cpp │ ├── LASTDIG.cpp │ ├── LASTDIG2.cpp │ ├── LCA.cpp │ ├── LENGFACT.cpp │ ├── LITE.cpp │ ├── LKS.cpp │ ├── MAJOR.cpp │ ├── MARBLES.cpp │ ├── MATSUM.cpp │ ├── MCOINS.cpp │ ├── MICEMAZE.cpp │ ├── MISERMAN.cpp │ ├── MIXTURES.cpp │ ├── MRECAMAN.cpp │ ├── MSE06H.cpp │ ├── MULTQ3.cpp │ ├── NY10A.cpp │ ├── OFFSIDE.cpp │ ├── ONP.cpp │ ├── PALIN.cpp │ ├── PARTY.cpp │ ├── PHONELST.cpp │ ├── PIGBANK.cpp │ ├── PPATH.cpp │ ├── PSYCHON.cpp │ ├── PT07Y.cpp │ ├── PT07Z.cpp │ ├── PTIME.cpp │ ├── RKS.cpp │ ├── RPLB.cpp │ ├── RPLN.cpp │ ├── SAMER08D.cpp │ ├── SBANK.cpp │ ├── SCPC11B.cpp │ ├── SEQ.cpp │ ├── SHPATH.cpp │ ├── SILVER.cpp │ ├── SKYLINE.cpp │ ├── SORT2D.cpp │ ├── SPP.cpp │ ├── STAMPS.cpp │ ├── STPAR.cpp │ ├── SUBSUMS.cpp │ ├── TDKPRIME.cpp │ ├── TRICOUNT.cpp │ ├── TRT top down.cpp │ ├── TWOSQRS.cpp │ ├── UFPR14D.cpp │ ├── WACHOVIA.cpp │ ├── WOWSUBSTR.cpp │ ├── WOWSUBSTR2.cpp │ └── ZSUM.cpp └── Uva │ ├── Network connections.cpp │ ├── graph connectivity.cpp │ └── the closest pair problem.cpp ├── README.md └── Sorting_and_searching ├── MergeSort.cpp ├── Quicksort using lomuto partitioning.cpp └── Ternary search.cpp /Data structures/0-1_trie.cpp: -------------------------------------------------------------------------------- 1 | 2 | const ll BITS = 32; 3 | vector to_binary(ll num){ 4 | vector v; 5 | while( num > 0 ){ 6 | v.pb(num%2); 7 | num >>= 1; 8 | } 9 | while( v.size() != BITS ) v.pb(0); 10 | reverse(all(v)); 11 | return v; 12 | } 13 | 14 | ll from_binary(vector &v){ 15 | ll num = 0; 16 | reverse(all(v)); 17 | lp(i, BITS){ 18 | num = num + v[i]*(1 << i); 19 | } 20 | return num; 21 | } 22 | 23 | // 0-1 trie 24 | struct node{ 25 | ll cnt; 26 | node *c[2]; 27 | node(){ 28 | cnt = 0; 29 | c[0] = c[1] = NULL; 30 | } 31 | }; 32 | node *root = new node(); 33 | 34 | void insert(ll n){ 35 | vector binary = to_binary(n); 36 | ll sz = binary.size(); 37 | node *p = root; 38 | lp(i, sz){ 39 | if( p -> c[binary[i]] == NULL ){ 40 | p -> c[binary[i]] = new node(); 41 | } 42 | p = p -> c[binary[i]]; 43 | } 44 | (p -> cnt)++; 45 | } 46 | 47 | // pair of distinct indices with maximum xor in array 48 | ll maxQuery(ll n){ 49 | if( root -> c[0] == NULL && root -> c[1] == NULL ) return 0; // querying empty tree 50 | vector num = to_binary(n); 51 | node *p = root; 52 | vector ans; 53 | lp(i, BITS){ 54 | if( p -> c[1 - num[i]] ){ 55 | ans.pb(1); 56 | p = p -> c[1 - num[i]]; 57 | }else{ 58 | ans.pb(0); 59 | p = p -> c[num[i]]; 60 | } 61 | } 62 | ll res = from_binary(ans); 63 | return res; 64 | } 65 | 66 | // pair of distinct indices with minimum xor in array 67 | ll minQuery(ll n){ 68 | if( root -> c[0] == NULL && root -> c[1] == NULL ) return LLONG_MAX; // querying empty tree 69 | vector num = to_binary(n); 70 | node *p = root; 71 | vector ans; 72 | lp(i, BITS){ 73 | if( p -> c[num[i]] ){ 74 | ans.pb(0); 75 | p = p -> c[num[i]]; 76 | }else{ 77 | ans.pb(1); 78 | p = p -> c[1 - num[i]]; 79 | } 80 | } 81 | ll res = from_binary(ans); 82 | return res; 83 | } 84 | 85 | // helper function to print a trie -> DONT CALL DIRECTLY. This is only to be called from => print() function written just below this function 86 | void printHelp(node *p, ll val, vector &v){ 87 | if( p != root ) 88 | v.pb( val ); 89 | if( p -> cnt > 0 ){ 90 | ll cnt = p -> cnt; 91 | while(cnt--) lpit(it, v) cout << *it; 92 | cout << "\n"; 93 | } 94 | if( p -> c[0] ) printHelp( p -> c[0], 0, v); 95 | if( p -> c[1] ) printHelp( p -> c[1], 1, v); 96 | if( !v.empty()) v.pop_back(); 97 | } 98 | 99 | // to print all numbers present in the 0-1 trie, in binary 100 | void print(){ 101 | node *ptr = root; 102 | vector v; 103 | printHelp(ptr, -1, v); 104 | } 105 | 106 | -------------------------------------------------------------------------------- /Data structures/BIT.cpp: -------------------------------------------------------------------------------- 1 | ll bit[N]; 2 | 3 | void update(int i, int val){ 4 | while( i < N ){ 5 | bit[i] += val; 6 | i += (i&-i); 7 | } 8 | } 9 | 10 | ll getsum(int i){ 11 | ll sum = 0; 12 | while( i > 0 ){ 13 | sum += bit[i]; 14 | i -= (i&-i); 15 | } 16 | return sum; 17 | } 18 | -------------------------------------------------------------------------------- /Data structures/Lazy_propagation_range_sum_update.cpp: -------------------------------------------------------------------------------- 1 | vector v(N); 2 | vector tree(4*N), lazy(4*N); 3 | 4 | ll merge( ll left, ll right){ 5 | ll tmp; 6 | // build temp from left and right according to quest 7 | return tmp; 8 | } 9 | 10 | void ulazy( ll pos, ll lo, ll hi, ll qlo, ll qhi, ll val){ 11 | 12 | ll l = (pos<<1)+1, r = l+1; 13 | 14 | if( lazy[pos] ){ // if there are pending updates to current node 15 | 16 | // Here, add lazy[pos] effect to tree[pos](Current node) 17 | 18 | // lazy propagation to child nodes if they exist 19 | if( lo != hi ){ 20 | 21 | } 22 | 23 | // clear lazy[pos] 24 | lazy[pos] = 0; 25 | } 26 | 27 | if( lo > hi || lo > qhi || hi < qlo ) 28 | return; 29 | 30 | if( qlo <= lo && hi <= qhi ){ // queried segment totally overlaps the current range of function 31 | 32 | // add effect of entire range to tree[pos] 33 | 34 | // Add lazy to child nodes if they exist 35 | if( lo != hi ){ 36 | 37 | } 38 | return; 39 | } 40 | 41 | ll mid = mid(lo,hi); 42 | 43 | ulazy( l, lo, mid, qlo, qhi, val); 44 | ulazy( r, mid+1, hi, qlo, qhi, val); 45 | 46 | tree[pos] = merge( tree[l], tree[r] ); 47 | 48 | } 49 | 50 | ll qlazy( ll pos, ll lo, ll hi, ll qlo, ll qhi){ 51 | 52 | ll l = (pos<<1)+1, r = l+1; 53 | 54 | if( lo > qhi || lo > hi || hi < qlo ) 55 | return 0; 56 | 57 | if( lazy[pos] ){ // if there are pending updates to current node 58 | // Here, add lazy[pos] effect to tree[pos](Current node) 59 | 60 | // lazy propagation to child nodes if they exist 61 | if( lo != hi ){ 62 | 63 | } 64 | 65 | lazy[pos] = 0; 66 | } 67 | 68 | if( qlo <= lo && qhi >= hi ) // the segment of current node is completely inside queried range 69 | return tree[pos]; 70 | 71 | ll mid = mid( lo , hi ); 72 | 73 | if( qlo > mid) 74 | return qlazy( r, mid+1, hi, qlo, qhi); 75 | else if( qhi <= mid) 76 | return qlazy( l, lo, mid, qlo, qhi); 77 | 78 | ll left = qlazy( l, lo, mid, qlo, qhi); 79 | ll right = qlazy( r, mid+1, hi, qlo, qhi); 80 | 81 | return merge( left, right); 82 | } 83 | 84 | 85 | /* 86 | BEFORE EVERY TEST CASE 87 | 88 | lp(i, 4*n + 1){ 89 | lazy[i] = 0; 90 | tree[i] = 0; 91 | } 92 | 93 | */ -------------------------------------------------------------------------------- /Data structures/Priority_queue_STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i> n; 10 | vector v(n); 11 | priority_queue pq_max; // max heap 12 | priority_queue, greater > pq_min; // min heap 13 | lp(i,n){ 14 | cin >> v[i]; 15 | pq_min.push(v[i]); 16 | pq_max.push(v[i]); 17 | } 18 | lp(i,n){ 19 | cout << v[i] << " "; 20 | } 21 | cout << "\n\n"; 22 | cout << "max heap : \n"; 23 | while(!pq_max.empty()){ 24 | cout << pq_max.top() << "\n"; 25 | pq_max.pop(); 26 | } 27 | cout << "\n"; 28 | cout << "min heap : \n"; 29 | while(!pq_min.empty()){ 30 | cout << pq_min.top() << "\n"; 31 | pq_min.pop(); 32 | } 33 | 34 | // NOTE => If A and B are two priority queues of same data type, then A.swap(B) as well B.swap(A) 35 | // will swap the contents of A and B in constant time (Because C++ pointers rock) 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Data structures/Sparse_table.cpp: -------------------------------------------------------------------------------- 1 | /* ZERO-INDEXED SPARSE TABLE FOR RANGE SUM QUERY PROBLEM WITHOUT UPDATES*/ 2 | 3 | typedef long long ll; 4 | typedef vector vll; 5 | 6 | #define NMAX 100005 7 | #define LOGNMAX 16 8 | 9 | ll table[NMAX][LOGNMAX+1]; 10 | vll arr(NMAX); 11 | 12 | void build_sparse(int n){ 13 | // sparse table template for range sum query 14 | lp(i, n) table[i][0] = arr[i]; 15 | for(int j = 1 ; j <= LOGNMAX ; j++){ 16 | for(int i = 0 ; i <= n - (1 << j) ; i++){ 17 | table[i][j] = table[i][j-1] + table[i + (1 << (j-1))][j-1]; 18 | } 19 | } 20 | 21 | } 22 | 23 | ll query_sparse(int qlo, int qhi){ 24 | ll sum = 0, l = qlo, r = qhi; 25 | for(int j = LOGNMAX ; j>=0 ; j--){ 26 | if( l + (1< 2 | using namespace std; 3 | 4 | #define SIZE 26 5 | 6 | typedef long long ll; 7 | 8 | struct node{ 9 | node *parent; 10 | node *child[SIZE]; 11 | ll words; 12 | ll prefixes; 13 | }; 14 | 15 | node* newNode(){ 16 | node *a = new node; 17 | a->words = a->prefixes = 0; 18 | for(ll i=0 ; ichild[i] = NULL; 19 | return a; 20 | } 21 | 22 | void insert( node *a, string &s){ 23 | node *ptr = a; 24 | ll sz = s.size(); 25 | for(ll i=0 ; iprefixes++; 27 | ll idx = s[i]-'a'; 28 | if(ptr->child[idx] == NULL){ 29 | ptr->child[idx] = newNode(); 30 | ptr->child[idx]->parent = ptr; // assigning parent 31 | } 32 | ptr = ptr->child[idx]; 33 | } 34 | ptr->words++; 35 | } 36 | 37 | ll search( node *a, string &s){ 38 | node *ptr = a; 39 | ll sz = s.size(); 40 | for(ll i=0 ; ichild[idx] == NULL) 43 | return 0; 44 | ptr = ptr->child[idx]; 45 | } 46 | if(ptr != NULL && ptr->words) return ptr->words; 47 | return 0; 48 | } 49 | 50 | int main(){ 51 | ll n,q; 52 | cin >> n; 53 | node *root = newNode(); 54 | 55 | string s[n]; 56 | for(ll i=0 ; i> s[i]; 58 | insert( root, s[i]); 59 | } 60 | 61 | cin >> q; 62 | 63 | while(q--){ 64 | string ss; 65 | cin >> ss; 66 | 67 | ll res = search( root, ss); 68 | 69 | if(res){ 70 | cout << ss << " is present " << res << " times\n"; 71 | }else{ 72 | cout << ss << " is not present\n"; 73 | } 74 | 75 | 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Data structures/Union_find.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Disjoint set union 3 | 4 | => 0-indexed 5 | => implemented path compression and union by rank 6 | => O(logn) per mergeit/find/areConnected functions 7 | */ 8 | 9 | typedef long long ll; 10 | 11 | #define lp(i,b) for(ll i = ll(0); i v(N); 2 | vector tree(4*N), lazy(4*N); 3 | 4 | ll merge( ll left, ll right){ 5 | ll tmp; 6 | // build temp from left and right according to quest 7 | return tmp; 8 | } 9 | 10 | void ulazy( ll pos, ll lo, ll hi, ll qlo, ll qhi, ll val){ 11 | 12 | ll l = (pos<<1)+1, r = l+1; 13 | 14 | if( lazy[pos] ){ // if there are pending updates to current node 15 | 16 | // Here, add lazy[pos] effect to tree[pos](Current node) 17 | 18 | // lazy propagation to child nodes if they exist 19 | if( lo != hi ){ 20 | 21 | } 22 | 23 | // clear lazy[pos] 24 | lazy[pos] = 0; 25 | } 26 | 27 | if( lo > hi || lo > qhi || hi < qlo ) 28 | return; 29 | 30 | if( qlo <= lo && hi <= qhi ){ // queried segment totally overlaps the current range of function 31 | 32 | // add effect of entire range to tree[pos] 33 | 34 | // Add lazy to child nodes if they exist 35 | if( lo != hi ){ 36 | 37 | } 38 | return; 39 | } 40 | 41 | ll mid = mid(lo,hi); 42 | 43 | ulazy( l, lo, mid, qlo, qhi, val); 44 | ulazy( r, mid+1, hi, qlo, qhi, val); 45 | 46 | tree[pos] = merge( tree[l], tree[r] ); 47 | 48 | } 49 | 50 | ll qlazy( ll pos, ll lo, ll hi, ll qlo, ll qhi){ 51 | 52 | ll l = (pos<<1)+1, r = l+1; 53 | 54 | if( lo > qhi || lo > hi || hi < qlo ) 55 | return 0; 56 | 57 | if( lazy[pos] ){ // if there are pending updates to current node 58 | // Here, add lazy[pos] effect to tree[pos](Current node) 59 | 60 | // lazy propagation to child nodes if they exist 61 | if( lo != hi ){ 62 | 63 | } 64 | 65 | lazy[pos] = 0; 66 | } 67 | 68 | if( qlo <= lo && qhi >= hi ) // the segment of current node is completely inside queried range 69 | return tree[pos]; 70 | 71 | ll mid = mid( lo , hi ); 72 | 73 | if( qlo > mid) 74 | return qlazy( r, mid+1, hi, qlo, qhi); 75 | else if( qhi <= mid) 76 | return qlazy( l, lo, mid, qlo, qhi); 77 | 78 | ll left = qlazy( l, lo, mid, qlo, qhi); 79 | ll right = qlazy( r, mid+1, hi, qlo, qhi); 80 | 81 | return merge( left, right); 82 | } 83 | 84 | 85 | /* 86 | BEFORE EVERY TEST CASE 87 | 88 | lp(i, 4*n + 1){ 89 | lazy[i] = 0; 90 | tree[i] = 0; 91 | } 92 | 93 | */ -------------------------------------------------------------------------------- /Graph theory/Dijkstra's algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m,source; 6 | cin >> n >> m; 7 | vector > g[n+1]; // 1-indexed adjacency list for of graph 8 | 9 | int a,b,wt; 10 | for(int i = 0; i> a >> b >> wt; 12 | g[a].push_back(make_pair(b,wt)); 13 | g[b].push_back(make_pair(a,wt)); 14 | } 15 | 16 | cin >> source; 17 | 18 | // Dijkstra's algorithm begins from here 19 | priority_queue,vector >,greater > > pq;// min-heap ; In pair => (dist,from) 20 | vector distTo(n+1,INT_MAX); // 1-indexed array for calculating shortest paths; 21 | 22 | distTo[source] = 0; 23 | pq.push(make_pair(0,source)); // (dist,from) 24 | 25 | while( !pq.empty() ){ 26 | int dist = pq.top().first; 27 | int prev = pq.top().second; 28 | pq.pop(); 29 | 30 | vector >::iterator it; 31 | for( it = g[prev].begin() ; it != g[prev].end() ; it++){ 32 | int next = it->first; 33 | int nextDist = it->second; 34 | if( distTo[next] > distTo[prev] + nextDist){ 35 | distTo[next] = distTo[prev] + nextDist; 36 | pq.push(make_pair(distTo[next], next)); 37 | } 38 | } 39 | 40 | } 41 | 42 | cout << "The distances from source, " << source << ", are : \n"; 43 | for(int i = 1 ; i<=n ; i++) cout << distTo[i] << " "; 44 | cout << "\n"; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Number theory/Birthday paradox.cpp: -------------------------------------------------------------------------------- 1 | #include // learnt from geeksforgeeks.org 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | double p ; // p is required the probability that 2 people out of n people in a room have same birthday 7 | cin >> p; // p <1 and p >=0 8 | cout << ceil(sqrt(2*365*log(1/(1-p)))); // this is the smallest number of people 9 | return 0; // needed in a room to make the probability >= p 10 | } 11 | -------------------------------------------------------------------------------- /Number theory/Sieve of eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define loop(i,a,b) for(int i = int(a); i 2 | using namespace std; 3 | typedef long long ll; 4 | #define MAX 1000 5 | #define MOD 1000000 6 | ll cat[MAX+1]; 7 | 8 | ll catalan(){ 9 | cat[0] = 1; 10 | cat[1] = 1; 11 | for(int i=2 ; i<=MAX ; i++){ 12 | cat[i] = 0; 13 | for(int j=0 ; j> t; 25 | while(t--){ 26 | cin >> n; 27 | cout << cat[n] << "\n"; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Online judge solutions/Codechef/FENWITTER.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | user : vivace 3 | topic : Fenwick tree based question. Knowledge about working of Fenwick tree required but code of fenwick tree not required. 4 | */ 5 | 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | #define lp(i,a) for(ll i = (ll)0 ; i < ll(a) ; i++) 12 | 13 | pair cnt(string &str){ 14 | ll size = (ll)(str.length()); 15 | ll ones = 0, suff = 0; 16 | bool flag = true; 17 | for(ll i = size-1 ; i>=0 ; i--){ 18 | if( str[i] == '1' ){ 19 | ones++; 20 | if( flag ) suff++; 21 | }else{ 22 | flag = false; 23 | } 24 | } 25 | return make_pair(ones,suff); 26 | } 27 | 28 | int main(){ 29 | ll t,n; 30 | string str1,str2,str3; 31 | cin >> t; 32 | lp(i,t){ 33 | cin >> str1 >> str2 >> str3 >> n; 34 | pair cnt1 = cnt(str1), cnt2 = cnt(str2), cnt3 = cnt(str3); 35 | ll ans = cnt1.first + n*cnt2.first + cnt3.first; 36 | if( cnt3.second < str3.length() ) ans -= cnt3.second; 37 | else if( cnt2.second < str2.length() ) ans -= (cnt3.first + cnt2.second); 38 | else ans -= (cnt3.first + n*cnt2.first + cnt1.second); 39 | 40 | ans++; // for first lookup to BIT 41 | cout << ans << "\n"; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Online judge solutions/Codechef/QHOUSE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ll l = 1,r = 1000,t = 1000,b = 0; 8 | ll a; 9 | ll mid = 1000,prev = -5,curr = 1000; 10 | string s; 11 | while(r>l){ 12 | cout << "? " << mid << " " << 0 << "\n"; 13 | fflush(stdout); 14 | cin >> s; 15 | if(s=="YES"){ 16 | l = mid; 17 | }else{ 18 | r = mid-1; 19 | } 20 | mid = (l+r+1)/2; 21 | } 22 | 23 | ll area = pow((l)*2,2); 24 | 25 | ll height = 2*l; 26 | l = 1 , r = 1000; 27 | cout << "? " << r << " " << height << "\n"; 28 | fflush(stdout); 29 | cin >> s; 30 | int base; 31 | if(s=="YES"){ 32 | base = 2000; 33 | }else{ 34 | r = 1000; 35 | mid = 1000; 36 | while(r>l){ 37 | 38 | cout << "? " << mid << " " << height << "\n"; 39 | fflush(stdout); 40 | cin >> s; 41 | if(s=="YES"){ 42 | l = mid; 43 | }else{ 44 | r = mid-1; 45 | } 46 | mid = (l+r+1)/2; 47 | } 48 | base = 2*l; 49 | } 50 | 51 | cout << "? " << 0 << " " << 1000 << "\n"; 52 | fflush(stdout); 53 | cin >> s; 54 | int alt; 55 | if(s=="YES"){ 56 | alt = 1000-height; 57 | }else{ 58 | r = 1000; 59 | l = 1; 60 | mid = 1000; 61 | while(r>l){ 62 | 63 | cout << "? " << 0 << " " << mid << "\n"; 64 | fflush(stdout); 65 | cin >> s; 66 | if(s=="YES"){ 67 | l = mid; 68 | }else{ 69 | r = mid-1; 70 | } 71 | mid = (l+r+1)/2; 72 | } 73 | alt = l-height; 74 | } 75 | 76 | area += (base*alt)/2; 77 | cout << "! " << area << "\n"; 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1092A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ll n, k, t; 8 | cin >> t; 9 | while(t--){ 10 | cin >> n >> k; 11 | string str = ""; 12 | for(ll i = 0 ; i < n ; i++){ 13 | for(ll j = 0 ; j < k ; j++){ 14 | str.push_back('a' + j); 15 | if( str.length() == n ) break; 16 | } 17 | if( str.length() == n ) break; 18 | } 19 | cout << str << "\n"; 20 | } 21 | 22 | 23 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1092B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ll n; 8 | cin >> n; 9 | vector a(n); 10 | for(ll i = 0 ; i < n ; i++) cin >> a[i]; 11 | sort(a.begin(), a.end()); 12 | ll minn = 0; 13 | for(ll i = 1 ; i < n ; i+=2){ 14 | minn += (a[i] - a[i - 1]); 15 | } 16 | 17 | ll sum = 0; 18 | for(ll i = n - 1 ; i > 0 ; i-=2){ 19 | sum += (a[i] - a[i - 1]); 20 | } 21 | cout << min(minn, sum) << "\n"; 22 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1102A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ll n; 8 | cin >> n; 9 | if( n%2 == 0 ){ 10 | if( (n/2)%2 == 0 ){ 11 | cout << 0 << "\n"; 12 | }else{ 13 | cout << 1 << "\n"; 14 | } 15 | }else{ 16 | if( n == 1 ) cout << 1 << "\n"; 17 | else if ( n == 3 ) cout << 0 << "\n"; 18 | else{ 19 | ll a = n-1; 20 | if( ((n-1)/2)%2 == 0 ) cout << 1 << "\n"; 21 | else cout << 0 << "\n"; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1102B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ll n, k; 8 | cin >> n >> k; 9 | vector a(n); 10 | for(ll i = 0 ; i < n ; i++) cin >> a[i]; 11 | map > indices; 12 | for(ll i = 0 ; i < n ; i++) indices[a[i]].push_back(i); 13 | for(auto it: indices){ 14 | if( it.second.size() > k ){ 15 | cout << "NO\n"; 16 | return 0; 17 | } 18 | } 19 | 20 | // color with 1 color first 21 | vector ans(n); 22 | map given; 23 | for(auto it: indices){ 24 | vector locs = it.second; 25 | 26 | } 27 | 28 | ll col = 1; 29 | for(auto it: indices){ 30 | vector locs = it.second; 31 | for(auto jt: locs){ 32 | ans[jt] = col; 33 | col = (col + 1)%(k+1); 34 | if( col == 0 ) col = 1; 35 | } 36 | } 37 | cout << "YES\n"; 38 | for(auto it: ans) cout << it << " "; 39 | cout << "\n"; 40 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1102C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ll n, x, y; 8 | cin >> n >> x >> y; 9 | vector a(n); 10 | for(ll i = 0 ; i < n ; i++) cin >> a[i]; 11 | if( x > y ){ 12 | cout << n << "\n"; 13 | return 0; 14 | } 15 | 16 | ll cnt = 0; 17 | for(ll i = 0 ; i < n ; i++){ 18 | if( a[i] <= x ) cnt++; 19 | } 20 | 21 | if( cnt%2 == 0 ) cout << cnt / 2 << "\n"; 22 | else cout << (cnt+1)/2 << "\n"; 23 | 24 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1106A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i<=ll(b) ; i++) 6 | 7 | int main(){ 8 | ll n, t, k; 9 | cin >> n; 10 | char m[n][n]; 11 | char cross = 'X'; 12 | lp(i, n-1){ 13 | lp(j, n - 1){ 14 | cin >> m[i][j]; 15 | } 16 | } 17 | ll ans = 0; 18 | for(int i = 1 ; i < n - 1 ; i++){ 19 | for(int j = 1 ; j < n - 1 ; j++){ 20 | if( m[i][j] == cross && m[i-1][j-1] == cross && m[i-1][j+1] == cross && m[i+1][j-1] == cross && m[i+1][j+1] == cross){ 21 | ans++; 22 | } 23 | } 24 | } 25 | cout << ans << "\n"; 26 | 27 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1106B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i> n >> m; 22 | vector a(n), c(n); 23 | vector v(n); 24 | lp(i, n) cin >> a[i]; 25 | lp(i, n) cin >> c[i]; 26 | 27 | lp(i, n){ 28 | v[i].rem = a[i]; 29 | v[i].type = i+1; 30 | v[i].cost = c[i]; 31 | v[i].index = i; 32 | } 33 | 34 | map revMap; 35 | 36 | 37 | sort(v.begin(), v.end(), cmp); 38 | 39 | 40 | lp(i, n){ 41 | revMap[v[i].type] = i; 42 | } 43 | 44 | 45 | 46 | ll index = 0; 47 | while(m--){ 48 | ll type, cnt; 49 | cin >> type >> cnt; 50 | ll pos = revMap[type]; 51 | if( v[pos].rem >= cnt ){ 52 | v[pos].rem -= cnt; 53 | cout << cnt*v[pos].cost << "\n"; 54 | }else{ 55 | ll left = cnt; 56 | ll ans = 0; 57 | if( v[pos].rem ){ 58 | left -= v[pos].rem; 59 | ans = v[pos].rem*v[pos].cost; 60 | v[pos].rem = 0; 61 | } 62 | while(index < n && left != 0){ 63 | if( v[index].rem > left ){ 64 | ans += (left*v[index].cost); 65 | v[index].rem -= left; 66 | left = 0; 67 | }else if( v[index].rem == left ){ 68 | ans += (left*v[index].cost); 69 | v[index].rem = 0; 70 | left = 0; 71 | index++; 72 | }else{ 73 | ans += (v[index].rem*v[index].cost); 74 | left -= v[index].rem; 75 | v[index].rem = 0; 76 | index++; 77 | } 78 | } 79 | if( left != 0 && index >= n ) cout << 0 << "\n"; 80 | else cout << ans << "\n"; 81 | } 82 | 83 | } 84 | 85 | 86 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1106C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i> n; 12 | vector a(n); 13 | lp(i, n) cin >> a[i]; 14 | sort(a.begin(), a.end()); 15 | ll l = 0, r = n - 1; 16 | ll ans = 0; 17 | while(l < r){ 18 | ans += (a[l] + a[r])*(a[l] + a[r]); 19 | l++; 20 | r--; 21 | } 22 | cout << ans << "\n"; 23 | 24 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1106D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i> n >> m; 10 | vector g[n+1]; 11 | lp(i, m){ 12 | ll a, b; 13 | cin >> a >> b; 14 | g[a].push_back(b); 15 | g[b].push_back(a); 16 | } 17 | 18 | priority_queue, greater > pq; 19 | pq.push(1); 20 | vector ans; 21 | vector vis(n+1, false); 22 | vis[1] = 1; 23 | 24 | vector added(n+1, false); 25 | 26 | while(!pq.empty()){ 27 | ll curr = pq.top(); 28 | pq.pop(); 29 | if( !added[curr] ){ 30 | ans.push_back(curr); 31 | added[curr] = 1; 32 | } 33 | for(vector::iterator it = g[curr].begin() ; it!= g[curr].end(); it++){ 34 | ll u = curr, v = *it; 35 | if( !vis[v] ){ 36 | pq.push(v); 37 | vis[v] = 1; 38 | } 39 | } 40 | } 41 | 42 | lp(i, n) cout << ans[i] << " "; cout << "\n"; 43 | 44 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1110A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i> b >> k; 11 | ll parity = 0; 12 | lp(i, k-1){ 13 | cin >> a; 14 | parity = (parity + (a%2)*(b%2))%2; 15 | } 16 | cin >> a; 17 | parity = (parity + a%2)%2; 18 | if( parity ) cout << "odd\n"; 19 | else cout << "even\n"; 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1110C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i= beg && a <= end ){ 14 | if( a != end ){ 15 | cout << end << "\n"; 16 | return; 17 | }else{ 18 | ll maxx = 1; 19 | for(ll i = 2; i*i <= a ; i++){ 20 | if( a%i == 0 ){ 21 | ll num1 = i, num2 = a-i; 22 | maxx = max(maxx, __gcd(num1, num2)); 23 | num1 = a/i; 24 | num2 = a-num1; 25 | maxx = max(maxx, __gcd(num1, num2)); 26 | } 27 | } 28 | cout << maxx << "\n"; 29 | return; 30 | } 31 | } 32 | } 33 | } 34 | 35 | int main(){ 36 | cin >> q; 37 | lp(i, q){ 38 | cin >> a; 39 | solve(a); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1114A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | 14 | int main(){ 15 | ll a, b, c, green, purple, black; 16 | cin >> a >> b >> c >> green >> purple >> black; 17 | if( green < a ){ 18 | cout << "NO\n"; 19 | return 0; 20 | } 21 | green -= a; 22 | if( green + purple < b ){ 23 | cout << "NO\n"; 24 | return 0; 25 | } 26 | ll left = green + purple; 27 | left -= b; 28 | if( left + black < c ){ 29 | cout << "NO\n"; 30 | return 0; 31 | } 32 | cout << "YES\n"; 33 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1114B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | 14 | ll n, m, k; 15 | vector a; 16 | 17 | int main(){ 18 | cin >> n >> m >> k; 19 | a.resize(n); 20 | lp(i, n) cin >> a[i]; 21 | 22 | vector > tmp; 23 | lp(i, n){ 24 | tmp.push_back({a[i], i+1}); 25 | } 26 | 27 | sort(tmp.begin(), tmp.end()); 28 | reverse(tmp.begin(), tmp.end()); 29 | 30 | ll sum = 0; 31 | lp(i, m*k) sum += tmp[i].first; 32 | 33 | map mp; 34 | lp(i, m*k) mp[tmp[i].first]++; 35 | 36 | vector ans; 37 | 38 | ll index = 0; 39 | lp(i, k){ 40 | ll left = m; 41 | for(; index < n && left > 0; index++){ 42 | if(mp[a[index]]){ 43 | mp[a[index]]--; 44 | left--; 45 | if(left == 0){ 46 | if( i != k-1 ) ans.push_back(index+1); 47 | } 48 | } 49 | } 50 | } 51 | 52 | cout << sum << "\n"; 53 | for(auto it: ans) cout << it << " "; 54 | cout << "\n"; 55 | 56 | 57 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1114C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | 14 | ll n, b; 15 | 16 | bool overflow(ll a, ll b){ 17 | ll x = a*b; 18 | if( a != 0 && (x/a) != b ) return true; 19 | return false; 20 | } 21 | 22 | int main(){ 23 | cin >> n >> b; 24 | map pf; 25 | ll tmp = b; 26 | ll cnt = 0; 27 | while(tmp%2==0){ 28 | cnt++; 29 | tmp/=2; 30 | } 31 | if(cnt > 0) pf[2] = cnt; 32 | 33 | ll i = 3; 34 | while(tmp > 1 && i*i <= tmp){ 35 | if( tmp%i == 0 ){ 36 | cnt = 0; 37 | while(tmp%i == 0){ 38 | cnt++; 39 | tmp/=i; 40 | } 41 | pf[i] = cnt; 42 | } 43 | i += 2; 44 | } 45 | if(tmp > 1) pf[tmp] = 1; 46 | 47 | ll minn = 1e18 + 2; 48 | for(auto it: pf){ // logn 49 | ll N = n; 50 | ll num = it.first; 51 | ll pwr = 0; 52 | while(N/num){ 53 | pwr += N/num; 54 | ll initial_num = num; 55 | 56 | if( overflow(num, it.first) ) break; 57 | num*=it.first; 58 | } 59 | ll cc = pwr; 60 | ll bb = it.second; 61 | ll div = cc/bb; 62 | ll rem = cc - (cc/bb)*bb; 63 | 64 | if( rem >= bb) continue; 65 | 66 | minn = min(minn, pwr/it.second); 67 | } 68 | cout << minn << "\n"; 69 | 70 | 71 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1130A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | //Compiler version g++ 6.3.0 5 | 6 | int main(){ 7 | ll n; 8 | cin >> n; 9 | vector a(n); 10 | for(ll i=0; i> a[i]; 12 | } 13 | ll req = ((n&1)?(n/2+1):(n/2)); 14 | for(ll d=-1000; d<=1000; d++){ 15 | if(d==0) continue; 16 | ll cnt=0; 17 | for(ll i=0; i 0)?(cnt+1):(cnt)); 19 | } 20 | if(cnt >= req){ 21 | cout << d; 22 | return 0; 23 | } 24 | 25 | } 26 | 27 | cout << 0; 28 | 29 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1130B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | //Compiler version g++ 6.3.0 5 | 6 | int main(){ 7 | ll n; 8 | cin >> n; 9 | ll num; 10 | vector > v[n+1]; 11 | for(ll i=0; i<2*n ; i++){ 12 | cin >> num; 13 | v[num].push_back(make_pair(i, 0)); 14 | } 15 | 16 | ll ans = 0, prev = 0; 17 | for(ll i=1; i<=n; i++){ 18 | pair p = v[i][0], q = v[i][1]; 19 | ll idx = ((p.first < q.first)?(0):(1)); 20 | ans += abs(v[i][idx].first - prev); 21 | v[i][idx].second = 1; 22 | prev = v[i][idx].first; 23 | } 24 | 25 | prev = 0; 26 | for(ll i=1; i<=n; i++){ 27 | pair p = v[i][0], q = v[i][1]; 28 | if(p.second == false){ 29 | ans += abs(p.first-prev); 30 | prev = p.first; 31 | }else{ 32 | ans += abs(q.first-prev); 33 | prev = q.first; 34 | } 35 | } 36 | 37 | cout << ans; 38 | 39 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1130C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | //Compiler version g++ 6.3.0 5 | ll dx[4] = {1, -1, 0, 0}; 6 | ll dy[4] = {0, 0, 1, -1}; 7 | ll n, r1, c1, r2, c2; 8 | bool vis[55][55]; 9 | ll g[55][55], cmp[55][55]; 10 | ll curr = 0; 11 | vector > > component; 12 | vector > comp; 13 | 14 | bool ok(ll x, ll y){ 15 | if(x>= 1 && y >= 1 && x <= n && y <= n) 16 | return true; 17 | return false; 18 | } 19 | 20 | void dfs(ll x, ll y){ 21 | if(vis[x][y]) return; 22 | cmp[x][y] = curr; 23 | comp.push_back({x, y}); 24 | vis[x][y] = true; 25 | for(ll i = 0; i < 4; i++){ 26 | ll xx = x + dx[i]; 27 | ll yy = y + dy[i]; 28 | if( ok(xx, yy) && (!vis[xx][yy]) && (!g[xx][yy]) ){ 29 | dfs(xx, yy); 30 | } 31 | } 32 | 33 | } 34 | 35 | ll dist(ll a, ll b, ll c, ll d){ 36 | ll ac = (a-c)*(a-c); 37 | ll bd = (b-d)*(b-d); 38 | return ac + bd; 39 | } 40 | 41 | int main(){ 42 | cin >> n >> r1 >> c1 >> r2 >> c2; 43 | ll num; 44 | for(ll i=1; i<=n ; i++){ 45 | string str; 46 | cin >> str; 47 | for(ll j=1; j<=n ; j++){ 48 | g[i][j] = str[j-1]-'0'; 49 | } 50 | } 51 | 52 | for(ll i=1; i<=n ; i++){ 53 | for(ll j=1; j<=n ; j++){ 54 | if(!g[i][j] && !vis[i][j]){ 55 | dfs(i, j); 56 | component.push_back(comp); 57 | curr++; 58 | comp.clear(); 59 | } 60 | } 61 | } 62 | 63 | 64 | ll source = cmp[r1][c1], target = cmp[r2][c2]; 65 | ll ans = 0; 66 | if( source == target ){ 67 | ans = 0; 68 | }else{ 69 | ans = 1e6; 70 | ll sz = component[source].size(); 71 | ll tz = component[target].size(); 72 | for(ll i = 0; i < sz ; i++){ 73 | for(ll j = 0; j < tz ; j++){ 74 | ll a = component[source][i].first; 75 | ll b = component[source][i].second; 76 | ll c = component[target][j].first; 77 | ll d = component[target][j].second; 78 | ans = min(ans, dist(a, b, c, d)); 79 | } 80 | } 81 | } 82 | cout << ans << "\n"; 83 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1130D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | //Compiler version g++ 5 | 6 | ll n, m; 7 | 8 | ll dist(ll a, ll b){ 9 | if(b >= a) return b-a; 10 | return b-a+n; 11 | } 12 | 13 | int main(){ 14 | cin >> n >> m; 15 | vector last(n+1, n+10), freq(n+1); 16 | for(ll i = 0; i < m ; i++){ 17 | ll a, b; 18 | cin >> a >> b; 19 | freq[a]++; 20 | last[a] = min(last[a], dist(a, b)); 21 | } 22 | 23 | for(ll beg = 1; beg <= n ; beg++){ 24 | ll ans = -1; 25 | for(ll i = 1; i <= n ; i++){ 26 | if(freq[i] == 0) continue; 27 | ll curr = dist(beg, i); 28 | curr += (n * (freq[i] - 1)); 29 | curr += last[i]; 30 | ans = max(ans, curr);; 31 | } 32 | cout << ans << " "; 33 | } 34 | 35 | cout << "\n"; 36 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1130E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | //Compiler version g++ 5 | 6 | ll n, m; 7 | 8 | ll dist(ll a, ll b){ 9 | if(b >= a) return b-a; 10 | return b-a+n; 11 | } 12 | 13 | int main(){ 14 | cin >> n >> m; 15 | vector last(n+1, n+10), freq(n+1); 16 | for(ll i = 0; i < m ; i++){ 17 | ll a, b; 18 | cin >> a >> b; 19 | freq[a]++; 20 | last[a] = min(last[a], dist(a, b)); 21 | } 22 | 23 | for(ll beg = 1; beg <= n ; beg++){ 24 | ll ans = -1; 25 | for(ll i = 1; i <= n ; i++){ 26 | if(freq[i] == 0) continue; 27 | ll curr = dist(beg, i); 28 | curr += (n * (freq[i] - 1)); 29 | curr += last[i]; 30 | ans = max(ans, curr);; 31 | } 32 | cout << ans << " "; 33 | } 34 | 35 | cout << "\n"; 36 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1133A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | 14 | ll h1, h2, m1, m2; 15 | char ch; 16 | string str; 17 | 18 | ll util(char ch){ 19 | return ch-'0'; 20 | } 21 | 22 | int main(){ 23 | cin >> str; 24 | h1 = util(str[0])*10 + util(str[1]); 25 | m1 = util(str[3])*10 + util(str[4]); 26 | cin >> str; 27 | h2 = util(str[0])*10 + util(str[1]); 28 | m2 = util(str[3])*10 + util(str[4]); 29 | ll min1 = h1*60 + m1; 30 | ll min2 = h2*60 + m2; 31 | 32 | ll m = (min1 + min2)/2; 33 | ll hh = m/60, mm = m%60; 34 | 35 | if( hh < 10 ) cout << "0"; 36 | cout << hh << ":"; 37 | if( mm < 10 ) cout << "0"; 38 | cout << mm << "\n"; 39 | 40 | 41 | 42 | 43 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1133B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | inline ll char2digit(char ch){ return ch-'0';} 14 | 15 | ll n, k; 16 | vector d; 17 | vector candies; 18 | 19 | 20 | int main(){ 21 | cin >> n >> k; 22 | d.resize(n); 23 | candies.resize(k); 24 | lp(i, n){ 25 | cin >> d[i]; 26 | candies[d[i]%k]++; 27 | } 28 | ll ans = 0; 29 | ans += 2*(candies[0]/2); 30 | 31 | for(ll i = 1 ; i< k/2 ; i++){ 32 | if(candies[i] && candies[k-i] ){ 33 | ll a = candies[i], b = candies[k-i]; 34 | ans += (min(a, b)*2); 35 | } 36 | } 37 | 38 | if(k%2 && k > 1){ 39 | ll a = k/2, b = k - k/2; 40 | if( candies[a] && candies[b] ){ 41 | a = candies[a], b = candies[b]; 42 | ans += min(a, b)*2; 43 | } 44 | }else if(k > 1){ 45 | if( candies[k/2] ) ans += 2*(candies[k/2]/2); 46 | } 47 | cout << ans << "\n"; 48 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1133C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | inline ll char2digit(char ch){ return ch-'0';} 14 | 15 | ll n; 16 | vector a; 17 | 18 | int main(){ 19 | cin >> n; 20 | a.resize(n); 21 | lp(i, n) cin >> a[i]; 22 | sort(a.begin(), a.end()); 23 | 24 | ll i = 0, ans = 0; 25 | for(auto it: a){ 26 | ll beg = it; 27 | ll end = it+5; 28 | auto iter = upper_bound(a.begin() + i, a.end(), end); 29 | iter--; 30 | ll pos = iter - a.begin(); 31 | 32 | ans = max(ans, pos - i + 1); 33 | i++; 34 | } 35 | cout << ans << "\n"; 36 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1133D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 11 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 12 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 13 | inline ll char2digit(char ch){ return ch-'0';} 14 | 15 | ll n; 16 | vector a, b; 17 | map, bool>, ll> mp; 18 | 19 | int main(){ 20 | cin >> n; 21 | a.resize(n); 22 | b.resize(n); 23 | 24 | lp(i, n) cin >> a[i]; 25 | lp(i, n) cin >> b[i]; 26 | 27 | ll ans = 0; 28 | ll d0 = 0, dont_care = 0; 29 | 30 | lp(i, n){ 31 | bool neg = false; 32 | if( a[i] == 0 ){ 33 | if( b[i] == 0 ){ 34 | dont_care++; 35 | } 36 | continue; 37 | } 38 | if( b[i] == 0 ){ 39 | d0++; 40 | continue; 41 | } 42 | 43 | if( (a[i] < 0 && b[i] > 0) || (a[i] > 0 && b[i] < 0) ) neg = true; 44 | 45 | a[i] = abs(a[i]); 46 | b[i] = abs(b[i]); 47 | 48 | ll gcd = __gcd(a[i], b[i]); 49 | a[i] /= gcd; 50 | b[i] /= gcd; 51 | pair, bool> p = {{a[i], b[i]}, neg}; 52 | mp[p]++; 53 | ans = max(ans, mp[p]); 54 | } 55 | 56 | ans = max(ans, d0); 57 | cout << ans + dont_care << "\n"; 58 | 59 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1133E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma comment(linker, "/STACK:336777216") 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 12 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 13 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 14 | inline ll char2digit(char ch){ return ch-'0';} 15 | 16 | ll n, k; 17 | vector a, teamSize; 18 | ll dp[5005][5005]; 19 | 20 | int main(){ 21 | cin >> n >> k; 22 | a.resize(n); 23 | teamSize.resize(n); 24 | lp(i, n) cin >> a[i]; 25 | sort(a.begin(), a.end()); 26 | 27 | ll l = 0, r = 0; 28 | lp(i, n){ 29 | while( r < n && a[r] - a[i] <= 5 ) r++; 30 | if( r >= i ) teamSize[i] = r-i; 31 | } 32 | 33 | // dp[i][j] = max team size amongst players 0...i, having at most j teams 34 | 35 | for(ll i = 0; i < n ; i++){ 36 | for(ll j = 0 ; j <= k ; j++){ 37 | dp[i+1][j] = max(dp[i+1][j], dp[i][j]); 38 | if( j + 1 <= k ) 39 | dp[i+ teamSize[i]][j+1] = max(dp[i + teamSize[i]][j+1], dp[i][j] + teamSize[i]); 40 | } 41 | } 42 | 43 | cout << dp[n][k] << "\n"; 44 | 45 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1139C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma comment(linker, "/STACK:336777216") 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 12 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 13 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 14 | inline ll char2digit(char ch){ return ch-'0';} 15 | 16 | ll n, k; 17 | vector> g; 18 | vector vis; 19 | 20 | ll dfs(ll v){ 21 | vis[v] = 1; 22 | ll sum = 1; 23 | for(auto u: g[v]){ 24 | if( !vis[u] ){ 25 | sum += dfs(u); 26 | } 27 | } 28 | return sum; 29 | } 30 | 31 | int main(){ 32 | cin >> n >> k; 33 | g.resize(n+1); 34 | vis.resize(n+1); 35 | 36 | lp(i, n-1){ 37 | ll a, b, col; 38 | cin >> a >> b >> col; 39 | if( col ) continue; 40 | g[a].push_back(b); 41 | g[b].push_back(a); 42 | } 43 | 44 | ll ans = fme(n, k); 45 | lpp(i, 1, n+1){ 46 | if( !vis[i] ){ 47 | ll component_size = dfs(i); 48 | ans = (ans - fme(component_size, k) + MOD)%MOD; 49 | } 50 | } 51 | 52 | cout << ans << "\n"; 53 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/1141F2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma comment(linker, "/STACK:336777216") 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 12 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 13 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 14 | inline ll char2digit(char ch){ return ch-'0';} 15 | 16 | ll n; 17 | vector a; 18 | map > > mp; 19 | 20 | int main(){ 21 | cin >> n; 22 | a.resize(n); 23 | lp(i, n) cin >> a[i]; 24 | 25 | lp(i, n){ 26 | ll sum = 0; 27 | lpp(j, i, n){ 28 | sum += a[j]; 29 | mp[sum].push_back({i, j}); 30 | } 31 | } 32 | 33 | for(auto it: mp){ 34 | ll key = it.first; 35 | sort(mp[key].begin(), mp[key].end(), [](pair a, pair b){ return a.second < b.second;}); 36 | } 37 | 38 | ll ans = 0; 39 | vector > ans_blocks; 40 | for(auto it: mp){ 41 | ll len = 0; 42 | ll sum = it.first; 43 | ll previous_end = -1; 44 | vector > blocks = it.second; 45 | vector > curr_blocks; 46 | 47 | for(auto block: blocks){ 48 | if( block.first > previous_end ){ 49 | len++; 50 | curr_blocks.push_back(block); 51 | previous_end = block.second; 52 | } 53 | } 54 | if( len > ans ){ 55 | ans = len; 56 | ans_blocks = curr_blocks; 57 | } 58 | } 59 | 60 | cout << ans << "\n"; 61 | for(auto block: ans_blocks){ 62 | cout << block.first + 1 << " " << block.second + 1 << "\n"; 63 | } 64 | 65 | 66 | } -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/189A topdown.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace1 4 | TOPICS : dynamic programming(recursion with memoization / top-down) 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define MAX 1e6 11 | #define MOD 1e7 12 | 13 | typedef double lf; 14 | typedef long long ll; 15 | typedef long double LF; 16 | typedef unsigned long long ull; 17 | typedef set sll; 18 | typedef vector vll; 19 | typedef map msl; 20 | typedef pair ii; 21 | typedef vector vpll; 22 | typedef map mll; 23 | typedef vector vvll; 24 | typedef list lll; 25 | typedef vector vlll; 26 | typedef pair iii; 27 | 28 | #define rtn return 29 | #define gc getchar 30 | #define pb push_back 31 | #define f first 32 | #define s second 33 | #define mp(x,y) make_pair(x,y) 34 | #define all(a) a.begin(),a.end() 35 | #define allr(a) a.rbegin(),a.rend() 36 | #define lp(i,a,b) for(ll i = ll(a); i= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 45 | if(isNeg) x *= -1; return x; 46 | } 47 | 48 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 49 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 50 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 51 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 52 | inline ll lcm(ll a,ll b){rtn (a*b)%gcd(a,b); } 53 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 54 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 55 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 56 | 57 | ll f(ll dp[],ll a[],ll i){ 58 | if(i<0) return LLONG_MIN; 59 | if(dp[i]!=-1) return dp[i]; 60 | ll temp = max(max(f(dp,a,i-a[2]),f(dp,a,i-a[1])),f(dp,a,i-a[0])); 61 | if(temp < -1){ 62 | dp[i] = LLONG_MIN; 63 | }else{ 64 | dp[i] = temp+1; 65 | } 66 | return dp[i]; 67 | } 68 | 69 | int main(){ 70 | ios::sync_with_stdio(false); 71 | ll n,a[3]; 72 | cin >> n >> a[0] >> a[1] >> a[2]; 73 | sort(a,a+3); 74 | ll dp[n+1]; 75 | dp[0] = 0; 76 | lp(i,1,n+1) dp[i] = -1; 77 | 78 | cout << f(dp,a,n) << "\n"; 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/189A.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace1 4 | Topics : Dynamic programming (Tabulation/bottom-up) 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define MAX 1e6 11 | #define MOD 1e7 12 | 13 | typedef double lf; 14 | typedef long long ll; 15 | typedef long double LF; 16 | typedef unsigned long long ull; 17 | typedef set sll; 18 | typedef vector vll; 19 | typedef map msl; 20 | typedef pair ii; 21 | typedef vector vpll; 22 | typedef map mll; 23 | typedef vector vvll; 24 | typedef list lll; 25 | typedef vector vlll; 26 | typedef pair iii; 27 | 28 | #define rtn return 29 | #define gc getchar 30 | #define pb push_back 31 | #define f first 32 | #define s second 33 | #define mp(x,y) make_pair(x,y) 34 | #define all(a) a.begin(),a.end() 35 | #define allr(a) a.rbegin(),a.rend() 36 | #define lp(i,a,b) for(ll i = ll(a); i= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 45 | if(isNeg) x *= -1; return x; 46 | } 47 | 48 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 49 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 50 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 51 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 52 | inline ll lcm(ll a,ll b){rtn (a*b)%gcd(a,b); } 53 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 54 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 55 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 56 | 57 | int main(){ 58 | ios::sync_with_stdio(false); 59 | ll n,a[3]; 60 | cin >> n >> a[0] >> a[1] >> a[2]; 61 | sort(a,a+3); 62 | ll dp[n+1]; 63 | 64 | lp(i,0,n+1) dp[i] = -10000000; 65 | dp[0]=0; 66 | lp(i,0,3) dp[a[0]] = 1; 67 | 68 | lp(i,0,n+1){ 69 | lp(j,0,3){ 70 | if(a[j] > i) break; 71 | dp[i] = max(dp[i-a[j]]+1,dp[i]); 72 | } 73 | } 74 | cout << dp[n] ; 75 | cout << "\n"; 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/735C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace1 4 | TOPICS : goldbach's conjecture, number theory 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | typedef long long ll; 11 | bool isPrime(ll n){ 12 | if(n==2) return true; 13 | if(n%2==0) return false; 14 | int root = sqrt(n); 15 | for(int i=3 ; i<=root ; i+=2){ 16 | if(n%i==0) return false; 17 | } 18 | return true; 19 | } 20 | 21 | int main(){ 22 | ll n; 23 | cin >> n; 24 | if(isPrime(n)) cout << 1; 25 | else if(n%2==0) cout << 2; 26 | else if(isPrime(n-2)) cout << 2; 27 | else cout << 3; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/742B.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | TOPICS : STL 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | #define lp(i,a,b) for(ll i = ll(a); i> n >> x; 17 | map m; 18 | ll arr[n]; 19 | lp(i,0,n){ 20 | cin >> arr[i]; 21 | map::iterator it = m.find(arr[i]^x); 22 | if(it!=m.end()){ 23 | ans += it->second; 24 | } 25 | m[arr[i]] = m[arr[i]]+1; 26 | } 27 | cout << ans << "\n"; 28 | } 29 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/742C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace1 4 | TOPICS : Least Common Multiple (LCM) , adhoc 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define MAX 1e6 12 | #define MOD 1e7 13 | 14 | typedef double lf; 15 | typedef long long ll; 16 | typedef long double LF; 17 | typedef unsigned long long ull; 18 | typedef set sll; 19 | typedef vector vll; 20 | typedef map msl; 21 | typedef pair ii; 22 | typedef vector vpll; 23 | typedef map mll; 24 | typedef vector vvll; 25 | typedef list lll; 26 | typedef vector vlll; 27 | typedef pair iii; 28 | 29 | #define rtn return 30 | #define gc getchar 31 | #define pb push_back 32 | #define f first 33 | #define s second 34 | #define mp(x,y) make_pair(x,y) 35 | #define sz size() 36 | #define all(a) a.begin(),a.end() 37 | #define allr(a) a.rbegin(),a.rend() 38 | #define lp(i,a,b) for(int i = int(a); i= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 48 | if(isNeg) x *= -1; return x; 49 | } 50 | 51 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 52 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 53 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 54 | inline bool isPalin(string &s){ int len = s.sz-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 55 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 56 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 57 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 58 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 59 | 60 | int check(int crush[],int mark[],int start){ 61 | int i = start,count = 0; 62 | while(!mark[i]){ 63 | mark[i] = true; 64 | i = crush[i]; 65 | count++; 66 | } 67 | if(i!=start) return -1; 68 | return count; 69 | } 70 | 71 | int main(){ 72 | ios::sync_with_stdio(false); 73 | int n,ans=1,tmp; 74 | cin >> n; 75 | int crush[n+1],mark[n+1]; 76 | lp(i,1,n+1) cin >> crush[i]; 77 | lp(i,1,n+1){ 78 | mset(mark); 79 | tmp = check(crush,mark,i); 80 | if(tmp==-1){ 81 | cout << tmp << "\n"; 82 | return 0; 83 | } 84 | 85 | if(tmp%2) ans = lcm(ans,tmp); 86 | else ans = lcm(ans,tmp/2); 87 | 88 | } 89 | cout << ans << "\n"; 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/746C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | TOPICS : adhoc 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | double s,x1,x2,t1,t2,p,d; 14 | cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d; 15 | 16 | int igorTime = abs(x1-x2)*t2; 17 | int d1 = ((x2>x1)?(1):(-1)); 18 | 19 | double igorSpeed = 1/t2,tramSpeed = 1/t1; 20 | 21 | if(igorSpeed > tramSpeed) cout << igorTime; 22 | else{ 23 | int t; 24 | if(d1==-1){ 25 | if(d1==d){ 26 | if(p=x2 && p<=x1){ 29 | t = (p+2*s-x2)*t1; 30 | }else{ 31 | t = (p-x2)*t1;//t = (s+x2-2*x1)*t1; 32 | } 33 | }else{ 34 | if(p=x2 && p<=x1){ 37 | t = (2*s-p-x2)*t1; 38 | }else{ 39 | t = (2*s-p-x2)*t1; 40 | } 41 | } 42 | }else{ 43 | if(d1==d){ 44 | if(p=x1 && p<=x2){ 47 | t = (2*s+x2-p)*t1; 48 | }else{ 49 | t = (2*s+x2-p)*t1; 50 | } 51 | }else{ 52 | if(p=x1 && p<=x2){ 55 | t = (p+x2)*t1; 56 | }else{ 57 | t = (p+x2)*t1; 58 | } 59 | } 60 | } 61 | cout << min(igorTime,t); 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/749C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace1 4 | TOPICS : queues, simulation 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | int n; 13 | cin >> n; 14 | char c; 15 | queue d,r; 16 | for(int i=0 ; i> c; 18 | if(c=='D') d.push(i); 19 | else r.push(i); 20 | } 21 | while(!d.empty() && !r.empty()){ 22 | if(d.front() < r.front()){ 23 | r.pop(); 24 | d.push(d.front()+n); 25 | d.pop(); 26 | }else{ 27 | d.pop(); 28 | r.push(r.front()+n); 29 | r.pop(); 30 | } 31 | } 32 | if(d.empty()) cout << 'R'; 33 | else cout << 'D'; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/762B.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | 5 | TOPICS : sorting, greedy, 2-pointer technique 6 | 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | typedef double lf; 13 | typedef long long ll; 14 | typedef long double LF; 15 | typedef unsigned long long ull; 16 | typedef set sll; 17 | typedef vector vll; 18 | typedef map msl; 19 | typedef pair pll; 20 | typedef vector vpll; 21 | typedef map mll; 22 | 23 | #define gc getchar 24 | #define pb push_back 25 | #define f first 26 | #define s second 27 | #define mp make_pair 28 | #define lp(i,a,b) for(int i = int(a); i= '0' && c <= '9' ; c = gc() ) x = (x << 1) + (x << 3) + c - '0'; 40 | if(isNeg) x *= -1; return x; 41 | } 42 | 43 | int main(){ 44 | ll u,p,b,total,val; 45 | cin >> u >> p >> b; 46 | vll usb,ps2; 47 | cin >> total; 48 | string s; 49 | lp(i,0,total){ 50 | cin >> val >> s; 51 | if(s[0]=='U') usb.pb(val); 52 | else ps2.pb(val); 53 | } 54 | usb.pb(INT_MAX) , ps2.pb(INT_MAX); // sentinel 55 | sort(all(usb)); 56 | sort(all(ps2)); 57 | ll ans = 0 , count = 0; 58 | ll l = 0,r = 0 ; 59 | ll us = usb.size()-1 , ps = ps2.size()-1; 60 | ll i = 0; 61 | while(total && u && i 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long ll; 12 | typedef long double ld; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair pii; 18 | typedef pair ii; 19 | typedef vector vpll; 20 | typedef map mll; 21 | typedef vector vvll; 22 | typedef list lll; 23 | typedef vector vlll; 24 | typedef pair iii; 25 | 26 | #define rtn return 27 | #define gc getchar 28 | #define pb push_back 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define all(a) a.begin(),a.end() 33 | #define allr(a) a.rbegin(),a.rend() 34 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 35 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 36 | #define mid(s,e) (s+(e-s)/2) 37 | 38 | ll ip(){ 39 | ll x = 0; bool isNeg = false; char c; 40 | c = gc(); 41 | if(c == '-') isNeg = true , c = gc(); 42 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 43 | if(isNeg) x *= -1; return x; 44 | } 45 | 46 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 47 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 48 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 49 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 50 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 51 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 52 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 53 | inline int in(){ int NR=0; register char c=gc(); while( c < 48 || c > 57 ){c=gc();} 54 | while(c>47 && c< 58){ NR = (NR << 3) + (NR << 1) + (c - 48); c=gc();} return NR; 55 | } 56 | 57 | ll dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}; 58 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 59 | 60 | #define MOD 1000000007 61 | #define N 100010 62 | 63 | int main(){ 64 | ios::sync_with_stdio(false); 65 | cin.tie(NULL); 66 | ll t,n,m,e,a,b,q,w,k; 67 | //ll p,q,curr,c; 68 | ///////////// START FROM HERE //////////////////// 69 | cin >> n >> m; 70 | set s[m+1]; 71 | bool flag = false; 72 | lp(i,1,m){ 73 | cin >> a; 74 | lp(j,1,a){ 75 | cin >> b; 76 | s[i].insert(b); 77 | } 78 | } 79 | 80 | lp(i,1,m){ 81 | bool safe = false; 82 | lpit(it,s[i]){ 83 | if(s[i].find(-*it)!=s[i].end()){ 84 | safe = true; 85 | break; 86 | } 87 | } 88 | if(!safe){ 89 | cout << "YES\n"; 90 | return 0; 91 | } 92 | } 93 | 94 | cout << "NO\n"; 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /Online judge solutions/Codeforces/982C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma comment(linker, "/STACK:336777216") 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define lp(i, b) for(ll i = ll(0); i0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}return r%m;} 12 | inline ll fme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1){r*=a;r%=m;}a*=a;a%=m;b>>=1;}return r%m;} 13 | inline ll sfme(ll a,ll b,ll m=MOD) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}return r%m;} 14 | inline ll char2digit(char ch){ return ch-'0';} 15 | 16 | int n; 17 | vector> g; 18 | vector sub; 19 | int cnt = 0; 20 | 21 | void dfs(int node, int par){ 22 | for(auto u: g[node]){ 23 | if( u != par ){ 24 | dfs(u, node); 25 | if( sub[u]&1 ){ 26 | sub[node] += sub[u]; 27 | }else{ 28 | cnt++; 29 | } 30 | } 31 | } 32 | } 33 | 34 | int main(){ 35 | cin >> n; 36 | g.resize(n+1); 37 | sub.resize(n+1, 1); 38 | 39 | 40 | lp(i, n-1){ 41 | int a, b; 42 | cin >> a >> b; 43 | g[a].push_back(b); 44 | g[b].push_back(a); 45 | } 46 | 47 | if( n&1 ){ 48 | cout << -1 << "\n"; 49 | return 0; 50 | } 51 | 52 | dfs(1, -1); 53 | 54 | cout << cnt << "\n"; 55 | 56 | } -------------------------------------------------------------------------------- /Online judge solutions/Csacademy/35B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MOD 1000000007 5 | #define N 100010 6 | 7 | typedef double lf; 8 | typedef long long ll; 9 | typedef long double ld; 10 | typedef unsigned long long ull; 11 | typedef vector vll; 12 | typedef vector >vpll; 13 | typedef vector vvll; 14 | 15 | #define sd(a) scanf("%d",&a) 16 | #define sstr(a) scanf("%s",a) 17 | #define sl(a) scanf("%lld",&a) 18 | #define debug(a) printf("check%d\n",a) 19 | #define clr(a) memset(a,0,sizeof(a)) 20 | #define rtn return 21 | #define gc getchar 22 | #define pb push_back 23 | #define ff first 24 | #define ss second 25 | #define mp(x,y) make_pair(x,y) 26 | #define all(a) a.begin(),a.end() 27 | #define allr(a) a.rbegin(),a.rend() 28 | #define lp(i,b) for(ll i = ll(0); i> n; 46 | vll arr(n), ans(n); 47 | lp(i ,n) cin >> arr[i]; 48 | ll beg = 0, end = 0; 49 | 50 | for(int i = 0 ; i arr[i+1] ){ 52 | end++; 53 | }else{ 54 | for(int j = beg ; j<=end ; j++){ 55 | ans[j] = end-beg+1; 56 | } 57 | beg = i+1; 58 | end = i+1; 59 | } 60 | } 61 | 62 | lp(i, n){ 63 | if( !ans[i] ){ 64 | ans[i] = end-beg+1; 65 | } 66 | } 67 | lp(i, n) cout << ans[i] << " "; cout << "\n"; 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Online judge solutions/Csacademy/35C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef double lf; 5 | typedef long long ll; 6 | typedef long double ld; 7 | typedef unsigned long long ull; 8 | typedef vector vll; 9 | typedef vector >vpll; 10 | typedef vector vvll; 11 | 12 | #define sd(a) scanf("%d",&a) 13 | #define sstr(a) scanf("%s",a) 14 | #define sl(a) scanf("%lld",&a) 15 | #define debug(a) printf("check%d\n",a) 16 | #define clr(a) memset(a,0,sizeof(a)) 17 | #define rtn return 18 | #define gc getchar 19 | #define pb push_back 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define all(a) a.begin(),a.end() 24 | #define allr(a) a.rbegin(),a.rend() 25 | #define lp(i,b) for(ll i = ll(0); i> n; 34 | ll arr[n][n]; 35 | vll row(n), col(n); 36 | ll maxx = -1; 37 | lp(i, n){ 38 | lp(j, n){ 39 | cin >> arr[i][j]; 40 | row[i] += arr[i][j]; 41 | col[j] += arr[i][j]; 42 | } 43 | } 44 | 45 | lp(i, n){ 46 | maxx = max(maxx, row[i]); 47 | maxx = max(maxx, col[i]); 48 | } 49 | //cout << "maxx : " << maxx << "\n"; 50 | lp(i, n){ 51 | //cout << "i : " << i << "\n"; 52 | ll need = maxx-row[i]; 53 | //cout << "need : " << need << "\n"; 54 | lp(j, n){ 55 | if( need == 0 ) break; 56 | if( col[j] == maxx ) continue; 57 | if( col[j] + need > maxx){ 58 | arr[i][j] += maxx - col[j]; 59 | need -= maxx-col[j]; 60 | col[j] = maxx; 61 | }else{ 62 | arr[i][j] += need; 63 | col[j] += need; 64 | row[i] = maxx; 65 | need = 0; 66 | } 67 | } 68 | } 69 | lp(i, n){ 70 | lp(j, n){ 71 | cout << arr[i][j] << " "; 72 | } 73 | cout << "\n"; 74 | } 75 | cout << "\n"; 76 | return 0; 77 | } -------------------------------------------------------------------------------- /Online judge solutions/Hackerearth/Finding mino.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long ll; 12 | typedef long double ld; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair ii; 18 | typedef vector vpll; 19 | typedef map mll; 20 | typedef vector vvll; 21 | typedef list lll; 22 | typedef vector vlll; 23 | typedef pair iii; 24 | 25 | #define rtn return 26 | #define gc getchar 27 | #define pb push_back 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define all(a) a.begin(),a.end() 32 | #define allr(a) a.rbegin(),a.rend() 33 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 34 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 35 | #define mid(s,e) (s+(e-s)/2) 36 | 37 | ll ip(){ 38 | ll x = 0; bool isNeg = false; char c; 39 | c = gc(); 40 | if(c == '-') isNeg = true , c = gc(); 41 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 42 | if(isNeg) x *= -1; return x; 43 | } 44 | 45 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 46 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 47 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 48 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 49 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 50 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 51 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 52 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 53 | 54 | #define MOD 1000000007 55 | #define MAX 200100 56 | 57 | ld f(ld x){ 58 | return ((2*x*x)-(12*x)+7); 59 | } 60 | 61 | int main(){ 62 | //ios::sync_with_stdio(false); 63 | ll t,n,m,e,a,b,k; 64 | //ll p,q,curr,c; 65 | ///////////// START FROM HERE //////////////////// 66 | 67 | cin >> n; 68 | while(n--){ 69 | ld lo,hi,l,r; 70 | cin >> lo >> hi; 71 | 72 | lp(i,1,100){ 73 | 74 | l = lo + (hi-lo)/3; 75 | r = hi - (hi-lo)/3; 76 | 77 | if(f(l) < f(r)){ 78 | hi = r; 79 | }else{ 80 | lo = l; 81 | } 82 | 83 | } 84 | 85 | cout << fixed << setprecision(0) << f(lo)<< "\n"; 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerearth/Rjit need leaves.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long ll; 12 | typedef long double ld; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair ii; 18 | typedef vector vpll; 19 | typedef map mll; 20 | typedef vector vvll; 21 | typedef list lll; 22 | typedef vector vlll; 23 | typedef pair iii; 24 | 25 | #define rtn return 26 | #define gc getchar 27 | #define pb push_back 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define all(a) a.begin(),a.end() 32 | #define allr(a) a.rbegin(),a.rend() 33 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 34 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 35 | #define mid(s,e) (s+(e-s)/2) 36 | 37 | ll ip(){ 38 | ll x = 0; bool isNeg = false; char c; 39 | c = gc(); 40 | if(c == '-') isNeg = true , c = gc(); 41 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 42 | if(isNeg) x *= -1; return x; 43 | } 44 | 45 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 46 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 47 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 48 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 49 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 50 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 51 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 52 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 53 | 54 | #define MOD 1000000007 55 | #define MAX 200100 56 | 57 | int main(){ 58 | ios::sync_with_stdio(false); 59 | ll t,n,m,e,a,b,k; 60 | //ll p,q,curr,c; 61 | ///////////// START FROM HERE //////////////////// 62 | cin >> t; 63 | 64 | while(t--){ 65 | cin >> n >> k; 66 | multimap mp; 67 | lp(i,1,n){ 68 | cin >> a; 69 | ll id = 1; 70 | multimap::iterator it = mp.find(a+1); 71 | if(it!=mp.end()){ 72 | ll temp = it->ss; 73 | mp.insert(mp(a,temp)); 74 | mp.erase(it); 75 | }else{ 76 | mp.insert(mp(a,id)); 77 | id++; 78 | } 79 | } 80 | 81 | if(mp.size()<=k){ 82 | cout << k -mp.size() << "\n"; 83 | }else{ 84 | cout << -1 << "\n"; 85 | } 86 | 87 | } 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerearth/The exam.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long int ll; 12 | typedef long double LF; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair ii; 18 | typedef vector vpll; 19 | typedef map mll; 20 | typedef vector vvll; 21 | typedef list lll; 22 | typedef vector vlll; 23 | typedef pair iii; 24 | 25 | #define rtn return 26 | #define gc getchar 27 | #define pb push_back 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define all(a) a.begin(),a.end() 32 | #define allr(a) a.rbegin(),a.rend() 33 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 34 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 35 | #define mid(s,e) (s+(e-s)/2) 36 | 37 | ll ip(){ 38 | ll x = 0; bool isNeg = false; char c; 39 | c = gc(); 40 | if(c == '-') isNeg = true , c = gc(); 41 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 42 | if(isNeg) x *= -1; return x; 43 | } 44 | 45 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 46 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 47 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 48 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 49 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 50 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 51 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 52 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 53 | 54 | #define MOD 1000000007 55 | #define MAX 200100 56 | 57 | int main(){ 58 | //ios::sync_with_stdio(false); 59 | ll t,x,l,n,m,e,a,b,k; 60 | //ll p,q,curr,c; 61 | ///////////// START FROM HERE //////////////////// 62 | 63 | cin >> t; 64 | while(t--){ 65 | cin >> x >> l >> n; 66 | 67 | // after nt turn , <=L , after (n-1)th, <= L/2 , after (n-2)th turn <= l/(2**2) , after [ n-(n-1) ]th == after first turn, <= L/(2**(n-1)) . 68 | 69 | if(!n){ 70 | cout << 0 << "\n"; 71 | continue; 72 | }else if(n > 59){ // 2**60 > 10**18 73 | cout << x << "\n"; 74 | cout << "yo : " << x - (l>>(n-1)) << "\n"; 75 | continue; 76 | } 77 | 78 | x = x-(l>>(n-1)); 79 | 80 | if(x > 0 ){ 81 | cout << x << "\n"; 82 | }else{ 83 | cout << 0 << "\n"; 84 | } 85 | } 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/The coin change problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 10 | int n,m; 11 | cin >> n >> m; 12 | int arr[m]; 13 | for(int i=0 ; i> arr[i]; 14 | sort(arr,arr+m); 15 | long long int t[m+1][n+1]; 16 | 17 | for(int i=0 ; i<=m ; i++) t[i][0] = 1; 18 | for(int i=0 ; i<=n ; i++) t[0][i] = 0; 19 | 20 | for(int i=1 ; i<=m ; i++){ 21 | for(int j=1 ; j<=n ; j++){ 22 | if(j >= arr[i-1] ){ 23 | t[i][j] = t[i-1][j] + t[i][(j-arr[i-1])]; 24 | }else{ 25 | t[i][j] = t[i-1][j]; 26 | } 27 | } 28 | } 29 | cout << t[m][n]; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/The longest common subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | double lf; 8 | typedef long long ll; 9 | typedef long double LF; 10 | typedef unsigned long long ull; 11 | using namespace std; 12 | 13 | int main(){ 14 | int n,m; 15 | cin >> n >> m; 16 | vector x(n+1),y(m+1); 17 | for(int i=1 ; i<=n ; i++) cin >> x[i]; 18 | for(int i=1 ; i<=m ; i++) cin >> y[i]; 19 | 20 | int t[m+1][n+1]; 21 | for(int i=0 ; i<=m ; i++) t[i][0] = 0; 22 | for(int i=1 ; i<=n ; i++) t[0][i] = 0; 23 | 24 | for(int i=1 ; i<=m ; i++){ 25 | for(int j=1 ; j<=n ; j++){ 26 | if(x[j]==y[i]) t[i][j] = ++t[i-1][j-1]; 27 | else t[i][j] = max(t[i-1][j],t[i][j-1]); 28 | } 29 | } 30 | stack lcs; 31 | int i=m,j=n; 32 | while(i > 0 && j > 0 ){ 33 | if(y[i]==x[j]){ 34 | lcs.push(y[i]); 35 | i--; 36 | j--; 37 | }else{ 38 | if(t[i-1][j]==t[i][j-1]) j--; 39 | else if(t[i-1][j] > t[i][j-1]) i--; 40 | else j--; 41 | } 42 | } 43 | while(!lcs.empty()){ 44 | cout << lcs.top() << " "; 45 | lcs.pop(); 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/anagrams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin >>t ; 8 | string s; 9 | 10 | for(int i=0 ; i> s; 12 | int count1[26]={0},count2[26]={0}; 13 | int len=s.length(); 14 | if(len%2==0){ 15 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int n,c,m,val; 12 | cin >> n >> c >> m; 13 | int maxx = m*c; 14 | bool flag = true; 15 | for(int i=0 ; i> val; 17 | if(val > m*c ) { 18 | flag = false; 19 | break; 20 | } 21 | } 22 | cout << ((flag)?("Yes\n"):("No\n")) ; 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/chocolate feast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i=0 ; i> n >>c >>m ; 10 | int count=0,wrappers=0; 11 | while(n>=c){ 12 | wrappers+=n/c; 13 | count+=n/c; 14 | while(wrappers>=m){ 15 | count+=wrappers/m; 16 | wrappers=wrappers/m+wrappers%m; 17 | } 18 | n=(n%c); 19 | } 20 | cout << count << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/counting sort 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n; 11 | cin >> n ; 12 | int num[n]; 13 | for(int i=0 ; i> num[i]; 14 | int count[100]={0}; 15 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n; 11 | cin >> n ; 12 | int num[n]; 13 | for(int i=0 ; i> num[i]; 14 | int count[100]={0}; 15 | for(int i=0 ; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main() { 8 | int n; 9 | cin >> n; 10 | int stick[n]; 11 | for(int i=0 ; i> stick[i]; 13 | } 14 | sort(stick,stick+n); 15 | int i=0,minLength; 16 | while(inext!=NULL){ 16 | slow = slow->next; 17 | fast = fast->next->next; 18 | if(slow == fast){ 19 | return 1; 20 | } 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/encryption.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main(){ 10 | string s; 11 | cin >> s; 12 | int length=s.length(); 13 | int row=floor(sqrt(length)),col=ceil(sqrt(length)); 14 | if(row*col 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | int n1; 8 | int n2; 9 | int n3; 10 | cin >> n1 >> n2 >> n3; 11 | vector h1(n1); 12 | int sum1=0,sum2=0,sum3=0; 13 | for(int i=0; i> h1[i]; 15 | sum1+=h1[i]; 16 | } 17 | vector h2(n2); 18 | for(int i=0 ; i> h2[i]; 20 | sum2+=h2[i]; 21 | } 22 | vector h3(n3); 23 | for(int i=0 ; i> h3[i]; 25 | sum3+=h3[i]; 26 | } 27 | 28 | int flag=0; 29 | 30 | while(sum1!=sum2 || sum2!=sum3){ 31 | if(sum1==0 || sum2==0 || sum3==0){ 32 | cout << 0 ; 33 | flag++; 34 | break; 35 | }else{ 36 | if(sum1>=sum2){ 37 | if(sum1>=sum3){ 38 | sum1-=h1.front(); 39 | h1.erase(h1.begin()); 40 | } 41 | else{ 42 | sum3-=h3.front(); 43 | h3.erase(h3.begin()); 44 | } 45 | }else{ 46 | if(sum2>=sum3){ 47 | sum2-=h2.front(); 48 | h2.erase(h2.begin()); 49 | }else{ 50 | sum3-=h3.front(); 51 | h3.erase(h3.begin()); 52 | } 53 | } 54 | } 55 | } 56 | if(!flag){ 57 | cout << sum1<< endl; 58 | } 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/find digit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i = 0; i < t; i++){ 8 | long int n; 9 | cin >> n; 10 | int permanentN=n; 11 | int count=0; 12 | while(n!=0){ 13 | if(permanentN%((n%10)?n%10:permanentN+1)==0){ 14 | count++; 15 | } 16 | n/=10; 17 | } 18 | cout << count < 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,m; 7 | cin >> n>>m; 8 | int c[m]; 9 | for(int i=0 ; i> c[i]; 11 | } 12 | 13 | sort(c,c+m); 14 | int maximum=0; 15 | int maxStart=c[0]; 16 | int maxEnd=n-1-c[m-1]; 17 | int diff=0; 18 | for(int i=0 ; i<=m-2 ; i++){ 19 | diff=c[i+1]-c[i]; 20 | if(((diff%2)?(diff+1/2):diff/2 )> maximum){ 21 | maximum=((diff%2>1)?(diff+1)/2:diff/2); 22 | } 23 | } 24 | cout << max(maximum,max(maxStart,maxEnd)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/gears of war.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int q; 12 | cin >> q; 13 | while(q--){ 14 | int n; 15 | cin >> n; 16 | cout << ((n%2)?("No"):("Yes"))<< "\n"; 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/greedy florist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum(int a[],int lo,int hi){ 5 | int res = 0; 6 | for(int i=lo ; i<=hi ; i++){ 7 | res+=a[i]; 8 | } 9 | return res; 10 | } 11 | 12 | void merge(int arr[],int lo,int mid,int hi){ 13 | int ll = mid-lo+1 , rr = hi-mid; 14 | int left[mid-lo+2],right[hi-mid+1]; 15 | for(int i=0 ; i=0){ 56 | if(per_index >= k) per_index = 0; 57 | res += (count[per_index]+1)*price[i]; 58 | count[per_index]++; 59 | per_index++; 60 | i--; 61 | } 62 | return res; 63 | } 64 | 65 | int main(){ 66 | int n,k; 67 | cin >> n >> k; 68 | int per[k],price[n],count[k]; 69 | for(int i=0 ; i> price[i]; 70 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int binarySearch(vector > &arr,int low,int high,int x){ 8 | int mid; 9 | bool flag = false; 10 | while(low<=high){ 11 | mid = low+(high-low)/2; 12 | if(arr[mid].firstx){ 15 | high = mid-1; 16 | }else{ 17 | flag = true; 18 | break; 19 | } 20 | } 21 | if(flag) return arr[mid].second; 22 | else return -1; 23 | } 24 | 25 | int main() { 26 | int t; 27 | cin >> t; 28 | int m,n; 29 | while(t--){ 30 | cin >> m >> n; 31 | vector > cost(n); 32 | for(int i=0 ; i> cost[i].first; 34 | cost[i].second = i+1; 35 | } 36 | sort(cost.begin(),cost.end()); 37 | for(int i=0 ; i 2 | #include 3 | using namespace std; 4 | 5 | long long inversions=0; 6 | 7 | void merge(long long arr[],long long low,long long mid,long long high){ 8 | int n1=mid-low+1; 9 | long long n2=high-mid; 10 | long long left[n1+1],right[n2+1]; 11 | for(int i=0 ; i right[j]){ 18 | arr[k]=right[j]; 19 | j++; 20 | }else{ 21 | arr[k]=left[i]; 22 | i++; 23 | inversions+=j; 24 | } 25 | } 26 | } 27 | 28 | void mergeSort(long long arr[],long long low,long long high){ 29 | if( low> t; 40 | for(int j=0 ; j> n; 43 | long long num[n]; 44 | for(int i=0 ; i> num[i]; 45 | mergeSort(num,0,n-1); 46 | cout << inversions <<"\n" ; 47 | inversions=0; 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/jumping on the clouds revisited.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n,k; 11 | int energy=100; 12 | cin >> n >> k; 13 | int cloud[n]; 14 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main(){ 9 | int d1,m1,y1,d2,m2,y2; 10 | cin >> d1 >> m1 >> y1 >>d2 >>m2 >> y2; 11 | if(y1>y2)cout << 10000; 12 | else if(y1m2 )cout <<500*(m1-m2); 14 | else if(d1>d2)cout <<15*(d1-d2); 15 | else cout <<0; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/liza's workbook.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n,k; 6 | cin >> n >> k; 7 | int problemsInChapter[n]; 8 | for(int i=0 ; i> problemsInChapter[i]; 10 | } 11 | int startPage=1; 12 | int special=0; 13 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | int n,m; 11 | scanf("%d%d",&n,&m); 12 | int s1[n+1],s2[m+1]; 13 | for(int i=1 ; i<=n ; i++) cin >> s1[i]; 14 | for(int i=1 ; i<=m ; i++) cin >> s2[i]; 15 | int t[m+1][n+1]; 16 | 17 | for(int i=0 ; i<=n ; i++) t[0][i] = 0; 18 | for(int i=0 ; i<=m ; i++) t[i][0] = 0; 19 | 20 | for(int i=1 ; i<=m ; i++){ 21 | for(int j=1 ; j<=n ; j++){ 22 | if(s2[i]==s1[j]) t[i][j] = t[i-1][j-1]+1; 23 | else t[i][j] = max(t[i][j-1],t[i-1][j]); 24 | } 25 | } 26 | 27 | int i=m,j=n; 28 | stack lcs; 29 | while(i>0 && j>0){ 30 | if(s2[i]==s1[j]){ 31 | lcs.push(s2[i]); 32 | i--; 33 | j--; 34 | } 35 | else{ 36 | if(t[i-1][j] == t[i][j-1]) j--; 37 | else if(t[i][j-1] > t[i-1][j]) j--; 38 | else i--; 39 | } 40 | } 41 | while(!lcs.empty()){ 42 | cout << lcs.top() << " "; 43 | lcs.pop(); 44 | } 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/luck balance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | int main() { 9 | long int n,k; 10 | cin >> n >> k; 11 | long int luck=0; 12 | vector L; 13 | for(int i=0 ;i>l >>t; 16 | if(t==0){ 17 | luck+=l; 18 | }else{ 19 | L.push_back(l); 20 | } 21 | } 22 | int size=L.size(); 23 | sort(L.begin(),L.end()); 24 | if(size>=k){ 25 | for(int i=size-1 ; i>=abs(size-k) ; i--){ 26 | luck+=L[i]; 27 | } 28 | } 29 | else{ 30 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool checkPresence(vector &vec,int p,int size){ 9 | bool check=false; 10 | for(int i=0 ; i> t; 22 | for(int i=0 ; i> n >> a >> b; 25 | vector differentNumber; 26 | int size=0; 27 | for(int j=0 ; j<=n-1 ; j++){ 28 | int c=(n-1-j)*a+j*b; 29 | if(!checkPresence(differentNumber,c,size)){ 30 | differentNumber.push_back(c); 31 | size++; 32 | } 33 | } 34 | sort(differentNumber.begin(),differentNumber.end()); 35 | for(int j=0 ; j 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int t; 10 | cin >> t; 11 | while(t--){ 12 | int n; 13 | cin >> n; 14 | int health[n]; 15 | long long int sum = 0; 16 | for(int i=0 ; i> health[i]; 18 | sum += health[i]; 19 | } 20 | long long max ; 21 | long long int ans = sum; 22 | sort(health,health+n); 23 | for(int i=1 ; i<=n ; i++){ 24 | sum -= health[i-1]; 25 | max = (long long)sum*(i+1); 26 | if(max > ans) ans = max; 27 | } 28 | cout << ans << "\n"; 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/mark and his toys using sorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int n; 8 | long k; 9 | cin >> n >> k; 10 | vector price(n); 11 | 12 | for(int i=0 ; i> price[i]; 14 | } 15 | sort(price.begin(),price.end()); 16 | int i=0,count=0; 17 | 18 | while(k>=price[i]){ 19 | k=k-price[i]; 20 | count++; 21 | i++; 22 | } 23 | cout << count; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/mark and his toys.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | long int minimum(vector a,int size){ 10 | long int min=INT_MAX; 11 | for(int i=0 ; i a,int size){ 19 | long int min=0; 20 | int indexOfMin=0; 21 | for(int i=0 ; imin){ 23 | min=a[i]; 24 | indexOfMin=i; 25 | } 26 | } 27 | return indexOfMin; 28 | } 29 | 30 | int main() 31 | { 32 | int n; 33 | long k; 34 | cin >> n >> k; 35 | vector prices(n); 36 | for(int i=0 ; i> prices[i]; 38 | } 39 | 40 | int count=0; 41 | 42 | while(k>=minimum(prices,prices.size())){ 43 | k=k-minimum(prices,prices.size()); 44 | cout << k <<" yo" << endl; 45 | count++; 46 | int a=indexOfMinimum(prices,prices.size()); 47 | prices.erase(prices.begin()+a,prices.begin()+a); 48 | } 49 | 50 | cout << count << endl; 51 | 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/maximum element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int main() { 12 | int n; 13 | stack s1,s2; 14 | cin >> n; 15 | int MAX = INT_MIN; 16 | while(n--){ 17 | int a; 18 | cin >> a; 19 | if(a == 1){ 20 | int b; 21 | cin >> b; 22 | MAX = max(MAX,b); 23 | s1.push(b); 24 | }else if(a == 2){ 25 | if(s1.top()!=MAX){ 26 | s1.pop(); 27 | }else{ 28 | s1.pop(); 29 | MAX = INT_MIN; 30 | while(!s1.empty()){ 31 | MAX = max(MAX,s1.top()); 32 | s2.push(s1.top()); 33 | s1.pop(); 34 | } 35 | while(!s2.empty()){ 36 | s1.push(s2.top()); 37 | s2.pop(); 38 | } 39 | } 40 | } 41 | else{ 42 | cout << MAX << "\n"; 43 | } 44 | } 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/maximum subarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int max_subarray_sum_kadane(vector &v,int &globalLeft,int &globalRight){ 5 | 6 | int lo = 0,hi = v.size(); 7 | int currentLeft = lo,currentRight = hi,currentSum = 0; 8 | int globalSum = INT_MIN; 9 | 10 | for(int i=lo ; i globalSum){ 13 | globalSum = currentSum; 14 | currentRight = i; 15 | globalLeft = currentLeft; 16 | globalRight = currentRight; 17 | } 18 | if(currentSum < 0){ 19 | currentSum = 0; 20 | currentLeft = currentRight = i+1; 21 | } 22 | } 23 | 24 | bool flag = false; 25 | for(int i=lo ; i 0 ){ 27 | flag = true; 28 | break; 29 | } 30 | } 31 | if(!flag){ 32 | globalSum = INT_MIN; 33 | for(int i=lo ; i> t; 46 | while(t--){ 47 | int n; // size of array/vector 48 | cin >> n; 49 | vector v(n); 50 | for(int i=0 ; i> v[i]; 51 | 52 | int globalLeft,globalRight; 53 | 54 | int globalSum = max_subarray_sum_kadane(v,globalLeft,globalRight); 55 | //int window = globalRight-globalLeft; 56 | cout << globalSum << " "; 57 | 58 | int disjointSum = 0; 59 | 60 | for(int i=0 ; i 0) disjointSum += v[i]; 62 | } 63 | if(disjointSum == 0) disjointSum = *max_element(v.begin(),v.end()); 64 | cout << disjointSum << "\n"; 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/minimum distances.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int num[n]; 8 | for(int i=0 ; i> num[i]; 10 | } 11 | int distance=n; 12 | int count=0; 13 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | long long int p,q; 11 | cin >>p >>q; 12 | int count=0; 13 | for(long long int i=p ; i<=q ; i++){ 14 | long int temp=i*i; 15 | long int j=i*i; 16 | int digits=0; 17 | long long int l=0,r=0; 18 | while(temp!=0){ 19 | temp/=10; 20 | digits++; 21 | } 22 | int leftHalf=digits/2; 23 | int rightHalf=digits-leftHalf; 24 | int leftDigit[leftHalf],rightDigit[rightHalf]; 25 | for(int k=0 ; k=0 ; k--){ 29 | r=10*r+rightDigit[k]; 30 | } 31 | for(int k=0 ; k=0 ; k--){ 35 | l=10*l+leftDigit[k]; 36 | } 37 | 38 | if(l+r==i){ 39 | count++; 40 | cout << i << " "; 41 | } 42 | 43 | } 44 | if(!count){ 45 | cout << "INVALID RANGE" ; 46 | } 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/new year chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int p=0 ; p> n; 11 | int num[n]; 12 | for(int i=0 ; i> num[i]; 13 | // insertion sort 14 | for(int i=n-2 ; i>=0 ; i--){ 15 | int key=num[i]; 16 | int j=i+1; 17 | swaps=0; 18 | while(j<=n-1 && num[j] 2){ 22 | flag=false; 23 | break; 24 | } 25 | } 26 | if(flag){ 27 | num[j-1]=key; 28 | ans+=swaps; 29 | } 30 | else break; 31 | } 32 | if(flag) cout << ans << "\n"; 33 | else cout << "Too chaotic\n"; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/nikita and the game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int total(int sum[],int low,int high){ 6 | if(high==low) return 0; 7 | int points = 0; 8 | int j=low-1; 9 | for(int i=low ; i<=high ; i++){ 10 | if( (((i==0)?sum[0]:sum[i]-sum[j])) == (sum[high]-sum[i]) ){ 11 | points++; 12 | points += max( total(sum,low,i) , total(sum,i+1,high) ); 13 | break; 14 | } 15 | } 16 | return points; 17 | } 18 | 19 | int main(){ 20 | int t; 21 | cin >> t; 22 | int n; 23 | while(t--){ 24 | cin >> n; 25 | int n1=n; 26 | int sum[n]; 27 | cin >> n ; 28 | sum[0] = n; 29 | for(int i=1 ; i> n; 31 | sum[i] = sum[i-1]+n; 32 | } 33 | int points = total(sum,0,n1-1); 34 | cout << points << "\n"; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/quicksort 1 partion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin >> n; 8 | int num[n]; 9 | for(int i=0 ; i> num[i]; 10 | int pivot=num[0]; 11 | vector left,right,equal; 12 | for(int i=0 ; ipivot) right.push_back(num[i]); 14 | else if(num[i] 2 | #include 3 | 4 | using namespace std; 5 | 6 | void partition(vector &arr){ 7 | int size=arr.size(); 8 | 9 | if(size<2){ 10 | return ; 11 | } 12 | 13 | vector left,equal,right; 14 | int pivot=arr[0]; 15 | for(int i=0 ; ipivot) right.push_back(arr[i]); 18 | else equal.push_back(arr[i]); 19 | } 20 | partition(left); 21 | partition(right); 22 | 23 | int leftSize=left.size(),rightSize=right.size(),equalSize=equal.size(); 24 | 25 | for(int i=0 ; i> n; 41 | 42 | vector arr(n); 43 | for(int i = 0; i < (int)n; ++i) { 44 | cin >> arr[i]; 45 | } 46 | 47 | partition(arr); 48 | 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/quicksort in-place.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap2numbers(int *a,int*b ){ 5 | int t=*a; 6 | *a=*b; 7 | *b=t; 8 | } 9 | 10 | int partition(int arr[],int firstIndex,int lastIndex,int sizeOfFullArray){ 11 | int pivot=arr[lastIndex]; 12 | int i=firstIndex-1; 13 | for(int j=firstIndex ; j> n; 38 | int num[n]; 39 | for(int i=0 ; i> num[i]; 40 | quickSort(num,0,n-1,n); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/running time of quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int qswaps=0; 9 | 10 | int insertionSortShifts(int arr[],int arrSize){ 11 | int counter=0; 12 | for(int j=1;j<=(arrSize-1);j++){ 13 | int key=arr[j]; 14 | int i=j-1; 15 | while(i>=0 && arr[i]>key){ 16 | arr[i+1]=arr[i]; 17 | i--; 18 | counter++; 19 | } 20 | arr[i+1]=key; 21 | } 22 | return counter; 23 | } 24 | 25 | void swap2numbers(int *a,int *b){ 26 | int t=*a; 27 | *a=*b; 28 | *b=t; 29 | } 30 | 31 | int partition(int arr[],int low,int high){ 32 | int pivot=arr[high]; 33 | int i=low-1; 34 | for(int j=low ; j> n; 58 | int inum[n]; 59 | int qnum[n]; 60 | for(int i=0 ; i> inum[i]; 62 | qnum[i]=inum[i]; 63 | } 64 | int ishifts=insertionSortShifts(inum,n); 65 | quickSort(qnum,0,n-1); 66 | cout << ishifts-qswaps; 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Online judge solutions/Hackerrank/sherlock and array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | for(int i=0 ; i> n; 10 | int num[n]; 11 | long long int sum[n]={0}; 12 | int count=0; 13 | //array input and storing sum of first i elements in i-1 index(ith storage container) 14 | cin >> num[0]; 15 | sum[0]=num[0]; 16 | for(int j=1 ; j> num[j] ; 18 | sum[j]=sum[j-1]+num[j] ; 19 | } 20 | if(n==1){ 21 | count++; 22 | }else{ 23 | for(int j=0 ; j 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | //01 hh //34 mm //67ss //89 AM/PM 9 | int main(){ 10 | char s[10]; 11 | for(int i=0 ; i<10 ; i++){ 12 | cin >> s[i]; 13 | } 14 | if(s[0]==1 && s[1]==2 && s[3]==0 && s[4]==0 && s[6]==0 && s[7]==0){ 15 | if(s[8]=='A'){ 16 | s[0]=0; 17 | s[1]=0; 18 | for(int i=0 ; i<=7 ; i++ ){ 19 | cout << s[i]; 20 | } 21 | 22 | }else{ 23 | for(int i=0 ; i<=7 ; i++ ){ 24 | cout << s[i]; 25 | } 26 | } 27 | }else if(s[0]=1 && s[1]==2){ 28 | if(s[8]=='A'){ 29 | s[0]=0; 30 | s[1]=0; 31 | for(int i=0 ; i<=7 ; i++ ){ 32 | cout << s[i]; 33 | } 34 | }else{ 35 | for(int i=0 ; i<=7 ; i++ ){ 36 | cout << s[i]; 37 | } 38 | } 39 | }else{ 40 | if(s[8]=='P'){ 41 | s[0]=1; 42 | s[1]=(s[1]+12)%10; 43 | for(int i=0 ; i<=7 ; i++ ){ 44 | cout << s[i]; 45 | } 46 | }else{ 47 | for(int i=0 ; i<=7 ; i++ ){ 48 | cout << s[i]; 49 | } 50 | } 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ABA12D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,a) for(ll i = (ll)0 ; i < ll(a) ; i++) 6 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 7 | #define all(a) a.begin(),a.end() 8 | 9 | vector squares, nums; 10 | #define MAX 1000001 11 | 12 | bool notPrime[MAX]; 13 | inline ll fme(ll x,ll n){ll ans=1; while(n>0){if(n&1){ ans*=x;} x*=x;n>>=1;}return ans;} 14 | void prime_sieve(){ for(ll i=2 ; i*i &mp){ 17 | int numbers = 0; 18 | ll i = 2; 19 | ll N = n; 20 | while( n > 1 && i*i <= N){ 21 | if( n%i == 0 ){ 22 | numbers++; 23 | if( numbers > 1 ) return false; 24 | int cnt = 0; 25 | while( n%i == 0 ){ 26 | cnt++; 27 | n /= i; 28 | } 29 | mp[i] = cnt; 30 | } 31 | i++; 32 | } 33 | 34 | return true; 35 | } 36 | 37 | void precompute(){ 38 | nums.push_back(2); 39 | for(int i = 2; i<= 10000 ; i++ ){ 40 | if( i*i < MAX ){ 41 | squares.push_back(i*i); 42 | }else{ 43 | break; 44 | } 45 | } 46 | ll len = squares.size(); 47 | lp(i, len){ 48 | map mp; 49 | if( factorize_brute(squares[i], mp) ){ 50 | map::iterator it = mp.begin(); 51 | ll p = (ll)it->first, n = (ll)it->second; 52 | ll sum = (fme(p,n+1) - 1)/(p-1); 53 | if( !notPrime[sum] ){ 54 | nums.push_back(squares[i]); 55 | } 56 | } 57 | } 58 | } 59 | 60 | int main(){ 61 | ios::sync_with_stdio(false); 62 | cin.tie(NULL); 63 | ll t,a,b; 64 | cin >> t; 65 | prime_sieve(); 66 | precompute(); 67 | while(t--){ 68 | cin >> a >> b; 69 | int ans = upper_bound(all(nums), b) - lower_bound(all(nums), a); 70 | cout << ans << "\n"; 71 | } 72 | } -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ABSYS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | ++t; 8 | getchar(); 9 | 10 | while(--t){ 11 | getchar(); 12 | int num1 = 0,num2 = 0 ,num3 = 0; 13 | bool m_gone = false; 14 | char ch; 15 | 16 | while(1){ 17 | ch = getchar(); 18 | if(ch!='m' && ch!=' '){ 19 | num1 = num1*10+ch-'0'; 20 | }else{ 21 | break; 22 | } 23 | } 24 | if(ch == 'm'){ 25 | while(getchar()!='+'){ 26 | 27 | } 28 | getchar(); 29 | cin >> num2; 30 | getchar();getchar();getchar(); 31 | cin >> num3; 32 | cout << num3-num2 << " + " << num2 << " = " << num3 << "\n"; 33 | getchar(); 34 | }else{ // first number complete 35 | getchar();getchar(); 36 | while(1){ 37 | ch = getchar(); 38 | if(ch!='m' && ch!=' '){ 39 | num2 = num2*10+ch-'0'; 40 | }else{ 41 | break; 42 | } 43 | } 44 | if(ch=='m'){ 45 | while(getchar()!='='){ 46 | 47 | } 48 | getchar(); 49 | cin >> num3; 50 | cout << num1 << " + " << num3-num1 << " = " << num3 << "\n"; 51 | getchar(); 52 | }else{ // 2nd number complete 53 | cout << num1 << " + " << num2 << " = " << num1 + num2 << "\n"; 54 | while(getchar()!='\n'){ 55 | 56 | } 57 | } 58 | } 59 | } 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ACODE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define loop(i,a,b) for(ll i = ll(a); i> s; 16 | ll len =(ll) s.size(); 17 | if(s[0] == '0') break; 18 | ll dp[len+1]; 19 | dp[0] = 1; 20 | dp[1] = 1; 21 | loop(i,1,len){ 22 | if(s[i] == '0' || s[i-1] == '0' || (i!=len-1 && s[i+1]=='0') || isSingle(s[i-1],s[i]))dp[i+1] = dp[i]; 23 | else dp[i+1] = dp[i]+dp[i-1]; 24 | } 25 | cout << dp[len] << "\n"; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ACPC10A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a,b,c; 6 | while(1){ 7 | cin >> a >> b >> c; 8 | if(a or b or c){ 9 | 10 | if(b==(a+c)/2.0) cout << "AP " << 2*c-b << endl; 11 | 12 | else if(b*b==a*c) cout << "GP " << c*(float(c))/b << endl ; 13 | 14 | }else{ 15 | break; 16 | } 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ADAINDEX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define SIZE 26 5 | 6 | typedef long long ll; 7 | 8 | struct node{ 9 | node *child[SIZE]; 10 | ll words; 11 | ll prefixes; 12 | }; 13 | 14 | node* newNode(){ 15 | node *a = new node; 16 | a->words = a->prefixes = 0; 17 | for(ll i=0 ; ichild[i] = NULL; 18 | return a; 19 | } 20 | 21 | void insert( node *a, string &s){ 22 | node *ptr = a; 23 | ll sz = s.size(); 24 | for(ll i=0 ; ichild[idx] == NULL){ 27 | ptr->child[idx] = newNode(); 28 | } 29 | ptr = ptr->child[idx]; 30 | ptr->prefixes++; 31 | } 32 | ptr->words++; 33 | } 34 | 35 | ll countPrefixes( node *a, string &s){ 36 | node *ptr = a; 37 | ll sz = s.size(); 38 | for(ll i=0 ; ichild[idx] == NULL) 41 | return 0; 42 | ptr = ptr->child[idx]; 43 | } 44 | if(ptr != NULL && ptr->prefixes) return ptr->prefixes; 45 | return 0; 46 | } 47 | 48 | int main(){ 49 | ll n,q; 50 | cin >> n >> q; 51 | node *root = newNode(); 52 | string s[n]; 53 | for(ll i=0 ; i> s[i]; 55 | insert( root, s[i]); 56 | } 57 | 58 | while(q--){ 59 | string ss; 60 | cin >> ss; 61 | ll res = countPrefixes( root, ss); 62 | 63 | cout << res << "\n"; 64 | 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ADDREV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | for(int i=0 ; i> a >> b ; 11 | while(a!=0){ 12 | rev_a=rev_a*10 + a%10; 13 | a/=10; 14 | } 15 | while(b!=0){ 16 | rev_b=rev_b*10 + b%10; 17 | b/=10; 18 | } 19 | int sumOfRev=rev_a+rev_b; 20 | int sum=0; 21 | while(sumOfRev!=0){ 22 | sum=sum*10+sumOfRev%10; 23 | sumOfRev/=10; 24 | } 25 | cout << sum << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/AIBOHP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lcs(string &s){ 5 | string t; 6 | t.resize(s.length()); 7 | reverse_copy(s.begin(),s.end(),t.begin()); 8 | //cout << s << endl << t << endl; 9 | int dp[s.length()+1][t.length()+1]; 10 | int n = s.length(); 11 | for(int i=0 ; i<=n ; i++){ 12 | dp[0][i] = 0; 13 | dp[i][0] = 0; 14 | } 15 | for(int i=1 ; i<=n ; i++){ 16 | for(int j=1 ; j<=n ; j++){ 17 | if(s[i-1]==t[j-1]) dp[i][j] = dp[i-1][j-1]+1; 18 | else dp[i][j] = max(dp[i][j-1],dp[i-1][j]); 19 | } 20 | } 21 | //cout << "dp " << dp[n][n] << endl; 22 | return dp[n][n]; 23 | 24 | } 25 | 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while(t--){ 30 | string s; 31 | cin >> s; 32 | cout << s.length() - lcs(s) << "\n"; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ALICESIE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX 1e6 5 | #define MOD 1e7 6 | 7 | typedef double lf; 8 | typedef long long ll; 9 | typedef long double LF; 10 | typedef unsigned long long ull; 11 | typedef set sll; 12 | typedef vector vll; 13 | typedef map msl; 14 | typedef pair ii; 15 | typedef vector vpll; 16 | typedef map mll; 17 | typedef vector vvll; 18 | typedef list lll; 19 | typedef vector vlll; 20 | typedef pair iii; 21 | 22 | #define rtn return 23 | #define gc getchar 24 | #define pb push_back 25 | #define f first 26 | #define s second 27 | #define mp(x,y) make_pair(x,y) 28 | #define sz size() 29 | #define all(a) a.begin(),a.end() 30 | #define allr(a) a.rbegin(),a.rend() 31 | #define lp(i,a,b) for(int i = int(a); i= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 40 | if(isNeg) x *= -1; return x; 41 | } 42 | 43 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 44 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 45 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 46 | inline bool isPalin(string &s){ int len = s.sz-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 47 | inline ll lcm(ll a,ll b){rtn (a*b)%gcd(a,b); } 48 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 49 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 50 | 51 | // ncr mod p 52 | // inverse modulo 53 | // fermat 54 | // lucas/wilson 55 | // smallest prime factor sieve 56 | 57 | int main(){ 58 | ios::sync_with_stdio(false); 59 | ll t,n,k,m,maxx = 0,minn = 0,beg = 0,end = 0,lo = 0,hi = 0,l = 0, r = 0,count = 0,sum = 0,pos = 0; 60 | // cin.tie(0); 61 | /*------- template ends here ------*/ 62 | cin >> t; 63 | while(t--){ 64 | cin >> n; 65 | cout << ceil(n/2.0) << "\n"; 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/AMR10G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define gc getchar 5 | 6 | int ip(){ 7 | int x = 0; bool isNeg = false; char c; 8 | c = gc(); 9 | if(c == '-') isNeg = true , c = gc(); 10 | for( ; c >= '0' && c <= '9' ; c = gc() ) x = (x << 1) + (x << 3) + c - '0'; 11 | if(isNeg) x *= -1; return x; 12 | } 13 | 14 | int main(){ 15 | int n,k; 16 | //ios::sync_with_stdio(false); 17 | //cin.tie(0); 18 | int t; 19 | cin >> t; 20 | while(t--){ 21 | n = ip() , k = ip(); 22 | vector v(n); 23 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | bool isPalin(string &s){ 5 | int len = s.size()-1; 6 | for(int i=0 ; i<=len/2 ; i++){ 7 | if(!(s[i]==s[len-i])) return false; 8 | } 9 | return true; 10 | } 11 | 12 | int main() { 13 | ios::sync_with_stdio(false); 14 | int t; 15 | cin >> t; 16 | string s; 17 | while(t--){ 18 | cin >> s; 19 | if(isPalin(s)) cout << "YES\n"; 20 | else cout << "NO\n"; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ANARC09A.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long ll; 12 | typedef long double ld; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair pii; 18 | typedef pair ii; 19 | typedef vector vpll; 20 | typedef map mll; 21 | typedef vector vvll; 22 | typedef list lll; 23 | typedef vector vlll; 24 | typedef pair iii; 25 | 26 | #define rtn return 27 | #define gc getchar 28 | #define pb push_back 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define all(a) a.begin(),a.end() 33 | #define allr(a) a.rbegin(),a.rend() 34 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 35 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 36 | #define mid(s,e) (s+(e-s)/2) 37 | 38 | ll ip(){ 39 | ll x = 0; bool isNeg = false; char c; 40 | c = gc(); 41 | if(c == '-') isNeg = true , c = gc(); 42 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 43 | if(isNeg) x *= -1; return x; 44 | } 45 | 46 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 47 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 48 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 49 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 50 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 51 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 52 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 53 | inline int in(){ int NR=0; register char c=gc(); while( c < 48 || c > 57 ){c=gc();} 54 | while(c>47 && c< 58){ NR = (NR << 3) + (NR << 1) + (c - 48); c=gc();} return NR; 55 | } 56 | 57 | ll dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}; 58 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 59 | 60 | #define MOD 1000000007 61 | #define N 100010 62 | 63 | int main(){ 64 | ios::sync_with_stdio(false); 65 | //cin.tie(NULL); 66 | ll t,n,m,e,a,b,q,w,k; 67 | string s; 68 | t = 1; 69 | //ll p,q,curr,c; 70 | ///////////// START FROM HERE //////////////////// 71 | while(1){ 72 | cin >> s; 73 | if(s[0]=='-') break; 74 | ll len = s.length(); 75 | ll ans = 0, open = 0; 76 | lp(i,0,len-1){ 77 | if(s[i]=='}'){ 78 | if(open > 0){ 79 | open--; 80 | }else{ 81 | ans++; 82 | open++; 83 | } 84 | }else{ 85 | open++; 86 | } 87 | } 88 | cout << t << ". " << ans + open/2 << "\n"; 89 | t++; 90 | } 91 | 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/AP2-AP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin >> t; 6 | for(int i=0 ; i> t3 >> t3rdLast >> sum; 9 | long long n=(2*sum)/(t3+t3rdLast); 10 | long long d=(t3rdLast-t3)/(n-5); 11 | cout << n <<"\n"; 12 | long long a=t3-2*d; 13 | for(int j=0 ; j 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i=0 ; i> ng >> nm ; 11 | int max_ng=0,max_nm=0 ; 12 | int ng_strength,nm_strength ; 13 | for(int j=0 ; j> ng_strength ; 15 | if(ng_strength > max_ng) max_ng = ng_strength ; 16 | } 17 | for(int j=0 ; j> nm_strength ; 19 | if(nm_strength > max_nm) max_nm = nm_strength ; 20 | } 21 | if(max_ng>=max_nm) cout << "Godzilla\n"; 22 | else cout << "MechaGodzilla\n"; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ATOMS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | //cin.tie(NULL); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | double n,k,m; 11 | long long ans = 0; 12 | cin >> n >> k >> m; 13 | if(n >= m) cout << 0; 14 | else{ 15 | while(k*n > n && k*n <= m) 16 | ++ans , n = k*n; 17 | cout << ans; 18 | } 19 | cout << "\n"; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/BAT3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lp(i,a) for(int i = (int)0 ; i < int(a) ; i++) 5 | 6 | int dp[1005], arr[1005]; 7 | 8 | int main(){ 9 | int t, n, m; 10 | scanf("%d",&t); 11 | while(t--){ 12 | scanf("%d%d",&n,&m); 13 | lp(i,n) scanf("%d",&arr[i]), dp[i] = 1; 14 | dp[0] = 1; 15 | for(int i = 1 ; i < n ; i++){ 16 | for(int j = 0 ; j < i ; j++){ 17 | if( arr[j] > arr[i] || j == m ) 18 | dp[i] = max( dp[i], dp[j] + 1); 19 | } 20 | } 21 | 22 | int maxx = 1; 23 | lp(i,n) 24 | maxx = max( maxx, dp[i]); 25 | printf("%d\n", maxx); 26 | } 27 | } -------------------------------------------------------------------------------- /Online judge solutions/Spoj/BEENUMS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 1000000000 4 | using namespace std; 5 | typedef long long ll; 6 | 7 | int main() { 8 | ll i=1 ; 9 | set s; 10 | ll t=1; 11 | while(t <= MAX){ 12 | s.insert(t); 13 | i++; 14 | t = 3*i*i - 3*i + 1; 15 | } 16 | // your code goes here 17 | ll n; 18 | while(1){ 19 | scanf("%lld",&n); 20 | if(n==-1) break; 21 | else{ 22 | if(s.find(n)!=s.end()){ 23 | cout << "Y\n"; 24 | }else{ 25 | cout <<"N\n"; 26 | } 27 | } 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/BLACKOUT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | ll sum[2005][2005], arr[2005][2005], dp[1005][1005]; 6 | 7 | int main(){ 8 | //ios::sync_with_stdio(false); 9 | //cin.tie(NULL); 10 | ll n,m,q,k; // k -> knapsack capacity 11 | cin >> n >> m >> q >> k; 12 | for(int j = 1 ; j<=m ; j++){ 13 | cin >> arr[1][j]; 14 | sum[1][j] = sum[1][j-1] + arr[1][j]; 15 | } 16 | for(int i = 2 ; i<=n ; i++){ 17 | ll a = 0; 18 | for(int j = 1 ; j<=m ; j++){ 19 | cin >> arr[i][j]; 20 | a += arr[i][j]; 21 | sum[i][j] = sum[i-1][j] + a; 22 | } 23 | } 24 | 25 | ll val[q+1], wt[q+1]; 26 | int x1,x2,y1,y2; 27 | for(int i = 1 ; i<=q ; i++){ 28 | cin >> x1 >> y1 >> x2 >> y2; 29 | val[i] = (x2-x1+1)*(y2-y1+1); 30 | wt[i] = sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1]; 31 | } 32 | 33 | for(int i = 1; i<=q ; i++){ 34 | for(int j = 1 ; j<=k ; j++){ 35 | if( wt[i] > j ) dp[i][j] = dp[i-1][j]; 36 | else dp[i][j] = max(dp[i-1][j], dp[i-1][j-wt[i]] + val[i]); 37 | } 38 | } 39 | 40 | cout << dp[q][k] << "\n"; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Online judge solutions/Spoj/BUBBLESORT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MOD 10000007 4 | using namespace std; 5 | 6 | long long inversions=0; 7 | 8 | void merge(long long arr[],long long low,long long mid,long long high){ 9 | int n1=mid-low+1; 10 | long long n2=high-mid; 11 | long long left[n1+1],right[n2+1]; 12 | for(int i=0 ; i right[j]){ 19 | arr[k]=right[j]; 20 | j++; 21 | }else{ 22 | arr[k]=left[i]; 23 | i++; 24 | inversions+=j; 25 | } 26 | } 27 | } 28 | 29 | void mergeSort(long long arr[],long long low,long long high){ 30 | if( low> t; 41 | cout <<"\n"; 42 | for(int j=1 ; j<=t ; j++){ 43 | int n; 44 | cin >> n; 45 | long long num[n]; 46 | for(int i=0 ; i> num[i]; 47 | mergeSort(num,0,n-1); 48 | cout << "Case " << j << ":" << inversions%MOD <<"\n" ; 49 | inversions=0; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/BUGLIFE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | 7 | bool flag; 8 | 9 | void dfs(list g[],int v,int mark[],int col[],int prevCol){ 10 | mark[v] = true; 11 | //cout << v << "marked true \n"; 12 | int currCol; 13 | if(prevCol==1) currCol = -1; 14 | else currCol = 1; 15 | 16 | list::iterator it; 17 | for( it=g[v].begin() ; it!=g[v].end() ; it++){ 18 | if(!mark[*it]){ 19 | col[*it] = currCol; 20 | dfs(g,*it,mark,col,currCol); 21 | }else{ 22 | if(col[*it]==prevCol){ 23 | flag = false; 24 | return; 25 | } 26 | } 27 | if(!flag) return; 28 | } 29 | } 30 | 31 | int main() { 32 | ios::sync_with_stdio(false); 33 | //cin.tie(NULL); 34 | int t; 35 | cin >> t; 36 | for(int j=1 ; j<=t ; j++){ 37 | flag = true; 38 | int v,e,a,b; 39 | cin >> v >> e; 40 | int mark[v],col[v]; 41 | memset(mark,0,sizeof(mark)); 42 | memset(col,0,sizeof(col)); 43 | list g[v]; 44 | for(int i=0; i> a >> b; 46 | g[a-1].pb(b-1); 47 | g[b-1].pb(a-1); 48 | } 49 | col[0] = 1; 50 | for(int i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | typedef double lf; 8 | typedef long long ll; 9 | typedef long double LF; 10 | typedef unsigned long long ull; 11 | using namespace std; 12 | 13 | int ans(int h,int w,int *s,int *sum,int i,int j){ 14 | if(i<0 || i>=h || j<0 || j>=w) return 0; 15 | if( *(sum+i*w+j) ) return *(sum+i*w+j); 16 | *(sum+i*w+j) = max( ans(h,w,s,sum,i-1,j) , max( ans(h,w,s,sum,i-1,j-1) , ans(h,w,s,sum,i-1,j+1) ) ) + *(s+i*w+j); 17 | return *(sum+i*w+j); 18 | } 19 | 20 | int main(){ 21 | int t; 22 | cin >> t; 23 | while(t--){ 24 | int h,w; 25 | cin >> h >> w; 26 | int s[h][w],sum[h][w]; 27 | memset(sum,0,h*w*sizeof(int)); 28 | for(int i=0 ; i> s[i][j]; 31 | } 32 | } 33 | int maximum = INT_MIN; 34 | for(int j=w-1 ; j>=0 ; j--){ 35 | maximum=max(maximum,ans(h,w,(int*)s,(int*)sum,h-1,j)); 36 | } 37 | cout << maximum << "\n"; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/CADYDIST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | long long n; 8 | while(1){ 9 | cin >> n; 10 | if(n==0) return 0; 11 | vector strength(n),price(n); 12 | for(int i=0 ; i> strength[i]; 13 | for(int i=0 ; i> price[i]; 14 | 15 | sort( strength.begin() , strength.end() ) ; 16 | sort( price.begin() , price.end() ) ; 17 | 18 | reverse( price.begin() , price.end() ); 19 | 20 | long long ans=0; 21 | 22 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | while(1){ 7 | cin >> n; 8 | if(n!=-1){ 9 | int num[n]; 10 | int sum=0; 11 | for(int i=0 ; i> num[i]; 13 | sum+=num[i]; 14 | } 15 | if(sum%n!=0){ 16 | cout << -1 << endl ; 17 | }else{ 18 | int candyPerPacket=sum/n,moves=0; 19 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i=1 ; i<=t ; i++){ 8 | long n; 9 | cin >> n; 10 | long start=1; 11 | char starter='x'; 12 | if(n==1){ 13 | cout << "TERM 1 IS 1/1" << endl; 14 | continue; 15 | } 16 | for(long j=2 ; 1 ; j++){ 17 | long end=start + j; 18 | 19 | if(starter=='x') starter='y'; 20 | else starter='x'; 21 | 22 | if( n>start && n<=end ){ 23 | long permanentN=n; 24 | n=n-start; 25 | long ans_x=0,ans_y=0; 26 | if(starter=='x'){ 27 | ans_x=(j-n+1); 28 | ans_y=n; 29 | }else{ 30 | ans_x=n; 31 | ans_y=j-n+1; 32 | } 33 | 34 | cout << "TERM " << permanentN << " IS " << ans_x << "/" << ans_y << endl; 35 | break; 36 | 37 | } 38 | 39 | start=end; 40 | } 41 | } 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/CEQU.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | typedef double lf; 12 | typedef long long ll; 13 | typedef long double LF; 14 | typedef unsigned long long ull; 15 | typedef set sll; 16 | typedef vector vll; 17 | typedef map msl; 18 | typedef pair pll; 19 | typedef vector vpll; 20 | typedef map mll; 21 | 22 | #define loop(i,a,b) for(int i = int(a); i<=int(b) ; i++) 23 | #define itvll(c,it) for(vll::iterator it = c.begin() ; it!=c.end() ; it++) 24 | #define itmll(c,it) for(mll::iterator it = c.begin() ; it!=c.end() ; it++) 25 | #define itmsl(c,it) for(msl::iterator it = c.begin() ; it!=c.end() ; it++) 26 | 27 | ll ncr(int n,int r){ ll ans = 1 ; if(r > n-r) r = n-r; for(int i=0 ; i> t; 33 | for(ll i=1 ; i<=t ; ++i){ 34 | cin >> a >> b >> c; 35 | cout << "Case " << i << ": "; 36 | if(c%gcd(a,b)) cout << "No\n"; 37 | else cout << "Yes\n"; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/CIRCLE_E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin >> t; 8 | while(t--){ 9 | double r1,r2,r3; 10 | cin >> r1 >> r2 >> r3 ; 11 | double a = r1*r2 + r2*r3 + r3*r1; 12 | double b = r1*r2*r3; 13 | double c = r1+r2+r3; 14 | printf("%0.6f\n",(b / (a + (2 * sqrt(b*c) ) ) ) ); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/CNTDO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int t,n; 8 | cin >> t; 9 | while(t--){ 10 | cin >> n; 11 | int a; 12 | map m; 13 | for(int i=0 ; i> a; 15 | if(m.find(a)!=m.end()){ 16 | m[a]++; 17 | }else{ 18 | m[a] = 1; 19 | } 20 | } 21 | int ans = 0; 22 | for(auto &it : m){ 23 | if(m.find(2*it.first)!=m.end()) 24 | ans += it.second; 25 | } 26 | cout << ans << "\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/CODESPTB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long inversions=0; 6 | 7 | void merge(int arr[],int low,int mid,int high){ 8 | int n1=mid-low+1; 9 | int n2=high-mid; 10 | int left[n1+1],right[n2+1]; 11 | for(int i=0 ; i right[j]){ 18 | arr[k]=right[j]; 19 | j++; 20 | }else{ 21 | arr[k]=left[i]; 22 | i++; 23 | inversions+=j; 24 | } 25 | } 26 | } 27 | 28 | void mergeSort(int arr[],int low,int high){ 29 | if( low> t; 40 | for(int j=0 ; j> n; 43 | int num[n]; 44 | for(int i=0 ; i> num[i]; 45 | mergeSort(num,0,n-1); 46 | cout << inversions <<"\n" ; 47 | inversions=0; 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/COINS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | map maxCoins; 8 | 9 | 10 | ll coins(ll n){ 11 | if(maxCoins.find(n)!=maxCoins.end()){ 12 | return maxCoins[n]; 13 | }else{ 14 | maxCoins[n] = max(coins(n/2)+coins(n/3)+coins(n/4),n); 15 | } 16 | return maxCoins[n]; 17 | } 18 | 19 | int main(){ 20 | ll n; 21 | while(cin >> n){ 22 | maxCoins[0] = 0; 23 | cout << coins(n) << "\n"; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/COMDIV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a,int b){if(!a) return b; return gcd(b%a,a); } 5 | #define gc getchar 6 | int ip(){ 7 | int x = 0; bool isNeg = false; char c; 8 | c = gc(); 9 | if(c == '-') isNeg = true , c = gc(); 10 | for( ; c >= '0' && c <= '9' ; c = gc() ) x = (x << 1) + (x << 3) + c - '0'; 11 | if(isNeg) x *= -1; return x; 12 | } 13 | 14 | int main(){ 15 | //ios::sync_with_stdio(false) 16 | int t,a,b,h,ans,i; 17 | t = ip(); 18 | while(t--){ 19 | a = ip() ; b = ip(); 20 | h = gcd(a,b); 21 | ans = 0; 22 | for( i=1 ; i*i<=h ; i++){ 23 | if(h%i==0){ 24 | ans += 2; 25 | } 26 | } 27 | i--; 28 | if(i*i==h) ans--; 29 | cout << ans << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/CPRIME.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | #define MAX 100000000 8 | bitset isPrime; 9 | int arr[MAX+1]; 10 | void sieve(){ 11 | int root = sqrt(MAX); 12 | for(int i=2 ; i<=root ; ++i){ 13 | if(!isPrime[i]){ 14 | for(int j=i*i ; j<=MAX ; j+=i ) 15 | isPrime[j] = 1; 16 | } 17 | } 18 | arr[0] = 0; 19 | arr[1] = 0; 20 | arr[2] = 1; 21 | for(int i=3 ; i<=MAX ; i++){ 22 | if(isPrime[i] == 0) 23 | arr[i] = arr[i-1]+1; 24 | else 25 | arr[i] = arr[i-1]; 26 | } 27 | } 28 | 29 | int main(){ 30 | sieve(); 31 | int n; 32 | while(1){ 33 | scanf("%d",&n); 34 | if(!n) break; 35 | //float pix = (upper_bound(arr.begin() , arr.end() , n) - arr.begin()+1); 36 | printf("%.1f\n",100*abs((arr[n] - (float)n/log(n) ))/(float)arr[n] ); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/DICT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ALPHABETSIZE 26 5 | 6 | bool flag = false; // to check if any answer exists or not 7 | 8 | struct Trie{ 9 | struct node{ 10 | char data; 11 | bool word; 12 | node *child[ALPHABETSIZE]; 13 | }; 14 | 15 | public: 16 | node *root; 17 | Trie(); // constructor 18 | void insert(string &str); 19 | void init( node *ptr); 20 | void query(string &prefix); 21 | void recur(node *n,string &s); 22 | }; 23 | 24 | Trie::Trie(){ 25 | root = new node(); 26 | init(root); 27 | } 28 | 29 | void Trie::init(node *ptr){ 30 | ptr->word = false; 31 | for(int i = 0 ; i<26 ; i++) 32 | ptr->child[i] = NULL; 33 | } 34 | 35 | void Trie::insert(string &str){ 36 | node *ptr = root; 37 | int sz = str.size(); 38 | for(int i = 0 ; ichild[idx] == NULL ){ 41 | ptr->child[idx] = new node; 42 | init(ptr->child[idx]); 43 | ptr->child[idx]->data = str[i]; 44 | } 45 | ptr = ptr->child[idx]; 46 | } 47 | 48 | ptr->word = true; 49 | 50 | } 51 | 52 | 53 | 54 | void Trie::query(string &prefix){ 55 | node *ptr = root; 56 | int sz = prefix.size(); 57 | for(int i = 0 ; ichild[idx] == NULL) 60 | return; 61 | ptr = ptr->child[idx]; 62 | } 63 | 64 | recur(ptr,prefix); 65 | } 66 | 67 | void Trie::recur(node *n, string &prefix){ 68 | node *ptr = n; 69 | for(int i = 0; i<26 ; i++){ 70 | if( ptr->child[i] != NULL ){ 71 | string curr = prefix; 72 | curr += ptr->child[i]->data; 73 | if( ptr->child[i]->word ){ 74 | cout << curr << "\n"; 75 | flag = true; 76 | } 77 | 78 | recur( ptr->child[i], curr); 79 | } 80 | } 81 | } 82 | 83 | 84 | int main(){ 85 | int n,t; 86 | cin >> n; 87 | Trie T; 88 | for(int i = 0 ; i> str; 91 | T.insert(str); 92 | } 93 | 94 | cin >> t; 95 | for(int i = 1 ; i <= t ; i++){ 96 | string prefix; 97 | cin >> prefix; 98 | 99 | cout << "Case #" << i << ":\n"; 100 | T.query(prefix); 101 | 102 | if( !flag ) 103 | cout << "No match.\n"; 104 | else 105 | flag = false; 106 | } 107 | 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/DQUERY.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define gc getchar 11 | #define all(a) a.begin(),a.end() 12 | #define lp(i,a,b) for(int i = int(a); i<=int(b) ; i++) 13 | 14 | 15 | int ip(){ 16 | int x = 0; bool isNeg = false; char c; 17 | c = gc(); 18 | if(c == '-') isNeg = true , c = gc(); 19 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 20 | if(isNeg) x *= -1; return x; 21 | } 22 | 23 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 24 | 25 | #define MOD 1000000007 26 | #define N 100010 27 | #define MAX 1000005 28 | int block; 29 | int cnt[MAX], arr[N]; 30 | 31 | int ans; 32 | 33 | struct query{ 34 | int l,r,idx; 35 | }; 36 | 37 | bool cmp(query a, query b){ 38 | if(a.l/block != b.l/block) return (a.l/block < b.l/block); 39 | return (a.r < b.r); 40 | } 41 | 42 | void add(int idx){ 43 | cnt[arr[idx]]++; 44 | if(cnt[arr[idx]]==1) ++ans; 45 | } 46 | 47 | void rem(int idx){ 48 | cnt[arr[idx]]--; 49 | if(!cnt[arr[idx]]) --ans; 50 | } 51 | 52 | int main(){ 53 | ios::sync_with_stdio(false); 54 | cin.tie(NULL); 55 | int t,n,m,e,a,b,w,k; 56 | //ll p,q,curr,c; 57 | ///////////// START FROM HERE //////////////////// 58 | 59 | cin >> n; 60 | block = sqrt(n); 61 | 62 | lp(i,0,n-1) cin >> arr[i]; 63 | 64 | int que; 65 | cin >> que; 66 | 67 | query q[que]; 68 | int res[que]; 69 | 70 | lp(i,0,que-1){ 71 | cin >> a >> b; 72 | a--; b--; 73 | q[i].l = a; q[i].r = b; 74 | q[i].idx = i; 75 | } 76 | 77 | sort(q,q+que, cmp); 78 | 79 | int cl = 0, cr = 0; 80 | lp(i,0,que-1){ 81 | int l = q[i].l, r = q[i].r; 82 | while(cl > l){ 83 | add(cl-1); 84 | cl--; 85 | } 86 | 87 | while(cl < l){ 88 | rem(cl); 89 | cl++; 90 | } 91 | 92 | while(cr <= r){ 93 | add(cr); 94 | cr++; 95 | } 96 | 97 | while(cr > r+1){ 98 | rem(cr-1); 99 | cr--; 100 | } 101 | 102 | res[q[i].idx] = ans; 103 | } 104 | 105 | lp(i,0,que-1){ 106 | cout << res[i] << "\n"; 107 | } 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/DSUBSEQ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | const ll mod = 1000000007; 7 | 8 | int main(){ 9 | ll t; 10 | cin >> t; 11 | while( t-- ){ 12 | string str; 13 | cin >> str; 14 | ll n = str.size(); 15 | ll dp[n + 1]; 16 | vector ext(26); 17 | 18 | dp[0] = 1; 19 | 20 | for(ll i = 1 ; i <= n ; i++){ 21 | ll pos = str[i - 1] - 'A'; 22 | dp[i] = (2*dp[i - 1])%mod ; 23 | dp[i] = (dp[i] - ext[pos] + mod)%mod; 24 | ext[pos] = dp[i-1]; 25 | } 26 | 27 | cout << dp[n] << "\n"; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Online judge solutions/Spoj/EDIST top down.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string s1,s2; 5 | 6 | int edit(vector > &v,int a,int b){ 7 | if( a == 0 || b == 0 ) return v[a][b]; 8 | if(v[a][b] != -1) return v[a][b]; 9 | if(s1[a-1]==s2[b-1]) v[a][b] = edit(v,a-1,b-1); 10 | else v[a][b] = min(min(edit(v,a-1,b) +1,edit(v,a,b-1)+1),edit(v,a-1,b-1)+1); 11 | return v[a][b]; 12 | } 13 | 14 | int main(){ 15 | int t; 16 | cin >> t; 17 | while(t--){ 18 | cin >> s1 >> s2; 19 | int a = s1.size(), b = s2.size(); 20 | if(a > b){ 21 | string temp = s2; 22 | s2 = s1; 23 | s1 = temp; 24 | a = s1.length() ; 25 | b = s2.length() ; 26 | } 27 | vector > v(a+1,vector (b+1,-1)); 28 | for(int i=0 ; i<=a ; i++) v[i][0] = i; 29 | for(int i=0 ; i<=b ; i++) v[0][i] = i; 30 | cout << edit(v,a,b) << "\n"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/EDIST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | string str1,str2; 7 | cin >> t; 8 | while(t--){ 9 | cin >> str1 >> str2; 10 | int a = str1.size(), b = str2.size(); 11 | if(a > b){ 12 | string temp = str2; 13 | str2 = str1; 14 | str1 = temp; 15 | a = str1.length() ; 16 | b = str2.length() ; 17 | } 18 | int dp[a+1][b+1]; 19 | for(int i=0 ; i<=a ; i++) dp[i][0] = i; 20 | for(int j=0 ; j<=b ; j++) dp[0][j] = j; 21 | 22 | for(int i=1 ; i<=a ; i++){ 23 | for(int j=1 ; j<=b ; j++){ 24 | if(str1[i-1]==str2[j-1]) dp[i][j] = dp[i-1][j-1]; 25 | else{ 26 | dp[i][j] = min(dp[i-1][j-1]+1,min(dp[i-1][j]+1,dp[i][j-1]+1)); 27 | } 28 | } 29 | } 30 | cout << dp[a][b] << "\n"; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/EDIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | char s[1001]; 7 | while(scanf("%s",s)!=EOF){ 8 | int i=0; 9 | int ans_low_case_start = 0,ans_upp_case_start = 0; 10 | while(s[i]!='\0'){ // low is first char 11 | if(i%2){ 12 | if(!(s[i]>='A' && s[i]<='Z')) ans_low_case_start++; 13 | }else{ 14 | if(!(s[i]>='a' && s[i]<='z')) ans_low_case_start++; 15 | } 16 | ++i; 17 | } 18 | i=0; 19 | while(s[i]!='\0'){ // upp is first char 20 | if(i%2){ 21 | if(!(s[i]>='a' && s[i]<='z')) ans_upp_case_start++; 22 | }else{ 23 | if(!(s[i]>='A' && s[i]<='Z')) ans_upp_case_start++; 24 | } 25 | ++i; 26 | } 27 | printf("%d\n",min(ans_low_case_start,ans_upp_case_start)); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/EIGHT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i=0 ; i> k; 10 | long long int j=1; 11 | int arrForKLessThan5[4]={192,442,692,942}; 12 | int arrForKGreaterThan4[4]={942,192,442,692}; 13 | if(k<=4){ 14 | cout << arrForKLessThan5[k-1] << endl; 15 | }else{ 16 | long long j=(k%4)?(k/4):(k/4-1); 17 | cout << j << arrForKGreaterThan4[k%4] << endl; 18 | } 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ETF using prime sieve and totient sieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define MAX 1000000 5 | int phi[MAX+1]; 6 | bool isPrime[MAX+1]; 7 | 8 | void sieve(){ 9 | int root = sqrt(MAX); 10 | for(int i=2 ; i<=root ; i++){ 11 | if(!isPrime[i]){ 12 | for(int j=i*2 ; j<=MAX ; j+=i){ 13 | isPrime[j] = true; 14 | } 15 | } 16 | } 17 | } 18 | 19 | void etf(){ 20 | for(int i=1 ; i<=MAX ; i++) phi[i] = i; 21 | for(int i=2 ; i<=MAX ; i++){ 22 | if(!isPrime[i]){ 23 | for(int j=i ; j<=MAX ; j+=i){ 24 | phi[j] -= phi[j]/i; 25 | } 26 | } 27 | } 28 | } 29 | 30 | int main() { 31 | sieve(); 32 | etf(); 33 | int t; 34 | cin >> t; 35 | while(t--){ 36 | int n; 37 | cin >> n; 38 | cout << phi[n] << "\n"; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ETF using totient sieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 1000000 4 | int phi[MAX+1]; 5 | 6 | void etf(){ 7 | for(int i=1 ; i<=MAX ; i++) phi[i] = i; 8 | for(int i=2 ; i<=MAX ; i++){ 9 | if(phi[i]==i){ 10 | for(int j=i ; j<=MAX ; j+=i){ 11 | phi[j] -= phi[j]/i; 12 | } 13 | } 14 | } 15 | } 16 | 17 | int main() { 18 | etf(); 19 | int t; 20 | cin >> t; 21 | while(t--){ 22 | int n; 23 | cin >> n; 24 | cout << phi[n] << "\n"; 25 | } 26 | for(int i=1 ; i<=MAX ; i++) cout << phi[i] << " "; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ETF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; // 4 4 | // 4(1/2) 5 | int phi(int n){ 6 | int result = n; 7 | bool flag = false; 8 | while(n%2==0){ 9 | flag = true; 10 | n /= 2; 11 | } 12 | 13 | if(flag) 14 | result -=result/2; 15 | 16 | int root = sqrt(n); 17 | for( int i=3 ; i<=root ; i+=2 ){ 18 | flag = false; 19 | if(n%i==0){ 20 | flag = true; 21 | while(n%i==0){ 22 | n = n/i; 23 | } 24 | } 25 | if(flag) 26 | result -= result/i; 27 | } 28 | if(n>1){ 29 | result -= result/n; 30 | } 31 | return result; 32 | } 33 | 34 | int main(){ 35 | int t; 36 | cin >> t; 37 | while(t--){ 38 | int n ; 39 | cin >> n ; 40 | cout << phi(n) <<"\n"; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/ETFD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 1000000 6 | int phi[MAX+1]; // for precomputation 7 | int depth[MAX+1]; // for precomputation 8 | 9 | void etfs(){ // precomputation 10 | for(int i=1 ; i<=MAX ; i++) phi[i] = i; 11 | for(int i=2 ; i<=MAX ; i++){ 12 | if(phi[i]==i){ 13 | for(int j=i ; j<=MAX ; j+=i){ 14 | phi[j] -= phi[j]/i; 15 | } 16 | } 17 | } 18 | } 19 | 20 | void depthCalc(){ // precomputation 21 | depth[1] = 0; 22 | for(int i=2 ; i<=MAX ; i++){ 23 | depth[i] = depth[phi[i]]+1; 24 | } 25 | } 26 | 27 | int main() { 28 | etfs(); 29 | depthCalc(); 30 | vector v[20]; 31 | for(int i=1 ; i<=MAX ; i++){ 32 | if(depth[i] < 20) v[depth[i]].push_back(i); 33 | } // precomputation ends here 34 | int t,m,n,k; 35 | cin >> t; 36 | while(t--){ 37 | cin >> m >> n >> k; 38 | vector::iterator lb,ub; 39 | lb = lower_bound(v[k].begin(),v[k].end(),m); 40 | ub = upper_bound(v[k].begin(),v[k].end(),n); 41 | cout << (ub-lb) << "\n"; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/FACEFRND.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n,a,m; 7 | cin >> n; 8 | int temp = n; 9 | set s; 10 | while(n--){ 11 | cin >> a >> m; 12 | s.insert(a); 13 | while(m--){ 14 | cin >> a; 15 | s.insert(a); 16 | } 17 | } 18 | cout << s.size()-temp; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/FACT0.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // your code goes here 7 | while(1){ 8 | long long n; 9 | cin >> n; 10 | if(n){ 11 | int count = 0; 12 | while(n%2==0){ 13 | count++; 14 | n/=2; 15 | } 16 | 17 | if(count) 18 | cout << 2 <<"^" << count << " "; 19 | 20 | for(int i=3 ; i<=sqrt(n) ; i+=2){ 21 | if(n%i==0){ 22 | count = 0; 23 | while(n%i==0){ 24 | count++; 25 | n/=i; 26 | } 27 | cout << i <<"^" << count << " "; 28 | } 29 | } 30 | if(n>1) cout << n << "^" << 1 ; 31 | cout << endl; 32 | }else break; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/FACTCG2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 10000001 4 | 5 | int sp[MAX]; 6 | bool isPrime[MAX]; 7 | 8 | bool sp_sieve(){ 9 | for(int i=2 ; i 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | #define gc getchar 7 | #define lp(i,a,b) for(int i = int(a); i= '0' && c <= '9' ; c = gc() ) x = (x << 1) + (x << 3) + c - '0'; 14 | if(isNeg) x *= -1; return x; 15 | } 16 | 17 | int main(){ 18 | ios::sync_with_stdio(false); 19 | //cin.tie(0); 20 | int t,n; 21 | cin >> t; 22 | lp(i,1,t+1){ 23 | cin >> n; 24 | int coins[n],dp[n+1]; 25 | lp(i,0,n) cin >> coins[i]; 26 | dp[0] = 0; 27 | dp[1] = coins[0]; 28 | lp(i,2,n+1){ 29 | dp[i] = max(dp[i-1],coins[i-1]+dp[i-2]); 30 | } 31 | cout << "Case " << i << ": " << dp[n] << "\n"; 32 | } 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/FASHION.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin >> t; 8 | for(int i=0 ; i> n; 11 | int men[n],women[n]; 12 | for(int j=0 ; j> men[j]; 13 | for(int j=0 ; j> women[j]; 14 | sort(men,men+n); 15 | sort(women,women+n); 16 | int sum=0; 17 | for(int j=0 ; j 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // your code goes here 8 | int g,b; 9 | while(1){ 10 | cin >> g >> b; 11 | if(g == -1) break; 12 | else{ 13 | cout << ceil((double)max(g,b)/(min(g,b)+1)) << "\n"; 14 | } 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/GSS1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define N 50005 4 | typedef long long ll; 5 | ll n,q; 6 | struct node{ 7 | ll sum,preSum,suffSum,maxSum; 8 | }; 9 | 10 | node tree[4*N]; 11 | ll arr[N]; 12 | 13 | node merge(node l,node r){ 14 | node res; 15 | 16 | res.sum = l.sum+r.sum; 17 | res.preSum = max(l.preSum, l.sum+r.preSum); 18 | res.suffSum = max(r.suffSum, l.suffSum + r.sum); 19 | res.maxSum = max(l.suffSum + r.preSum, max(l.maxSum,r.maxSum)); 20 | 21 | return res; 22 | 23 | } 24 | 25 | void build(ll pos,ll lo,ll hi){ 26 | if(lo==hi){ 27 | tree[pos].sum = arr[lo]; 28 | tree[pos].preSum = arr[lo]; 29 | tree[pos].suffSum = arr[lo]; 30 | tree[pos].maxSum = arr[lo]; 31 | return; 32 | } 33 | 34 | ll mid = (lo+hi)>>1; 35 | 36 | ll left = (pos<<1)+1 , right = left+1; 37 | 38 | build(left,lo,mid); 39 | build(right,mid+1,hi); 40 | 41 | tree[pos] = merge(tree[left],tree[right]); 42 | 43 | } 44 | 45 | node query(ll pos,ll lo,ll hi,ll qlo,ll qhi){ 46 | 47 | if(qlo<=lo && qhi>=hi) 48 | return tree[pos]; 49 | 50 | ll mid = (lo+hi)>>1; 51 | 52 | if(qlo > mid){ 53 | return query( (pos<<1)+2, mid+1, hi, qlo, qhi); 54 | }else if(qhi <= mid){ 55 | return query( (pos<<1)+1, lo, mid, qlo, qhi); 56 | } 57 | 58 | node left = query( (pos<<1)+1, lo, mid, qlo, qhi); 59 | node right = query( (pos<<1)+2, mid+1, hi, qlo, qhi); 60 | 61 | return merge(left,right); 62 | 63 | } 64 | 65 | int main(){ 66 | ios::sync_with_stdio(false); 67 | cin >> n; 68 | for(ll i=0 ; i> arr[i]; 69 | 70 | build(0,0,n-1); 71 | 72 | cin >> q; 73 | while(q--){ 74 | ll l,r; 75 | cin >> l >> r; 76 | node ans = query(0,0,n-1,l-1,r-1); 77 | cout << ans.maxSum << "\n"; 78 | } 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/GSS3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define N 50005 4 | typedef long long ll; 5 | ll n,q; 6 | struct node{ 7 | ll sum,preSum,suffSum,maxSum; 8 | }; 9 | 10 | node tree[4*N]; 11 | ll arr[N]; 12 | //ll pre[N],suff[N]; 13 | 14 | node merge(node l,node r){ 15 | node res; 16 | 17 | res.sum = l.sum+r.sum; 18 | res.preSum = max(l.preSum, l.sum+r.preSum); 19 | res.suffSum = max(r.suffSum, l.suffSum + r.sum); 20 | res.maxSum = max(l.suffSum + r.preSum, max(l.maxSum,r.maxSum)); 21 | 22 | return res; 23 | 24 | } 25 | 26 | void build(ll pos,ll lo,ll hi){ 27 | if(lo==hi){ 28 | tree[pos].sum = arr[lo]; 29 | tree[pos].preSum = arr[lo]; 30 | tree[pos].suffSum = arr[lo]; 31 | tree[pos].maxSum = arr[lo]; 32 | return; 33 | } 34 | 35 | ll mid = (lo+hi)>>1; 36 | 37 | ll left = (pos<<1)+1 , right = left+1; 38 | 39 | build(left,lo,mid); 40 | build(right,mid+1,hi); 41 | 42 | tree[pos] = merge(tree[left],tree[right]); 43 | 44 | } 45 | 46 | node query(ll pos,ll lo,ll hi,ll qlo,ll qhi){ 47 | 48 | if(qlo<=lo && qhi>=hi) 49 | return tree[pos]; 50 | 51 | ll mid = (lo+hi)>>1; 52 | 53 | if(qlo > mid){ 54 | return query( (pos<<1)+2, mid+1, hi, qlo, qhi); 55 | }else if(qhi <= mid){ 56 | return query( (pos<<1)+1, lo, mid, qlo, qhi); 57 | } 58 | 59 | node left = query( (pos<<1)+1, lo, mid, qlo, qhi); 60 | node right = query( (pos<<1)+2, mid+1, hi, qlo, qhi); 61 | 62 | return merge(left,right); 63 | 64 | } 65 | 66 | void update(ll pos,ll lo,ll hi,ll val,ll index){ 67 | if(lo==hi){ 68 | arr[index] = val; 69 | tree[pos].sum = val; 70 | tree[pos].preSum = val; 71 | tree[pos].suffSum = val; 72 | tree[pos].maxSum = val; 73 | return; 74 | } 75 | 76 | ll mid = (lo+hi)>>1; 77 | if(index <= mid){ 78 | update( (pos<<1)+1, lo, mid, val, index); 79 | }else{ 80 | update( (pos<<1)+2, mid+1, hi, val, index); 81 | } 82 | 83 | ll left = (pos<<1)+1 , right = left+1; 84 | 85 | tree[pos] = merge(tree[left],tree[right]); 86 | } 87 | 88 | int main(){ 89 | ios::sync_with_stdio(false); 90 | cin >> n; 91 | for(ll i=0 ; i> arr[i]; 92 | build(0,0,n-1); 93 | 94 | cin >> q; 95 | while(q--){ 96 | ll type,l,r,index,val; 97 | cin >> type; 98 | if(type){ 99 | cin >> l >> r; 100 | node ans = query(0,0,n-1,l-1,r-1); // -1 because mine is zero indexed 101 | cout << ans.maxSum << "\n"; 102 | } 103 | else{ 104 | cin >> index >> val; 105 | update(0,0,n-1,val,index-1); // -1 because mine is zero indexed 106 | } 107 | } 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/HANGOVER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | float c; 6 | while(1){ 7 | cin >> c; 8 | if(c==0) break; 9 | int count=0; 10 | float sum=0; 11 | for(float i=2.0 ; 1 ; i++){ 12 | sum+=(1/i); 13 | count++; 14 | if(sum>=c) break; 15 | } 16 | cout << count <<" card(s)" << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/HAYBALE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define loop(i,a,b) for(int i=(int)a ; i<(int)b ; ++i) 5 | int main(){ 6 | ios::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int n,l,r; 9 | short k; 10 | cin >> n >> k; 11 | short arr[n+1] = {0}; 12 | loop(i,0,k){ 13 | cin >> l >> r ; 14 | ++arr[l],--arr[r+1]; 15 | } 16 | 17 | loop(i,1,n) 18 | arr[i] += arr[i-1]; 19 | 20 | sort(arr,arr+n); 21 | cout << arr[n/2]; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/INVCNT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long inversions=0; 6 | 7 | void merge(long long arr[],long long low,long long mid,long long high){ 8 | int n1=mid-low+1; 9 | long long n2=high-mid; 10 | long long left[n1+1],right[n2+1]; 11 | for(int i=0 ; i right[j]){ 18 | arr[k]=right[j]; 19 | j++; 20 | }else{ 21 | arr[k]=left[i]; 22 | i++; 23 | inversions+=j; 24 | } 25 | } 26 | } 27 | 28 | void mergeSort(long long arr[],long long low,long long high){ 29 | if( low> t; 40 | cout <<"\n"; 41 | for(int j=0 ; j> n; 44 | long long num[n]; 45 | for(int i=0 ; i> num[i]; 46 | mergeSort(num,0,n-1); 47 | cout << inversions <<"\n\n" ; 48 | inversions=0; 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/IOIPALIN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int dp[5005][5005]; 6 | 7 | int main(){ 8 | //ios::sync_with_stdio(false); 9 | int n; 10 | cin >> n; 11 | string s,r; 12 | cin >> s; 13 | 14 | r = s; 15 | 16 | reverse( r.begin() , r.end() ); 17 | 18 | 19 | 20 | for(int i=1 ; i<=n ; i++){ 21 | for(int j=1; j<=n ; j++){ 22 | if(s[i-1]==r[j-1]){ 23 | dp[i][j] = dp[i-1][j-1] + 1; 24 | }else{ 25 | dp[i][j] = max(dp[i][j-1], dp[i-1][j]); 26 | } 27 | } 28 | } 29 | 30 | cout << n - dp[n][n] << "\n"; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/KNAPSACK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int f(int n,int w,int *dp,vector &wt,vector &val,int N,int W){ 5 | if(*(dp+W*n+w)>=0) return *(dp+W*n+w); 6 | if(n==0 || w ==0 ) return 0; 7 | if(wt[n] > w) *(dp+W*n+w)= f(n-1,w,dp,wt,val,N,W); 8 | else{ 9 | *(dp+W*n+w) = max( f(n-1,w,dp,wt,val,N,W), f(n-1,w-wt[n],dp,wt,val,N,W) + val[n]); 10 | } 11 | return *(dp+W*n+w); 12 | } 13 | 14 | int main(){ 15 | int s,n; 16 | cin >> s >> n; 17 | vector wt(n),val(n); 18 | for(int i=0 ; i> wt[i] >> val[i]; 19 | int dp[n][s+1]; 20 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | #define lp(i,a) for(int i = (int)0 ; i < int(a) ; i++) 5 | 6 | char g[1005][1005]; 7 | bool mark[1005][1005]; 8 | int dist[1005][1005]; 9 | 10 | void dfs(int parDist, int i, int j, int r, int c){ 11 | if( g[i][j] != '.' || mark[i] [j] || i >= r || j >= c || i < 0 || j < 0 ) return; 12 | mark[i][j] = 1; 13 | dist[i][j] = parDist + 1; 14 | 15 | dfs(dist[i][j], i + 1, j, r, c); 16 | dfs(dist[i][j], i, j + 1, r, c); 17 | dfs(dist[i][j], i - 1, j, r, c); 18 | dfs(dist[i][j], i, j - 1, r, c); 19 | 20 | } 21 | 22 | int main(){ 23 | int t, r, c; 24 | scanf("%d",&t); 25 | while(t--){ 26 | scanf("%d%d",&c,&r); 27 | int xx = -1,yy = -1; 28 | bool flag = false; 29 | lp(i,r){ 30 | lp(j,c){ 31 | scanf(" %c", &g[i][j]); 32 | if( !flag && g[i][j] == '.' ){ 33 | flag = true; 34 | xx = i, yy = j; 35 | } 36 | } 37 | } 38 | 39 | if( xx == -1 ){ 40 | printf("Maximum rope length is 0.\n"); 41 | continue; 42 | } 43 | 44 | dfs(0, xx, yy, r, c); 45 | int maxx = -1, xmax = -1, ymax = -1; 46 | lp(i,r){ 47 | lp(j,c){ 48 | if( dist[i][j] > maxx ){ 49 | maxx = dist[i][j]; 50 | xmax = i, ymax = j; 51 | } 52 | dist[i][j] = 0; 53 | mark[i][j] = 0; 54 | } 55 | } 56 | dist[xmax][ymax] = 0; 57 | dfs(0, xmax, ymax, r, c); 58 | maxx = -1; 59 | lp(i,r){ 60 | lp(j,c){ 61 | maxx = max(maxx, dist[i][j]); 62 | } 63 | } 64 | 65 | printf("Maximum rope length is %d.\n", maxx-1); 66 | 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Online judge solutions/Spoj/LASTDIG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i=0 ; i> a >> b; 10 | int ans=1; 11 | a=a%10; 12 | while(b>0){ 13 | if(b%2) ans=(ans*a)%10; 14 | b/=2; 15 | a=(a*a)%10; 16 | } 17 | cout << ans << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/LASTDIG2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin >> n; 6 | while(n--){string a;cin >> a;long long b;cin >> b;int l=a[a.length()-1]-'0'; 7 | int arr2[4]={6,2,4,8},arr3[4]={1,3,9,7},arr4[2]={6,4},arr7[4]={1,7,9,3},arr8[4]={6,8,4,2},arr9[2]={1,9}; 8 | if(b==0)cout << 1;else if(l==0)cout << 0;else if(l==1)cout << 1; 9 | else if(l==2){ 10 | cout << arr2[b%4]; 11 | } 12 | else if(l==3){ 13 | cout << arr3[b%4]; 14 | }else if(l==4){ 15 | cout << arr4[b%2]; 16 | } 17 | else if(l==5)cout << 5; 18 | else if(l==6)cout << 6; 19 | else if(l==7){ 20 | cout << arr7[b%4]; 21 | } 22 | else if(l==8){ 23 | cout << arr8[b%4]; 24 | } 25 | else if(l==9){ 26 | cout << arr9[b%2]; 27 | } 28 | cout << "\n"; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/LENGFACT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define E 2.71828182845904523536 5 | int main() { 6 | int t; 7 | scanf("%d",&t); 8 | long long n; 9 | while(t--){ 10 | scanf("%lld",&n); 11 | if(n<=1) printf("1\n"); 12 | else{ 13 | long long ans = (long long) (n*log10(n/E) + log10(2*acos(-1)*n)/2); 14 | printf("%lld\n",ans+1); 15 | } 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/LITE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int maxx = 100010; 5 | int tree[4*maxx][2]; 6 | int lazy[4*maxx]; 7 | 8 | void prop(int pos, int lo, int hi){ 9 | int l = 2*pos+1; 10 | if( lo != hi ){ 11 | lazy[l] = 1 - lazy[l]; 12 | lazy[l+1] = 1 - lazy[l+1]; 13 | } 14 | swap(tree[pos][0], tree[pos][1]); 15 | lazy[pos] = 0; 16 | } 17 | 18 | void merge(int pos){ 19 | int l = 2*pos+1; 20 | tree[pos][0] = tree[l][0] + tree[l+1][0]; 21 | tree[pos][1] = tree[l][1] + tree[l+1][1]; 22 | } 23 | 24 | void build(int pos, int lo, int hi){ 25 | if( lo == hi ){ 26 | tree[pos][0] = 1; 27 | return; 28 | } 29 | int mid = (lo+hi)/2; 30 | int l = 2*pos+1; 31 | build(l, lo, mid); 32 | build(l+1, mid+1, hi); 33 | tree[pos][0] = tree[l][0] + tree[l+1][0]; 34 | } 35 | 36 | void update(int pos, int lo, int hi, int qlo, int qhi){ 37 | int l = 2*pos+1; 38 | if( lazy[pos] ) prop(pos, lo, hi); 39 | if( qlo > hi || qhi < lo ) return; 40 | if( qlo <= lo && qhi >= hi ){ 41 | swap(tree[pos][0], tree[pos][1]); 42 | if( lo != hi ){ 43 | lazy[l] = 1 - lazy[l]; 44 | lazy[l+1] = 1 - lazy[l+1]; 45 | } 46 | return; 47 | } 48 | int mid = (lo+hi)/2; 49 | update(l, lo, mid, qlo, qhi); 50 | update(l+1, mid+1, hi, qlo, qhi); 51 | merge(pos); 52 | } 53 | 54 | int query(int pos, int lo, int hi, int qlo, int qhi){ 55 | int l = 2*pos+1; 56 | if( lazy[pos] ){ 57 | prop(pos, lo, hi); 58 | } 59 | if( qlo > hi || qhi < 0 ) return 0; 60 | if( qlo <= lo && hi <= qhi ) return tree[pos][1]; 61 | int mid = (lo+hi)/2; 62 | if( qlo > mid ) return query(l+1, mid+1, hi, qlo, qhi); 63 | else if( qhi <= mid ) return query(l, lo, mid, qlo, qhi); 64 | int left = query(l, lo, mid, qlo, qhi); 65 | int right = query(l+1, mid+1, hi, qlo, qhi); 66 | return left+right; 67 | } 68 | 69 | int main(){ 70 | int n, q; 71 | scanf("%d %d", &n, &q); 72 | build(0, 0, n-1); 73 | while(q--){ 74 | int type, a, b; 75 | scanf("%d %d %d", &type, &a, &b); 76 | if( type ) printf("%d\n", query(0, 0, n-1, a-1, b-1)); 77 | else{ 78 | update(0, 0, n-1, a-1, b-1); 79 | } 80 | } 81 | } 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/MARBLES.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long ll; 12 | typedef long double ld; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair pii; 18 | typedef pair ii; 19 | typedef vector vpll; 20 | typedef map mll; 21 | typedef vector vvll; 22 | typedef list lll; 23 | typedef vector vlll; 24 | typedef pair iii; 25 | 26 | #define rtn return 27 | #define gc getchar 28 | #define pb push_back 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define all(a) a.begin(),a.end() 33 | #define allr(a) a.rbegin(),a.rend() 34 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 35 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 36 | #define mid(s,e) (s+(e-s)/2) 37 | 38 | ll ip(){ 39 | ll x = 0; bool isNeg = false; char c; 40 | c = gc(); 41 | if(c == '-') isNeg = true , c = gc(); 42 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 43 | if(isNeg) x *= -1; return x; 44 | } 45 | 46 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r-1){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 47 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 48 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 49 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 50 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 51 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 52 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 53 | inline int in(){ int NR=0; register char c=gc(); while( c < 48 || c > 57 ){c=gc();} 54 | while(c>47 && c< 58){ NR = (NR << 3) + (NR << 1) + (c - 48); c=gc();} return NR; 55 | } 56 | 57 | ll dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}; 58 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 59 | 60 | #define MOD ULLONG_MAX 61 | #define N 100010 62 | 63 | int main(){ 64 | ios::sync_with_stdio(false); 65 | cin.tie(NULL); 66 | ll t,n,m,e,a,b,q,w,k; 67 | //ll p,q,curr,c; 68 | ///////////// START FROM HERE //////////////////// 69 | cin >> t; 70 | while(t--){ 71 | cin >> n >> k; 72 | if(k>n) cout << 0 << "\n"; 73 | else if(k==1) cout << 1 << "\n"; 74 | else{ 75 | cout << ncr(n-1,k-1) << "\n"; 76 | } 77 | } 78 | 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/MIXTURES.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | Topics : Dynamic programming, modular arithmetic 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int sub(int a, int b){ 12 | return ((a%100)-(b%100)+100)%100; 13 | } 14 | 15 | int cost(int pre[], int i, int j, int k){ 16 | int a = sub(pre[k], pre[i-1]); 17 | int b = sub(pre[j], pre[k]); 18 | return a*b; 19 | } 20 | 21 | int main(){ 22 | int n; 23 | while( scanf("%d",&n) != EOF ){ 24 | int arr[n+1], pre[n+1]; 25 | pre[0] = 0; 26 | for(int i = 1 ; i<=n ; i++){ 27 | scanf("%d", arr + i); 28 | pre[i] = pre[i-1] + arr[i]; 29 | } 30 | int dp[n+1][n+1]; // ans dp[1][n] 31 | 32 | for(int i = 0 ; i<=n ; i++){ 33 | dp[i][i] = 0; 34 | dp[0][i] = 0; 35 | dp[i][0] = 0; 36 | } 37 | 38 | for(int j = 2 ; j <= n ; j++){ 39 | for(int i = j-1 ; i > 0 ; i--){ 40 | dp[i][j] = INT_MAX; 41 | for(int k = i ; k < j ; k++){ 42 | dp[i][j] = min( dp[i][j], dp[i][k] + dp[k+1][j] + cost(pre,i,j,k) ); 43 | } 44 | } 45 | } 46 | 47 | cout << dp[1][n] << "\n"; 48 | 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/MULTQ3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int maxx = 100010; 5 | int tree[4*maxx][3]; 6 | int a[maxx]; 7 | int lazy[4*maxx]; 8 | 9 | void prop(int pos, int lo, int hi){ 10 | int l = 2*pos+1; 11 | if( lo != hi ){ 12 | lazy[l] = (lazy[l]+lazy[pos])%3; 13 | lazy[l+1] = (lazy[l+1]+lazy[pos])%3; 14 | } 15 | if( lazy[pos] == 1 ){ 16 | int a = tree[pos][2]; 17 | tree[pos][2] = tree[pos][1]; 18 | tree[pos][1] = tree[pos][0]; 19 | tree[pos][0] = a; 20 | }else{ 21 | int a = tree[pos][0]; 22 | tree[pos][0] = tree[pos][1]; 23 | tree[pos][1] = tree[pos][2]; 24 | tree[pos][2] = a; 25 | } 26 | lazy[pos] = 0; 27 | } 28 | 29 | void merge(int pos){ 30 | int l = 2*pos+1; 31 | tree[pos][0] = tree[l][0]+tree[l+1][0]; 32 | tree[pos][1] = tree[l][1]+tree[l+1][1]; 33 | tree[pos][2] = tree[l][2]+tree[l+1][2]; 34 | } 35 | 36 | void build(int pos, int lo, int hi){ 37 | if( lo == hi ){ 38 | tree[pos][0] = 1; 39 | tree[pos][1] = tree[pos][2] = 0; 40 | return; 41 | } 42 | int mid = (lo+hi)/2; 43 | int l = 2*pos+1; 44 | build(l, lo, mid); 45 | build(l+1, mid+1, hi); 46 | tree[pos][1] = tree[pos][2] = 0; 47 | tree[pos][0] = tree[l][0] + tree[l+1][0]; 48 | } 49 | 50 | void update(int pos, int lo, int hi, int qlo, int qhi){ 51 | int l = 2*pos+1; 52 | if( lazy[pos] ) prop(pos, lo, hi); 53 | if( qlo > hi || qhi < lo ) return; 54 | if( qlo <= lo && qhi >= hi ){ 55 | int a = tree[pos][2]; 56 | tree[pos][2] = tree[pos][1]; 57 | tree[pos][1] = tree[pos][0]; 58 | tree[pos][0] = a; 59 | if( lo != hi ){ 60 | lazy[l] = (lazy[l]+1)%3; 61 | lazy[l+1] = (lazy[l+1]+1)%3; 62 | } 63 | return; 64 | } 65 | int mid = (lo+hi)/2; 66 | update(l, lo, mid, qlo, qhi); 67 | update(l+1, mid+1, hi, qlo, qhi); 68 | merge(pos); 69 | } 70 | 71 | int query(int pos, int lo, int hi, int qlo, int qhi){ 72 | int l = 2*pos+1; 73 | if( lazy[pos] ){ 74 | prop(pos, lo, hi); 75 | } 76 | if( qlo > hi || qhi < 0 ) return 0; 77 | if( qlo <= lo && hi <= qhi ) return tree[pos][0]; 78 | int mid = (lo+hi)/2; 79 | if( qlo > mid ) return query(l+1, mid+1, hi, qlo, qhi); 80 | else if( qhi <= mid ) return query(l, lo, mid, qlo, qhi); 81 | int left = query(l, lo, mid, qlo, qhi); 82 | int right = query(l+1, mid+1, hi, qlo, qhi); 83 | return left+right; 84 | } 85 | 86 | int main(){ 87 | int n, q; 88 | scanf("%d %d", &n, &q); 89 | build(0, 0, n-1); 90 | while(q--){ 91 | int type, a, b; 92 | scanf("%d %d %d", &type, &a, &b); 93 | if( type ) printf("%d\n", query(0, 0, n-1, a, b)); 94 | else{ 95 | update(0, 0, n-1, a, b); 96 | } 97 | } 98 | } 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/NY10A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int count(vector &v,string s){ 6 | for(int i=0 ; i<8 ; i++){ 7 | if(s == v[i]) return i; 8 | } 9 | } 10 | 11 | int main() { 12 | vector v(8); 13 | v[0] = "TTT"; 14 | v[1] = "TTH"; 15 | v[2] = "THT"; 16 | v[3] = "THH"; 17 | v[4] = "HTT"; 18 | v[5] = "HTH"; 19 | v[6] = "HHT"; 20 | v[7] = "HHH"; 21 | int p; 22 | cin >> p; 23 | for(int i=1 ; i<=p ; i++){ 24 | int n; 25 | cin >> n; 26 | string s; 27 | cin >> s; 28 | int ans[8] = {0}; 29 | string temp = ""; 30 | for(int j=1 ; j<=38 ; j++){ 31 | temp+=s[j-1]; 32 | temp+=s[j]; 33 | temp+=s[j+1]; 34 | ans[count(v,temp)]++; 35 | temp.clear(); 36 | } 37 | cout << n << " "; 38 | for(int j=0 ; j<8 ; j++) cout << ans[j] << " "; 39 | cout << "\n"; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/OFFSIDE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // your code goes here 6 | int n; 7 | while(1){ 8 | int a,d; 9 | scanf("%d %d",&a,&d); 10 | if(a && d){ 11 | int b; 12 | int min = 1000000,least = 1000000,secondLeast = 10000000,leastIndex; 13 | 14 | for(int i=0 ; i b) min = b; 17 | } 18 | 19 | int c[d]; 20 | for(int i=0 ; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int precedance(char a){ 7 | if(a=='+' || a=='-') return 0; 8 | else if(a=='*' || a=='/' ) return 1; 9 | else if(a=='^') return 2; 10 | else return -1 ; // for left parenthesis in stack 11 | } 12 | 13 | bool isOperator(char a){ 14 | if(a=='+' || a=='-' || a=='*' || a=='/' || a=='^') return true; 15 | else return false; 16 | } 17 | 18 | 19 | int main() { 20 | stack p; 21 | int t; 22 | cin >> t; 23 | string s; 24 | for(int i=0 ; i> s; 26 | p.push('('); 27 | s.push_back(')'); 28 | string q; 29 | for(int j=0 ; !p.empty() ; j++){ 30 | if( !isOperator(s[j]) && s[j]!='(' && s[j]!=')') q.push_back(s[j]); 31 | else if( s[j]=='(' ) p.push(s[j]); 32 | else if( s[j]==')' ){ 33 | while(p.top()!='('){ 34 | q.push_back( p.top() ); 35 | p.pop(); 36 | } 37 | p.pop(); 38 | } 39 | else{ 40 | while(precedance(p.top()) >= precedance(s[j])){ 41 | q.push_back(p.top()); 42 | p.pop(); 43 | } 44 | p.push(s[j]); 45 | } 46 | } 47 | cout << q <<"\n"; 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/PALIN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool all9(string &s){ 5 | int len = s.size(); 6 | for(int i=0 ; i> t; 43 | string s; 44 | while(t--){ 45 | cin >> s; 46 | len = s.size(); 47 | if( allZero(s) ) { 48 | cout << 1 << "\n"; 49 | continue; 50 | } 51 | if( len == 1 ){ 52 | if( s[0]<='8') 53 | cout << s[0]+1-'0'; 54 | else 55 | cout << 11; 56 | }else if( all9(s) ){ 57 | cout << 1 ; 58 | for(int i=1 ; i=0 ; i--){ 72 | if(s[i] > s[len-1-i]){ 73 | s[len-1-i] = s[i]; 74 | lo = i-1; 75 | break; 76 | } 77 | else if(s[i] < s[len-1-i]){ 78 | if(len%2) 79 | lo = len/2,hi = len/2; 80 | else 81 | lo = len/2-1,hi = len/2; 82 | case9(s,lo,hi); 83 | break; 84 | } 85 | } 86 | 87 | for(int i=0 ; i<=lo ; ++i) 88 | s[len-1-i] = s[i]; 89 | 90 | cout << s ; 91 | } 92 | cout << "\n"; 93 | } 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/PARTY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | int n,w; // w is max budget 8 | cin >> w >> n; 9 | while(n!=0 && w!=0){ 10 | int cost[n],fun[n]; 11 | int dp[n+1][w+1]; 12 | for(int i=0 ; i> cost[i] >> fun[i]; 14 | 15 | for(int i=0 ; i<=n ; ++i){ 16 | for(int j=0 ; j<=w ; ++j){ 17 | if(i==0 || j==0) 18 | dp[i][j] = 0; 19 | else if(cost[i-1] > j) 20 | dp[i][j] = dp[i-1][j]; 21 | else 22 | dp[i][j] = max(dp[i-1][j],dp[i-1][j-cost[i-1]] + fun[i-1]); 23 | } 24 | } 25 | int ans; 26 | for(int j=0 ; j<=w ; ++j){ 27 | if(dp[n][w]==dp[n][j]){ 28 | ans = j ; 29 | break; 30 | } 31 | } 32 | cout << ans << " " << dp[n][w] << "\n"; 33 | cin >> w >> n; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/PHONELST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define SIZE 10 5 | 6 | typedef long long ll; 7 | 8 | // trie node 9 | struct node{ 10 | node *child[SIZE]; 11 | ll words; 12 | ll prefixes; 13 | }; 14 | 15 | node* newNode(){ 16 | node *a = new node; 17 | a->words = a->prefixes = 0; 18 | for(ll i=0 ; ichild[i] = NULL; 19 | return a; 20 | } 21 | 22 | void insert( node *a, string &s){ 23 | node *ptr = a; 24 | ll sz = s.size(); 25 | for(ll i=0 ; iprefixes++; 27 | ll idx = s[i]-'0'; 28 | if(ptr->child[idx] == NULL){ 29 | ptr->child[idx] = newNode(); 30 | } 31 | ptr = ptr->child[idx]; 32 | } 33 | ptr->words++; 34 | } 35 | 36 | ll countPrefixes( node *a, string &s){ 37 | node *ptr = a; 38 | ll sz = s.size(); 39 | for(ll i=0 ; ichild[idx] == NULL) 42 | return 0; 43 | ptr = ptr->child[idx]; 44 | } 45 | if(ptr != NULL && ptr->prefixes) return ptr->prefixes; 46 | return 0; 47 | } 48 | 49 | int main(){ 50 | ll t; 51 | cin >> t; 52 | while(t--){ 53 | ll n,q; 54 | cin >> n; 55 | node *root = newNode(); 56 | 57 | string s[n]; 58 | for(ll i=0 ; i> s[i]; 60 | insert( root, s[i]); 61 | } 62 | 63 | ll res = 0; 64 | for(ll i=0 ; i 7 | using namespace std; 8 | 9 | #define MOD 1000000007 10 | #define N 100010 11 | 12 | typedef double lf; 13 | typedef long long ll; 14 | typedef long double ld; 15 | typedef unsigned long long ull; 16 | typedef set sll; 17 | typedef vector vll; 18 | typedef map msl; 19 | typedef pair pii; 20 | typedef pair ii; 21 | typedef vector vpll; 22 | typedef map mll; 23 | typedef vector matrix; 24 | typedef list lll; 25 | typedef vector vlll; 26 | typedef pair iii; 27 | 28 | #define rtn return 29 | #define gc getchar 30 | #define pb push_back 31 | #define ff first 32 | #define ss second 33 | #define mp(x,y) make_pair(x,y) 34 | #define all(a) a.begin(),a.end() 35 | #define allr(a) a.rbegin(),a.rend() 36 | #define lp(i,b) for(ll i = ll(0); i> t; 60 | while(t--){ 61 | cin >> n; 62 | map mp; 63 | while(n > 1){ 64 | mp[spf[n]]++; 65 | n /= spf[n]; 66 | } 67 | int even = 0, odd = 0; 68 | lpit(it,mp){ 69 | if( it->ss & 1 ) odd++; 70 | else even++; 71 | } 72 | cout << (( even > odd ) ? "Psycho Number" : "Ordinary Number") << "\n"; 73 | } 74 | 75 | 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/PT07Y.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Graph{ 8 | int v; 9 | list *adj; 10 | public: 11 | bool *visited; 12 | Graph(int v); 13 | void addEdge(int u,int v); 14 | void dfs(); 15 | void dfsUtil(int v); 16 | }; 17 | 18 | Graph::Graph(int v){ 19 | this->v = v; 20 | adj = new list [v+1]; 21 | visited = new bool[v+1]; 22 | for(int i=1 ; i<=v ; i++) visited[i] = false; 23 | } 24 | 25 | void Graph::addEdge(int u,int v){ 26 | adj[u].push_front(v); 27 | adj[v].push_front(u); 28 | } 29 | 30 | void Graph::dfsUtil(int v){ 31 | visited[v] = true; 32 | list::iterator it; 33 | for(it = adj[v].begin() ; it!=adj[v].end() ; it++){ 34 | if(!visited[*it]){ 35 | dfsUtil(*it); 36 | } 37 | } 38 | } 39 | 40 | int main() { 41 | int n,m; 42 | cin >> n >>m; 43 | Graph g(n); 44 | int u,v; 45 | for(int i=0 ; i> u >> v; 47 | g.addEdge(u,v); 48 | } 49 | if(n-1!=m){ 50 | cout << "NO"; 51 | return 0; 52 | } 53 | else{ 54 | g.dfsUtil(1); 55 | } 56 | for(int i=1 ; i<=n ; i++){ 57 | if(!g.visited[i]){ 58 | cout << "NO"; 59 | return 0; 60 | } 61 | } 62 | cout << "YES"; 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/RKS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | bool cmp(pair > p1,pair > p2){ 5 | if (p1.first>p2.first) return true; 6 | else if (p1.first==p2.first){ 7 | if (p1.second.first>n>>c; 16 | vector vec(n); 17 | map > mp; 18 | vector > > q; 19 | for (int i=0;i>vec[i]; 21 | if (mp.find(vec[i])!=mp.end()) mp[vec[i]].first++; 22 | else mp.insert(make_pair(vec[i],make_pair(1,0))); 23 | } 24 | vector v(n); 25 | for (int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int ans(int n,int k,int val[],vector > &dp){ 6 | if(n < 0) return 0; 7 | if(dp[n][k] > -1) return dp[n][k]; 8 | if(n ==0 || k==0 ) 9 | dp[n][k] = 0; 10 | else if(val[n-1] > k) 11 | dp[n][k] = ans(n-1,k,val,dp); 12 | else 13 | dp[n][k] = max(ans(n-1,k,val,dp),ans(n-2,k-val[n-1],val,dp)+val[n-1] ); 14 | return dp[n][k]; 15 | } 16 | 17 | int main(){ 18 | ios::sync_with_stdio(false); 19 | cin.tie(NULL); 20 | int t,n,k; 21 | cin >> t; 22 | int i=1; 23 | while(t--){ 24 | cin >> n >> k; 25 | vector > v(n+1,vector(k+1,-1)); 26 | int val[n]; 27 | for(int i=0 ; i> val[i]; 29 | cout << "Scenario #" << i <<": " << ans(n,k,val,v) << "\n"; 30 | ++i; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/SAMER08D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int k; 6 | while(1){ 7 | cin >> k; 8 | if( !k ) break; 9 | string a, b; 10 | cin >> a >> b; 11 | int n = a.size(), m = b.size(); 12 | int dp[n+1][m+1]; 13 | for(int i = 0 ; i <= n ; i++){ 14 | for(int j = 0 ; j <= m ; j++){ 15 | dp[i][j] = 0; 16 | } 17 | } 18 | 19 | for(int i = 1 ; i <= n ; i++){ 20 | for(int j = 1 ; j <= m ; j++){ 21 | int c = 1; 22 | while( i-c>=0 && j-c>=0 && a[i-c]==b[j-c] ){ 23 | if( c >= k ){ 24 | dp[i][j] = max(dp[i][j], dp[i-c][j-c]+c); 25 | } 26 | c++; 27 | } 28 | if( i>=1 ) dp[i][j] = max(dp[i][j], dp[i-1][j]); 29 | if( j>=1 ) dp[i][j] = max(dp[i][j], dp[i][j-1]); 30 | } 31 | } 32 | 33 | 34 | 35 | cout << dp[n][m] << "\n"; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Online judge solutions/Spoj/SBANK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios::sync_with_stdio(false); 6 | // cin.tie(0); 7 | int t,n; 8 | cin >> t; 9 | map m; 10 | while(t--){ 11 | cin >> n; 12 | 13 | string s; 14 | 15 | for(int i=0 ; i> s; 18 | str += s; 19 | for(int i=0 ; i<5 ; i++){ 20 | cin >> s; 21 | str += " "; 22 | str += s; 23 | } 24 | if(m.find(str)!=m.end()){ 25 | m[str] += 1; 26 | }else{ 27 | m[str] = 1; 28 | } 29 | } 30 | map::iterator it; 31 | for(it = m.begin() ; it!=m.end() ; it++){ 32 | cout << it->first << " " << it->second << "\n"; 33 | } 34 | m.clear(); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/SILVER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | //ios::sync_with_stdio(false); 6 | //cin.tie(NULL); 7 | while( 1 ){ 8 | double n; 9 | cin >> n; 10 | if( !n ) break; 11 | cout << int(log2(n)) << "\n"; 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/SKYLINE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define MAX 1000 5 | #define MOD 1000000 6 | ll cat[MAX+1]; 7 | 8 | ll catalan(){ 9 | cat[0] = 1; 10 | cat[1] = 1; 11 | for(int i=2 ; i<=MAX ; i++){ 12 | cat[i] = 0; 13 | for(int j=0 ; j> n; 25 | if(n==0) break; 26 | cout << cat[n] << "\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/SORT2D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AARUSH JUNEJA 3 | @vivace 4 | IIT(ISM),Dhanbad 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef double lf; 11 | typedef long long ll; 12 | typedef long double ld; 13 | typedef unsigned long long ull; 14 | typedef set sll; 15 | typedef vector vll; 16 | typedef map msl; 17 | typedef pair pii; 18 | typedef pair ii; 19 | typedef vector vpll; 20 | typedef map mll; 21 | typedef vector vvll; 22 | typedef list lll; 23 | typedef vector vlll; 24 | typedef pair iii; 25 | 26 | #define rtn return 27 | #define gc getchar 28 | #define pb push_back 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define all(a) a.begin(),a.end() 33 | #define allr(a) a.rbegin(),a.rend() 34 | #define lp(i,a,b) for(ll i = ll(a); i<=ll(b) ; i++) 35 | #define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) 36 | #define mid(s,e) (s+(e-s)/2) 37 | 38 | ll ip(){ 39 | ll x = 0; bool isNeg = false; char c; 40 | c = gc(); 41 | if(c == '-') isNeg = true , c = gc(); 42 | for( ; c >= '0' && c <= '9' ; c = gc()); x = (x << 1) + (x << 3) + c - '0'; 43 | if(isNeg) x *= -1; return x; 44 | } 45 | 46 | inline ll ncr(ll n,ll r){ ll ans = 1 ; if(r > n-r) r = n-r; lp(i,0,r){ans*=(n-i) ; ans/=(i+1); } rtn ans; } 47 | inline ll gcd(ll a,ll b){if(!a) rtn b; rtn gcd(b%a,a); } 48 | inline ll fme(ll x,ll n,ll mod){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;}rtn ans%mod;} 49 | inline bool isPalin(string &s){ int len = s.size()-1;lp(i,0,(len/2)+1){if(!(s[i]==s[len-i])) rtn false;} rtn true;} 50 | inline ll lcm(ll a,ll b){rtn (a*b)/gcd(a,b); } 51 | inline ll fmm(ll a,ll b,ll m) {ll r=0;a%=m;b%=m;while(b>0){if(b&1){r+=a;r%=m;}a+=a;a%=m;b>>=1;}rtn r%m;} 52 | inline ll sfme(ll a,ll b,ll m) {ll r=1;a%=m;while(b>0){if(b&1)r=fmm(r,a,m);a=fmm(a,a,m);b>>=1;}rtn r%m;} 53 | inline int in(){ int NR=0; register char c=gc(); while( c < 48 || c > 57 ){c=gc();} 54 | while(c>47 && c< 58){ NR = (NR << 3) + (NR << 1) + (c - 48); c=gc();} return NR; 55 | } 56 | 57 | ll dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}; 58 | //--------------------------------TEMPLATE ENDS HERE--------------------------------------------------// 59 | 60 | #define MOD 1000000007 61 | #define N 100010 62 | 63 | bool cmp(pair &a, pair &b){ 64 | return (a.ff < b.ff || (a.ff==b.ff && a.ss > b.ss) ); 65 | } 66 | 67 | int main(){ 68 | ios::sync_with_stdio(false); 69 | cin.tie(NULL); 70 | ll t,n,m,e,a,b,q,w,k; 71 | //ll p,q,curr,c; 72 | ///////////// START FROM HERE //////////////////// 73 | cin >> t; 74 | while(t--){ 75 | cin >> n; 76 | vector > v(n); 77 | lp(i,0,n-1) cin >> v[i].ff >> v[i].ss ; 78 | sort(all(v),cmp); 79 | lp(i,0,n-1) cout << v[i].ff << " " << v[i].ss << "\n"; 80 | } 81 | 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/STAMPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int t; 8 | cin >> t; 9 | for(int i=1 ; i<=t ; i++){ 10 | int need,friends; 11 | cin >> need >> friends; 12 | 13 | vector stamps(friends); 14 | for(int j=0 ; j> stamps[j]; 16 | } 17 | 18 | sort(stamps.begin(),stamps.end()); 19 | 20 | int stamps_borrowed=0; 21 | int ans=0; 22 | bool flag=0; 23 | 24 | for(int j=friends-1 ; j>=0 ; j--){ 25 | stamps_borrowed+=stamps[j]; 26 | ans++; 27 | if(stamps_borrowed >= need ){ 28 | flag=1; 29 | break; 30 | } 31 | } 32 | cout << "Scenario #" << i << ":" << endl ; 33 | if(flag){ 34 | cout << ans ; 35 | }else{ 36 | cout << "impossible" ; 37 | } 38 | 39 | cout << endl << endl; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/STPAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // your code goes here 7 | while(1){ 8 | int n; 9 | cin >> n; 10 | if(!n) break; 11 | else{ 12 | stack s; 13 | s.push(0); 14 | int arr[n]; 15 | for(int i=0; i> arr[i]; 17 | } 18 | bool flag = true; 19 | int need = 1; 20 | int i=0; 21 | while(need<=n){ 22 | if(arr[i]==need){need++ ; 23 | i++ ; 24 | } 25 | else if(s.top()==need){ 26 | need++ ; 27 | s.pop() ; 28 | } 29 | else if(s.top() 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 100000000 6 | vector prime(MAX+1); 7 | unsigned arr[5000000]; 8 | 9 | void sieve(){ 10 | int root = sqrt(MAX); 11 | for(int i=2 ; i<=root ; i++){ 12 | if(!prime[i]){ 13 | for(int j=i*i ; j<=MAX ; j+=i) 14 | prime[j] = true; 15 | } 16 | } 17 | int index = 0; 18 | int i=3; 19 | arr[index++] = 2; 20 | while(index < 5000000 && i<=MAX){ 21 | if(!prime[i]){ 22 | arr[index] = i; 23 | index++; 24 | } 25 | i+=2; 26 | } 27 | } 28 | 29 | int main() { 30 | // your code goes here 31 | sieve(); 32 | int t,n; 33 | scanf("%d",&t); 34 | while(t--){ 35 | scanf("%d",&n); 36 | printf("%d\n",arr[n-1]); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/TRICOUNT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | for(int i=0 ; i> n; 10 | long long ans=(n*(n+1)*(n+2))/6; 11 | if(n%2==0){ 12 | n=n/2; 13 | ans=ans+((n*(n+1)*(4*n-1))/6); 14 | } 15 | else{ 16 | n=n/2; 17 | ans=ans+((n*(n+1)*(4*n+5))/6); 18 | } 19 | cout << ans << endl ; 20 | 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/UFPR14D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n ; 7 | int num[n+1]; 8 | long sum=0; 9 | for(int i=1 ; i<=n ; i++){ 10 | cin >> num[i]; 11 | sum+=num[i]; 12 | num[i]=sum; 13 | } 14 | int q; 15 | cin >> q; 16 | int a,b; 17 | for(int i=0 ; i> a >> b; 19 | if(a==1){ 20 | cout << num[b] << "\n"; 21 | }else{ 22 | cout << num[b]-num[a-1] << "\n"; 23 | } 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/WACHOVIA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define loop(i,a,b) for(int i=(int)a ; i<(int)b ; i++) 5 | typedef long long ll; 6 | ll knap(int wt[],int val[],int k,int m){ 7 | ll dp[m+1][k+1]; 8 | loop(i,0,m+1) dp[i][0] = 0; 9 | loop(i,0,m+1){ 10 | loop(j,1,k+1){ 11 | if( i==0 || k==0 ) 12 | dp[i][j] = 0; 13 | else if(wt[i-1] > j ) 14 | dp[i][j] = dp[i-1][j]; 15 | else 16 | dp[i][j] = max( dp[i-1][j] ,dp[i-1][j-wt[i-1]]+val[i-1] ); 17 | } 18 | } 19 | 20 | return dp[m][k]; 21 | } 22 | 23 | int main() { 24 | int t; 25 | cin >> t; 26 | int k,m; 27 | while(t--){ 28 | cin >> k >> m; 29 | int wt[m],val[m]; 30 | loop(i,0,m) 31 | cin >> wt[i] >> val[i]; 32 | cout << "Hey stupid robber, you can get " << knap(wt,val,k,m) << ".\n"; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Online judge solutions/Spoj/WOWSUBSTR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i < ll(b) ; i++) 6 | #define lpp(i, a, b) for(ll i = (ll)a ; i <= (ll)b ; i++) 7 | #define mod 100007 8 | 9 | inline ll fme(ll x,ll n){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;} return ans%mod;} 10 | 11 | ll add(ll a, ll b){ 12 | a += b; 13 | if(a >= mod) 14 | a -= mod; 15 | return a; 16 | } 17 | 18 | ll mul(ll a, ll b){ 19 | if( a >= mod ) a %= mod; 20 | if( b >= mod ) b %= mod; 21 | return (a * b) % mod; 22 | } 23 | 24 | ll res(ll n){ 25 | ll sum = 0; 26 | sum = (n*(n+1)*(n+2))/6; 27 | return sum % mod; 28 | } 29 | 30 | ll res1(ll n){ 31 | ll sum = 0; 32 | sum = (n*(n+1))>>1; 33 | return sum % mod; 34 | } 35 | 36 | int main(){ 37 | ios::sync_with_stdio(0); 38 | cin.tie(NULL); 39 | int t, len; 40 | string str; // str contains only small letters 41 | cin >> t; 42 | ll tt = 1; 43 | while(t--){ 44 | cin >> str; 45 | len = str.length(); 46 | int last[26]; // last occurence of str[i]-'a' 47 | lp(i, 26) last[i] = -1; 48 | 49 | last[str[0]-'a'] = 0; 50 | int beganAt = 0, curr_sub = 1; 51 | ll ans = 0; 52 | for(int i = 1 ; i 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define lp(i,b) for(ll i = ll(0); i < ll(b) ; i++) 6 | #define lpp(i, a, b) for(ll i = (ll)a ; i <= (ll)b ; i++) 7 | #define mod 100007 8 | 9 | inline ll fme(ll x,ll n){ll ans=1;x%=mod;while(n>0){if(n&1){ ans*=x;ans%=mod;} x*=x;x%=mod;n>>=1;} return ans%mod;} 10 | 11 | ll add(ll a, ll b){ 12 | a += b; 13 | if(a >= mod) 14 | a -= mod; 15 | return a; 16 | } 17 | 18 | ll mul(ll a, ll b){ 19 | if( a >= mod ) a %= mod; 20 | if( b >= mod ) b %= mod; 21 | return (a * b) % mod; 22 | } 23 | 24 | ll res(ll n){ 25 | ll sum = 0; 26 | sum = (n*(n+1)*(n+2))/6; 27 | return sum % mod; 28 | } 29 | 30 | ll res1(ll n){ 31 | ll sum = 0; 32 | sum = (n*(n+1))>>1; 33 | return sum % mod; 34 | } 35 | 36 | int main(){ 37 | ios::sync_with_stdio(0); 38 | cin.tie(NULL); 39 | ll t, len, range; 40 | string str; // str contains only small letters 41 | cin >> t; 42 | ll tt = 1; 43 | while(t--){ 44 | cin >> len >> range; 45 | ll last[range + 1], arr[len]; // last occurence of str[i]-'a'a 46 | lp(i, range + 1) last[i] = -1; 47 | lp(i, len) cin >> arr[i]; 48 | last[arr[0]] = 0; 49 | ll beganAt = 0, curr_sub = 1; 50 | ll ans = 0; 51 | for(ll i = 1 ; i 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | #define MOD 10000007 6 | ll fast_expo(ll x,ll y){ 7 | ll res = 1; 8 | x = x%MOD; 9 | 10 | while(y > 0){ 11 | if (y & 1) 12 | res = (res*x)%MOD; 13 | 14 | y = y>>1; 15 | x = (x*x)%MOD; 16 | } 17 | return res; 18 | } 19 | 20 | int main(){ 21 | ll n,k; 22 | while(1){ 23 | cin >> n >> k; 24 | if(n==0 && k==0) break; 25 | ll ans = (fast_expo(n,k) + fast_expo(n,n) + (2*fast_expo(n-1,k))%MOD + (2*fast_expo(n-1,n-1))%MOD )%MOD ; 26 | cout << ans <<"\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Online judge solutions/Uva/Network connections.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | #define lp(i,a,b) for(ll i=ll(a) ; i> t; 37 | while(t--){ 38 | scanf("%d",&n); getchar(); 39 | int par[n],sz[n]; 40 | init(par,sz,n); 41 | 42 | int succ = 0,unsucc = 0; 43 | while( (ch=getchar()) && isalpha(ch) ){ 44 | cin >> u >> v; getchar(); 45 | if(ch=='c') merge(par,sz,u,v); 46 | else{ 47 | if(areConn(u,v,par)) ++succ; 48 | else ++unsucc; 49 | } 50 | 51 | } 52 | cout << succ << "," << unsucc << "\n\n"; 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # acm-icpc-practice 2 | This is my collection of various algorithms and data structures that I feel that are needed in competitive programming . 3 | All of this stuff is implemented by me myself after learning it from random resources. 4 | This repository also contains my accepted solutions of questions on various online judges. 5 | I will keep adding stuff here from time to time . 6 | -------------------------------------------------------------------------------- /Sorting_and_searching/MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define loop(i,a,b) for(int i = int(a); i 2 | using namespace std; 3 | 4 | #define loop(i,a,b) for(int i = int(a); iint(b) ; i--) // reverse iterating loop 6 | 7 | void swap2numbers(int *a,int *b){ 8 | int t = *a; 9 | *a = *b; 10 | *b = t; 11 | } 12 | 13 | int partition(int arr[],int low,int high){ 14 | int pivot = arr[low]; 15 | int i = high+1; 16 | rloop(j,high,low){ 17 | if( arr[j] >= pivot ){ 18 | i--; 19 | swap2numbers(&arr[i],&arr[j]); 20 | } 21 | } 22 | swap2numbers(&arr[i-1],&arr[low]); 23 | return i-1; 24 | } 25 | 26 | void quickSort(int arr[],int low,int high){ 27 | if( low < high ){ 28 | int pivotIndex = partition(arr,low,high); 29 | quickSort(arr,low,pivotIndex-1); 30 | quickSort(arr,pivotIndex+1,high); 31 | } 32 | } 33 | 34 | int main(){ 35 | int n; 36 | cin >> n; 37 | int num[n]; 38 | loop(i,0,n) cin >> num[i]; 39 | 40 | quickSort(num,0,n-1); 41 | 42 | loop(i,0,n) cout << num[i] << " "; 43 | 44 | return 0; 45 | } 46 | --------------------------------------------------------------------------------