├── 2d-array.rs ├── 2s-complement.cc ├── a-very-big-sum.clj ├── ab0.cc ├── ai ├── battery.py ├── binomial-distribution-1.py ├── binomial-distribution-2.py ├── binomial-distribution-3.py ├── botclean.cc ├── botcleanr.py ├── computing-the-correlation.py ├── correlation-and-regression-lines-6.py ├── correlation-and-regression-lines-7.py ├── correlation-and-regression-lines-8.py ├── markov-snakes-and-ladders.py ├── maze-escape.cc ├── nlp-compute-the-cross-entropy.hs ├── nlp-compute-the-perplexity.hs ├── normal-distribution-1.py ├── normal-distribution-2.py ├── normal-distribution-3.py ├── pacman-bfs.py ├── pacman-dfs.py ├── predicting-house-prices.py ├── predicting-office-space-price.py ├── saveprincess.py ├── saveprincess2.py ├── stat-warmup.py ├── stockprediction.py ├── temperature-predictions.py ├── the-best-aptitude-test.py ├── the-trigram.hs └── tic-tac-toe.cc ├── almost-equal-advanced.cc ├── almost-equal-advanced_another.cc ├── almost-sorted-interval.cc ├── anagram.rb ├── and-product.cc ├── and-xor-or_bst.cc ├── and-xor-or_stack.cc ├── angry-children.rb ├── arithmetic-progressions.cc ├── array-and-simple-queries.cc ├── array-rotation-2.hs ├── arrays-ds.clj ├── ashton-and-string.cc ├── bfsshortreach.cc ├── bst-maintenance.cc ├── burger-happiness.cc ├── bytelandian-tours.cc ├── changing-bits.cc ├── chocolate-feast.rb ├── clique.clj ├── coloring-tree.cc ├── common-child.cc ├── compare-two-linked-lists.py ├── components-in-graph.py ├── contests ├── 101hack32 │ ├── binary-string-game.cc │ ├── falling-rocks-2.cc │ ├── sorting-under-square-distance.cc │ ├── straight-flush.cc │ └── tree-construction.cc ├── 101hack37 │ ├── long-permutation.hs │ ├── tree-splitting.cc │ ├── tripartite-matching.cc │ └── tripartite-matching.edge.cc ├── 2013-acm-acpc │ ├── increasing-shortest-path.cc │ ├── main.cc │ ├── mario-kart.cc │ ├── modified-lcs.cc │ ├── nassas-robot.cc │ ├── omar-loves-candies.cc │ ├── omars-bug.cc │ ├── super-ants.cc │ ├── swyper-keyboard.cc │ └── the-alphabet-sticker.cc ├── 2014-icpc-central-europe-regional-contest-practice │ ├── bricks.cc │ ├── good-morning.ml │ ├── mountainous-landscape.cc │ ├── outer-space-invaders.cc │ ├── parades.cc │ ├── pork-barrel.cc │ ├── pork-barrel_persistent_segment_tree.cc │ ├── sums.ml │ ├── the-imp.cc │ ├── virus-synthesis.cc │ ├── vocabulary.cc │ └── wheels.ml ├── 2014-icpc-north-central-regional-north-america-practice │ ├── anagram-pyramids.cc │ ├── aquarium-tank.cc │ ├── continued-fractions.cc │ ├── locked-treasure.cc │ ├── preorder-traversals.cc │ ├── rank-order.cc │ ├── restaurant-ratings.cc │ ├── server.cc │ └── the-binary-search-efficiency-doubter.cc ├── 2014-icpc-northwestern-european-regional-practice │ ├── biking-duck.cc │ ├── cent-savings.cc │ ├── digi-comp-ii.cc │ ├── euclidean-tsp.cc │ ├── finding-lines.cc │ ├── gathering.cc │ ├── hyacinth.cc │ ├── indoorienteering.cc │ ├── judging-troubles.cc │ └── knapsack-collection.cc ├── 2015-acm-acpc │ ├── banana-1.rs │ ├── christmas-tree.cc │ ├── christmas-tree_TLE.rs │ ├── dna-evolution.cc │ ├── dna-evolution_RE.rs │ ├── fractionstellar.rs │ ├── journey-2.cc │ ├── road-network-1.rs │ ├── the-lion-king.cc │ ├── the-minions-quiz.rs │ ├── uberfication.cc │ └── uberfication_WA.rs ├── 5-days-of-game-theory │ ├── a-game-of-stones.hs │ ├── day-1-a-chessboard-game.hs │ ├── day-2-nim-game.hs │ ├── day-2-poker-nim.hs │ ├── deforestation.hs │ ├── digits-square-board.hs │ ├── fun-game.hs │ ├── misere-nim.hs │ ├── nimble.hs │ ├── powers-of-two-game.hs │ ├── tower-breakers-2.hs │ ├── tower-breakers-3.hs │ └── tower-breakers.hs ├── basic-statistics-warmup-2.py ├── blackrock-codesprint │ └── currency-arbitrage.rs ├── code-cpp-3 │ ├── accessing-inherited-functions.cc │ ├── attending-workshops.cc │ ├── attribute-parser.cc │ ├── bitset-1.cc │ ├── box-it.cc │ ├── overload-operators.cc │ ├── variable-sized-arrays.cc │ └── virtual-functions.cc ├── code-cpp-4 │ ├── cpp-class-template-specialization.cc │ ├── cpp-variadics.cc │ ├── exceptional-server.cc │ ├── inherited-code.cc │ ├── magic-spells.cc │ ├── preprocessor-solution.cc │ └── prettyprint.cc ├── codewhiz-java-march-2016 │ ├── java-bitset.java │ ├── java-covariance.java │ ├── java-lambda-expressions.java │ ├── java-list.java │ ├── java-primality-test.java │ ├── maximum-and-minimum.java │ └── serve-the-students.java ├── hourrank-10 │ ├── accessory-collection.cc │ └── bomber-man.cc ├── hourrank-2 │ ├── jane-traveling.cc │ └── square-segments.cc ├── hourrank-4 │ ├── new-year-chaos.cc │ └── newyear-present.cc ├── indeed-prime-codesprint │ ├── divyam-and-sorted-list.cc │ ├── flatland-roads.cc │ ├── goty-and-the-play.cc │ ├── hats.cc │ └── the-ultimate-question.cc ├── infinitum15 │ ├── angels-in-space.cc │ ├── birthdays.cc │ ├── k-element-sequences.cc │ ├── maximum-values.cc │ └── modular-roots.cc ├── interfacecse-codedash │ ├── anagram.ml │ ├── dash-4.ml │ ├── squares2.ml │ └── stones2.ml ├── intro-to-statistics │ ├── basic-probability-puzzles-1.hs │ ├── basic-probability-puzzles-2.hs │ ├── basic-probability-puzzles-3.hs │ ├── basic-probability-puzzles-4.hs │ ├── day-5-intro-to-linear-regression.py │ └── day-5-introduction-to-correlation.py ├── june-world-codesprint │ ├── aorb.cc │ ├── equal-stacks.cc │ ├── gridland-provinces.cc │ ├── johnland.cc │ ├── minimum-distances.hs │ ├── r-tree-decoration.cc │ ├── tle.fast.vertical-paths.cc │ ├── tle.vertical-paths.cc │ ├── vertical-paths.cc │ └── vertical-paths.fast.cc ├── lambda-calculi-10 │ ├── arctic-heist.ml │ ├── calculate-the-medians.ml │ ├── composition-of-2-primes.ml │ ├── flip-the-tree.ml │ ├── knight-and-queen.ml │ ├── password-compression-i.ml │ └── password-compression-ii.ml ├── lambda-calculi-9 │ ├── convolutional-coding.ml │ ├── klotski.ml │ ├── lists-and-gcd.ml │ ├── minimum-multiple.ml │ ├── mirko-at-construction-site.ml │ ├── stocks-prediction.ml │ └── string-reductions.ml ├── lambda-calculi-march-2016 │ ├── fighting-armies.hs │ ├── functions-or-not.hs │ ├── lambda-march-compute-the-area-of-a-polygon.hs │ ├── lambda-march-compute-the-perimeter-of-a-polygon.hs │ ├── lambda-march-concave-polygon.hs │ ├── simplify-the-algebraic-expressions.hs │ └── tree-manager.hs ├── may-world-codesprint │ ├── absolute-permutation.cc │ ├── coloring-tree-1.cc │ ├── compare-the-triplets.hs │ ├── davaro-and-travelling.cc │ ├── davaro-and-travelling.other.cc │ ├── div-and-span.cc │ ├── richie-rich.cc │ ├── square-ten-tree.cc │ └── xor-quadruples.cc ├── real-data-contest-2015 │ └── normally-distributed-scores.cc ├── regular-expresso │ ├── balanced-strings.rb │ ├── match-maker.rb │ ├── matching-same-text-again-again.rb │ ├── matching-specific-string.js │ ├── positive-lookahead.js │ ├── restricted-repetitions.rb │ ├── vowels-in-the-back.js │ └── winning-tic-tac-toe.rb ├── rookierank │ ├── antiprime-numbers.cc │ ├── birthday-cake-candles.cc │ ├── counting-valleys.cc │ ├── extremely-dangerous-virus.hs │ └── magic-square-forming.cc ├── w17 │ ├── counting-permutations.cc │ ├── cross-the-river.ml │ ├── garden-planning.by_row.cc │ ├── garden-planning.cc │ └── roads-building.ml ├── w18 │ ├── gg.cc │ ├── rhombographs.cc │ └── two-centers.cc ├── w20 │ ├── cat-jogging.cc │ ├── divisible-sum-pairs.hs │ ├── non-divisible-subset.hs │ ├── simple-game.cc │ └── synchronous-shopping.cc ├── w21 │ ├── borrowing-money.bron.tle.cc │ ├── borrowing-money.cc │ ├── kangaroo.hs │ ├── lazy-sorting.hs │ ├── luck-balance.hs │ └── n-letter.cc ├── w22 │ ├── box-moving.cc │ ├── cookie-party.cc │ ├── number-of-sequences.cc │ ├── polygon-making.cc │ └── sequential-prefix-function.cc ├── womens-codesprint │ ├── annual-car-race.cc │ ├── consonant-reversal.hs │ ├── delete-them-softly.cc │ ├── even-training.hs │ ├── grouping-the-items.cc │ ├── mars-and-the-binary-search-tree.cc │ ├── path-covering.cc │ └── smriti-and-strings.cc ├── world-codesprint-5 │ ├── balanced-forest.cc │ ├── camelcase.hs │ ├── challenging-palindromes.cc │ ├── challenging-palindromes.manacher.cc │ ├── longest-increasing-subsequence-arrays.cc │ ├── mining.cc │ ├── short-palindrome.cc │ ├── string-construction.hs │ └── ticket-to-ride.cc ├── world-codesprint-6 │ ├── abbr.cc │ ├── beautiful-3-set.cc │ ├── bon-appetit.cc │ ├── bonetrousle.cc │ ├── combination-lock.cc │ ├── flipping-the-matrix.cc │ ├── functional-palindromes.cc │ ├── hard-drive-disks.cc │ └── hard-drive-disks.short.cc ├── world-codesprint-april │ ├── beautiful-triplets.cc │ ├── fibonacci-numbers-tree.cc │ ├── gena.cc │ ├── jumping-on-the-clouds.cc │ ├── kmp-problem.cc │ ├── kmp-problem.hs │ ├── little-alexey-and-sum-of-maximums.hs │ └── move-the-coins.cc └── zenhacks │ ├── candy-shop.cc │ ├── circles-1.cc │ ├── connect-query.cc │ ├── decrypt-1.cc │ ├── knight-or-knave.cc │ ├── nth-degree-lucky-number.cc │ ├── numpart.cc │ ├── pairing.cc │ ├── primedist.cc │ ├── shopping.cc │ ├── white-falcon-and-rooms.cc │ ├── zenland.cc │ └── zentree.cc ├── count-strings.cc ├── countingsort1.cc ├── cube-summation.ml ├── delete-a-node-from-a-linked-list.py ├── diagonal-difference.clj ├── dijkstrashortreach.cc ├── direct-connections.cc ├── dortmund-dilemma.cc ├── dynamic-array.py ├── dynamic-summation.cc ├── favourite-sequence.cc ├── filling-jars.cc ├── find-digits.rb ├── find-maximum-index-product.cc ├── find-strings.cc ├── find-strings.generalized-suffix-tree.cc ├── floyd-city-of-blinding-lights.cc ├── fp ├── ape-war.ml ├── area-under-curves-and-volume-of-revolving-a-curv.hs ├── bangalore-bank.hs ├── bitter-chocolate.ml ├── boleyn-salary.ml ├── boleyn-salary_wavelet_matrix.ml ├── brainf-k-interpreter-fp.ml ├── common-divisors.ml ├── convex-hull-fp.ml ├── dice-path.hs ├── different-ways-fp.ml ├── down-with-abstractions.ml ├── down-with-abstractions_heap.ml ├── elementary-watson.ml ├── eval-ex.ml ├── expressions-v2.ml ├── fibonacci-fp.ml ├── fp-array-of-n-elements.hs ├── fp-filter-array.ml ├── fp-filter-positions-in-a-list.ml ├── fp-hello-world-n-times.ml ├── fp-list-length.ml ├── fp-list-replication.ml ├── fp-reverse-a-list.ml ├── fp-sum-of-odd-elements.ml ├── fp-update-list.ml ├── functional-programming-warmups-in-recursion---fibonacci-numbers.ml ├── functional-programming-warmups-in-recursion---gcd.ml ├── functions-and-fractals-sierpinski-triangles.ml ├── functions-or-not.hs ├── huge-gcd-fp.ml ├── infer.ml ├── infer.short.ml ├── intuitive-language.ml ├── kmp-fp.ml ├── kundu-and-bubble-wrap.ml ├── lambda-calculus-reductions-1.txt ├── lambda-calculus-reductions-2.txt ├── lambda-calculus-reductions-3.txt ├── lambda-calculus-reductions-4.txt ├── lambda-march-compute-the-perimeter-of-a-polygon.hs ├── lambda-march-concave-polygon.hs ├── mango.hs ├── messy-medians_treap.ml ├── messy-medians_two_leftist_heap.ml ├── messy-medians_two_pairing_heap.ml ├── minimum-multiple.ml ├── missing-numbers-fp.ml ├── number-of-binary-search-tree.ml ├── order-exercises.hs ├── pascals-triangle.ml ├── password-cracker-fp.ml ├── pentagonal-numbers.ml ├── prefix-compression.hs ├── range-minimum-query.ml ├── regex-to-string-fp.ml ├── reverse-factorization.ml ├── rotate-string.ml ├── sherlock-and-the-maze.hs ├── sherlock-and-the-maze.ml ├── simplify-the-algebraic-expressions.hs ├── string-compression.hs ├── string-o-permute.ml ├── super-queens-on-a-chessboard.ml ├── swap-nodes.ml ├── valid-bst.ml └── while-language-fp.ml ├── game-of-throne-ii.rb ├── game-of-thrones.hs ├── gem-stones.rb ├── get-the-value-of-the-node-at-a-specific-position-from-the-tail.py ├── grid-walking.cc ├── hacker-country.cc ├── hamming-distance.cc ├── heavy-light-2-white-falcon.cc ├── heavy-light-white-falcon.cc ├── heavy-light-white-falcon.lct.cc ├── heavy-light-white-falcon.lct.flip.cc ├── helix.cc ├── hyper-strings.cc ├── insert-a-node-at-a-specific-position-in-a-linked-list.py ├── insert-a-node-at-the-tail-of-a-linked-list.py ├── is-fibo.rb ├── jagia-playing-with-numbers.cc ├── jim-and-his-lan-party.cc ├── jim-and-his-lan-party_binary_search.cc ├── jim-and-his-lan-party_merge_to_larger.cc ├── jim-and-the-skyscrapers.ml ├── journey-scheduling.cc ├── journey-to-the-moon.rb ├── kingdom-connectivity.cc ├── kruskalmstrsub.cc ├── kth-ancestor.cc ├── kundu-and-tree.py ├── letter-islands.cc ├── liars.cc ├── library-query.cc ├── manasa-and-stones.rb ├── manipulative-numbers.cc ├── matrix.cc ├── maximizing-xor.rb ├── median.cc ├── merge-two-sorted-linked-lists.py ├── merging-communities.py ├── misc ├── delete-duplicate-value-nodes-from-a-sorted-linked-list.cc ├── find-hackerrank.rb ├── hackerrank-tweets.rb ├── trignometric-ratios.rb └── valid-pan-format.rb ├── missile-defend.cc ├── p-sequences.cc ├── palindrome-border.cc ├── play-game.rb ├── plus-minus.clj ├── poisonous-plants.cc ├── police-operation.cc ├── print-the-elements-of-a-linked-list-in-reverse.py ├── print-the-elements-of-a-linked-list.py ├── qheap1.hs ├── quadrant-queries.cc ├── queens-on-board.cc ├── quicksort1.cc ├── recalling-early-days-gp-with-trees.cc ├── red-john-is-back.cc ├── repair-roads.cc ├── repeat-k-sums.cc ├── reverse-a-linked-list.py ├── reverse-shuffle-merge.cc ├── road-network.cc ├── rooted_tree.cc ├── roy-and-alpha-beta-trees.cc ├── rust-murderer.cc ├── sam-and-substrings.cc ├── savita-and-friends.cc ├── separate-the-chocolate.cc ├── separate-the-chocolate_simple.cc ├── shell ├── awk-1.sh ├── awk-2.sh ├── awk-3.sh ├── awk-4.sh ├── bash-tutorials---compute-the-average.sh ├── bash-tutorials---looping-with-numbers.sh ├── bash-tutorials-concatenate-an-array-with-itself.sh ├── bash-tutorials-count-the-number-of-elements-in-an-array.sh ├── bash-tutorials-display-the-third-element-of-an-array.sh ├── bash-tutorials-filter-an-array-with-patterns.sh ├── bash-tutorials-read-in-an-array.sh ├── bash-tutorials-remove-the-first-capital-letter-from-each-array-element.sh ├── bash-tutorials-slice-an-array.sh ├── fractal-trees-all.sh ├── lonely-integer-2.sh ├── paste-1.sh ├── paste-2.sh ├── paste-3.sh ├── paste-4.sh ├── sed-command-4.sh ├── sed-command-5.sh ├── text-processing-cut-1.sh ├── text-processing-cut-2.sh ├── text-processing-cut-3.sh ├── text-processing-cut-4.sh ├── text-processing-in-linux-the-grep-command-5.sh └── text-processing-in-linux-the-sed-command-2.sh ├── sherlock-and-anagrams.py ├── sherlock-and-cost.cc ├── sherlock-and-gcd.rb ├── sherlock-and-queries.rb ├── sherlock-and-valid-string.cc ├── simple-array-sum.clj ├── sparse-arrays.py ├── square-subsequences.cc ├── staircase.clj ├── starfleet.cc ├── string-similarity.cc ├── string-similarity.suffix-tree.cc ├── string-transmission.cc ├── subsequence-weighting.ml ├── substring-diff.cc ├── subtrees-and-paths.cc ├── taxicab-drivers-problem.cc ├── the-love-letter-mystery.rb ├── ticket.cc ├── tree-height-of-a-binary-tree.cc ├── tree-inorder-traversal.cc ├── tree-postorder-traversal.cc ├── tree-preorder-traversal.cc ├── triplets.cc ├── tsp-grid.cc ├── two-strings.hs ├── weird-queries.cc ├── white-falcon-and-tree.cc ├── xor-key_implicit_wavelet_tree.cc ├── xor-key_range_tree.cc ├── xor-subsequence.cc ├── xoring-ninja.cc └── xoring-ninja_formula.cc /2d-array.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::max; 2 | use std::io::stdin; 3 | 4 | fn read_line() -> String { 5 | let mut s = String::new(); 6 | stdin().read_line(&mut s); 7 | s.trim_right().to_string() 8 | } 9 | 10 | fn main() { 11 | let mut a = vec![vec![0; 6]; 6]; 12 | for i in 0..6 { 13 | let line = read_line(); 14 | let mut it = line.split(" "); 15 | for j in 0..6 { 16 | a[i][j] = it.next().unwrap().parse::().unwrap(); 17 | } 18 | } 19 | let mut mx = std::i64::MIN; 20 | for i in 1..5 { 21 | for j in 1..5 { 22 | mx = max(mx, a[i][j]+a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1]); 23 | } 24 | } 25 | println!("{}", mx); 26 | } 27 | -------------------------------------------------------------------------------- /2s-complement.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | ll pos(int n) 14 | { 15 | ll s = 0; 16 | for (int k = 30; k >= 0; k--) 17 | if (n & 1 << k) { 18 | n -= 1 << k; 19 | s += n+1; 20 | s += k ? ll(k) << k-1 : 0; 21 | } 22 | return s; 23 | } 24 | 25 | ll neg(int n) 26 | { 27 | ll s = -32 * ll(n); 28 | for (int k = 30; k >= 0; k--) 29 | if (~n >> k & 1) { 30 | n += 1 << k; 31 | s += n; 32 | s -= k ? ll(k) << k-1 : 0; 33 | } 34 | return s; 35 | } 36 | 37 | int main() 38 | { 39 | for (int cc = ri(); cc--; ) { 40 | int a = ri(), b = ri(); 41 | printf("%lld\n", a >= 0 ? pos(b) - (a ? pos(a-1) : 0) 42 | : b < 0 ? neg(a) - (~ b ? neg(b+1) : 0) 43 | : neg(a)+pos(b)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /a-very-big-sum.clj: -------------------------------------------------------------------------------- 1 | (import 'java.util.Scanner) 2 | 3 | (def scan (Scanner. *in*)) 4 | 5 | (let [n (.nextInt scan)] 6 | (println (loop [i n sum 0] 7 | (if (= i 0) 8 | sum 9 | (recur (dec i) (+ sum (.nextInt scan))))))) 10 | -------------------------------------------------------------------------------- /ai/battery.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import numpy as np 3 | X, Y = [], [] 4 | with open('trainingdata.txt') as f: 5 | for line in f.readlines(): 6 | x, y = map(float, line.split(',')) 7 | if x <= 4: 8 | X.append(x) 9 | Y.append(y) 10 | poly = np.poly1d(np.polyfit(X, Y, 1)) 11 | x = float(input()) 12 | print('{:.2f}'.format(poly(x) if x <= 4 else 8)) 13 | -------------------------------------------------------------------------------- /ai/binomial-distribution-1.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import binom 2 | n = 4 3 | p = 0.8 4 | print('{:.3f}'.format(1 - binom.cdf(2, n, p))) 5 | print('{:.3f}'.format(binom.cdf(4-3, n, p))) 6 | -------------------------------------------------------------------------------- /ai/binomial-distribution-2.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import binom 2 | print('{:.3f}'.format(1 - binom.cdf(3-1, 6, 1.09/(1+1.09)))) 3 | -------------------------------------------------------------------------------- /ai/binomial-distribution-3.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import binom 2 | print('{:.3f}'.format(binom.cdf(2, 10, 0.12))) 3 | print('{:.3f}'.format(1 - binom.cdf(2-1, 10, 0.12))) 4 | -------------------------------------------------------------------------------- /ai/botcleanr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | n = 5 3 | x, y = map(int, input().split()) 4 | a = [input() for _ in range(5)] 5 | for i in range(5): 6 | for j in range(5): 7 | if a[i][j] == 'd': 8 | if i == x and j == y: 9 | print('CLEAN') 10 | elif i < x: 11 | print('UP') 12 | elif i > x: 13 | print('DOWN') 14 | elif j < y: 15 | print('LEFT') 16 | elif j > y: 17 | print('RIGHT') 18 | exit() 19 | -------------------------------------------------------------------------------- /ai/computing-the-correlation.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def pearson(xs, ys): 4 | n = len(xs) 5 | e_x = sum(xs)/n 6 | e_x2 = sum(x*x for x in xs)/n 7 | var_x = e_x2-e_x**2 8 | e_y = sum(ys)/n 9 | e_y2 = sum(y*y for y in ys)/n 10 | var_y = e_y2-e_y**2 11 | return sum((x-e_x)*(y-e_y) for x, y in zip(xs, ys))/n/math.sqrt(var_x*var_y) 12 | 13 | n = int(input()) 14 | xs, ys, zs = [], [], [] 15 | for i in range(n): 16 | x, y, z = list(map(int, input().split())) 17 | xs.append(x) 18 | ys.append(y) 19 | zs.append(z) 20 | print('{:.2f}'.format(pearson(xs, ys))) 21 | print('{:.2f}'.format(pearson(ys, zs))) 22 | print('{:.2f}'.format(pearson(xs, zs))) 23 | -------------------------------------------------------------------------------- /ai/correlation-and-regression-lines-6.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import pearsonr 2 | xs = [15,12,8,8,7,7,7,6,5,3] 3 | ys = [10,25,17,11,13,17,20,13,9,15] 4 | print('{:.3f}'.format(pearsonr(xs, ys)[0])) 5 | -------------------------------------------------------------------------------- /ai/correlation-and-regression-lines-7.py: -------------------------------------------------------------------------------- 1 | xs = [15,12,8,8,7,7,7,6,5,3] 2 | ys = [10,25,17,11,13,17,20,13,9,15] 3 | n = len(xs) 4 | sumx = sum(xs) 5 | sumy = sum(ys) 6 | sumxy = sum(x*y for x, y in zip(xs, ys)) 7 | sumxx = sum(x*x for x in xs) 8 | print('{:.3f}'.format((n*sumxy-sumx*sumy) / (n*sumxx-sumx**2))) 9 | #from scipy.stats import linregress 10 | #print('{:.3f}'.format(linregress(xs, ys).slope)) 11 | -------------------------------------------------------------------------------- /ai/correlation-and-regression-lines-8.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | xs = [15,12,8,8,7,7,7,6,5,3] 3 | ys = [10,25,17,11,13,17,20,13,9,15] 4 | b, a = np.linalg.lstsq([[1,x] for x in xs], ys)[0] 5 | print('{:.1f}'.format(10*a+b)) 6 | -------------------------------------------------------------------------------- /ai/maze-escape.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string a[3]; 8 | cin >> a[0]; 9 | cin >> a[0] >> a[1] >> a[2]; 10 | if (a[2][2] == '#' && a[1][2] != '#') cout << "RIGHT"; 11 | else if (a[0][2] == '#' && a[0][1] != '#') cout << "UP"; 12 | else if (a[0][0] == '#' && a[1][0] != '#') cout << "LEFT"; 13 | else if (a[2][0] == '#' && a[2][1] != '#') cout << "DOWN"; 14 | else cout << "UP"; 15 | cout << endl; 16 | } 17 | -------------------------------------------------------------------------------- /ai/nlp-compute-the-cross-entropy.hs: -------------------------------------------------------------------------------- 1 | import Numeric 2 | main = putStrLn $ showFFloat (Just 2) (logBase 2 170) "" 3 | -------------------------------------------------------------------------------- /ai/nlp-compute-the-perplexity.hs: -------------------------------------------------------------------------------- 1 | main = print $ round $ 2 ** 9.91 2 | -------------------------------------------------------------------------------- /ai/normal-distribution-1.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import norm 2 | mu = 30 3 | sigma = 4 4 | print('{:.3f}'.format(norm.cdf(40, mu, sigma))) 5 | print('{:.3f}'.format(1 - norm.cdf(21, mu, sigma))) 6 | print('{:.3f}'.format(norm.cdf(35, mu, sigma) - norm.cdf(30, mu, sigma))) 7 | -------------------------------------------------------------------------------- /ai/normal-distribution-2.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import norm 2 | mu = 20 3 | sigma = 2 4 | print('{:.3f}'.format(norm.cdf(19.5, mu, sigma))) 5 | print('{:.3f}'.format(norm.cdf(22, mu, sigma) - norm.cdf(20, mu, sigma))) 6 | -------------------------------------------------------------------------------- /ai/normal-distribution-3.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import norm 2 | mu = 70 3 | sigma = 10 4 | print('{:.2f}'.format((1 - norm.cdf(80, mu, sigma)) * 100)) 5 | print('{:.2f}'.format((1 - norm.cdf(60, mu, sigma)) * 100)) 6 | print('{:.2f}'.format(norm.cdf(60, mu, sigma) * 100)) 7 | -------------------------------------------------------------------------------- /ai/pacman-bfs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | from collections import deque 3 | r0, c0 = map(int, input().split()) 4 | r1, c1 = map(int, input().split()) 5 | n, m = map(int, input().split()) 6 | a = [input() for _ in range(n)] 7 | p = [[None]*m for _ in range(n)] 8 | p[r0][c0] = (-1, -1) 9 | 10 | expanded = [] 11 | q = deque([(r0,c0)]) 12 | while q: 13 | r, c = q.popleft() 14 | expanded.append((r,c)) 15 | if r == r1 and c == c1: 16 | break 17 | for d in ((-1,0),(0,-1),(0,1),(1,0)): 18 | rr, cc = r+d[0], c+d[1] 19 | if 0 <= rr < n and 0 <= cc < m and a[rr][cc] != '%' and not p[rr][cc]: 20 | p[rr][cc] = (r,c) 21 | q.append((rr,cc)) 22 | 23 | print(len(expanded)) 24 | for r, c in expanded: 25 | print(r, c) 26 | 27 | path = [] 28 | while r1 != r0 or c1 != c0: 29 | path.append((r1, c1)) 30 | r1, c1 = p[r1][c1] 31 | path.append((r0, c0)) 32 | print(len(path)-1) 33 | for r, c in path[::-1]: 34 | print(r, c) 35 | -------------------------------------------------------------------------------- /ai/pacman-dfs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | r0, c0 = map(int, input().split()) 3 | r1, c1 = map(int, input().split()) 4 | n, m = map(int, input().split()) 5 | a = [input() for _ in range(n)] 6 | p = [[None]*m for _ in range(n)] 7 | 8 | expanded = [] 9 | st = [(r0,c0)] 10 | while st: 11 | r, c = st.pop() 12 | expanded.append((r,c)) 13 | if r == r1 and c == c1: 14 | break 15 | for d in ((-1,0),(0,-1),(0,1),(1,0)): 16 | rr, cc = r+d[0], c+d[1] 17 | if 0 <= rr < n and 0 <= cc < m and a[rr][cc] != '%' and not p[rr][cc]: 18 | p[rr][cc] = (r,c) 19 | st.append((rr,cc)) 20 | 21 | print(len(expanded)) 22 | for r, c in expanded: 23 | print(r, c) 24 | 25 | path = [] 26 | while r1 != r0 or c1 != c0: 27 | path.append((r1, c1)) 28 | r1, c1 = p[r1][c1] 29 | path.append((r0, c0)) 30 | print(len(path)-1) 31 | for r, c in reversed(path): 32 | print(r, c) 33 | -------------------------------------------------------------------------------- /ai/predicting-house-prices.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | f, n = map(int, input().split()) 3 | A = np.zeros((n, f+1)) 4 | B = np.zeros(n) 5 | for i in range(n): 6 | xs = list(map(float, input().split())) 7 | A[i] = [1] + xs[:-1] 8 | B[i] = xs[-1] 9 | coef = np.linalg.lstsq(A, B)[0] 10 | for i in range(int(input())): 11 | print(np.dot(coef, [1] + list(map(float, input().split())))) 12 | -------------------------------------------------------------------------------- /ai/predicting-office-space-price.py: -------------------------------------------------------------------------------- 1 | from sklearn.preprocessing import PolynomialFeatures 2 | from sklearn.linear_model import LinearRegression 3 | import numpy as np 4 | 5 | f, n = map(int, input().split()) 6 | poly = PolynomialFeatures(3) 7 | A, B = np.empty((n, f)), np.empty(n) 8 | for i in range(n): 9 | xs = list(map(float, input().split())) 10 | A[i] = xs[:-1] 11 | B[i] = xs[-1] 12 | lr = LinearRegression() 13 | lr.fit(poly.fit_transform(A), B) 14 | for i in range(int(input())): 15 | xs = list(map(float, input().split())) 16 | print(lr.predict(poly.fit_transform([xs]))[0]) 17 | -------------------------------------------------------------------------------- /ai/saveprincess.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | n = int(input()) 3 | a = [] 4 | for i in range(n): 5 | a.append(input()) 6 | for j in range(n): 7 | if a[i][j] == 'm': 8 | x, y = i, j 9 | if a[i][j] == 'p': 10 | xx, yy = i, j 11 | while x != xx or y != yy: 12 | if x < xx: 13 | print('DOWN') 14 | x += 1 15 | continue 16 | if x > xx: 17 | print('UP') 18 | x -= 1 19 | continue 20 | if y < yy: 21 | print('RIGHT') 22 | y += 1 23 | continue 24 | if y > yy: 25 | print('LEFT') 26 | y -= 1 27 | continue 28 | -------------------------------------------------------------------------------- /ai/saveprincess2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | n = int(input()) 3 | r, c = map(int, input().split()) 4 | for i in range(n): 5 | a = input() 6 | for j in range(n): 7 | if a[j] == 'p': 8 | if i < r: 9 | print('UP') 10 | exit() 11 | if i > r: 12 | print('DOWN') 13 | exit() 14 | if j < c: 15 | print('LEFT') 16 | exit() 17 | if j > c: 18 | print('RIGHT') 19 | exit() 20 | -------------------------------------------------------------------------------- /ai/stat-warmup.py: -------------------------------------------------------------------------------- 1 | import math 2 | import numpy as np 3 | from scipy import stats 4 | 5 | n = int(input()) 6 | xs = np.array(list(map(int, input().split()))) 7 | mean = xs.mean() 8 | std = xs.std() 9 | confidence_interval_critical_value = 1.96 10 | print('{:.1f}'.format(mean)) 11 | print('{:.1f}'.format(np.median(xs))) 12 | print(stats.mode(xs)[0][0]) 13 | print('{:.1f}'.format(std)) 14 | print('{:.1f} {:.1f}'.format(mean-confidence_interval_critical_value*std/math.sqrt(n), mean+confidence_interval_critical_value*std/math.sqrt(n))) 15 | -------------------------------------------------------------------------------- /ai/stockprediction.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import math 3 | import numpy as np 4 | 5 | def stochastic_oscillator(a, k): 6 | mn = a[-k:].min() 7 | mx = a[-k:].max() 8 | return (a[-1]-mn)/(mx-mn) if mn < mx else 0 9 | 10 | m, k, _ = input().split() 11 | m = float(m) 12 | k = int(k) 13 | names = [] 14 | own = [] 15 | prices = [] 16 | for data in range(k): 17 | temp = input().split() 18 | names.append(temp[0]) 19 | own.append(int(temp[1])) 20 | prices.append([float(i) for i in temp[2:7]]) 21 | 22 | prices = np.array(prices) 23 | std = prices.std(1) 24 | output = [] 25 | for i in reversed(np.argsort(std)): 26 | sa = stochastic_oscillator(prices[i], 3) 27 | if sa > 0.8 and own[i]: 28 | output.append('{} SELL {}'.format(names[i], own[i])) 29 | elif sa < 0.2 and m: 30 | num = int(math.floor(m / prices[i][-1] - 1e-9)) 31 | if num: 32 | m -= num * prices[i][-1] 33 | output.append('{} BUY {}'.format(names[i], num)) 34 | print(len(output)) 35 | for line in output: 36 | print(line) 37 | -------------------------------------------------------------------------------- /ai/the-best-aptitude-test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | for _ in range(int(input())): 4 | input() 5 | gpa = list(map(float, input().split())) 6 | max_coef = -2 7 | for i in range(5): 8 | xs = list(map(float, input().split())) 9 | coef = np.corrcoef(gpa, xs)[0,1] 10 | if coef > max_coef: 11 | max_coef = coef 12 | max_i = i 13 | print(max_i+1) 14 | -------------------------------------------------------------------------------- /ai/the-trigram.hs: -------------------------------------------------------------------------------- 1 | main = putStrLn "around the world" 2 | --{-# LANGUAGE OverloadedStrings #-} 3 | --import Control.Monad.State 4 | --import Data.Char 5 | --import Data.List 6 | --import qualified Data.Map as M 7 | --import Data.Functor 8 | --import qualified Data.ByteString.Char8 as B 9 | -- 10 | --main = do 11 | -- ws <- (B.words . B.map toLower . B.filter (liftM2 (||) isAlphaNum isSpace)) <$> B.getContents 12 | -- print ws 13 | -- let m = foldl' (\m x -> M.insertWith (+) x (1::Int) m) M.empty $ zip3 ws (tail ws) (tail $ tail ws) 14 | -- let ((x,y,z), c) = M.foldWithKey (\k v o -> if v >= snd o then (k,v) else o) (("","",""),0) m 15 | -- print c 16 | -- B.putStrLn $ B.concat [x, " ", y, " ", z] 17 | -------------------------------------------------------------------------------- /almost-sorted-interval.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 1000000; 16 | int c[N]; 17 | 18 | int main() 19 | { 20 | int n = ri(); 21 | ll ans = 0; 22 | stack inc, dec; 23 | REP(i, n) { 24 | int x = ri()-1; 25 | for (; ! inc.empty() && inc.top() > x; inc.pop()) 26 | if (! --c[dec.top()]) 27 | dec.pop(); 28 | inc.push(x); 29 | c[x] = 1; 30 | for (; ! dec.empty() && dec.top() < x; dec.pop()) 31 | c[x] += c[dec.top()]; 32 | dec.push(x); 33 | ans += c[x]; 34 | } 35 | printf("%lld\n", ans); 36 | } 37 | -------------------------------------------------------------------------------- /anagram.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | s = gets.chomp 3 | if s.size % 2 == 1 4 | p -1 5 | else 6 | c = Hash.new 0 7 | s[0...s.size/2].each_byte {|i| c[i] += 1 } 8 | s[s.size/2..-1].each_byte {|i| c[i] -= 1 } 9 | p c.each_value.inject(0) {|x,y| x+y.abs }/2 10 | end 11 | } 12 | -------------------------------------------------------------------------------- /and-product.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ri() 5 | { 6 | int x; 7 | scanf("%d", &x); 8 | return x; 9 | } 10 | 11 | int main() 12 | { 13 | for (int n = ri(); n--; ) { 14 | unsigned a = ri(), b = ri(); 15 | printf("%u\n", a == b ? a : a & -1u << 31-__builtin_clz(a^b)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /and-xor-or_bst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #include 6 | #include 7 | using namespace __gnu_pbds; 8 | 9 | typedef pair pii; 10 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 11 | #define REP(i, n) FOR(i, 0, n) 12 | #define fi first 13 | #define se second 14 | 15 | int ri() 16 | { 17 | int x; 18 | scanf("%d", &x); 19 | return x; 20 | } 21 | 22 | const int N = 1000000; 23 | pii a[N]; 24 | 25 | int main() 26 | { 27 | tree t; 28 | int n = ri(), ans = 0; 29 | REP(i, n) 30 | a[i] = {ri(), i}; 31 | sort(a, a+n); 32 | REP(i, n) { 33 | t[a[i].se] = a[i].fi; 34 | auto succ = t.upper_bound(a[i].se); 35 | if (succ != t.end()) 36 | ans = max(ans, a[i].fi ^ succ->se); 37 | auto pred = t.find(a[i].se); 38 | if (pred != t.begin()) { 39 | --pred; 40 | ans = max(ans, a[i].fi ^ pred->se); 41 | } 42 | } 43 | printf("%d\n", ans); 44 | } 45 | -------------------------------------------------------------------------------- /and-xor-or_stack.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | int main() 16 | { 17 | stack s; 18 | int n = ri(), ans = 0; 19 | REP(i, n) { 20 | int x = ri(); 21 | while (! s.empty() && s.top() > x) { 22 | ans = max(ans, s.top() ^ x); 23 | s.pop(); 24 | } 25 | if (! s.empty()) 26 | ans = max(ans, s.top() ^ x); 27 | s.push(x); 28 | } 29 | printf("%d\n", ans); 30 | } 31 | -------------------------------------------------------------------------------- /angry-children.rb: -------------------------------------------------------------------------------- 1 | n = gets.to_i 2 | k = gets.to_i 3 | a = n.times.map { gets.to_i }.sort! 4 | s = a[k-1]-a[0] 5 | k.upto(n-1) {|i| 6 | t = a[i]-a[i-k+1] 7 | s = t if t < s 8 | } 9 | p s 10 | -------------------------------------------------------------------------------- /array-rotation-2.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad.State 2 | import Data.Char 3 | import Data.Functor 4 | import Data.List 5 | import Data.Maybe 6 | import Text.Printf 7 | import qualified Data.ByteString.Char8 as B 8 | 9 | int = state $ fromJust . B.readInt . B.dropWhile isSpace 10 | 11 | parse = do 12 | n <- int 13 | d <- int 14 | a <- replicateM n int 15 | return (n, d, a) 16 | 17 | main = do 18 | (n, d, a) <- evalState parse <$> B.getContents 19 | let (b, c) = splitAt d a 20 | forM_ c (printf "%d ") 21 | forM_ b (printf "%d ") 22 | putChar '\n' 23 | -------------------------------------------------------------------------------- /arrays-ds.clj: -------------------------------------------------------------------------------- 1 | (require '[clojure.string :refer [join split]]) 2 | 3 | (println (join " " (reverse (split (second (line-seq (java.io.BufferedReader. *in*))) #" ")))) 4 | -------------------------------------------------------------------------------- /bfsshortreach.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | typedef pair pii; 9 | #define REP(i, n) for (int i = 0; i < (n); i++) 10 | #define pb push_back 11 | 12 | int ri() 13 | { 14 | int x; 15 | scanf("%d", &x); 16 | return x; 17 | } 18 | 19 | const int N = 3000; 20 | int d[N]; 21 | vector es[N]; 22 | 23 | void bfs(int n, int src) 24 | { 25 | fill_n(d, n, -1); 26 | d[src] = 0; 27 | queue q; 28 | q.push(src); 29 | while (! q.empty()) { 30 | int u = q.front(); 31 | q.pop(); 32 | for (int v: es[u]) 33 | if (d[v] < 0) { 34 | d[v] = d[u]+1; 35 | q.push(v); 36 | } 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | for (int cc = ri(); cc--; ) { 43 | int n = ri(), m = ri(); 44 | REP(i, n) 45 | es[i].clear(); 46 | while (m--) { 47 | int u = ri()-1, v = ri()-1; 48 | es[u].pb(v); 49 | es[v].pb(u); 50 | } 51 | int s = ri()-1; 52 | bfs(n, s); 53 | REP(i, n) 54 | if (i != s) 55 | printf("%d ", ~ d[i] ? 6*d[i] : -1); 56 | puts(""); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /chocolate-feast.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | n, c, m = gets.split.map &:to_i 3 | w = s = n/c 4 | while w >= m 5 | s += w/m 6 | w = w%m+w/m 7 | end 8 | p s 9 | } 10 | -------------------------------------------------------------------------------- /clique.clj: -------------------------------------------------------------------------------- 1 | (import 'java.util.Scanner) 2 | 3 | (def scan (Scanner. *in*)) 4 | 5 | (defn turan [N R] 6 | (let [sz0 (quot N R) 7 | sz1 (inc sz0) 8 | c1 (rem N R) 9 | c0 (- R c1)] 10 | (+ (* c0 sz0 c1 sz1) 11 | (quot (* c0 (dec c0) sz0 sz0) 2) 12 | (quot (* c1 (dec c1) sz1 sz1) 2)))) 13 | 14 | (dotimes [_ (.nextInt scan)] 15 | (let [N (.nextInt scan) 16 | M (.nextInt scan)] 17 | (println 18 | (loop [l 1 r N] 19 | (if (= l r) 20 | l 21 | (let [mi (quot (+ l r) 2) 22 | x (turan N mi)] 23 | (if (< x M) 24 | (recur (inc mi) r) 25 | (recur l mi)))))))) 26 | -------------------------------------------------------------------------------- /common-child.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 5001; 17 | char a[N], b[N]; 18 | int s[2][N]; 19 | 20 | int main() 21 | { 22 | scanf("%s%s", a, b); 23 | int m = strlen(a), n = strlen(b); 24 | REP(i, m) 25 | REP(j, n) 26 | s[i+1&1][j+1] = a[i] == b[j] ? s[i&1][j]+1 : max(s[i&1][j+1], s[i+1&1][j]); 27 | printf("%d\n", s[m&1][n]); 28 | } 29 | -------------------------------------------------------------------------------- /compare-two-linked-lists.py: -------------------------------------------------------------------------------- 1 | def CompareLists(headA, headB): 2 | while headA is not None and headB is not None: 3 | if headA.data != headB.data: 4 | return 0 5 | headA, headB = headA.next, headB.next 6 | return 1 if headA is None and headB is None else 0 7 | -------------------------------------------------------------------------------- /components-in-graph.py: -------------------------------------------------------------------------------- 1 | def find(x): 2 | while uf[x] >= 0: 3 | if uf[uf[x]] >= 0: 4 | uf[x] = uf[uf[x]] 5 | x = uf[x] 6 | return x 7 | 8 | n = int(input()) 9 | uf = [-1]*(2*n) 10 | for i in range(n): 11 | x, y = map(int, input().split()) 12 | x, y = find(x-1), find(y-1) 13 | if x != y: 14 | if uf[x] > uf[y]: 15 | x, y = y, x 16 | uf[x] += uf[y] 17 | uf[y] = x 18 | mn, mx = 2*n, 0 19 | for i in range(2*n): 20 | if uf[i] < -1: 21 | mn = min(mn, - uf[i]) 22 | mx = max(mx, - uf[i]) 23 | print(mn, mx) 24 | -------------------------------------------------------------------------------- /contests/101hack32/binary-string-game.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 7 | #define REP(i, n) for (int i = 0; i < (n); i++) 8 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 1001, M = 1000000; 18 | char a[M+1]; 19 | int nim[N], mex[N]; 20 | 21 | int main() 22 | { 23 | int tick = 0; 24 | ROF(_, 0, ri()) { 25 | int k = ri(); 26 | scanf("%s", a); 27 | 28 | fill_n(nim, k, 0); 29 | FOR(i, k, N) { 30 | tick++; 31 | REP(j, i-k+1) 32 | mex[nim[j]^nim[i-k-j]] = tick; 33 | nim[i] = find_if_not(mex, mex+N, bind(equal_to(), placeholders::_1, tick)) - mex; 34 | } 35 | int sum = 0; 36 | for (int j, i = 0; a[i]; ) 37 | if (a[i] == '0') 38 | i++; 39 | else { 40 | for (j = i+1; a[j] == '1'; j++); 41 | sum ^= nim[k != 2 || j-i < 36 ? j-i : (j-i-36)%34+36]; 42 | i = j; 43 | } 44 | puts(sum ? "Alice" : "Bob"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /contests/101hack32/sorting-under-square-distance.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | typedef pair pii; 8 | #define REP(i, n) for (int i = 0; i < (n); i++) 9 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 10 | 11 | int ri() 12 | { 13 | int x; 14 | scanf("%d", &x); 15 | return x; 16 | } 17 | 18 | const int N = 100000; 19 | int a[N], c[N], emp[N]; 20 | pii b[N]; 21 | 22 | void add(int n, int x) 23 | { 24 | for (; x < n; x |= x+1) 25 | emp[x]++; 26 | } 27 | 28 | int getSum(int x) 29 | { 30 | int s = 0; 31 | for (; x; x &= x-1) 32 | s += emp[x-1]; 33 | return s; 34 | } 35 | 36 | int main() 37 | { 38 | int n = ri(); 39 | REP(i, n) { 40 | a[i] = ri(); 41 | b[i] = {a[i], i}; 42 | } 43 | sort(b, b+n); 44 | REP(i, n) 45 | c[a[i] = lower_bound(b, b+n, pii{a[i], i}) - b] = i; 46 | ll ans = 0; 47 | ROF(i, 0, n) { 48 | int j = c[i]; 49 | ans += i-(j-getSum(j)); 50 | add(n, j); 51 | } 52 | printf("%lld\n", ans); 53 | } 54 | -------------------------------------------------------------------------------- /contests/101hack32/straight-flush.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int main() 9 | { 10 | char a[9]; 11 | int b[15] = {}, c[26] = {}; 12 | REP(i, 5) { 13 | fgets(a, sizeof a, stdin); 14 | c[a[1]-'A']++; 15 | switch (a[0]) { 16 | case '2' ... '9': b[a[0]-'0']++; break; 17 | case 'T': b[10]++; break; 18 | case 'J': b[11]++; break; 19 | case 'Q': b[12]++; break; 20 | case 'K': b[13]++; break; 21 | case 'A': b[1]++; b[14]++; break; 22 | } 23 | } 24 | FOR(i, 1, 11) 25 | if (b[i] && b[i+1] && b[i+2] && b[i+3] && b[i+4] && *max_element(c, c+26) == 5) 26 | return puts("YES"), 0; 27 | puts("NO"); 28 | } 29 | -------------------------------------------------------------------------------- /contests/101hack32/tree-construction.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 1000; 15 | int d[N], p[N]; 16 | 17 | int main() 18 | { 19 | int n = ri(); 20 | multimap a; 21 | REP(i, n) 22 | a.emplace(d[i] = ri(), i); 23 | while (a.size() > 1) { 24 | auto it = a.begin(); 25 | int v = it->second; 26 | a.erase(it); 27 | it = a.end(); 28 | --it; 29 | int u = it->second; 30 | a.erase(it); 31 | p[v] = u+1; 32 | a.emplace(--d[u], u); 33 | } 34 | REP(i, n) 35 | printf("%d\n", p[i]); 36 | } 37 | -------------------------------------------------------------------------------- /contests/101hack37/long-permutation.hs: -------------------------------------------------------------------------------- 1 | import Data.Array 2 | import Data.Functor 3 | import Data.List 4 | 5 | main = do 6 | [n, m] <- (map read . words) <$> getLine :: IO [Int] 7 | as <- (map (pred . read) . words) <$> getLine :: IO [Int] 8 | let a = listArray (0, n-1) as 9 | go i x | i < 0 = x 10 | | y == n-1 = y+i 11 | | otherwise = go (i-1) y 12 | where 13 | y = a ! (x+1) 14 | print $ go (m-1) (-1) + 1 15 | -------------------------------------------------------------------------------- /contests/101hack37/tripartite-matching.edge.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 8 | #define REP(i, n) FOR(i, 0, n) 9 | 10 | const long N = 100000, SN = 300; 11 | 12 | vector a[3][N]; 13 | unordered_set b[3][N]; 14 | 15 | int main() 16 | { 17 | ios_base::sync_with_stdio(0); 18 | long n, m, u, v, ans = 0; 19 | cin >> n; 20 | REP(k, 3) 21 | for (cin >> m; m--; ) { 22 | cin >> u >> v; 23 | a[k][u-1].push_back(v-1); 24 | a[k][v-1].push_back(u-1); 25 | b[k][u-1].insert(v-1); 26 | b[k][v-1].insert(u-1); 27 | } 28 | REP(u, n) 29 | for (long v: a[0][u]) 30 | if (a[2][u].size() < a[1][v].size()) 31 | for (long w: a[2][u]) 32 | ans += b[1][v].count(w); 33 | else 34 | for (long w: a[1][v]) 35 | ans += b[2][u].count(w); 36 | cout << ans << endl; 37 | } 38 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/increasing-shortest-path.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define REP(i, n) for (int i = 0; i < (n); i++) 8 | #define fi first 9 | #define se second 10 | 11 | int ri() 12 | { 13 | int x; 14 | scanf("%d", &x); 15 | return x; 16 | } 17 | 18 | const int N = 150, M = 3000; 19 | int a[N][N][N]; 20 | pair> e[M]; 21 | 22 | int main() 23 | { 24 | int cc = ri(); 25 | while (cc--) { 26 | int n = ri(), m = ri(), q = ri(); 27 | memset(a, 0x3f, sizeof a); 28 | REP(i, n) 29 | a[i][i][0] = 0; 30 | REP(i, m) { 31 | int v = ri()-1, u = ri()-1, w = ri(); 32 | e[i] = {w, {v, u}}; 33 | } 34 | sort(e, e+m); 35 | REP(i, m) 36 | REP(j, n) 37 | REP(k, n-1) 38 | a[j][e[i].se.se][k+1] = min(a[j][e[i].se.se][k+1], a[j][e[i].se.fi][k]+e[i].fi); 39 | while (q--) { 40 | int v = ri()-1, u = ri()-1, w = min(ri()+1, n), ans = *min_element(a[v][u], a[v][u]+w); 41 | printf("%d\n", ans < 0x3f3f3f ? ans : -1); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int ri() 4 | { 5 | int x; 6 | scanf("%d", &x); 7 | return x; 8 | } 9 | 10 | int main() 11 | { 12 | int cc = ri(); 13 | while (cc--) { 14 | int n = ri(), m = ri(), x = ri(); 15 | puts((n-1)%m == x-1 ? "YES" : "NO"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/mario-kart.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 100, X = 1000; 17 | int a[N], dp[X], d[N], q[N]; 18 | 19 | int main() 20 | { 21 | int cc = ri(); 22 | while (cc--) { 23 | int n = ri(), m = ri(), l = ri(); 24 | REP(i, n) 25 | a[i] = ri(); 26 | sort(a, a+n); 27 | fill_n(dp, X, l+1); 28 | dp[0] = 0; 29 | REP(i, m) { 30 | int c = ri(), v = ri(); 31 | ROF(j, v, X) 32 | dp[j] = min(dp[j], dp[j-v]+c); 33 | } 34 | fill_n(d, n, -1); 35 | d[0] = 0; 36 | q[0] = 0; 37 | for (int fr = 0, re = 1; fr < re; fr++) { 38 | int v = q[fr]; 39 | REP(u, n) 40 | if (d[u] < 0 && dp[abs(a[v]-a[u])] <= l) { 41 | d[u] = d[v]+1; 42 | q[re++] = u; 43 | } 44 | } 45 | printf("%d\n", d[n-1]); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/modified-lcs.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | typedef pair pii; 8 | #define fi first 9 | #define se second 10 | 11 | int ri() 12 | { 13 | int x; 14 | scanf("%d", &x); 15 | return x; 16 | } 17 | 18 | ll rl() 19 | { 20 | ll x; 21 | scanf("%lld", &x); 22 | return x; 23 | } 24 | 25 | pii halfEuclid(int a, int b) 26 | { 27 | int x = 1, y = 0; 28 | while (b) { 29 | int q = a/b, t = a%b; 30 | a = b; b = t; 31 | t = x-q*y; x = y; y = t; 32 | } 33 | return {a, x}; 34 | } 35 | 36 | int main() 37 | { 38 | int cc = ri(); 39 | while (cc--) { 40 | ll n1 = rl(); 41 | int f1 = ri(), d1 = ri(); 42 | ll n2 = rl(); 43 | int f2 = ri(), d2 = ri(); 44 | auto r = halfEuclid(d1, d2); 45 | int j1 = d2/r.fi, j2 = d1/r.fi, 46 | i1 = ((f2-f1)/r.fi*r.se%j1+j1)%j1, 47 | i2 = (f1+ll(d1)*i1-f2)/d2; 48 | printf("%lld\n", 1 + min((n1-i1-1)/j1, (n2-i2-1)/j2)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/nassas-robot.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ri() 5 | { 6 | int x; 7 | scanf("%d", &x); 8 | return x; 9 | } 10 | 11 | char a[100001]; 12 | 13 | int main() 14 | { 15 | int cc = ri(); 16 | while (cc--) { 17 | int x = 0, y = 0, z = 0; 18 | scanf("%s", a); 19 | for (char *p = a; *p; p++) 20 | switch (*p) { 21 | case 'L': x--; break; 22 | case 'R': x++; break; 23 | case 'U': y++; break; 24 | case 'D': y--; break; 25 | case '?': z++; break; 26 | } 27 | printf("%d %d %d %d\n", x-z, y-z, x+z, y+z); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/omar-loves-candies.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | #define REP(i, n) for (int i = 0; i < (n); i++) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 1000; 17 | ll a[N][N]; 18 | 19 | int main() 20 | { 21 | int cc = ri(); 22 | while (cc--) { 23 | int n = ri(), m = ri(); 24 | REP(i, n) 25 | REP(j, m) { 26 | a[i][j] = ri(); 27 | if (i) a[i][j] += a[i-1][j]; 28 | if (j) a[i][j] += a[i][j-1]; 29 | if (i && j) a[i][j] -= a[i-1][j-1]; 30 | } 31 | ll ans = LLONG_MIN; 32 | REP(i, n) 33 | REP(j, m) { 34 | ll t = a[n-1][m-1]; 35 | if (i) t -= a[i-1][m-1]; 36 | if (j) t -= a[n-1][j-1]; 37 | if (i && j) t += a[i-1][j-1]; 38 | ans = max(ans, t); 39 | } 40 | printf("%lld\n", ans); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/omars-bug.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | int main() 15 | { 16 | int cc = ri(); 17 | while (cc--) { 18 | int n = ri(), x = ri(), y = ri(), f = 0; 19 | if (y == 1) 20 | REP1(i, n) 21 | printf("%d%c", i+(i>=x), i == n ? '\n' : ' '); 22 | else { 23 | REP1(i, n-1) 24 | printf("%d ", i); 25 | printf("%d\n", max(x, n)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /contests/2013-acm-acpc/the-alphabet-sticker.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | const int N = 10000, MOD = 1000000007; 14 | char a[N+1]; 15 | 16 | int main() 17 | { 18 | int cc = ri(); 19 | while (cc--) { 20 | ll ans = 1; 21 | scanf("%s", a); 22 | for (int j, i = 0; a[i]; ) 23 | if (a[i] != '?') 24 | i++; 25 | else { 26 | for (j = i; a[j] && a[j] == '?'; j++); 27 | if (i && a[j] && a[i-1] != a[j]) 28 | ans = ans*(j-i+1)%MOD; 29 | i = j; 30 | } 31 | printf("%lld\n", ans); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /contests/2014-icpc-central-europe-regional-contest-practice/good-morning.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let () = 4 | for i = 1 to read_int 0 do 5 | let n = read_int 0 in 6 | let rec g x q = 7 | if x = 0 then 8 | true 9 | else ( 10 | let p = x mod 10 in 11 | if q < 0 then 12 | g (x/10) p 13 | else if p = 0 then 14 | if q > 0 then 15 | false 16 | else 17 | g (x/10) p 18 | else if q = 0 then 19 | if p mod 3 = 0 then 20 | false 21 | else 22 | g (x/10) p 23 | else if (p-1) mod 3 > (q-1) mod 3 || (p-1)/3 > (q-1)/3 then 24 | false 25 | else 26 | g (x/10) p 27 | ) in 28 | let rec f x = 29 | if g (n+x) (-1) then n+x 30 | else if g (n-x) (-1) then n-x 31 | else f (x+1) 32 | in 33 | f 0 |> Printf.printf "%d\n" 34 | done 35 | -------------------------------------------------------------------------------- /contests/2014-icpc-central-europe-regional-contest-practice/sums.ml: -------------------------------------------------------------------------------- 1 | open Int64 2 | 3 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 4 | let read_int64 _ = Scanf.bscanf Scanf.Scanning.stdib " %Ld" (fun x -> x) 5 | 6 | let () = 7 | for i = 1 to read_int 0 do 8 | let n = read_int64 0 in 9 | let n2 = add n n in 10 | let rec f x = 11 | if mul x x > n2 then 12 | 0 13 | else if rem n2 x = 0L then ( 14 | let y = div n2 x in 15 | if logand (sub x y) 1L <> 0L then 16 | to_int x 17 | else 18 | f (add x 1L) 19 | ) else 20 | f (add x 1L) 21 | in 22 | let x = f 2L in 23 | if x = 0 then 24 | print_endline "IMPOSSIBLE" 25 | else ( 26 | let y = div n2 (of_int x) |> to_int in 27 | Printf.printf "%Ld = %d" n ((y-x+1)/2); 28 | for i = (y-x+1)/2+1 to (y+x)/2 do 29 | Printf.printf " + %d" i 30 | done; 31 | print_endline "" 32 | ) 33 | done 34 | -------------------------------------------------------------------------------- /contests/2014-icpc-central-europe-regional-contest-practice/the-imp.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef pair pii; 8 | #define REP(i, n) for (int i = 0; i < (n); i++) 9 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 10 | #define fi first 11 | #define se second 12 | 13 | int ri() 14 | { 15 | int x; 16 | scanf("%d", &x); 17 | return x; 18 | } 19 | 20 | const int N = 150000; 21 | pii a[N]; 22 | 23 | int main() 24 | { 25 | for (int cases = ri(); cases--; ) { 26 | int n = ri(), k = ri(), dp[9+2] = {}; 27 | REP(i, n) { 28 | a[i].fi = ri(); 29 | a[i].se = ri(); 30 | } 31 | dp[k+1] = INT_MAX; 32 | sort(a, a+n); 33 | ROF(i, 0, n) 34 | REP(j, k+1) 35 | dp[j] = max(dp[j], min(a[i].fi, dp[j+1]) - a[i].se); 36 | printf("%d\n", dp[0]); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /contests/2014-icpc-north-central-regional-north-america-practice/locked-treasure.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef int64_t ll; 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int S = 30+1; 17 | ll c[S][S]; 18 | 19 | int main() 20 | { 21 | REP(i, S) { 22 | c[i][0] = 1; 23 | REP1(j, i) 24 | c[i][j] = c[i-1][j-1] + c[i-1][j]; 25 | } 26 | int cases = ri(); 27 | REP1(cc, cases) { 28 | int n = ri(), k = ri(); 29 | printf("Case %d: %lld\n", cc, c[n][k-1]); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /contests/2014-icpc-north-central-regional-north-america-practice/preorder-traversals.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char c; 8 | int cases = 0, x, p = 0; 9 | vector s; 10 | bool tak = true; 11 | while (scanf("%d", &x) == 1) { 12 | if (x < 0) { 13 | printf("Case %d: %s\n", ++cases, tak ? "yes" : "no"); 14 | p = 0; 15 | tak = true; 16 | s.clear(); 17 | } else if (tak) { 18 | if (x <= p) 19 | tak = false; 20 | else { 21 | while (s.size() && s.back() <= x) { 22 | if (s.back() == x) 23 | tak = false; 24 | else 25 | p = s.back(); 26 | s.pop_back(); 27 | } 28 | s.push_back(x); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /contests/2014-icpc-north-central-regional-north-america-practice/rank-order.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef pair pii; 8 | #define REP(i, n) for (int i = 0; i < (n); i++) 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 1000000; 18 | pii a[N], b[N]; 19 | 20 | int main() 21 | { 22 | int cases = 0, n; 23 | while (scanf("%d", &n) == 1) { 24 | REP(i, n) 25 | a[i] = {ri(), i}; 26 | REP(i, n) 27 | b[i] = {ri(), i}; 28 | sort(a, a+n, greater()); 29 | sort(b, b+n, greater()); 30 | int ans = 0; 31 | REP(i, n) 32 | if (a[i].second != b[i].second) { 33 | ans = i+1; 34 | break; 35 | } 36 | printf("Case %d: ", ++cases); 37 | if (ans) printf("%d\n", ans); 38 | else puts("agree"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /contests/2014-icpc-north-central-regional-north-america-practice/restaurant-ratings.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int S = 30+15, M = 15; 17 | ll c[S][M]; 18 | 19 | int main() 20 | { 21 | REP(i, S) { 22 | c[i][0] = 1; 23 | FOR(j, 1, min(M, i+1)) 24 | c[i][j] = c[i-1][j-1] + c[i-1][j]; 25 | } 26 | int cases = 0, n, a[M]; 27 | while (n = ri()) { 28 | int s = 0; 29 | ll ans = 0; 30 | REP(i, n) 31 | s += a[i] = ri(); 32 | REP(i, s) 33 | ans += c[i+n-1][n-1]; 34 | REP(i, n-1) { 35 | REP(j, a[i]) 36 | ans += c[s-j+n-2-i][n-2-i]; 37 | s -= a[i]; 38 | } 39 | printf("Case %d: %lld\n", ++cases, ans+1); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /contests/2014-icpc-north-central-regional-north-america-practice/server.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (int i = 0; i < (n); i++) 5 | 6 | int main() 7 | { 8 | int cases = 0, n, t, x; 9 | while (scanf("%d%d", &n, &t) == 2) { 10 | int j = 0; 11 | REP(i, n) { 12 | scanf("%d", &x); 13 | if (i == j && x <= t) 14 | t -= x, j++; 15 | } 16 | printf("Case %d: %d\n", ++cases, j); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /contests/2014-icpc-north-central-regional-north-america-practice/the-binary-search-efficiency-doubter.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 5 | 6 | const int N = 10000000; 7 | int a[N]; 8 | 9 | int main() 10 | { 11 | int cases = 0, n; 12 | FOR(i, 1, N) 13 | a[i] = i + a[i/2] + a[(i-1)/2]; 14 | while (scanf("%d", &n) == 1) 15 | printf("Case %d: %d\n", ++cases, a[n]); 16 | } 17 | -------------------------------------------------------------------------------- /contests/2014-icpc-northwestern-european-regional-practice/cent-savings.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 6 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int D = 20; 16 | int dp[D+1]; 17 | #define ROUND(x) (((x)+5)/10*10) 18 | 19 | int main() 20 | { 21 | int n = ri(), d = ri(); 22 | REP1(i, n) { 23 | int a = ri(); 24 | ROF(j, 0, d) 25 | dp[j+1] = min(dp[j+1]+a, ROUND(dp[j]+a)); 26 | dp[0] += a; 27 | } 28 | printf("%d\n", ROUND(dp[d])); 29 | } 30 | -------------------------------------------------------------------------------- /contests/2014-icpc-northwestern-european-regional-practice/euclidean-tsp.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double n, p, s, v; 6 | 7 | double f(double c) 8 | { 9 | return n*pow(log(n)/log(2), sqrt(2)*c)/p+s/(c*v); 10 | } 11 | 12 | double goldenSectionSearch(double a, double b) 13 | { 14 | double r = (sqrt(5)-1)/2; 15 | double x1 = b-(b-a)*r, x2 = a+(b-a)*r, f1 = f(x1), f2 = f(x2); 16 | while (b-a > 1e-10) { 17 | if (f1 < f2) { 18 | b = x2; x2 = x1; f2 = f1; 19 | x1 = b-(b-a)*r; f1 = f(x1); 20 | } else { 21 | a = x1; x1 = x2; f1 = f2; 22 | x2 = a+(b-a)*r; f2 = f(x2); 23 | } 24 | } 25 | return (a+b)/2; 26 | } 27 | 28 | int main() 29 | { 30 | cin >> n >> p >> s >> v; 31 | p *= 1e9; 32 | double c = goldenSectionSearch(1, 40); 33 | cout.setf(ios::fixed); 34 | cout.precision(46); 35 | cout << f(c)+s/v << ' ' << c << endl; 36 | } 37 | -------------------------------------------------------------------------------- /contests/2014-icpc-northwestern-european-regional-practice/hyacinth.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define pb push_back 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 10000; 17 | int id, col[N][2]; 18 | vector e[N]; 19 | 20 | void dfs(int u, int p, int c) 21 | { 22 | int cc = id++; 23 | col[u][0] = c; 24 | col[u][1] = cc; 25 | for (int v: e[u]) 26 | if (v != p) { 27 | dfs(v, u, cc); 28 | swap(c, cc); 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | int n = ri(); 35 | REP(i, n-1) { 36 | int u = ri()-1, v = ri()-1; 37 | e[u].pb(v); 38 | e[v].pb(u); 39 | } 40 | if (n == 2) 41 | puts("0 1\n0 1"); 42 | else { 43 | id++; 44 | dfs(0, -1, 0); 45 | REP(i, n) 46 | printf("%d %d\n", col[i][0], col[i][1]); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /contests/2014-icpc-northwestern-european-regional-practice/judging-troubles.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define REP(i, n) for (int i = 0; i < (n); i++) 9 | #define fi first 10 | #define se second 11 | 12 | int main() 13 | { 14 | int n; 15 | string a; 16 | map b, c; 17 | cin >> n; 18 | REP(i, n) { 19 | cin >> a; 20 | b[a]++; 21 | } 22 | REP(i, n) { 23 | cin >> a; 24 | c[a]++; 25 | } 26 | int ans = 0; 27 | for (auto &x: b) 28 | ans += min(x.se, c[x.fi]); 29 | cout << ans << endl; 30 | } 31 | -------------------------------------------------------------------------------- /contests/2015-acm-acpc/banana-1.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::io::stdin; 3 | 4 | fn read_i64() -> i64 { 5 | let mut s = String::new(); 6 | stdin().read_line(&mut s); 7 | return s.trim_right().parse().unwrap(); 8 | } 9 | 10 | fn main() { 11 | let n = read_i64(); 12 | let mut map = HashMap::new(); 13 | for _ in 0..n { 14 | let mut s = String::new(); 15 | stdin().read_line(&mut s); 16 | let v: Vec = s.trim_right().split(" = ").map(|x| x.to_string()).collect(); 17 | map.insert(v[0].clone(), v[1].clone()); 18 | } 19 | for _ in 0..read_i64() { 20 | let mut s = String::new(); 21 | stdin().read_line(&mut s); 22 | s = String::new(); 23 | stdin().read_line(&mut s); 24 | let v: Vec = s.trim_right().split(" ").map(|x| map.get(x).unwrap().clone()).collect(); 25 | println!("{}", v.join(" ")); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /contests/2015-acm-acpc/journey-2.cc: -------------------------------------------------------------------------------- 1 | use std::cmp::min; 2 | use std::io::stdin; 3 | 4 | fn read_line() -> String { 5 | let mut s = String::new(); 6 | stdin().read_line(&mut s); 7 | s.trim_right().to_string() 8 | } 9 | 10 | fn read_usize() -> usize { 11 | read_line().parse().unwrap() 12 | } 13 | 14 | const MOD: usize = 1000000007; 15 | 16 | fn main() { 17 | for _ in 0..read_usize() { 18 | let line = read_line(); 19 | let mut it = line.split(" "); 20 | let n = it.next().unwrap().parse::().unwrap(); 21 | let k = min(n, it.next().unwrap().parse::().unwrap()); 22 | let mut a = vec![0; n+1]; 23 | let mut s = vec![0; n+2]; 24 | let mut two = 1; 25 | for i in 0..k+1 { 26 | a[i] = two; 27 | s[i+1] = (s[i]+a[i]) % MOD; 28 | two = two*2 % MOD; 29 | } 30 | for i in k+1..n+1 { 31 | a[i] = (MOD+s[i]-s[i-k]) % MOD; 32 | s[i+1] = (s[i]+a[i]) % MOD; 33 | } 34 | println!("{}", a[n]); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /contests/2015-acm-acpc/the-minions-quiz.rs: -------------------------------------------------------------------------------- 1 | use std::io::stdin; 2 | 3 | fn read_line() -> String { 4 | let mut s = String::new(); 5 | stdin().read_line(&mut s); 6 | s.trim_right().to_string() 7 | } 8 | 9 | fn read_usize() -> usize { 10 | read_line().parse().unwrap() 11 | } 12 | 13 | fn main() { 14 | for _ in 0..read_usize() { 15 | let mut line = read_line(); 16 | let mut line2 = read_line(); 17 | let mut it = line.split(" "); 18 | let m: usize = it.next().unwrap().parse().unwrap(); 19 | let n: usize = it.next().unwrap().parse().unwrap(); 20 | it = line2.split(" "); 21 | let mut x: usize = it.next().unwrap().parse().unwrap(); 22 | for i in 0..m+n { 23 | let y = it.next().unwrap().parse().unwrap(); 24 | if i < m { 25 | x &= y; 26 | } else { 27 | x |= y; 28 | } 29 | } 30 | println!("{}", x); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/a-game-of-stones.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import qualified Data.Vector.Unboxed.Mutable as U 3 | 4 | n = 100 5 | 6 | nim = do 7 | a <- U.new $ n+1 8 | forM_ [0..n] $ \i -> do 9 | t2 <- i >= 2 & U.read a (i-2) 10 | t3 <- i >= 3 & U.read a (i-3) 11 | t5 <- i >= 5 & U.read a (i-5) 12 | U.write a i $ t2 || t3 || t5 13 | return a 14 | where 15 | x & y = 16 | if x 17 | then fmap not y 18 | else return False 19 | infixr 3 & 20 | 21 | main = do 22 | cases <- readLn 23 | a <- nim 24 | replicateM cases $ do 25 | n <- readLn 26 | t <- U.read a n 27 | putStrLn $ if t then "First" else "Second" 28 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/day-1-a-chessboard-game.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import qualified Data.Vector as V 4 | import qualified Data.Vector.Mutable as MV 5 | 6 | n = 23 7 | 8 | nim = do 9 | a <- MV.new $ n*n 10 | let 11 | f x y 12 | | x < 0 || y < 0 = return False 13 | | otherwise = not <$> MV.unsafeRead a (n*x+y) 14 | forM_ [0..n-1+n-1] $ \i -> 15 | forM_ [0..n-1] $ \j -> 16 | when (0 <= i-j && i-j < n) $ do 17 | t0 <- f (i-j-2) (j-1) 18 | t1 <- f (i-j-1) (j-2) 19 | t2 <- f (i-j-2) (j+1) 20 | t3 <- f (i-j+1) (j-2) 21 | MV.unsafeWrite a (n*(i-j)+j) $ t0 || t1 || t2 || t3 22 | V.unsafeFreeze a 23 | 24 | main = do 25 | a <- nim 26 | cases <- readLn 27 | replicateM cases $ do 28 | [x, y] <- fmap (map (pred . read) . words) getLine :: IO [Int] 29 | putStrLn $ if a V.! (n*x+y) then "First" else "Second" 30 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/day-2-nim-game.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Bits 3 | import Data.Functor 4 | import Data.List 5 | 6 | main = do 7 | cases <- readLn 8 | replicateM cases $ do 9 | getLine 10 | t <- (map read . words) <$> getLine :: IO [Int] 11 | putStrLn $ if foldl1' xor t /= 0 then "First" else "Second" 12 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/day-2-poker-nim.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Bits 3 | import Data.Functor 4 | import Data.List 5 | 6 | main = do 7 | cases <- readLn 8 | replicateM cases $ do 9 | getLine 10 | t <- (map read . words) <$> getLine :: IO [Int] 11 | putStrLn $ if foldl1' xor t == 0 then "Second" else "First" 12 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/deforestation.hs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MaskRay/HackerRank/2154268e24ecc0db8a80f4f40e8bf105e2064cd9/contests/5-days-of-game-theory/deforestation.hs -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/fun-game.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Function 3 | import Data.Functor 4 | import Data.List 5 | 6 | main = do 7 | cases <- readLn 8 | replicateM cases $ do 9 | getLine 10 | xs <- (map read . words) <$> getLine :: IO [Int] 11 | ys <- (map read . words) <$> getLine :: IO [Int] 12 | let go [] _ acc = acc 13 | go ((xy,x):xys) True acc = go xys False (acc+x) 14 | go ((xy,x):xys) False acc = go xys True (acc-(xy-x)) 15 | r = go (sortBy (flip compare) $ zipWith (\x y -> (x+y,x)) xs ys) True 0 16 | putStrLn $ case signum r of 17 | 1 -> "First" 18 | 0 -> "Tie" 19 | -1 -> "Second" 20 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/misere-nim.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Bits 3 | import Data.Functor 4 | import Data.List 5 | 6 | main = do 7 | cases <- readLn 8 | replicateM cases $ do 9 | getLine 10 | t <- (map read . words) <$> getLine :: IO [Int] 11 | let g = all (==1) t 12 | s = foldl1' xor t 13 | putStrLn $ if g && s /= 0 || not g && s == 0 then "Second" else "First" 14 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/nimble.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Bits 3 | import Data.Functor 4 | import Data.List 5 | 6 | main = do 7 | cases <- readLn 8 | replicateM cases $ do 9 | getLine 10 | t <- (zip [0..] . map read . words) <$> getLine :: IO [(Int, Int)] 11 | putStrLn $ if foldl' (\s (x,y) -> if odd y then s `xor` x else s) 0 t == 0 then "Second" else "First" 12 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/powers-of-two-game.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad.State 2 | import Data.Char 3 | import Data.Functor 4 | import Data.Maybe 5 | import qualified Data.ByteString.Char8 as B 6 | 7 | int = state $ fromJust . B.readInt . B.dropWhile isSpace 8 | 9 | parse = do 10 | cases <- int 11 | replicateM cases int 12 | 13 | main = do 14 | ns <- evalState parse <$> B.getContents 15 | forM_ ns $ \n -> 16 | putStrLn $ if n `rem` 8 == 0 then "Second" else "First" 17 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/tower-breakers-2.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Bits 3 | import Data.Functor 4 | import Data.List 5 | 6 | primes = go [2..1000] 7 | where 8 | go [] = [] 9 | go (x:xs) = x : go (xs \\ [x,x+x..1000]) 10 | 11 | f n = go n primes 0 12 | where 13 | go 1 _ acc = acc 14 | go _ [] acc = acc+1 15 | go x (p:ps) acc 16 | | p*p > x = acc+1 17 | | x `rem` p == 0 = go (x `quot` p) (p:ps) (acc+1) 18 | | otherwise = go x ps acc 19 | 20 | main = do 21 | cases <- readLn 22 | replicateM cases $ do 23 | getLine 24 | t <- (map (f . read) . words) <$> getLine :: IO [Int] 25 | print $ if foldl1' xor t == 0 then 2 else 1 26 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/tower-breakers-3.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Bits 3 | import Data.Functor 4 | import Data.List 5 | 6 | primes = go [2..400] 7 | where 8 | go [] = [] 9 | go (x:xs) = x : go (xs \\ [x,x+x..400]) 10 | 11 | f n = go n primes 0 0 12 | where 13 | go x (p:ps) t acc 14 | | x `rem` p == 0 = go (x `quot` p) (p:ps) (t+1) acc 15 | | otherwise = 16 | let g = if p == 2 then acc+min t 1 else acc+t in 17 | if p*p > x 18 | then if x == 1 then g else g+1 19 | else go x ps 0 g 20 | 21 | main = do 22 | cases <- readLn 23 | replicateM cases $ do 24 | getLine 25 | t <- (map (f . read) . words) <$> getLine :: IO [Int] 26 | print $ if foldl1' xor t == 0 then 2 else 1 27 | -------------------------------------------------------------------------------- /contests/5-days-of-game-theory/tower-breakers.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | 3 | main = do 4 | cases <- readLn 5 | replicateM cases $ do 6 | [n, m] <- fmap (map read . words) getLine :: IO [Int] 7 | print $ if odd n && m > 1 then 1 else 2 8 | -------------------------------------------------------------------------------- /contests/basic-statistics-warmup-2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy import stats 3 | 4 | input() 5 | xs = np.array(list(map(int, input().split()))) 6 | print('{:.1f}'.format(xs.mean())) 7 | print('{:.1f}'.format(np.median(xs))) 8 | print(stats.mode(xs)[0][0]) 9 | print('{:.1f}'.format(xs.std())) 10 | -------------------------------------------------------------------------------- /contests/blackrock-codesprint/currency-arbitrage.rs: -------------------------------------------------------------------------------- 1 | use std::io::stdin; 2 | use std::f64; 3 | 4 | fn read_line() -> String { 5 | let mut s = String::new(); 6 | let _ = stdin().read_line(&mut s); 7 | s.trim_right().to_string() 8 | } 9 | 10 | fn read_isize() -> isize { 11 | read_line().parse().unwrap() 12 | } 13 | 14 | fn main() { 15 | for _ in 0..read_isize() { 16 | let line = read_line(); 17 | let mut it = line.split(" "); 18 | let a = it.next().unwrap().parse::().unwrap(); 19 | let b = it.next().unwrap().parse::().unwrap(); 20 | let c = it.next().unwrap().parse::().unwrap(); 21 | println!("{}", ((1./a/b/c-1.)*100000.).floor().max(0.)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /contests/code-cpp-3/accessing-inherited-functions.cc: -------------------------------------------------------------------------------- 1 | class D : A, B, C 2 | { 3 | 4 | int val; 5 | public: 6 | //Initially val is 1 7 | D() 8 | { 9 | val=1; 10 | } 11 | 12 | 13 | //Implement this function 14 | void update_val(int x) 15 | { 16 | while (x % 2 == 0) x /= 2, A::func(val); 17 | while (x % 3 == 0) x /= 3, B::func(val); 18 | while (x % 5 == 0) x /= 5, C::func(val); 19 | } 20 | //For Checking Purpose 21 | void check(int); //Do not delete this line. 22 | }; 23 | -------------------------------------------------------------------------------- /contests/code-cpp-3/attending-workshops.cc: -------------------------------------------------------------------------------- 1 | struct Available_Workshops { vector> a; }; 2 | 3 | Available_Workshops *initialize(int *a, int *b, int n) 4 | { 5 | auto x = new Available_Workshops; 6 | x->a.resize(n); 7 | for (int i = 0; i < n; i++) 8 | x->a[i] = {a[i]+b[i], b[i]}; 9 | return x; 10 | } 11 | 12 | int CalculateMaxWorkshops(Available_Workshops *x) 13 | { 14 | vector> &a = x->a; 15 | sort(a.begin(), a.end()); 16 | int last = 0, s = 0; 17 | for (auto x: a) { 18 | if (last <= x.first-x.second) { 19 | last = x.first; 20 | s++; 21 | } 22 | } 23 | delete x; 24 | return s; 25 | } 26 | -------------------------------------------------------------------------------- /contests/code-cpp-3/bitset-1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int ri() 6 | { 7 | int x; 8 | scanf("%d", &x); 9 | return x; 10 | } 11 | 12 | union { 13 | uint8_t a[1<<28]; 14 | uint64_t b[1<<25]; 15 | } a; 16 | 17 | int main() { 18 | int n = ri(); 19 | unsigned s = ri(), p = ri(), q = ri(); 20 | for (; n; n--) { 21 | a.a[(s & ~(1u<<31))>>3] |= 1 << (s&7); 22 | s = s*p+q; 23 | } 24 | s = 0; 25 | for (n = 1<<25; n; ) 26 | s += __builtin_popcountll(a.b[--n]); 27 | printf("%u\n", s); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /contests/code-cpp-3/box-it.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Box 4 | { 5 | int l, b, h; 6 | public: 7 | Box() : l(0), b(0), h(0) { BoxesCreated++; } 8 | Box(int l, int b, int h) : l(l), b(b), h(h) { BoxesCreated++; } 9 | Box(const Box &o) : l(o.l), b(o.b), h(o.h) { BoxesCreated++; } 10 | ~Box() { BoxesDestroyed++; } 11 | int getLength() const { return l; } 12 | int getBreadth() const { return b; } 13 | int getHeight() const { return h; } 14 | long long CalculateVolume() const { return (long long)l * b * h; } 15 | bool operator<(const Box &o) { return make_tuple(l, b, h) < make_tuple(o.l, o.b, o.h); } 16 | friend ostream& operator<<(ostream &out, Box B) { 17 | out << B.l << ' ' << B.b << ' ' << B.h; 18 | return out; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /contests/code-cpp-3/overload-operators.cc: -------------------------------------------------------------------------------- 1 | Complex operator+(const Complex &x, const Complex &y) 2 | { 3 | return Complex{x.a + y.a, x.b + y.b}; 4 | } 5 | 6 | ostream &operator<<(ostream &out, const Complex &x) 7 | { 8 | out << x.a << "+i" << x.b; 9 | return out; 10 | } 11 | -------------------------------------------------------------------------------- /contests/code-cpp-3/variable-sized-arrays.cc: -------------------------------------------------------------------------------- 1 | void real(); 2 | real(); 3 | } 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | void real() 19 | { 20 | int n = ri(), q = ri(); 21 | vector> a(n); 22 | REP(i, n) { 23 | int k = ri(); 24 | a[i].resize(k); 25 | REP(j, k) 26 | a[i][j] = ri(); 27 | } 28 | while (q--) { 29 | int x = ri(), y = ri(); 30 | printf("%d\n", a[x][y]); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /contests/code-cpp-3/virtual-functions.cc: -------------------------------------------------------------------------------- 1 | class Person 2 | { 3 | public: 4 | string name; 5 | int age; 6 | virtual void getdata() = 0; 7 | virtual void putdata() = 0; 8 | virtual ~Person() {} 9 | }; 10 | 11 | #include 12 | #include 13 | 14 | class Professor : public Person 15 | { 16 | int id, publications; 17 | static int seq; 18 | public: 19 | Professor() : id(++seq) {} 20 | void getdata() override { cin >> name >> age >> publications; } 21 | void putdata() override { cout << name << ' ' << age << ' ' << publications << ' ' << id << endl; } 22 | }; 23 | 24 | int Professor::seq = 0; 25 | 26 | class Student : public Person 27 | { 28 | int id; 29 | array marks; 30 | static int seq; 31 | public: 32 | Student() : id(++seq) {} 33 | void getdata() override { cin >> name >> age; for (int &x: marks) cin >> x; } 34 | void putdata() override { cout << name << ' ' << age << ' ' << accumulate(marks.begin(), marks.end(), 0) << ' ' << id << endl; } 35 | }; 36 | 37 | int Student::seq = 0; 38 | -------------------------------------------------------------------------------- /contests/code-cpp-4/cpp-class-template-specialization.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | enum class Fruit { apple, orange, pear }; 4 | enum class Color { red, green, orange }; 5 | 6 | template struct Traits; 7 | 8 | template<> 9 | struct Traits { 10 | static string name(int x) { 11 | return x == 0 ? "apple" : x == 1 ? "orange" : x == 2 ? "pear" : "unknown"; 12 | } 13 | }; 14 | 15 | template<> 16 | struct Traits { 17 | static string name(int x) { 18 | return x == 0 ? "red" : x == 1 ? "green" : x == 2 ? "orange" : "unknown"; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | int t = 0; std::cin >> t; 25 | 26 | for (int i=0; i!=t; ++i) { 27 | int index1; std::cin >> index1; 28 | int index2; std::cin >> index2; 29 | cout << Traits::name(index1) << " "; 30 | cout << Traits::name(index2) << "\n"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /contests/code-cpp-4/cpp-variadics.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | struct reversed_binary_value {}; 6 | 7 | template<> 8 | struct reversed_binary_value<> { operator int() { return 0; } }; 9 | template 10 | struct reversed_binary_value<0, xs...> { operator int() { return 2 * reversed_binary_value(); } }; 11 | template 12 | struct reversed_binary_value<1, xs...> { operator int() { return 2 * reversed_binary_value() + 1; } }; 13 | 14 | 15 | template 16 | struct CheckValues { 17 | static void check(int x, int y) 18 | { 19 | CheckValues::check(x, y); 20 | CheckValues::check(x, y); 21 | } 22 | }; 23 | 24 | template 25 | struct CheckValues<0, digits...> { 26 | static void check(int x, int y) 27 | { 28 | int z = reversed_binary_value(); 29 | std::cout << (z+64*y==x); 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | int t; std::cin >> t; 36 | 37 | for (int i=0; i!=t; ++i) { 38 | int x, y; 39 | cin >> x >> y; 40 | CheckValues<6>::check(x, y); 41 | cout << "\n"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /contests/code-cpp-4/exceptional-server.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | class Server { 10 | private: 11 | static int load; 12 | public: 13 | static int compute(long long A, long long B) { 14 | load += 1; 15 | if(A < 0) { 16 | throw std::invalid_argument("A is negative"); 17 | } 18 | vector v(A, 0); 19 | int real = -1, cmplx = sqrt(-1); 20 | if(B == 0) throw 0; 21 | real = (A/B)*real; 22 | int ans = v.at(B); 23 | return real + A - B*ans; 24 | } 25 | static int getLoad() { 26 | return load; 27 | } 28 | }; 29 | int Server::load = 0; 30 | 31 | int main() { 32 | int T; cin >> T; 33 | while(T--) { 34 | long long A, B; 35 | cin >> A >> B; 36 | 37 | try { 38 | cout << Server::compute(A, B) << endl; 39 | } catch (bad_alloc& e) { 40 | cout << "Not enough memory" << endl; 41 | } catch (exception& e) { 42 | cout << "Exception: " << e.what() << endl; 43 | } catch (int e) { 44 | cout << "Other Exception" << endl; 45 | } 46 | 47 | 48 | } 49 | cout << Server::getLoad() << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /contests/code-cpp-4/inherited-code.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct BadLengthException : exception { 8 | string s; 9 | BadLengthException(int n) : s(to_string(n)) {} 10 | const char *what() const noexcept override { 11 | return s.c_str(); 12 | } 13 | }; 14 | 15 | bool checkUsername(string username) { 16 | bool isValid = true; 17 | int n = username.length(); 18 | if(n < 5) { 19 | throw BadLengthException(n); 20 | } 21 | for(int i = 0; i < n-1; i++) { 22 | if(username[i] == 'w' && username[i+1] == 'w') { 23 | isValid = false; 24 | } 25 | } 26 | return isValid; 27 | } 28 | 29 | int main() { 30 | int T; cin >> T; 31 | while(T--) { 32 | string username; 33 | cin >> username; 34 | try { 35 | bool isValid = checkUsername(username); 36 | if(isValid) { 37 | cout << "Valid" << '\n'; 38 | } else { 39 | cout << "Invalid" << '\n'; 40 | } 41 | } catch (BadLengthException e) { 42 | cout << "Too short: " << e.what() << '\n'; 43 | } 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /contests/code-cpp-4/preprocessor-solution.cc: -------------------------------------------------------------------------------- 1 | #define toStr(x) #x 2 | #define FUNCTION(f, op) void f(int& x, int& y) { if (y op x) x = y; } 3 | #define io(v) cin >> v 4 | #define foreach(v, i) for (size_t i = 0; i < v.size(); i++) 5 | #define INF 0x7fffffff 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | #if !defined toStr || !defined io || !defined FUNCTION || !defined INF 12 | #error Missing preprocessor definitions 13 | #endif 14 | 15 | FUNCTION(minimum, <) 16 | FUNCTION(maximum, >) 17 | 18 | int main(){ 19 | int n; cin >> n; 20 | vector v(n); 21 | foreach(v, i) { 22 | io(v)[i]; 23 | } 24 | int mn = INF; 25 | int mx = -INF; 26 | foreach(v, i) { 27 | minimum(mn, v[i]); 28 | maximum(mx, v[i]); 29 | } 30 | int ans = mx - mn; 31 | cout << toStr(Result =) <<' '<< ans; 32 | return 0; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /contests/code-cpp-4/prettyprint.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int T; cin >> T; 7 | cout << setiosflags(ios::uppercase); 8 | cout << setw(0xf) << internal; 9 | while(T--) { 10 | double A; cin >> A; 11 | double B; cin >> B; 12 | double C; cin >> C; 13 | 14 | cout << setw(0) << nouppercase << setiosflags(ios::showbase) << hex << long(A) << resetiosflags(ios::showbase) << endl; 15 | cout << showpos << right << setw(15) << setfill('_') << fixed << setprecision(2) << B << noshowpos << endl; 16 | cout << scientific << uppercase << setprecision(9) << C << endl; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /contests/codewhiz-java-march-2016/java-covariance.java: -------------------------------------------------------------------------------- 1 | class Flower { 2 | String whats_Your_Name() { 3 | return "I have many names and types"; 4 | } 5 | } 6 | 7 | class Jasmine extends Flower { 8 | String whats_Your_Name() { 9 | return "Jasmine"; 10 | } 11 | } 12 | 13 | class Lily extends Flower { 14 | String whats_Your_Name() { 15 | return "Lily"; 16 | } 17 | } 18 | 19 | class Lotus extends Flower { 20 | String whats_Your_Name() { 21 | return "Lotus"; 22 | } 23 | } 24 | 25 | class State { 26 | Flower your_National_Flower() { 27 | return new Flower(); 28 | } 29 | } 30 | 31 | class WestBengal extends State { 32 | Flower your_National_Flower() { 33 | return new Jasmine(); 34 | } 35 | } 36 | 37 | class Karnataka extends State { 38 | Flower your_National_Flower() { 39 | return new Lotus(); 40 | } 41 | } 42 | 43 | class AndhraPradesh extends State { 44 | Flower your_National_Flower() { 45 | return new Lily(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /contests/codewhiz-java-march-2016/java-list.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 | static StringTokenizer st; 7 | 8 | public static void main(String[] args) { 9 | int n = nextInt(); 10 | ArrayList a = new ArrayList(); 11 | for (int i = 0; i < n; i++) 12 | a.add(nextInt()); 13 | for (int m = nextInt(); m-- > 0; ) { 14 | String op = next(); 15 | int x = nextInt(); 16 | if (op.equals("Insert")) 17 | a.add(x, nextInt()); 18 | else { 19 | a.remove(x); 20 | } 21 | } 22 | for (int i = 0; i < a.size(); i++) 23 | System.out.format("%d%c", a.get(i), i == a.size()-1 ? '\n' : ' '); 24 | } 25 | 26 | static String next() { 27 | while (st == null || ! st.hasMoreElements()) { 28 | try { 29 | st = new StringTokenizer(br.readLine()); 30 | } catch (IOException e) { 31 | } 32 | } 33 | return st.nextToken(); 34 | } 35 | 36 | static int nextInt() { 37 | return Integer.parseInt(next()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /contests/codewhiz-java-march-2016/java-primality-test.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | BigInteger n = in.nextBigInteger(); 12 | in.close(); 13 | System.out.println(n.isProbablePrime(100) ? "prime" : "not prime"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /contests/codewhiz-java-march-2016/maximum-and-minimum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void print(int A[],int i){ 10 | try{ 11 | System.out.println(A[i]); 12 | } 13 | 14 | finally { 15 | List a = new ArrayList(); 16 | Collections.addAll(a, Arrays.stream(A).boxed().toArray(Integer[]::new)); 17 | System.out.format("%d %d\n", Collections.max(a), Collections.min(a)); 18 | } 19 | } 20 | 21 | public static void main(String[] args) { 22 | int N; 23 | Scanner st=new Scanner(System.in); 24 | N=st.nextInt(); 25 | int A[]=new int[N]; 26 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP1(i, n) for (remove_cv::type>::type i = 1; i <= (n); i++) 7 | 8 | int main() 9 | { 10 | long cc, l, a, n, d; 11 | cin >> cc; 12 | while (cc--) { 13 | cin >> l >> a >> n >> d; 14 | if (d > a) 15 | cout << "SAD\n"; 16 | else if (d == 1) 17 | cout << l*a << endl; 18 | else { 19 | long s = -1; 20 | REP1(i, (n-1)/(d-1)) { 21 | long x = n-1-(d-1)*i; // extra frequency of A 22 | if (x+a*i < l) continue; 23 | long c = (l-x)/i; // number of items >= i 24 | s = max(s, a*x + (2*a-c+1)*c/2*i + (a-c)*((l-x)%i)); 25 | } 26 | if (s < 0) 27 | cout << "SAD\n"; 28 | else 29 | cout << s << endl; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /contests/hourrank-10/bomber-man.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | const long R = 200; 10 | char a[R][R+1], b[R][R+1]; 11 | 12 | int main() 13 | { 14 | long r, c, n; 15 | cin >> r >> c >> n; 16 | REP(i, r) 17 | cin >> a[i]; 18 | if (n%2 == 0) { 19 | fill_n(a[0], c, 'O'); 20 | REP(i, r) 21 | cout << a[0] << '\n'; 22 | } else { 23 | n = n-1>>1; 24 | if (n > 1) 25 | n = (n+1)%2+1; 26 | while (n--) { 27 | REP(i, r) 28 | REP(j, c) 29 | b[i][j] = 30 | a[i][j] == 'O' || 31 | i && a[i-1][j] == 'O' || 32 | i+1 < r && a[i+1][j] == 'O' || 33 | j && a[i][j-1] == 'O' || 34 | j+1 < c && a[i][j+1] == 'O' ? '.' : 'O'; 35 | REP(i, r) 36 | copy_n(b[i], c, a[i]); 37 | } 38 | REP(i, r) 39 | cout << a[i] << '\n'; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /contests/hourrank-2/jane-traveling.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 500000; 16 | int a[5][2], d[5][N+1], e[5][N+1]; 17 | 18 | int main() 19 | { 20 | int n = ri(); 21 | REP(i, 5) { 22 | REP(j, 2) 23 | a[i][j] = ri(); 24 | fill_n(d[i], n+1, n+1); 25 | d[i][0] = 0; 26 | } 27 | REP1(i, n) 28 | REP(j, 5) 29 | REP(k, 2) { 30 | int jj = (j+k+1)%5; 31 | if (a[j][k] <= i && (d[jj][i-a[j][k]] + 1 < d[j][i] || 32 | d[jj][i-a[j][k]] + 1 == d[j][i] && jj < e[j][i])) { 33 | d[j][i] = d[jj][i-a[j][k]] + 1; 34 | e[j][i] = jj; 35 | } 36 | } 37 | if (d[0][n] > n) 38 | puts("-1"); 39 | else { 40 | bool first = true; 41 | for (int i = 0; ; ) { 42 | if (! first) putchar(' '); 43 | first = false; 44 | printf("%d", i+1); 45 | if (! n) break; 46 | int ii = e[i][n]; 47 | n -= a[i][(ii-i-1+5)%5]; 48 | i = ii; 49 | } 50 | puts(""); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /contests/hourrank-4/new-year-chaos.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 7 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 100000; 17 | int a[N]; 18 | 19 | void add(int n, int x) 20 | { 21 | for (; x < n; x |= x+1) 22 | a[x]++; 23 | } 24 | 25 | int getSum(int x) 26 | { 27 | int s = 0; 28 | for (; x; x &= x-1) 29 | s += a[x-1]; 30 | return s; 31 | } 32 | 33 | int main() 34 | { 35 | ROF(_, 0, ri()) { 36 | int n = ri(); 37 | long ans = 0; 38 | fill_n(a, n, 0); 39 | REP(i, n) { 40 | int x = ri()-1, y = x+getSum(n-x)-i; 41 | if (y > 2) 42 | ans = -1; 43 | if (~ ans && y > 0) { 44 | ans += y; 45 | add(n, n-x); 46 | } 47 | } 48 | if (ans < 0) 49 | puts("Too chaotic"); 50 | else 51 | printf("%ld\n", ans); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /contests/indeed-prime-codesprint/divyam-and-sorted-list.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 100000; 15 | int a[N], b[N]; 16 | 17 | int main() 18 | { 19 | for (int cc = ri(); cc--; ) { 20 | int n = ri(); 21 | REP(i, n) 22 | a[i] = ri(); 23 | REP(i, n) 24 | b[i] = ri(); 25 | int t = -1; 26 | bool ok = true; 27 | REP(i, n) { 28 | int mn = min(a[i], b[i]), mx = max(a[i], b[i]); 29 | if (t <= mn) 30 | t = mn; 31 | else if (t <= mx) 32 | t = mx; 33 | else { 34 | ok = false; 35 | break; 36 | } 37 | } 38 | puts(ok ? "YES" : "NO"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /contests/indeed-prime-codesprint/goty-and-the-play.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 100000; 15 | int a[N], c[N], f[N+1]; 16 | 17 | void add(int x, int v) 18 | { 19 | for (; x < N+1; x |= x+1) 20 | f[x] += v; 21 | } 22 | 23 | int getSum(int x) 24 | { 25 | int s = 0; 26 | for (; x; x &= x-1) 27 | s += f[x-1]; 28 | return s; 29 | } 30 | 31 | int main() 32 | { 33 | int m = ri(), n = ri(), q = ri(); 34 | REP(i, n) 35 | c[a[i] = ri()]++; 36 | REP(i, m) 37 | add(c[i], 1); 38 | while (q--) { 39 | int op = ri(), x = ri(); 40 | if (op == 2) 41 | printf("%d\n", getSum(x)); 42 | else { 43 | int y = ri(); 44 | add(c[a[x]], -1); 45 | add(--c[a[x]], 1); 46 | add(c[y], -1); 47 | add(++c[y], 1); 48 | a[x] = y; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /contests/indeed-prime-codesprint/the-ultimate-question.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (int i = 0; i < (n); i++) 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | int a[3], b[3]; 14 | 15 | int main() 16 | { 17 | REP(i, 3) 18 | a[i] = ri(); 19 | for (b[1] = 2; b[1]--; ) 20 | for (b[2] = 2; b[2]--; ) { 21 | int s = 0; 22 | for (int j, i = 0; i < 3; i = j) { 23 | int t = a[i]; 24 | for (j = i+1; j < 3 && b[j]; j++) 25 | t *= a[j]; 26 | s += t; 27 | } 28 | if (s == 42) { 29 | const char op[] = "+*"; 30 | printf("%d%c%d%c%d\n", a[0], op[b[1]], a[1], op[b[2]], a[2]); 31 | return 0; 32 | } 33 | } 34 | puts("This is not the ultimate question"); 35 | } 36 | -------------------------------------------------------------------------------- /contests/infinitum15/angels-in-space.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (decltype(n) i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const long N = 100; 15 | double x[N], y[N], z[N]; 16 | #define sqr(x) ((x)*(x)) 17 | #define len(i, j) (sqr(x[i]-x[j])+sqr(y[i]-y[j])+sqr(z[i]-z[j])) 18 | 19 | int main() 20 | { 21 | long n = ri(); 22 | REP(i, n) { 23 | x[i] = ri(); 24 | y[i] = ri(); 25 | z[i] = ri(); 26 | } 27 | double s = 0, c = 0; 28 | REP(k, n) 29 | REP(j, k) 30 | REP(i, j) { 31 | double ij = len(i, j), jk = len(j, k), ki = len(k, i); 32 | s += acos((ij+jk-ki)/(2*sqrt((ij*jk)))); 33 | c++; 34 | } 35 | printf("%.10lf\n", s/c); 36 | } 37 | -------------------------------------------------------------------------------- /contests/infinitum15/birthdays.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (long i = 0; i < (n); i++) 5 | #define ROF(i, a, b) for (long i = (b); --i >= (a); ) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | double rd() 15 | { 16 | double x; 17 | scanf("%lf", &x); 18 | return x; 19 | } 20 | 21 | int main() 22 | { 23 | ROF(_, 0, ri()) { 24 | double p = rd(); 25 | for (long n = 1; ; n++) { 26 | long x = n/4, y = n-x; 27 | double s = 1; 28 | REP(i, y) 29 | s *= (365-i)/365.0; 30 | REP(i, x) 31 | s *= (366-i-y)/366.0; 32 | if (s <= 1-p) { 33 | printf("%ld\n", n); 34 | break; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /contests/infinitum15/k-element-sequences.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (long i = 0; i < (n); i++) 5 | #define REP1(i, n) for (long i = 1; i <= (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const long MOD = 1000000007, N = 2000000; 15 | long fac[N+1]; 16 | 17 | long inv(long x) 18 | { 19 | long r = 1; 20 | while (x > 1) { 21 | r = r*(MOD-MOD/x)%MOD; 22 | x = MOD%x; 23 | } 24 | return r; 25 | } 26 | 27 | int main() 28 | { 29 | fac[0] = 1; 30 | REP1(i, N) 31 | fac[i] = fac[i-1]*i%MOD; 32 | for (int cc = ri(); cc--; ) { 33 | long n = ri()-1, k = ri()-1, p = 1, q = 1; 34 | REP(i, k) { 35 | p = p*(n-i)%MOD; 36 | q = q*(i+1)%MOD; 37 | } 38 | printf("%ld\n", fac[n]*inv(fac[k])%MOD*inv(fac[n-k])%MOD); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /contests/infinitum15/maximum-values.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ROF(i, a, b) for (decltype(b) i = (b); --i >= (a); ) 5 | 6 | long rl() 7 | { 8 | long x; 9 | scanf("%ld", &x); 10 | return x; 11 | } 12 | 13 | const long MOD = 1000000007; 14 | 15 | long inv(long x) 16 | { 17 | long r = 1; 18 | while (x > 1) { 19 | r = r*(MOD-MOD/x)%MOD; 20 | x = MOD%x; 21 | } 22 | return r; 23 | } 24 | 25 | long sum(long n) 26 | { 27 | return n*(n+1)%MOD*inv(2)%MOD; 28 | } 29 | 30 | long sum2(long n) 31 | { 32 | return n*(n+1)%MOD*(2*n+1)%MOD*inv(6)%MOD; 33 | } 34 | 35 | int main() 36 | { 37 | ROF(_, 0, rl()) { 38 | long l = rl()%MOD, r = rl()%MOD, 39 | s = (15*(sum2(r)-sum2(l-1))+4*(sum(r)-sum(l-1))-4*(r-l+1))%MOD; 40 | printf("%ld\n", (s+MOD)%MOD); 41 | } 42 | } 43 | 44 | /* 45 | * x^2 = k/3 46 | * y^2 = k/2 47 | * z^2 = k/6 48 | * phi1 = (2 * (sqrt 2 + sqrt 3) - 3) * k 49 | * phi2 = (8 * sqrt 6) * k^2 50 | * phi3 = k^2 + k - 1 51 | * S = 15 k^2 + 4 k - 4 52 | */ 53 | -------------------------------------------------------------------------------- /contests/interfacecse-codedash/anagram.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | let read_str _ = Scanf.bscanf Scanf.Scanning.stdib " %s" (fun x -> x) 3 | 4 | let () = 5 | for cc = 1 to read_int 0 do 6 | let a = read_str 0 in 7 | let n = String.length a in 8 | let b = Array.make 26 0 in 9 | if n mod 2 <> 0 then 10 | print_endline "-1" 11 | else ( 12 | for i = 0 to n/2-1 do 13 | let x = int_of_char a.[i] - 97 in 14 | b.(x) <- b.(x) + 1 15 | done; 16 | for i = n/2 to n-1 do 17 | let x = int_of_char a.[i] - 97 in 18 | b.(x) <- b.(x) - 1 19 | done; 20 | Printf.printf "%d\n" @@ Array.fold_left (fun s x -> s + abs x) 0 b / 2 21 | ) 22 | done 23 | -------------------------------------------------------------------------------- /contests/interfacecse-codedash/dash-4.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let () = 4 | for cc = 1 to read_int 0 do 5 | let n = read_int 0 in 6 | let a = Array.make_matrix n n 0 in 7 | let x = ref 0 in 8 | for i = 0 to (n-1)/2 do 9 | for j = i to n-1-i do 10 | incr x; 11 | a.(j).(i) <- !x 12 | done; 13 | for j = i+1 to n-1-i do 14 | incr x; 15 | a.(n-1-i).(j) <- !x 16 | done; 17 | for j = n-2-i downto i do 18 | incr x; 19 | a.(j).(n-1-i) <- !x 20 | done; 21 | for j = n-2-i downto i+1 do 22 | incr x; 23 | a.(i).(j) <- !x 24 | done 25 | done; 26 | for i = 0 to n-1 do 27 | for j = 0 to n-1 do 28 | Printf.printf "%d%c" a.(i).(j) (if j = n-1 then '\n' else ' ') 29 | done; 30 | print_endline "" 31 | done 32 | done 33 | -------------------------------------------------------------------------------- /contests/interfacecse-codedash/squares2.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let () = 4 | let f x = int_of_float @@ floor @@ sqrt @@ float_of_int x in 5 | for i = 1 to read_int 0 do 6 | let x = read_int 0 in 7 | let y = read_int 0 in 8 | Printf.printf "%d\n" (f y - f (x-1)) 9 | done 10 | -------------------------------------------------------------------------------- /contests/interfacecse-codedash/stones2.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let () = 4 | for cc = 1 to read_int 0 do 5 | let n = read_int 0 in 6 | let a = read_int 0 in 7 | let b = read_int 0 in 8 | let a, b = 9 | if a < b then 10 | a, b 11 | else 12 | b, a 13 | in 14 | let rec f l i = 15 | if i >= 0 then ( 16 | let x = a*i+b*(n-1-i) in 17 | if x <> l then 18 | Printf.printf "%d " x; 19 | f x (i-1) 20 | ) 21 | in 22 | f (-1) (n-1); 23 | print_char '\n' 24 | done 25 | -------------------------------------------------------------------------------- /contests/intro-to-statistics/basic-probability-puzzles-1.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Ratio 3 | import Text.Printf 4 | 5 | main = 6 | let t = (%36) . length . filter (<=9) . map sum $ replicateM 2 [1..3] in 7 | printf "%d/%d\n" (numerator t) (denominator t) 8 | -------------------------------------------------------------------------------- /contests/intro-to-statistics/basic-probability-puzzles-2.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Ratio 3 | import Text.Printf 4 | 5 | main = 6 | let t = (%36) . length $ [1 | x<-[1..6], y<-[1..6], x /= y, x+y == 6] in 7 | printf "%d/%d\n" (numerator t) (denominator t) 8 | -------------------------------------------------------------------------------- /contests/intro-to-statistics/basic-probability-puzzles-3.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Ratio 3 | import Text.Printf 4 | 5 | f = fromEnum 6 | 7 | main = 8 | let t = (%(7*9*8)) . length $ [1 | x<-[0..6], y<-[0..8], z<-[0..7], f(x<4)+f(y<5)+f(z<4) == 2] in 9 | printf "%d/%d\n" (numerator t) (denominator t) 10 | -------------------------------------------------------------------------------- /contests/intro-to-statistics/basic-probability-puzzles-4.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Ratio 3 | import Text.Printf 4 | 5 | f = fromEnum 6 | 7 | main = 8 | let t = (%(9*10*9`div`2)) . length $ [1 | x<-[0..8], y<-[0..9], z<-[y+1..9], f(x<4)+f(y<3)+f(z<3) == 1] in 9 | printf "%d/%d\n" (numerator t) (denominator t) 10 | -------------------------------------------------------------------------------- /contests/intro-to-statistics/day-5-intro-to-linear-regression.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | a = [(95,85),(85,95),(80,70),(70,65),(60,70)] 3 | coef = np.linalg.lstsq([[1, x[0]] for x in a], [x[1] for x in a])[0] 4 | print('{:.1f}'.format(coef[1]*80+coef[0])) 5 | -------------------------------------------------------------------------------- /contests/intro-to-statistics/day-5-introduction-to-correlation.py: -------------------------------------------------------------------------------- 1 | from scipy.stats import pearsonr, spearmanr 2 | a = [10, 9.8, 8, 7.8, 7.7, 7, 6, 5, 4, 2] 3 | b = [200, 44, 32, 24, 22, 17, 15, 12, 8, 4] 4 | print('{:.3f}'.format(pearsonr(a, b)[0])) 5 | print('{:.1f}'.format(spearmanr(a, b).correlation)) 6 | -------------------------------------------------------------------------------- /contests/june-world-codesprint/equal-stacks.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define PER1(i, n) for (remove_cv::type>::type i = (n); i > 0; i--) 7 | #define REP1(i, n) for (remove_cv::type>::type i = 1; i <= (n); i++) 8 | 9 | const long N = 100001; 10 | long a[N], b[N], c[N]; 11 | 12 | int main() 13 | { 14 | ios_base::sync_with_stdio(0); 15 | long n1, n2, n3; 16 | cin >> n1 >> n2 >> n3; 17 | PER1(i, n1) cin >> a[i]; 18 | PER1(i, n2) cin >> b[i]; 19 | PER1(i, n3) cin >> c[i]; 20 | REP1(i, n1) a[i] += a[i-1]; 21 | REP1(i, n2) b[i] += b[i-1]; 22 | REP1(i, n3) c[i] += c[i-1]; 23 | while (a[n1] != b[n2] || b[n2] != c[n3] || c[n3] != a[n1]) { 24 | long t = min(min(a[n1], b[n2]), c[n3]); 25 | t < a[n1] && n1--; 26 | t < b[n2] && n2--; 27 | t < c[n3] && n3--; 28 | } 29 | cout << a[n1] << endl; 30 | } 31 | -------------------------------------------------------------------------------- /contests/june-world-codesprint/minimum-distances.hs: -------------------------------------------------------------------------------- 1 | import Data.Functor 2 | import Data.List 3 | import qualified Data.Map as M 4 | 5 | main = do 6 | n <- readLn :: IO Int 7 | a <- (zip [0..] . map read . words) <$> getLine :: IO [(Int, Int)] 8 | case fst $ foldl' (\(ans,m) (i,x) -> 9 | let f (Just u) (Just v) = Just $ min u v 10 | f Nothing v = v 11 | f u Nothing = u 12 | in 13 | (f ans $ (i-) <$> M.lookup x m, M.insert x i m)) (Nothing,M.empty) a of 14 | Nothing -> putStrLn "-1" 15 | Just ans -> print ans 16 | -------------------------------------------------------------------------------- /contests/lambda-calculi-10/password-compression-i.ml: -------------------------------------------------------------------------------- 1 | let read_string _ = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x) 2 | 3 | let rec f () = 4 | let s = read_string() in 5 | if s <> "" then ( 6 | print_char s.[0]; 7 | f() 8 | ) 9 | 10 | let () = 11 | f(); 12 | print_newline() 13 | -------------------------------------------------------------------------------- /contests/lambda-calculi-10/password-compression-ii.ml: -------------------------------------------------------------------------------- 1 | let read_string _ = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x) 2 | 3 | let rec f () = 4 | let s = read_string() in 5 | if s <> "" then ( 6 | let i, s = Scanf.sscanf s "%d%s" (fun x y -> x,y) in 7 | print_char @@ if i < String.length s then s.[i] else ' '; 8 | f() 9 | ) 10 | 11 | let () = 12 | f(); 13 | print_newline() 14 | -------------------------------------------------------------------------------- /contests/lambda-calculi-9/string-reductions.ml: -------------------------------------------------------------------------------- 1 | let () = 2 | let b = Array.make 256 false in 3 | let a = read_line() |> Bytes.of_string in 4 | let n = String.length a in 5 | let rec go i j = 6 | if i >= n then 7 | j 8 | else ( 9 | let c = Bytes.get a i in 10 | if b.(c |> int_of_char) then 11 | go (i+1) j 12 | else ( 13 | b.(c |> int_of_char) <- true; 14 | Bytes.set a j c; 15 | go (i+1) (j+1) 16 | ) 17 | ) 18 | in 19 | let n = go 0 0 in 20 | Bytes.sub_string a 0 n |> print_endline; 21 | -------------------------------------------------------------------------------- /contests/lambda-calculi-march-2016/functions-or-not.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Function 3 | import Data.Functor 4 | import Data.List 5 | import Data.Maybe 6 | import qualified Data.Map as M 7 | 8 | f m [x,y] = 9 | if M.member x m 10 | then M.insertWith (\y yy -> if y == yy then y else (-1)) x y m 11 | else M.insert x y m 12 | 13 | run = do 14 | n <- readLn 15 | xs <- replicateM n $ (map (read :: String -> Int) . words) <$> getLine 16 | putStrLn $ if isNothing . findIndex (<0) . M.elems $ foldl f M.empty xs then "YES" else "NO" 17 | 18 | main = readLn >>= flip replicateM_ run 19 | -------------------------------------------------------------------------------- /contests/lambda-calculi-march-2016/lambda-march-compute-the-area-of-a-polygon.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import Data.List 4 | 5 | main = do 6 | n <- readLn 7 | xs <- replicateM n $ ((\[x,y] -> (read x, read y)) . words) <$> getLine :: IO [(Double, Double)] 8 | print $ abs $ (/2) $ sum $ zipWith (\(x0,y0) (x1,y1) -> x0*y1-y0*x1) (tail $ cycle xs) xs 9 | -------------------------------------------------------------------------------- /contests/lambda-calculi-march-2016/lambda-march-compute-the-perimeter-of-a-polygon.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import Data.List 4 | 5 | main = do 6 | n <- readLn 7 | xs <- replicateM n $ ((\[x,y] -> (read x, read y)) . words) <$> getLine :: IO [(Double, Double)] 8 | print $ sum $ zipWith (\(x0,y0) (x1,y1) -> sqrt $ (x0-x1)^2+(y0-y1)^2) (tail $ cycle xs) xs 9 | -------------------------------------------------------------------------------- /contests/lambda-calculi-march-2016/lambda-march-concave-polygon.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import Data.List 4 | 5 | sub (x0, y0) (x1, y1) = (x0-x1, y0-y1) 6 | 7 | cross (x0, y0) (x1, y1) = x0*y1-y0*x1 8 | 9 | monotoneChain xs' = 10 | let xs = sort xs' 11 | ys = reverse xs 12 | go [] _ acc = acc 13 | go (z:zs) n acc@ ~(y:x:xs) = 14 | if n > 1 && cross (sub y x) (sub z x) < 0 15 | then go (z:zs) (n-1) (x:xs) 16 | else go zs (n+1) (z:acc) 17 | in 18 | tail (go xs 0 []) ++ tail (go (tail ys) 1 [head ys]) 19 | 20 | main = do 21 | n <- readLn 22 | xs <- replicateM n $ ((\[x,y] -> (read x, read y)) . words) <$> getLine :: IO [(Int, Int)] 23 | let ys = monotoneChain xs 24 | putStrLn $ if length xs == length ys then "NO" else "YES" 25 | -------------------------------------------------------------------------------- /contests/may-world-codesprint/absolute-permutation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | int main() 9 | { 10 | cin.tie(0); 11 | ios_base::sync_with_stdio(0); 12 | long cases, n, k; 13 | for (cin >> cases; cases--; ) { 14 | cin >> n >> k; 15 | if (! k) 16 | REP(i, n) 17 | cout << i+1 << " \n"[i == n-1]; 18 | else if (n%k || n/k%2) 19 | cout << -1 << '\n'; 20 | else 21 | for (long i = 0; i < n; i += 2*k) { 22 | REP(j, k) 23 | cout << i+j+k+1 << ' '; 24 | REP(j, k) 25 | cout << i+j+1 << " \n"[i+j+k+1 == n]; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /contests/may-world-codesprint/compare-the-triplets.hs: -------------------------------------------------------------------------------- 1 | import Data.Functor 2 | import Text.Printf 3 | 4 | f x y = fromEnum $ x > y 5 | 6 | main = do 7 | [a,b,c] <- (map read . words) <$> getLine :: IO [Int] 8 | [aa,bb,cc] <- (map read . words) <$> getLine :: IO [Int] 9 | printf "%d %d\n" (f a aa + f b bb + f c cc) (f aa a + f bb b + f cc c) 10 | -------------------------------------------------------------------------------- /contests/may-world-codesprint/div-and-span.cc: -------------------------------------------------------------------------------- 1 | // There are Catalan[X] ways to place brackets, 2*X+1 positions to place parentheses 2 | // Catalan's convolution 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define REP1(i, n) for (remove_cv::type>::type i = 1; i <= (n); i++) 8 | 9 | const long X = 100000, Y = 200, Z = 2*(X+Y), MOD = 1000000007; 10 | long fac[Z+1]; 11 | 12 | long inv(long x) 13 | { 14 | long r = 1; 15 | for (; x > 1; x = MOD%x) 16 | r = r * (MOD-MOD/x) % MOD; 17 | return r; 18 | } 19 | 20 | int main() 21 | { 22 | cin.tie(0); 23 | ios_base::sync_with_stdio(0); 24 | fac[0] = 1; 25 | REP1(i, Z) 26 | fac[i] = fac[i-1] * i % MOD; 27 | long cases, m, n; 28 | for (cin >> cases; cases--; ) { 29 | cin >> m >> n; 30 | long base = fac[2*(n+m)] * inv(fac[n]) % MOD * inv(fac[2*(n+m)-n]) % MOD * (2*m+1) % MOD * inv(n+m*2+1) % MOD, 31 | catalan = fac[2*m] * inv(fac[m]) % MOD * inv(fac[m+1]) % MOD; 32 | cout << catalan * base % MOD * fac[m] % MOD * fac[n] % MOD << '\n'; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /contests/may-world-codesprint/richie-rich.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long N = 100000; 5 | char a[N+1]; 6 | 7 | int main() 8 | { 9 | long n, k, s = 0; 10 | cin >> n >> k >> a; 11 | for (long i = 0, j = n-1; i < j; i++, j--) 12 | if (a[i] != a[j]) 13 | s++; 14 | if (k < s) 15 | return (cout << "-1\n"), 0; 16 | for (long i = 0, j = n-1; i < j; i++, j--) 17 | if (a[i] == '9') { 18 | if (a[j] != '9') 19 | k--, s--, a[j] = '9'; 20 | } else if (a[j] == '9') 21 | k--, s--, a[i] = '9'; 22 | else if (a[i] == a[j]) { 23 | if (k-2 >= s) 24 | k -= 2, a[i] = a[j] = '9'; 25 | } else { 26 | if (k > s) 27 | k -= 2, s--, a[i] = a[j] = '9'; 28 | else if (a[i] < a[j]) 29 | k--, s--, a[i] = a[j]; 30 | else 31 | k--, s--, a[j] = a[i]; 32 | } 33 | if (n%2 && k && a[n/2] < '9') 34 | a[n/2] = '9'; 35 | cout << a << endl; 36 | } 37 | -------------------------------------------------------------------------------- /contests/may-world-codesprint/xor-quadruples.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 8 | #define REP1(i, n) for (remove_cv::type>::type i = 1; i <= (n); i++) 9 | 10 | int main() 11 | { 12 | long x[4], a, b, c, d, s = 0, t = 0; 13 | unordered_map e; 14 | cin >> x[0] >> x[1] >> x[2] >> x[3]; 15 | sort(x, x+4); 16 | a = x[0]; 17 | b = x[1]; 18 | c = x[2]; 19 | d = x[3]; 20 | REP1(i, c) { 21 | if (i <= b) 22 | REP1(k, min(a, i)) 23 | e[k^i]++; 24 | FOR(j, i, d+1) { 25 | if (e.count(i^j)) 26 | s += e[i^j]; 27 | if (i <= a) 28 | t += i*(i+1)/2; 29 | else 30 | t += a*(a+1)/2+(min(b,i)-a)*a; 31 | } 32 | } 33 | cout << t-s << endl; 34 | } 35 | -------------------------------------------------------------------------------- /contests/real-data-contest-2015/normally-distributed-scores.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double l = 2000, h = 1e9; 8 | while (h-l > 1e-3) { 9 | double m = (l+h)/2; 10 | if (0.5 * (1 + erf((m-2000)/200 * M_SQRT1_2)) < 0.9) 11 | l = m; 12 | else 13 | h = m; 14 | } 15 | printf("%.2lf\n", (l+h)/2); 16 | } 17 | -------------------------------------------------------------------------------- /contests/regular-expresso/balanced-strings.rb: -------------------------------------------------------------------------------- 1 | Regex_Pattern = /^(ab|ba)*$/ 2 | -------------------------------------------------------------------------------- /contests/regular-expresso/match-maker.rb: -------------------------------------------------------------------------------- 1 | Regex_Pattern = /^((?!aaaf|aba|abe|ai|bb|bd|bg|cg|dd|di|gc|ha|ie|jc|jd|jh).)*$/ 2 | 3 | 4 | -------------------------------------------------------------------------------- /contests/regular-expresso/matching-same-text-again-again.rb: -------------------------------------------------------------------------------- 1 | Regex_Pattern = /\A([a-z])(\w)(\s)(\W)(\d)(\D)([A-Z])([a-zA-Z])([aeiouAEIOU])(\S)\1\2\3\4\5\6\7\8\9\10\z/ 2 | -------------------------------------------------------------------------------- /contests/regular-expresso/matching-specific-string.js: -------------------------------------------------------------------------------- 1 | var Regex_Pattern = 'hackerrank'; 2 | -------------------------------------------------------------------------------- /contests/regular-expresso/positive-lookahead.js: -------------------------------------------------------------------------------- 1 | var Regex_Pattern = /o(?=oo)/g; //Do not delete `/` and `/g`. 2 | -------------------------------------------------------------------------------- /contests/regular-expresso/restricted-repetitions.rb: -------------------------------------------------------------------------------- 1 | Regex_Pattern = /^(hh+)\1+(aaa+)\2+(c+)\3(kk){2,10}k$/ 2 | -------------------------------------------------------------------------------- /contests/regular-expresso/vowels-in-the-back.js: -------------------------------------------------------------------------------- 1 | var Regex_Pattern = /^[a-z]*[aeiouy][a-z]{9}$/; //Do not delete '/'. Replace __________ with your regex. 2 | -------------------------------------------------------------------------------- /contests/regular-expresso/winning-tic-tac-toe.rb: -------------------------------------------------------------------------------- 1 | Regex_Pattern = /^(|...|.{6})([OX])\2\2|([OX])..\3..\3|([OX])...\4...\4|^..([OX]).\5.\5/ 2 | -------------------------------------------------------------------------------- /contests/rookierank/antiprime-numbers.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | 8 | const long N = 10810801; 9 | long f[N+1], pr[714823], np = 0; 10 | bool sieved[N]; 11 | 12 | int main() 13 | { 14 | ios_base::sync_with_stdio(0); 15 | cin.tie(0); 16 | f[1] = 1; 17 | FOR(i, 2, N) { 18 | if (! sieved[i]) { 19 | f[i] = 2; 20 | pr[np++] = i; 21 | } 22 | for (long j = 0; j < np && i*pr[j] < N; j++) { 23 | sieved[i*pr[j]] = true; 24 | long t = i, c = 1; 25 | for (; t%pr[j] == 0; t /= pr[j]) 26 | c++; 27 | f[i*pr[j]] = f[t]*(c+1); 28 | if (c > 1) break; 29 | } 30 | } 31 | FOR(i, 2, N) 32 | f[i] = max(f[i], f[i-1]); 33 | for (long t = f[1], j = 1, i = 1; i < N; ) { 34 | while (++j < N && f[j] == t); 35 | if (j < N) 36 | t = f[j]; 37 | while (i < j) 38 | f[++i] = j; 39 | } 40 | long q, n; 41 | for (cin >> q; q--; ) { 42 | cin >> n; 43 | cout << f[n] << '\n'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /contests/rookierank/birthday-cake-candles.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | const long N = 100000; 10 | long a[N]; 11 | 12 | int main() 13 | { 14 | ios_base::sync_with_stdio(0); 15 | long n, a, mx = LONG_MIN, c = 0; 16 | cin >> n; 17 | REP(i, n) { 18 | cin >> a; 19 | if (a > mx) 20 | mx = a, c = 1; 21 | else if (a == mx) 22 | c++; 23 | } 24 | cout << c << endl; 25 | } 26 | -------------------------------------------------------------------------------- /contests/rookierank/counting-valleys.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | const long N = 1000000; 9 | char a[N+1]; 10 | 11 | int main() 12 | { 13 | long n, h = 0, c = 0; 14 | cin >> n >> a; 15 | REP(i, n) 16 | if (a[i] == 'D') 17 | h--; 18 | else if (! ++h) 19 | c++; 20 | cout << c << endl; 21 | } 22 | -------------------------------------------------------------------------------- /contests/rookierank/extremely-dangerous-virus.hs: -------------------------------------------------------------------------------- 1 | import Data.Functor 2 | 3 | modulus = 1000000007 4 | 5 | powMod a n 6 | | n == 0 = 1 7 | | even n = x*x `rem` modulus 8 | | otherwise = x*x `rem` modulus*a `rem` modulus 9 | where 10 | x = powMod a (n `quot` 2) 11 | 12 | main = do 13 | [a,b,t] <- (map read . words) <$> getLine 14 | print $ powMod ((a+b) `quot` 2) t 15 | -------------------------------------------------------------------------------- /contests/rookierank/magic-square-forming.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 9 | #define REP(i, n) FOR(i, 0, n) 10 | 11 | int main() 12 | { 13 | long a[9], p[9], ans = 81; 14 | REP(i, 9) 15 | cin >> a[i]; 16 | iota(p, p+9, 1); 17 | do { 18 | if (p[0]+p[1]+p[2] == 15 && 19 | p[3]+p[4]+p[5] == 15 && 20 | p[6]+p[7]+p[8] == 15 && 21 | p[0]+p[3]+p[6] == 15 && 22 | p[1]+p[4]+p[7] == 15 && 23 | p[2]+p[5]+p[8] == 15 && 24 | p[0]+p[4]+p[8] == 15 && 25 | p[2]+p[4]+p[6] == 15) { 26 | long t = 0; 27 | REP(i, 9) 28 | t += abs(p[i]-a[i]); 29 | ans = min(ans, t); 30 | } 31 | } while (next_permutation(p, p+9)); 32 | cout << ans << endl; 33 | } 34 | -------------------------------------------------------------------------------- /contests/w18/gg.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 3000; 16 | char a[N+1]; 17 | int dp[N]; 18 | 19 | int main() 20 | { 21 | int n = ri(), m = ri(); 22 | scanf("%s", a); 23 | dp[0] = 1; 24 | FOR(i, 1, n) 25 | if (a[i-1] == 'G') 26 | ROF(j, 0, i-1) 27 | (dp[j] += dp[j+1]) %= m; 28 | else { 29 | int acc = 0; 30 | REP(j, i+1) { 31 | int t = (acc+dp[j])%m; 32 | dp[j] = acc; 33 | acc = t; 34 | } 35 | } 36 | int ans = 0; 37 | REP(i, n) 38 | (ans += dp[i]) %= m; 39 | printf("%d\n", ans); 40 | } 41 | -------------------------------------------------------------------------------- /contests/w18/rhombographs.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 7 | 8 | int main() 9 | { 10 | int a, b, x; 11 | cin >> a >> b >> x; 12 | if (a == 1 && b == x) { 13 | cout << "-1 -1" << endl; 14 | return 0; 15 | } 16 | REP1(i, x+1) { 17 | int j = x+2-i; 18 | if (a <= i*j && i*j <= b) { 19 | a = i; 20 | b = j; 21 | break; 22 | } 23 | } 24 | cout << a*b << ' ' << a*b*x/2 << endl; 25 | REP(i, a) 26 | REP(j, b) { 27 | FOR(k, i+1, a) 28 | cout << i*b+j+1 << ' ' << k*b+j+1 << endl; 29 | FOR(k, j+1, b) 30 | cout << i*b+j+1 << ' ' << i*b+k+1 << endl; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /contests/w18/two-centers.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 8 | #define pb push_back 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 5000; 18 | vector e[N]; 19 | int n, d[N], q[N], p[N]; 20 | 21 | int bfs(int u, int cut = -1) 22 | { 23 | int *fr = q, *re = q; 24 | fill_n(d, n, -1); 25 | d[*re++ = u] = 0; 26 | while (fr < re) { 27 | u = *fr++; 28 | for (int v: e[u]) 29 | if (d[v] < 0 && v != cut) { 30 | d[v] = d[u]+1; 31 | p[v] = u; 32 | *re++ = v; 33 | } 34 | } 35 | return re[-1]; 36 | } 37 | 38 | int main() 39 | { 40 | n = ri(); 41 | REP(i, n-1) { 42 | int u = ri()-1, v = ri()-1; 43 | e[u].pb(v); 44 | e[v].pb(u); 45 | } 46 | int v = bfs(bfs(0)); 47 | ROF(i, 0, d[v]/2) 48 | v = p[v]; 49 | int u = p[v]; 50 | printf("%d\n", max(d[bfs(bfs(v, u), u)], d[bfs(bfs(u, v), v)]) + 1 >> 1); 51 | } 52 | -------------------------------------------------------------------------------- /contests/w20/divisible-sum-pairs.hs: -------------------------------------------------------------------------------- 1 | import Data.Array 2 | import Data.Functor 3 | 4 | main = do 5 | [n, k] <- (map read . words) <$> getLine 6 | a <- (listArray (0, n-1) . map read . words) <$> getLine :: IO (Array Int Int) 7 | print $ sum [if (a!i+a!j) `rem` k == 0 then 1 else 0 | i <- [0..n-1], j <- [i+1..n-1]] 8 | -------------------------------------------------------------------------------- /contests/w20/non-divisible-subset.hs: -------------------------------------------------------------------------------- 1 | import Data.Array 2 | import Data.List hiding (foldl1', sum) 3 | import Data.Foldable 4 | import Data.Functor 5 | import Data.Maybe 6 | import Prelude hiding (sum) 7 | import qualified Data.ByteString.Char8 as B 8 | 9 | main = do 10 | [n, k] <- (map read . words) <$> getLine 11 | a <- (accumArray (+) 0 (0, k-1) . map ((\x -> (x `rem` k, 1)) . fst . fromJust . B.readInt) . B.words) <$> B.getLine :: IO (Array Int Int) 12 | print $ sum [if (2*i) `rem` k == 0 then min (a!i) 1 else max (a!i) (a!(k-i)) | i <- [0..k `quot` 2]] 13 | -------------------------------------------------------------------------------- /contests/w21/borrowing-money.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 8 | #define REP(i, n) FOR(i, 0, n) 9 | 10 | const long N = 34; 11 | long a[N], adj[N]; 12 | unordered_map> m; 13 | 14 | pair rec(long cand) 15 | { 16 | if (m.count(cand)) 17 | return m[cand]; 18 | auto& r = m[cand]; 19 | if (! cand) 20 | r = {0, 1}; 21 | else { 22 | long i = __builtin_ctzl(cand); 23 | auto x = rec(cand & ~ (1L< x.first) 26 | x = y; 27 | else if (y.first == x.first) 28 | x.second += y.second; 29 | r = x; 30 | } 31 | return r; 32 | } 33 | 34 | int main() 35 | { 36 | long n, m, u, v; 37 | cin >> n >> m; 38 | REP(i, n) 39 | cin >> a[i]; 40 | while (m--) { 41 | cin >> u >> v; 42 | u--, v--; 43 | adj[u] |= 1L << v; 44 | adj[v] |= 1L << u; 45 | } 46 | auto r = rec((1L << n) - 1); 47 | cout << r.first << ' ' << r.second << endl; 48 | } 49 | -------------------------------------------------------------------------------- /contests/w21/kangaroo.hs: -------------------------------------------------------------------------------- 1 | import Data.Functor 2 | 3 | main = do 4 | [x1,v1,x2,v2] <- (map read . words) <$> getLine 5 | putStrLn $ if v1 > v2 && (x2-x1) `mod` (v1-v2) == 0 then "YES" else "NO" 6 | -------------------------------------------------------------------------------- /contests/w21/lazy-sorting.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE ScopedTypeVariables #-} 2 | import Data.Functor 3 | import Data.List 4 | import Data.Ratio 5 | import Text.Printf 6 | 7 | fac = scanl (*) 1 [1..] :: [Integer] 8 | 9 | main = do 10 | n <- readLn :: IO Int 11 | b <- (map read . words) <$> getLine :: IO [Int] 12 | let a = group $ sort b 13 | ans = (realToFrac (fac!!n) /) . realToFrac . product $ map ((fac!!) . length) a :: Rational 14 | printf "%.6f\n" (if b == sort b then 0 else fromRational ans :: Double) 15 | -------------------------------------------------------------------------------- /contests/w21/luck-balance.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import Data.List 4 | 5 | main = do 6 | [n, k] <- (map read . words) <$> getLine 7 | (b, a) <- partition ((/=0).snd) <$> replicateM n (((\[x,y] -> (x,y)) . map read . words) <$> getLine) :: IO ([(Int,Int)], [(Int,Int)]) 8 | let (c, d) = splitAt k . reverse $ sort b 9 | print $ sum (map fst c) - sum (map fst d) + sum (map fst a) 10 | -------------------------------------------------------------------------------- /contests/w22/box-moving.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | const long N = 100000; 10 | long a[N], b[N]; 11 | 12 | int main() 13 | { 14 | long n, pos = 0, neg = 0; 15 | cin >> n; 16 | REP(i, n) 17 | cin >> a[i]; 18 | REP(i, n) 19 | cin >> b[i]; 20 | sort(a, a+n); 21 | sort(b, b+n); 22 | REP(i, n) { 23 | b[i] -= a[i]; 24 | if (b[i] > 0) pos += b[i]; 25 | else neg -= b[i]; 26 | } 27 | cout << (pos == neg ? pos : -1) << endl; 28 | } 29 | -------------------------------------------------------------------------------- /contests/w22/cookie-party.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long n, m; 7 | cin >> n >> m; 8 | cout << (n-m%n)%n << endl; 9 | } 10 | -------------------------------------------------------------------------------- /contests/w22/polygon-making.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | long a[50]; 10 | 11 | int main() 12 | { 13 | long n, tot = 0, s = 0; 14 | cin >> n; 15 | REP(i, n) { 16 | cin >> a[i]; 17 | tot += a[i]; 18 | } 19 | if (n < 3) 20 | cout << 3-n; 21 | else { 22 | s = *max_element(a, a+n); 23 | cout << s/(tot-s); 24 | } 25 | cout << endl; 26 | } 27 | -------------------------------------------------------------------------------- /contests/womens-codesprint/consonant-reversal.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad.State 2 | import Data.Char 3 | import Data.Functor 4 | import Data.Maybe 5 | import qualified Data.Set as S 6 | import Text.Printf 7 | import qualified Data.ByteString.Char8 as B 8 | 9 | main = do 10 | cases <- readLn 11 | let vowel = S.fromList "aeiou" 12 | replicateM_ cases $ do 13 | a <- B.getLine 14 | let b = B.reverse $ B.filter (`S.notMember` vowel) a 15 | f i j | i >= B.length a = return () 16 | f i j = 17 | if S.member c vowel 18 | then putChar c >> f (i+1) j 19 | else putChar (B.index b j) >> f (i+1) (j+1) 20 | where 21 | c = B.index a i 22 | f 0 0 23 | putChar '\n' 24 | -------------------------------------------------------------------------------- /contests/womens-codesprint/even-training.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad.State 2 | import Data.Char 3 | import Data.Functor 4 | import Data.Maybe 5 | import Text.Printf 6 | import qualified Data.ByteString.Char8 as B 7 | 8 | int = state $ fromJust . B.readInt . B.dropWhile isSpace 9 | 10 | parse = do 11 | n <- int 12 | a <- replicateM n int 13 | return (n, a) 14 | 15 | main = do 16 | (n, a) <- evalState parse <$> B.getContents 17 | let s = sum a 18 | putStr $ if even s then "Yes" else "No" 19 | printf " %d\n" s 20 | -------------------------------------------------------------------------------- /contests/womens-codesprint/grouping-the-items.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define REP(i, n) for (decltype(n) i = 0; i < (n); i++) 10 | 11 | const int N = 2000; 12 | char a[N+1]; 13 | 14 | int main() 15 | { 16 | int n; 17 | map, vector> m; 18 | map*> mm; 19 | scanf("%d", &n); 20 | REP(i, n) { 21 | scanf("%s", a); 22 | vector h(10, 0); 23 | for (int j = 0; a[j]; j++) 24 | h[a[j]-'0']++; 25 | m[h].push_back(a); 26 | } 27 | int opt = 0; 28 | for (auto &x: m) { 29 | sort(x.second.begin(), x.second.end(), greater()); 30 | x.second.erase(unique(x.second.begin(), x.second.end()), x.second.end()); 31 | if (x.second.size() > opt) 32 | opt = x.second.size(); 33 | } 34 | for (auto &x: m) 35 | if (x.second.size() == opt) 36 | mm[x.second[0]] = &x.second; 37 | printf("%zd\n", m.size()); 38 | for (auto &xx: mm) { 39 | for (auto &x: *xx.second) 40 | printf("%s ", x.c_str()); 41 | puts(""); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /contests/womens-codesprint/smriti-and-strings.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define ROF(i, a, b) for (remove_cv::type>::type i = (b); --i >= (a); ) 6 | 7 | long rl() 8 | { 9 | long x; 10 | scanf("%ld", &x); 11 | return x; 12 | } 13 | 14 | const long N = 1000000; 15 | char a[N+1]; 16 | 17 | int main() 18 | { 19 | ROF(_, 0, rl()) { 20 | scanf("%s", a); 21 | long m = rl(); 22 | char *q = a; 23 | for (char *p = a; *p; p++) { 24 | while (m && a < q && q[-1] < *p) 25 | q--, m--; 26 | *q++ = *p; 27 | } 28 | q[-m] = '\0'; 29 | puts(a); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /contests/world-codesprint-5/camelcase.hs: -------------------------------------------------------------------------------- 1 | import Data.Char 2 | import Data.Functor 3 | import Data.ByteString.Char8 as B 4 | 5 | main = (succ . B.length . B.filter isUpper) <$> B.getLine >>= print 6 | -------------------------------------------------------------------------------- /contests/world-codesprint-5/longest-increasing-subsequence-arrays.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | const long M = 500000, MOD = 1000000007; 9 | long inv[M+1]; 10 | 11 | int main() 12 | { 13 | long cases, n, m; 14 | inv[1] = 1; 15 | FOR(i, 2, M+1) 16 | inv[i] = (MOD-MOD/i)*inv[MOD%i]%MOD; 17 | for (cin >> cases; cases--; ) { 18 | cin >> m >> n; 19 | long sum = 0, t = 1, invn = inv[n]; 20 | REP(i, m-n) 21 | t = t*n%MOD; 22 | REP(i, m-n+1) { 23 | sum = (sum+t)%MOD; 24 | t = t*invn%MOD * (n-1)%MOD * (n+i)%MOD*inv[i+1]%MOD; 25 | } 26 | cout << sum << endl; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /contests/world-codesprint-5/string-construction.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import Data.List 4 | 5 | main = do 6 | cases <- readLn 7 | replicateM_ cases $ (length . group . sort) <$> getLine >>= print 8 | -------------------------------------------------------------------------------- /contests/world-codesprint-6/abbr.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 9 | #define REP(i, n) FOR(i, 0, n) 10 | 11 | const long N = 1000; 12 | char a[N+1], b[N+1], c[N+1][N+1]; 13 | 14 | int main() 15 | { 16 | long cases, m, n; 17 | for (cin >> cases; cases--; ) { 18 | cin >> a >> b; 19 | m = strlen(a); 20 | n = strlen(b); 21 | fill_n(c[0]+1, n, 0); 22 | c[0][0] = 1; 23 | REP(i, m) { 24 | c[i+1][0] = c[i][0] && islower(a[i]); 25 | REP(j, n) 26 | c[i+1][j+1] = c[i][j] && (islower(b[j]) ? a[i] == b[j] : toupper(a[i]) == b[j]) || islower(a[i]) && c[i][j+1]; 27 | } 28 | cout << (c[m][n] ? "YES" : "NO") << endl; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /contests/world-codesprint-6/beautiful-3-set.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Construction: |set| = K = floor(2*n/3)+1 3 | * For a set of tuples whose cardinality = K, its sum = K*N >= K*(K-1)/2*3, thus K <= floor(2*n/3)+1 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 10 | #define REP(i, n) FOR(i, 0, n) 11 | 12 | int main() 13 | { 14 | long n, m; 15 | cin >> n; 16 | m = n*2/3+1; 17 | cout << m << '\n'; 18 | REP(i, m) { 19 | long j = n%3 < 2 ? (n/3+i) % m : (n/3+2+i) % (m+1); 20 | cout << i << ' ' << j << ' ' << n-i-j << '\n'; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /contests/world-codesprint-6/bon-appetit.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | int main() 9 | { 10 | long n, k, a, s = 0; 11 | cin >> n >> k; 12 | REP(i, n) { 13 | cin >> a; 14 | if (i != k) s += a; 15 | } 16 | cin >> a; 17 | s = a-s/2; 18 | if (s > 0) cout << s << endl; 19 | else cout << "Bon Appetit\n"; 20 | } 21 | -------------------------------------------------------------------------------- /contests/world-codesprint-6/bonetrousle.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP1(i, n) for (remove_cv::type>::type i = 1; i <= (n); i++) 6 | 7 | const long N = 100000; 8 | long a[N]; 9 | 10 | bool f(long n, long k, long b) 11 | { 12 | long l = n/b-(b-1)/2; 13 | if (n < (2*l+b-1)*b/2) l--; 14 | long r = l+b-1, d = 0; 15 | n -= (2*l+b-1)*b/2; 16 | a[0] = l; 17 | REP1(i, b-1) { 18 | long t = n/(b-i); 19 | n -= t*(b-i); 20 | d += t; 21 | a[i] = l+i+d; 22 | } 23 | return 1 <= l && a[b-1] <= k; 24 | } 25 | 26 | int main() 27 | { 28 | ios_base::sync_with_stdio(0); 29 | cin.tie(0); 30 | long cases, n, k, b; 31 | for (cin >> cases; cases--; ) { 32 | cin >> n >> k >> b; 33 | if (! f(n, k, b)) { 34 | cout << -1 << endl; 35 | continue; 36 | } 37 | cout << a[0]; 38 | REP1(i, b-1) 39 | cout << ' ' << a[i]; 40 | cout << endl; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /contests/world-codesprint-6/combination-lock.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | int main() 10 | { 11 | long a[5], b, s = 0; 12 | REP(i, 5) 13 | cin >> a[i]; 14 | REP(i, 5) { 15 | cin >> b; 16 | b = abs(b-a[i]); 17 | s += min(b, 10-b); 18 | } 19 | cout << s << endl; 20 | } 21 | -------------------------------------------------------------------------------- /contests/world-codesprint-6/flipping-the-matrix.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define FOR(i, a, b) for (remove_cv::type>::type i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | const long N = 128; 10 | long a[2*N][2*N]; 11 | 12 | int main() 13 | { 14 | long cases, n; 15 | for (cin >> cases; cases--; ) { 16 | long s = 0; 17 | cin >> n; 18 | REP(i, 2*n) 19 | REP(j, 2*n) 20 | cin >> a[i][j]; 21 | REP(i, n) 22 | REP(j, n) 23 | s += max(max(a[i][j], a[2*n-1-i][j]), max(a[i][2*n-1-j], a[2*n-1-i][2*n-1-j])); 24 | cout << s << endl; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /contests/world-codesprint-april/beautiful-triplets.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (decltype(n) i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | int main() 15 | { 16 | int n = ri(), d = ri(), s = 0; 17 | set c; 18 | REP(i, n) { 19 | int a = ri(); 20 | c.insert(a); 21 | if (c.count(a-d) && c.count(a-2*d)) 22 | s++; 23 | } 24 | printf("%d\n", s); 25 | } 26 | -------------------------------------------------------------------------------- /contests/world-codesprint-april/gena.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (decltype(n) i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 10, S = 1048576; 15 | int a[N], d[S]; 16 | 17 | #define f(x, i) ((x) >> 2*(i) & 3) 18 | 19 | int main() 20 | { 21 | int n = ri(), x = 0; 22 | REP(i, n) 23 | x |= ri()-1 << 2*i; 24 | fill_n(d, S, -1); 25 | queue q; 26 | d[x] = 0; 27 | for (q.push(x); ! q.empty(); q.pop()) { 28 | x = q.front(); 29 | if (! x) break; 30 | int u[4] = {}; 31 | REP(i, n) 32 | if (! u[f(x, i)]) { 33 | u[f(x, i)] = 1; 34 | REP(j, 4) 35 | if (! u[j]) { 36 | int y = x & ~ (3 << 2*i) | (j << 2*i); 37 | if (d[y] < 0) { 38 | d[y] = d[x]+1; 39 | q.push(y); 40 | } 41 | } 42 | } 43 | } 44 | printf("%d\n", d[0]); 45 | } 46 | -------------------------------------------------------------------------------- /contests/world-codesprint-april/jumping-on-the-clouds.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (decltype(n) i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 100; 15 | int a[100]; 16 | 17 | int main() 18 | { 19 | int n = ri(); 20 | REP(i, n) 21 | a[i] = ri() ? n : i ? min(i ? a[i-1] : n, i > 1 ? a[i-2] : n) + 1 : 0; 22 | printf("%d\n", a[n-1]); 23 | } 24 | -------------------------------------------------------------------------------- /contests/world-codesprint-april/kmp-problem.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (int i = 0; i < (n); i++) 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | const int M = 26; 14 | int a[M]; 15 | 16 | int main() 17 | { 18 | int n = 0, x = -1, y = -1; 19 | REP(i, M) { 20 | a[i] = ri(); 21 | n += a[i]; 22 | if (a[i]) { 23 | if (x < 0 || a[i] < a[x]) { 24 | if (x >= 0 && y < 0) 25 | y = x; 26 | x = i; 27 | } 28 | } 29 | } 30 | putchar('a'+x); 31 | a[x]--; 32 | n--; 33 | if (y >= 0) { 34 | REP(i, M) 35 | REP(_, a[i]) 36 | putchar('a'+i); 37 | } else if (a[x] == n) 38 | REP(_, a[x]) 39 | putchar('a'+x); 40 | else { 41 | y = x+1; 42 | REP(i, a[x]) { 43 | for (; ! a[y]; y++); 44 | putchar('a'+x); 45 | putchar('a'+y); 46 | a[y]--; 47 | } 48 | for (; y < M; y++) 49 | REP(_, a[y]) 50 | putchar('a'+y); 51 | } 52 | puts(""); 53 | } 54 | -------------------------------------------------------------------------------- /contests/world-codesprint-april/kmp-problem.hs: -------------------------------------------------------------------------------- 1 | import Data.Function 2 | import Data.Functor 3 | import Data.List 4 | 5 | f [] = return () 6 | f ((cx,x):xs) = do 7 | putChar x 8 | f $ if cx == 1 then xs else (cx-1,x):xs 9 | 10 | 11 | g _ [] = return () 12 | 13 | -- The first two and the last characters are the same, thus we can not use 'x' 14 | g 0 [x] = g 1 [x] 15 | g 0 ((cx,x):(cy,y):xs) = do 16 | putChar y 17 | if cy == 1 18 | then g 1 $ (cx,x):xs 19 | else g 1 $ (cx,x):(cy-1,y):xs 20 | 21 | -- The first character is different to the last, and we can use 'x' 22 | g 1 ((cx,x):xs) = do 23 | putChar x 24 | if cx == 1 25 | then g 2 xs 26 | else g 0 $ (cx-1,x):xs 27 | 28 | -- The first character has been used up 29 | g 2 ((cx,x):xs) = do 30 | putChar x 31 | if cx == 1 32 | then g 2 xs 33 | else g 2 $ (cx-1,x):xs 34 | 35 | main = do 36 | a <- (map read . words) <$> getLine 37 | let (xc,x):xs = sort . filter ((0/=) . fst) $ zip a ['a'..] 38 | putChar x 39 | let ys = sortBy (compare `on` snd) $ if xc == 1 then xs else (xc-1,x):xs 40 | case ys of 41 | (_,y):_ | x == y -> g 1 ys 42 | _ -> f ys 43 | -------------------------------------------------------------------------------- /contests/world-codesprint-april/move-the-coins.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (decltype(n) i = 0; i < (n); i++) 6 | #define ROF(i, a, b) for (decltype(b) i = (b); --i >= (a); ) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 50000; 16 | int a[N], dep[N], pre[N], post[N], nim[N][2], tick; 17 | vector es[N]; 18 | 19 | void dfs(int u, int d, int p) 20 | { 21 | nim[u][d] += a[u]; 22 | dep[u] = d; 23 | pre[u] = tick++; 24 | for (int v: es[u]) 25 | if (v != p) { 26 | dfs(v, d^1, u); 27 | REP(i, 2) 28 | nim[u][i] ^= nim[v][i]; 29 | } 30 | post[u] = tick; 31 | } 32 | 33 | int main() 34 | { 35 | int n = ri(); 36 | REP(i, n) 37 | a[i] = ri(); 38 | REP(i, n-1) { 39 | int u = ri()-1, v = ri()-1; 40 | es[u].push_back(v); 41 | es[v].push_back(u); 42 | } 43 | dfs(0, 0, -1); 44 | ROF(_, 0, ri()) { 45 | int u = ri()-1, v = ri()-1; 46 | if (pre[u] <= pre[v] && pre[v] < post[u]) 47 | puts("INVALID"); 48 | else 49 | puts(nim[0][1] ^ (dep[u] ^ dep[v] ? 0 : nim[u][0] ^ nim[u][1]) ? "YES" : "NO"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /contests/zenhacks/candy-shop.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 250; 15 | const int dom[] = {1,2,5,10,20,50,100}; 16 | int a[N+1]; 17 | 18 | int main() 19 | { 20 | int n = ri(); 21 | a[0] = 1; 22 | for (int x: dom) 23 | FOR(i, x, n+1) 24 | a[i] += a[i-x]; 25 | printf("%d\n", a[n]); 26 | } 27 | -------------------------------------------------------------------------------- /contests/zenhacks/decrypt-1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 100; 16 | char a[N+1], b[N+1]; 17 | 18 | int main() 19 | { 20 | for (int cc = ri(); cc--; ) { 21 | scanf("%s%s", a, b); 22 | int n = strlen(a), ans = n; 23 | REP(i, 26) { 24 | int t = 0; 25 | REP(j, n) { 26 | int aa = (a[j]-'a'+i)%26, bb = b[j]-'a'; 27 | t += aa != bb; 28 | } 29 | ans = min(ans, t); 30 | } 31 | printf("%d\n", ans); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /contests/zenhacks/pairing.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 1000; 8 | char a[N+1], b[N+1]; 9 | 10 | int main() 11 | { 12 | int n, ans = 0; 13 | typedef tuple typ; 14 | multiset S; 15 | cin >> n; 16 | while (n--) { 17 | string c, cc, t; 18 | int s; 19 | cin >> c >> s >> cc >> t; 20 | typ g{c, s, cc, 'L'+'R'-t[0]}; 21 | if (S.count(g)) { 22 | S.erase(S.find(g)); 23 | ans++; 24 | } else 25 | S.insert(typ{c, s, cc, t[0]}); 26 | } 27 | printf("%d\n", ans); 28 | } 29 | -------------------------------------------------------------------------------- /contests/zenhacks/primedist.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 100000; 16 | bool sieve[N]; 17 | int pi[N]; 18 | 19 | int main() 20 | { 21 | int n = ri(); 22 | for (int i = 2; i*i < n; i++) 23 | if (! sieve[i]) 24 | for (int j = i*i; j < n; j += i) 25 | sieve[j] = true; 26 | FOR(i, 2, n) 27 | pi[i] = pi[i-1] + ! sieve[i]; 28 | ll ans = 0; 29 | REP(i, n) 30 | ans += (long long)(pi[i]-pi[n-1-i]) * ri(); 31 | printf("%lld\n", ans); 32 | } 33 | -------------------------------------------------------------------------------- /contests/zenhacks/zenland.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | typedef pair pll; 9 | #define REP(i, n) for (int i = 0; i < (n); i++) 10 | #define fi first 11 | #define pb push_back 12 | #define se second 13 | 14 | int ri() 15 | { 16 | int x; 17 | scanf("%d", &x); 18 | return x; 19 | } 20 | 21 | const int N = 100000; 22 | int a[N]; 23 | vector e[N]; 24 | 25 | pll dfs(int v, int p) 26 | { 27 | pll r = {a[v], 0}; 28 | for (int u: e[v]) 29 | if (u != p) { 30 | auto rr = dfs(u, v); 31 | r.fi += rr.fi; 32 | r.se += rr.se; 33 | } 34 | return {r.fi, r.se+abs(r.fi)}; 35 | } 36 | 37 | int main() 38 | { 39 | int n = ri(); 40 | REP(i, n) { 41 | int x = ri(), y = ri(); 42 | a[i] = x-y; 43 | } 44 | REP(i, n-1) { 45 | int v = ri(), u = ri(); 46 | e[v].pb(u); 47 | e[u].pb(v); 48 | } 49 | printf("%lld\n", dfs(0, -1).se); 50 | } 51 | -------------------------------------------------------------------------------- /countingsort1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (int i = 0; i < (n); i++) 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | int c[100]; 14 | 15 | int main() 16 | { 17 | int n = ri(); 18 | REP(i, n) 19 | c[ri()]++; 20 | REP(i, 100) 21 | printf("%d%c", c[i], i == 99 ? '\n' : ' '); 22 | } 23 | -------------------------------------------------------------------------------- /delete-a-node-from-a-linked-list.py: -------------------------------------------------------------------------------- 1 | def Delete(head, position): 2 | if position == 0: 3 | return head.next 4 | x = head 5 | for i in range(position-1): 6 | x = x.next 7 | x.next = x.next.next 8 | return head 9 | -------------------------------------------------------------------------------- /diagonal-difference.clj: -------------------------------------------------------------------------------- 1 | (import java.util.Scanner) 2 | 3 | (def scan (Scanner. *in*)) 4 | 5 | (let [n (.nextInt scan) 6 | m (* n n) 7 | a (repeatedly m #(.nextInt scan))] 8 | (println (Math/abs (- (reduce #(+ %1 (nth a %2)) 0 (range 0 m (inc n))) 9 | (reduce #(+ %1 (nth a %2)) 0 (range (dec n) (dec m) (dec n))))))) 10 | -------------------------------------------------------------------------------- /dynamic-array.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | n, q = map(int, input().split()) 3 | ans = 0 4 | a = [[] for i in range(n)] 5 | for i in range(q): 6 | op, x, y = map(int, input().split()) 7 | if op == 1: 8 | a[(ans^x)%n].append(y) 9 | else: 10 | b = a[(ans^x)%n] 11 | ans = b[y%len(b)] 12 | print(ans) 13 | -------------------------------------------------------------------------------- /favourite-sequence.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define pb push_back 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 400, M = 1000001; 17 | bool flag[M]; 18 | int d[M]; 19 | vector e[M]; 20 | 21 | int main() 22 | { 23 | for (int n = ri(); n--; ) { 24 | int u = -1; 25 | for (int k = ri(); k--; ) { 26 | int v = ri(); 27 | flag[v] = true; 28 | if (~ u) { 29 | e[u].pb(v); 30 | d[v]++; 31 | } 32 | u = v; 33 | } 34 | } 35 | priority_queue, greater> pq; 36 | REP(i, M) 37 | if (flag[i] && ! d[i]) 38 | pq.push(i); 39 | bool first = true; 40 | while (! pq.empty()) { 41 | int u = pq.top(); 42 | pq.pop(); 43 | if (first) first = false; 44 | else putchar(' '); 45 | printf("%d", u); 46 | for (int v: e[u]) 47 | if (! --d[v]) 48 | pq.push(v); 49 | } 50 | puts(""); 51 | } 52 | -------------------------------------------------------------------------------- /filling-jars.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | const int N = 10000000; 14 | ll fenwick[N]; 15 | int c[N]; 16 | 17 | static inline void add(int n, int x, int y) 18 | { 19 | for (; x < n; x |= x+1) 20 | fenwick[x] += y; 21 | } 22 | 23 | static inline ll get_sum(int x) 24 | { 25 | ll s = 0; 26 | for (; x; x &= x-1) 27 | s += fenwick[x-1]; 28 | return s; 29 | } 30 | 31 | int main() 32 | { 33 | int n = ri(), m = ri(); 34 | ll s = 0; 35 | while (m--) { 36 | int a = ri(), b = ri(), k = ri(); 37 | add(n, a-1, k); 38 | add(n, b, -k); 39 | } 40 | fill_n(c, n, 1); 41 | for (int x = n; x > 1; x--) { 42 | int y = x&x-1; 43 | s += c[x-1]*fenwick[x-1]; 44 | c[y-1] += c[x-1]; 45 | } 46 | s += c[0]*fenwick[0]; 47 | printf("%lld\n", s/n); 48 | } 49 | -------------------------------------------------------------------------------- /find-digits.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | y = x = gets.to_i 3 | c = 0 4 | while x > 0 5 | c += 1 if x%10 > 0 && y%(x%10) == 0 6 | x /= 10 7 | end 8 | p c 9 | } 10 | -------------------------------------------------------------------------------- /find-maximum-index-product.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 100000; 16 | int a[N], s[N], l[N]; 17 | 18 | int main() 19 | { 20 | int n = ri(), m = 0; 21 | ll ans = 0; 22 | REP(i, n) { 23 | a[i] = ri(); 24 | while (m && a[s[m-1]] < a[i]) 25 | ans = max(ans, l[s[--m]]*ll(i+1)); 26 | while (m && a[s[m-1]] == a[i]) 27 | m--; 28 | l[i] = m ? s[m-1]+1 : 0; 29 | s[m++] = i; 30 | } 31 | printf("%lld\n", ans); 32 | } 33 | -------------------------------------------------------------------------------- /floyd-city-of-blinding-lights.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 400; 16 | int d[N][N]; 17 | 18 | int main() 19 | { 20 | int n = ri(), m = ri(); 21 | REP(i, n) { 22 | fill_n(d[i], n, INT_MAX/2); 23 | d[i][i] = 0; 24 | } 25 | while (m--) { 26 | int u = ri()-1, v = ri()-1, w = ri(); 27 | d[u][v] = w; 28 | } 29 | REP(k, n) 30 | REP(i, n) 31 | REP(j, n) 32 | d[i][j] = min(d[i][j], d[i][k]+d[k][j]); 33 | for (m = ri(); m--; ) { 34 | int u = ri()-1, v = ri()-1; 35 | printf("%d\n", d[u][v] == INT_MAX/2 ? -1 : d[u][v]); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /fp/area-under-curves-and-volume-of-revolving-a-curv.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Text.Printf (printf) 3 | 4 | -- This function should return a list [area, volume]. 5 | solve :: Int -> Int -> [Int] -> [Int] -> [Double] 6 | solve l r a b = [sum ys * c, sum (map ((pi*) . join (*)) ys) * c] 7 | where 8 | l' = fromIntegral l :: Double 9 | r' = fromIntegral r 10 | xs = [l',l'+0.001..r'] 11 | c = (r'-l') / fromIntegral (length xs) :: Double 12 | ys = map (\x -> sum $ zipWith (\a b -> fromIntegral a * x ^^ b) a b) xs :: [Double] 13 | 14 | 15 | --Input/Output. 16 | main :: IO () 17 | main = getContents >>= mapM_ (printf "%.1f\n"). (\[a, b, [l, r]] -> solve l r a b). map (map read. words). lines 18 | -------------------------------------------------------------------------------- /fp/bangalore-bank.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE ViewPatterns #-} 2 | import Control.Monad 3 | import Data.List 4 | 5 | tr x = if x == 0 then 9 else x-1 6 | 7 | solve :: [Int] -> Int 8 | solve (map tr -> x:xs) = ((length xs+1)+) . minimum . snd . foldl' (\(y, xs) z -> 9 | let t = map (abs (y-z) +) xs in 10 | (z, snd . mapAccumR (\r x -> join (,) $ min (r+1) x) 99999 . snd . mapAccumL (\l x -> join (,) $ min (l+1) x) 99999 $ take y t ++ (xs!!z) : drop (y+1) t) 11 | ) (x, zero) $ xs 12 | where 13 | zero = replicate 10 0 14 | 15 | main = interact $ (++"\n") . show . solve . map read . words . last . lines 16 | -------------------------------------------------------------------------------- /fp/bitter-chocolate.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let () = 4 | for cc = 1 to read_int 0 do 5 | let rc = read_int 0 in 6 | let rb = read_int 0 in 7 | let ra = read_int 0 in 8 | let tick = ref 0 in 9 | let x = Array.make (ra+rb+rc) 0 in 10 | let nim = Array.init (ra+1) (fun _ -> Array.make_matrix (rb+1) (rc+1) 0) in 11 | for a = 0 to ra do 12 | for b = a to rb do 13 | for c = b to rc do 14 | incr tick; 15 | for aa = 0 to a-1 do 16 | x.(nim.(aa).(b).(c)) <- !tick 17 | done; 18 | for bb = 0 to b-1 do 19 | x.(nim.(min a bb).(bb).(c)) <- !tick 20 | done; 21 | for cc = 1 to c-1 do 22 | x.(nim.(min a cc).(min b cc).(cc)) <- !tick 23 | done; 24 | let rec go i = 25 | if x.(i) = !tick then 26 | go (i+1) 27 | else 28 | i 29 | in 30 | nim.(a).(b).(c) <- if a = 0 && b = 0 && c = 1 then 0 else go 0 31 | done 32 | done 33 | done; 34 | print_endline (if nim.(ra).(rb).(rc) > 0 then "WIN" else "LOSE") 35 | done 36 | -------------------------------------------------------------------------------- /fp/common-divisors.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | 3 | let gcd a b = 4 | let rec go a b = 5 | if b = 0 then 6 | a 7 | else 8 | go b (a mod b) 9 | in go a b 10 | 11 | let cnt x = 12 | let rec go p r = 13 | if p*p > x then 14 | r 15 | else if p*p = x then 16 | r+1 17 | else if x mod p = 0 then 18 | go (p+1) (r+2) 19 | else 20 | go (p+1) r 21 | in go 1 0 22 | 23 | let () = 24 | for i = 1 to read_int 0 do 25 | let a = read_int 0 in 26 | let b = read_int 0 in 27 | gcd a b |> cnt |> string_of_int |> print_endline 28 | done 29 | -------------------------------------------------------------------------------- /fp/dice-path.hs: -------------------------------------------------------------------------------- 1 | import Data.Array 2 | 3 | n = 60 4 | 5 | a = listArray ((0,0,1,1,1),(n,n,6,6,6)) 6 | [ g i j t f r | i <- [0..n], j <- [0..n], 7 | t <- [1..6], f <- [1..6], r <- [1..6] ] 8 | 9 | g 0 _ _ _ _ = 0 10 | g _ 0 _ _ _ = 0 11 | g 1 1 1 2 4 = 1 12 | g i j t f r = max (if x > 0 then x+t else 0) (if y > 0 then y+t else 0) 13 | where 14 | x = a ! (i,j-1,r,f,7-t) 15 | y = a ! (i-1,j,f,7-t,r) 16 | 17 | solve :: [Int] -> Int 18 | solve [m, n] = maximum [a ! (m,n,i,j,k) | i <- [1..6], j <- [1..6], k <- [1..6]] 19 | 20 | main = interact $ unlines . map (show . solve . map read . words) . tail . lines 21 | -------------------------------------------------------------------------------- /fp/different-ways-fp.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | let rec times n f x = 4 | if n = 0 then 5 | x 6 | else 7 | times (n-1) f (f (n-1) x) 8 | 9 | let modulo = 100000007 10 | 11 | let pow x y = 12 | let rec go x y r = 13 | if y = 0 then 14 | r 15 | else if y land 1 = 1 then 16 | go (x*x mod modulo) (y/2) (r*x mod modulo) 17 | else 18 | go (x*x mod modulo) (y/2) r 19 | in go x y 1 20 | 21 | let inv x = pow x (modulo-2) 22 | 23 | let binom n m = 24 | times m (fun i x -> x * (n-i) mod modulo * inv (i+1) mod modulo) 1 25 | 26 | let () = 27 | for i = 1 to read_int 0 do 28 | let n = read_int 0 in 29 | let m = read_int 0 in 30 | binom n m |> string_of_int |> print_endline 31 | done 32 | -------------------------------------------------------------------------------- /fp/eval-ex.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f " (fun x -> x) 3 | 4 | let myexp x = 5 | let rec go n y s = 6 | if n >= 10 then 7 | s 8 | else 9 | go (n+1) (y*.x/.float_of_int (n+1)) (s+.y) 10 | in 11 | go 0 1.0 0.0 12 | 13 | let () = 14 | for i = 1 to read_int 0 do 15 | let x = read_float 0 in 16 | myexp x |> Printf.printf "%.4f\n" 17 | done 18 | -------------------------------------------------------------------------------- /fp/fibonacci-fp.ml: -------------------------------------------------------------------------------- 1 | open Lazy 2 | 3 | let modulo = 100000007 4 | 5 | type 'a inf_list = Cons of 'a * 'a inf_list lazy_t 6 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 7 | let (|>) x f = f x 8 | let (%) f g x = g (f x) 9 | let rec rep n f = 10 | if n = 0 then 11 | () 12 | else ( 13 | f(); 14 | rep (n-1) f 15 | ) 16 | 17 | let tl (Cons (_, t)) = force t 18 | 19 | let rec map2 f (Cons (h,t)) (Cons (h',t')) = 20 | Cons (f h h', lazy (map2 f (force t) (force t'))) 21 | 22 | let rec nth (Cons (h,t)) = function 23 | | 0 -> h 24 | | n when n > 0 -> nth (force t) (n-1) 25 | 26 | let iter xs = 27 | let a = Array.make 10001 0 in 28 | let rec go i (Cons (h,t)) = 29 | if i = 10001 then 30 | () 31 | else ( 32 | a.(i) <- h; 33 | go (i+1) (force t) 34 | ) 35 | in 36 | go 0 xs; 37 | a 38 | 39 | let rec fibs = Cons (0, lazy (Cons (1, lazy (map2 (fun x y -> (x+y) mod modulo) fibs (tl fibs))))) 40 | 41 | let () = 42 | let n = read_int 0 in 43 | let a = iter fibs in 44 | rep n (read_int % Array.get a % string_of_int % print_endline) 45 | -------------------------------------------------------------------------------- /fp/fp-array-of-n-elements.hs: -------------------------------------------------------------------------------- 1 | fn n = [1..n] 2 | -------------------------------------------------------------------------------- /fp/fp-filter-array.ml: -------------------------------------------------------------------------------- 1 | let (|>) x f = f x 2 | 3 | let read_lines _ = 4 | let rec go acc = 5 | try 6 | let line = read_line () in 7 | go (line::acc) 8 | with End_of_file -> 9 | List.rev acc 10 | in 11 | go [] 12 | 13 | let rec filter pred = function 14 | | [] -> [] 15 | | x::xs -> 16 | if pred x then 17 | x::filter pred xs 18 | else 19 | filter pred xs 20 | 21 | let () = 22 | let x::xs = read_lines 0 |> List.map int_of_string in 23 | filter ((>) x) xs |> List.map string_of_int |> String.concat "\n" |> print_endline 24 | -------------------------------------------------------------------------------- /fp/fp-filter-positions-in-a-list.ml: -------------------------------------------------------------------------------- 1 | let (|>) x f = f x 2 | 3 | let read_lines _ = 4 | let rec go acc = 5 | try 6 | let line = read_line () in 7 | go (line::acc) 8 | with End_of_file -> 9 | List.rev acc 10 | in 11 | go [] 12 | 13 | let f xs = 14 | let rec go xs ys = 15 | match xs with 16 | | [] | [_] -> List.rev ys 17 | | _::y::xs' -> go xs' (y::ys) 18 | in 19 | go xs [] 20 | 21 | let () = 22 | let xs = read_lines 0 in 23 | f xs |> String.concat "\n" |> print_endline 24 | -------------------------------------------------------------------------------- /fp/fp-hello-world-n-times.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let () = 3 | for i = 1 to read_int () do 4 | print_endline "Hello World" 5 | done 6 | -------------------------------------------------------------------------------- /fp/fp-list-length.ml: -------------------------------------------------------------------------------- 1 | let (|>) x f = f x 2 | 3 | let read_lines _ = 4 | let rec go acc = 5 | try 6 | let line = read_line () in 7 | go (line::acc) 8 | with End_of_file -> 9 | List.rev acc 10 | in 11 | go [] 12 | 13 | let length xs = 14 | let rec go xs n = 15 | match xs with 16 | | [] -> n 17 | | x::xs' -> go xs' (n+1) 18 | in 19 | go xs 0 20 | 21 | let () = 22 | let xs = read_lines 0 in 23 | length xs |> string_of_int |> print_endline 24 | -------------------------------------------------------------------------------- /fp/fp-list-replication.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | 4 | let () = 5 | let n = read_int 0 in 6 | let rec go2 t x acc = 7 | if t = 0 then 8 | acc 9 | else 10 | go2 (t-1) x (x::acc) in 11 | let rec go acc = 12 | try 13 | let x = read_int 0 in 14 | go (go2 n x acc) 15 | with End_of_file -> 16 | acc 17 | in 18 | go [] |> List.rev |> List.map string_of_int |> String.concat "\n" |> print_endline 19 | -------------------------------------------------------------------------------- /fp/fp-reverse-a-list.ml: -------------------------------------------------------------------------------- 1 | let (|>) x f = f x 2 | 3 | let read_lines _ = 4 | let rec go acc = 5 | try 6 | let line = read_line () in 7 | go (line::acc) 8 | with End_of_file -> 9 | List.rev acc 10 | in 11 | go [] 12 | 13 | let reverse xs = 14 | let rec go xs ys = 15 | match xs with 16 | | [] -> ys 17 | | x::xs' -> go xs' (x::ys) 18 | in 19 | go xs [] 20 | 21 | let () = 22 | let xs = read_lines 0 in 23 | reverse xs |> String.concat "\n" |> print_endline 24 | -------------------------------------------------------------------------------- /fp/fp-sum-of-odd-elements.ml: -------------------------------------------------------------------------------- 1 | let (|>) x f = f x 2 | 3 | let read_lines _ = 4 | let rec go acc = 5 | try 6 | let line = read_line () in 7 | go (line::acc) 8 | with End_of_file -> 9 | List.rev acc 10 | in 11 | go [] 12 | 13 | let f xs = 14 | let rec go xs sum = 15 | match xs with 16 | | [] -> sum 17 | | x::xs' -> go xs' (if x land 1 = 1 then sum+x else sum) 18 | in 19 | go xs 0 20 | 21 | let () = 22 | let xs = read_lines 0 in 23 | List.map int_of_string xs |> f |> string_of_int |> print_endline 24 | -------------------------------------------------------------------------------- /fp/fp-update-list.ml: -------------------------------------------------------------------------------- 1 | let f arr = List.map abs arr 2 | 3 | 4 | let rec read_lines () = 5 | try let line = read_line () in 6 | line :: read_lines() 7 | with 8 | End_of_file -> [] 9 | 10 | 11 | 12 | let () = 13 | let inp = read_lines () in 14 | let arr = List.map int_of_string inp in 15 | let result = f arr in 16 | let output = List.map string_of_int result in 17 | print_string (String.concat "\n" output) ;; 18 | -------------------------------------------------------------------------------- /fp/functional-programming-warmups-in-recursion---fibonacci-numbers.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | let (%) f g x = g (f x) 4 | let rec rep n f = 5 | if n = 0 then 6 | () 7 | else ( 8 | f(); 9 | rep (n-1) f 10 | ) 11 | 12 | let fib n = 13 | let rec go n a b = 14 | if n = 0 then 15 | a 16 | else 17 | go (n-1) b (a+b) 18 | in go n 1 0 19 | 20 | let () = 21 | read_int 0 |> fib |> string_of_int |> print_endline 22 | -------------------------------------------------------------------------------- /fp/functional-programming-warmups-in-recursion---gcd.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | let (%) f g x = g (f x) 4 | let rec rep n f = 5 | if n = 0 then 6 | () 7 | else ( 8 | f(); 9 | rep (n-1) f 10 | ) 11 | 12 | let rec gcd a b = 13 | if b = 0 then 14 | a 15 | else 16 | gcd b (a mod b) 17 | 18 | let () = 19 | gcd (read_int 0) (read_int 0) |> string_of_int |> print_endline 20 | -------------------------------------------------------------------------------- /fp/functions-and-fractals-sierpinski-triangles.ml: -------------------------------------------------------------------------------- 1 | let () = 2 | let n = read_int () in 3 | let a = Array.init 32 (fun _ -> String.make 63 '_') in 4 | for i = 0 to 31 do 5 | for j = 31-i to 31+i do 6 | a.(i).[j] <- '1' 7 | done 8 | done; 9 | let rec go n (ux,uy as u) (blx,bly as bl) (brx,bry as br) = 10 | if n > 0 then ( 11 | let bx, by as b = blx, (bly+bry)/2 in 12 | let lx, ly as l = (ux+blx+1)/2, (bly+by)/2 in 13 | let rx, ry as r = (ux+brx+1)/2, (bry+by+1)/2 in 14 | for i = lx to blx do 15 | for j = ly+i-lx+1 to ry-i+lx-1 do 16 | a.(i).[j] <- '_' 17 | done 18 | done; 19 | go (n-1) u (lx-1,ly+1) (rx-1,ry-1); 20 | go (n-1) l bl (bx,by-1); 21 | go (n-1) r (bx,by+1) br 22 | ) 23 | in 24 | go n (0,31) (31,0) (31,62); 25 | for i = 0 to 31 do 26 | print_endline a.(i) 27 | done 28 | -------------------------------------------------------------------------------- /fp/functions-or-not.hs: -------------------------------------------------------------------------------- 1 | ../contests/lambda-calculi-march-2016/functions-or-not.hs -------------------------------------------------------------------------------- /fp/kundu-and-bubble-wrap.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | 4 | let () = 5 | let n = read_int () * read_int () in 6 | let rec go n sum = 7 | if n = 0 then 8 | sum 9 | else 10 | go (n-1) (sum+.(1.0/.float_of_int n)) 11 | in 12 | float_of_int n *. go n 0.0 |> string_of_float |> print_endline 13 | -------------------------------------------------------------------------------- /fp/lambda-calculus-reductions-1.txt: -------------------------------------------------------------------------------- 1 | y 2 | -------------------------------------------------------------------------------- /fp/lambda-calculus-reductions-2.txt: -------------------------------------------------------------------------------- 1 | w 2 | -------------------------------------------------------------------------------- /fp/lambda-calculus-reductions-3.txt: -------------------------------------------------------------------------------- 1 | CAN'T REDUCE 2 | -------------------------------------------------------------------------------- /fp/lambda-calculus-reductions-4.txt: -------------------------------------------------------------------------------- 1 | CAN'T REDUCE 2 | -------------------------------------------------------------------------------- /fp/lambda-march-compute-the-perimeter-of-a-polygon.hs: -------------------------------------------------------------------------------- 1 | ../contests/lambda-calculi-march-2016/lambda-march-compute-the-perimeter-of-a-polygon.hs -------------------------------------------------------------------------------- /fp/lambda-march-concave-polygon.hs: -------------------------------------------------------------------------------- 1 | ../contests/lambda-calculi-march-2016/lambda-march-concave-polygon.hs -------------------------------------------------------------------------------- /fp/mango.hs: -------------------------------------------------------------------------------- 1 | import Data.Functor 2 | import Data.List 3 | import Data.Maybe 4 | import qualified Data.ByteString.Lazy.Char8 as L 5 | 6 | main = do 7 | (n:m:ts) <- (map (fst . fromJust . L.readInt) . L.words) <$> L.getContents 8 | let (a, b) = splitAt n ts 9 | f l h | l == h-1 = l 10 | | otherwise = 11 | if sum ys <= m 12 | then f x h 13 | else f l x 14 | where 15 | x = (l+h) `div` 2 16 | ys = take x . sort $ zipWith (\a b -> a+(x-1)*b) a b 17 | print $ f 0 (n+1) 18 | -------------------------------------------------------------------------------- /fp/minimum-multiple.ml: -------------------------------------------------------------------------------- 1 | ../contests/lambda-calculi-9/minimum-multiple.ml -------------------------------------------------------------------------------- /fp/missing-numbers-fp.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let flip f x y = f y x 3 | 4 | module IntMap = Map.Make(struct type t = int let compare = compare end) 5 | 6 | let add x m = 7 | let cnt = try IntMap.find x m with Not_found -> 0 in 8 | IntMap.add x (cnt+1) m 9 | 10 | let remove x m = 11 | let cnt = try IntMap.find x m with Not_found -> 0 in 12 | IntMap.add x (cnt-1) m 13 | 14 | let () = 15 | let n = read_int 0 in 16 | let a = Array.init n read_int in 17 | let bag = Array.fold_left (flip remove) IntMap.empty a in 18 | let n = read_int 0 in 19 | let a = Array.init n read_int in 20 | let bag = Array.fold_left (flip add) bag a in 21 | IntMap.iter (fun x c -> 22 | let rec go c = 23 | if c <= 0 then 24 | () 25 | else 26 | Printf.printf "%d " x 27 | in go c 28 | ) bag 29 | -------------------------------------------------------------------------------- /fp/number-of-binary-search-tree.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | 4 | let maxn = 1000 5 | let modulo = 100000007 6 | 7 | let () = 8 | let a = Array.make (maxn+1) 1 in 9 | for i = 1 to maxn do 10 | let rec go j r = 11 | if j >= i then 12 | r 13 | else 14 | go (j+1) ((r+a.(j)*a.(i-1-j)) mod modulo) 15 | in 16 | a.(i) <- go 0 0 17 | done; 18 | for i = 1 to read_int 0 do 19 | a.(read_int 0) |> string_of_int |> print_endline 20 | done 21 | -------------------------------------------------------------------------------- /fp/pascals-triangle.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | 4 | let rec map2 f xs ys = 5 | match xs,ys with 6 | | ([],_) -> [] 7 | | (_,[]) -> [] 8 | | (x::xs',y::ys') -> f x y :: map2 f xs' ys' 9 | 10 | let rec pascal xs = function 11 | | 0 -> () 12 | | n -> 13 | List.map string_of_int xs |> String.concat " " |> print_endline; 14 | pascal (1::(map2 (+) (List.tl xs) xs)@[1]) (n-1) 15 | 16 | let () = read_int 0 |> pascal [1] 17 | -------------------------------------------------------------------------------- /fp/password-cracker-fp.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let read_str _ = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x) 3 | 4 | let () = 5 | for cc = 1 to read_int 0 do 6 | let n = read_int 0 in 7 | let ps = Array.init n read_str in 8 | let a = read_str 0 in 9 | let m = String.length a in 10 | let prev = Array.make (m+1) (-1) in 11 | prev.(0) <- 0; 12 | for i = 1 to m do 13 | for j = 0 to n-1 do 14 | let l = String.length ps.(j) in 15 | if i >= l && prev.(i-l) >= 0 && String.sub a (i-l) l = ps.(j) then 16 | prev.(i) <- j 17 | done 18 | done; 19 | if prev.(m) = -1 then 20 | print_endline "WRONG PASSWORD" 21 | else ( 22 | let rec trace i = 23 | if i = 0 then 24 | () 25 | else ( 26 | let id = prev.(i) in 27 | trace (i-String.length ps.(id)); 28 | Printf.printf "%s " ps.(id) 29 | ) 30 | in 31 | trace m; 32 | print_char '\n' 33 | ) 34 | done 35 | -------------------------------------------------------------------------------- /fp/pentagonal-numbers.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | let (|>) x f = f x 3 | let (%) f g x = g (f x) 4 | let rec rep n f = 5 | if n = 0 then 6 | () 7 | else ( 8 | f(); 9 | rep (n-1) f 10 | ) 11 | 12 | let () = 13 | rep (read_int 0) (read_int % (fun n -> (3*n*n-n)/2) % string_of_int % print_endline) 14 | -------------------------------------------------------------------------------- /fp/prefix-compression.hs: -------------------------------------------------------------------------------- 1 | import Data.List 2 | import Text.Printf 3 | 4 | main = do 5 | a <- getLine 6 | b <- getLine 7 | let c = takeWhile (uncurry (==)) $ zip a b 8 | l = length c 9 | a' = drop l a 10 | b' = drop l b 11 | printf "%d %s\n" l $ map fst c 12 | printf "%d %s\n" (length a') a' 13 | printf "%d %s\n" (length b') b' 14 | -------------------------------------------------------------------------------- /fp/rotate-string.ml: -------------------------------------------------------------------------------- 1 | let () = 2 | for i = 1 to read_int () do 3 | let a = read_line () in 4 | let n = String.length a in 5 | let aa = a ^ a in 6 | let rec go i j = 7 | if j < n then ( 8 | print_char aa.[i]; 9 | go (i+1) (j+1) 10 | ) 11 | in 12 | for j = 0 to n-1 do 13 | go ((j+1) mod n) 0; 14 | print_char (if j = n-1 then '\n' else ' ') 15 | done 16 | done 17 | -------------------------------------------------------------------------------- /fp/sherlock-and-the-maze.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.Functor 3 | import Data.List 4 | 5 | main = do 6 | t <- readLn 7 | replicateM_ t $ do 8 | [n, m, k] <- (map read . words) <$> getLine 9 | let g n m = sum . map (last . last) . take (k+1) . iterate (map (init . scanl (+) 0) . transpose) $ (0:replicate (m-1) 1):replicate (n-1) (replicate m 0) 10 | print $ if n * m == 1 then 1 else (g n m + g m n) `mod` 1000000007 11 | -------------------------------------------------------------------------------- /fp/simplify-the-algebraic-expressions.hs: -------------------------------------------------------------------------------- 1 | ../contests/lambda-calculi-march-2016/simplify-the-algebraic-expressions.hs -------------------------------------------------------------------------------- /fp/string-compression.hs: -------------------------------------------------------------------------------- 1 | import Data.List 2 | main = interact $ concatMap (\x -> if null $ tail x then x else head x:show (length x)) . group 3 | -------------------------------------------------------------------------------- /fp/string-o-permute.ml: -------------------------------------------------------------------------------- 1 | let rec iter () = 2 | try 3 | let s = read_line () in 4 | let rec go i = 5 | if i+1 >= String.length s then 6 | () 7 | else ( 8 | let t = s.[i] in 9 | s.[i] <- s.[i+1]; 10 | s.[i+1] <- t; 11 | go (i+2) 12 | ) 13 | in 14 | go 0; 15 | print_endline s; 16 | iter () 17 | with End_of_file -> 18 | () 19 | 20 | let () = 21 | read_line (); 22 | iter () 23 | -------------------------------------------------------------------------------- /fp/super-queens-on-a-chessboard.ml: -------------------------------------------------------------------------------- 1 | let queen n = 2 | let rec go i l m r c0 c1 = 3 | if i = 0 then 4 | 1 5 | else 6 | let mask = (1 lsl n - 1) land lnot (l lor m lor r) in 7 | let rec f j acc = 8 | if j < 0 then 9 | acc 10 | else 11 | let jj = 1 lsl j in 12 | if jj land mask <> 0 && abs (j-c0) <> 2 && abs (j-c1) <> 1 then 13 | f (j-1) (acc + go (i-1) ((l lor jj) lsr 1) (m lor jj) ((r lor jj) lsl 1) j c0) 14 | else 15 | f (j-1) acc 16 | in f (n-1) 0 17 | in go n 0 0 0 (-3) (-3) 18 | 19 | let () = 20 | read_int () |> queen |> Printf.printf "%d\n" 21 | -------------------------------------------------------------------------------- /fp/swap-nodes.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let () = 4 | let n = read_int 0 in 5 | let l = Array.make (n+1) 0 in 6 | let r = Array.make (n+1) 0 in 7 | for i = 1 to n do l.(i) <- read_int 0; r.(i) <- read_int 0 done; 8 | let rec go n = 9 | if n > 0 then ( 10 | let k = read_int 0 in 11 | let rec f d x = 12 | if x > 0 then ( 13 | if d mod k = 0 then ( 14 | let t = l.(x) in 15 | l.(x) <- r.(x); 16 | r.(x) <- t 17 | ); 18 | f (d+1) l.(x); 19 | Printf.printf "%d " x; 20 | f (d+1) r.(x) 21 | ) 22 | in 23 | f 1 1; 24 | print_char '\n'; 25 | go (n-1) 26 | ) 27 | in 28 | go @@ read_int 0 29 | -------------------------------------------------------------------------------- /fp/valid-bst.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 2 | 3 | type t = Leaf | Node of int * t * t 4 | exception No 5 | 6 | let rec add x = function 7 | | Leaf -> Node (x,Leaf,Leaf) 8 | | Node (y,l,r) -> 9 | if x < y then 10 | match r with 11 | | Leaf -> Node (y,add x l,Leaf) 12 | | _ -> raise No 13 | else 14 | Node (y,l,add x r) 15 | 16 | let () = 17 | for i = 1 to read_int 0 do 18 | let n = read_int 0 in 19 | let rec go n t ans = 20 | if n = 0 then 21 | ans 22 | else 23 | let x = read_int 0 in 24 | if ans then 25 | try 26 | go (n-1) (add x t) ans 27 | with No -> 28 | go (n-1) t false 29 | else 30 | go (n-1) t ans 31 | in 32 | print_endline (if go n Leaf true then "YES" else "NO") 33 | done 34 | -------------------------------------------------------------------------------- /game-of-throne-ii.rb: -------------------------------------------------------------------------------- 1 | MOD = 1000000007 2 | 3 | def inv x 4 | n = MOD-2 5 | s = 1 6 | while n > 0 7 | s = s*x%MOD if n & 1 == 1 8 | x = x*x%MOD 9 | n >>= 1 10 | end 11 | s 12 | end 13 | 14 | a = gets.strip 15 | c = [0]*256 16 | a.each_byte {|x| c[x] += 1 } 17 | if 127.times.count {|i| c[i]%2 == 1 } > a.size%2 18 | p 0 19 | else 20 | fac = [1]*(a.size/2+1) 21 | 1.upto(a.size/2) {|i| fac[i] = fac[i-1]*i%MOD } 22 | p 127.times.inject(fac[a.size/2]) {|ans,i| ans*inv(fac[c[i]/2])%MOD } 23 | end 24 | -------------------------------------------------------------------------------- /game-of-thrones.hs: -------------------------------------------------------------------------------- 1 | import Data.List 2 | import qualified Data.Map as M 3 | 4 | main = do 5 | a <- getLine 6 | let m = M.toList $ foldl' (\m c -> M.insertWith (+) c 1 m) M.empty a :: [(Char, Int)] 7 | putStrLn $ if length (filter (odd . snd) m) <= 1 then "YES" else "NO" 8 | -------------------------------------------------------------------------------- /gem-stones.rb: -------------------------------------------------------------------------------- 1 | c = Hash.new 0 2 | gets.to_i.times {|i| 3 | gets.strip.each_byte {|x| c[x] += 1 if c[x] > 0 || i == 0 } 4 | c.each_key {|k| c[k] = [c[k]-1, 1].min } if i > 0 5 | } 6 | p c.each_value.count 1 7 | -------------------------------------------------------------------------------- /get-the-value-of-the-node-at-a-specific-position-from-the-tail.py: -------------------------------------------------------------------------------- 1 | def GetNode(x, position): 2 | y = x 3 | for i in range(position+1): 4 | y = y.next 5 | while y is not None: 6 | x, y = x.next, y.next 7 | return x.data 8 | -------------------------------------------------------------------------------- /hacker-country.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | #define REP(i, n) for (int i = 0; i < (n); i++) 8 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 500; 18 | int g[N][N], d[N+1][N]; 19 | 20 | int gcd(int a, int b) 21 | { 22 | int t; 23 | while (b) 24 | t = a%b, a = b, b = t; 25 | return a; 26 | } 27 | 28 | int main() 29 | { 30 | int n = ri(); 31 | REP(i, n) 32 | REP(j, n) 33 | g[i][j] = ri(); 34 | d[0][0] = 0; 35 | REP1(k, n) 36 | REP(i, n) { 37 | d[k][i] = INT_MAX; 38 | REP(j, n) 39 | if (j != i) 40 | d[k][i] = min(d[k][i], d[k-1][j]+g[j][i]); 41 | } 42 | int optp = 1, optq = 0; 43 | REP(i, n) { 44 | int pp = 0, qq = 1; 45 | REP(k, n) { // all d[k][i] are finite 46 | int p = d[n][i]-d[k][i], q = n-k, x = gcd(p, q); 47 | p /= x; 48 | q /= x; 49 | if (ll(p)*qq > ll(q)*pp) 50 | pp = p, qq = q; 51 | } 52 | if (ll(pp)*optq < ll(qq)*optp) 53 | optp = pp, optq = qq; 54 | } 55 | printf("%d/%d\n", optp, optq); 56 | } 57 | -------------------------------------------------------------------------------- /insert-a-node-at-a-specific-position-in-a-linked-list.py: -------------------------------------------------------------------------------- 1 | def InsertNth(head, data, position): 2 | if position == 0: 3 | return Node(data, head) 4 | x = head 5 | for _ in range(position-1): 6 | x = x.next 7 | x.next = Node(data, x.next) 8 | return head 9 | -------------------------------------------------------------------------------- /insert-a-node-at-the-tail-of-a-linked-list.py: -------------------------------------------------------------------------------- 1 | def insertNodeAtTail(head, data): 2 | if head is None: 3 | return SinglyLinkedListNode(data) 4 | x = head 5 | while x.next is not None: 6 | x = x.next 7 | x.next = SinglyLinkedListNode(data) 8 | return head 9 | -------------------------------------------------------------------------------- /is-fibo.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | x = gets.to_i 3 | a, b, c = 0, 1, 1 4 | while b < x 5 | if (a+c)*b <= x 6 | a, b, c = a*a+b*b, (a+c)*b, b*b+c*c 7 | else 8 | a, b, c = b, c, b+c 9 | end 10 | end 11 | puts (b == x || x == 0 ? 'IsFibo' : 'IsNotFibo') 12 | } 13 | -------------------------------------------------------------------------------- /jagia-playing-with-numbers.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 1000000; 15 | ll a[N+1]; 16 | 17 | // 1000000 11110100001001000000 18 | // >999936 11110100001000000000 -> 64 19 | // >999424 11110100000000000000 -> 448 20 | // >983040 11110000000000000000 -> 15808 21 | // otherwise -> 48576 22 | int add(int x, ll v) 23 | { 24 | int c = 0; 25 | for (; x <= N; x += x & -x) 26 | a[x] += v * ++c; 27 | return x - N; 28 | } 29 | 30 | ll sum(int x) 31 | { 32 | ll s = 0; 33 | for (; x; x &= x-1) 34 | s += a[x]; 35 | return s; 36 | } 37 | 38 | int main() 39 | { 40 | for (int q = ri(); q--; ) { 41 | char op; 42 | scanf(" %c", &op); 43 | int x = ri(), y = ri(); 44 | if (op == 'R') 45 | printf("%lld\n", sum(y)-sum(x-1)); 46 | else { 47 | int z = ri(); 48 | REP(i, 50) { 49 | int t = add(x, y); // t <= 48576 50 | add(t, y); // return 48576 51 | add(48576, 998ll*y); 52 | x += z; 53 | if (x > N) 54 | x -= N; 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /jim-and-the-skyscrapers.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | 3 | let ( +% ) = Int64.add 4 | let ( *% ) = Int64.mul 5 | 6 | let () = 7 | let n = read_int 0 in 8 | let a = Array.init n read_int in 9 | let rec go i s ans = 10 | if i = n then 11 | ans *% 2L 12 | else 13 | match s with 14 | | [] -> go (i+1) [a.(i), 1] ans 15 | | (x,c)::s' -> 16 | if a.(i) > x then 17 | go i s' ans 18 | else if a.(i) = x then 19 | go (i+1) ((x,c+1)::s') (ans +% Int64.of_int c) 20 | else 21 | go (i+1) ((a.(i),1)::s) ans 22 | in 23 | Printf.printf "%Ld\n" @@ go 0 [] 0L 24 | -------------------------------------------------------------------------------- /journey-scheduling.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | #define REP(i, n) for (int i = 0; i < (n); i++) 8 | #define pb push_back 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 100000; 18 | vector e[N]; 19 | int s[N], ss[N], far[N]; 20 | 21 | void dfs(int u, int p) 22 | { 23 | for (int v: e[u]) 24 | if (v != p) { 25 | dfs(v, u); 26 | if (s[v]+1 > ss[u]) { 27 | ss[u] = s[v]+1; 28 | if (ss[u] > s[u]) 29 | swap(ss[u], s[u]); 30 | } 31 | } 32 | } 33 | 34 | void dfs2(int u, int p, int up) 35 | { 36 | far[u] = max(s[u], up); 37 | for (int v: e[u]) 38 | if (v != p) 39 | dfs2(v, u, max(s[v]+1 == s[u] ? ss[u] : s[u], up) + 1); 40 | } 41 | 42 | int main() 43 | { 44 | int n = ri(), m = ri(); 45 | REP(i, n-1) { 46 | int u = ri()-1, v = ri()-1; 47 | e[u].pb(v); 48 | e[v].pb(u); 49 | } 50 | dfs(0, -1); 51 | dfs2(0, -1, 0); 52 | ll diameter = *max_element(far, far+n); 53 | while (m--) { 54 | int x = ri()-1, y = ri(); 55 | printf("%lld\n", (y-1)*diameter+far[x]); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /journey-to-the-moon.rb: -------------------------------------------------------------------------------- 1 | n, m = gets.split.map &:to_i 2 | djs = [-1]*n 3 | m.times { 4 | a, b = gets.split.map &:to_i 5 | while djs[a] >= 0 6 | djs[a] = djs[djs[a]] if djs[djs[a]] >= 0 7 | a = djs[a] 8 | end 9 | while djs[b] >= 0 10 | djs[b] = djs[djs[b]] if djs[djs[b]] >= 0 11 | b = djs[b] 12 | end 13 | if a != b 14 | a,b = b,a if djs[a] > djs[b] 15 | djs[a] += djs[b] 16 | djs[b] = a 17 | end 18 | } 19 | s = 0 20 | sum = -(djs.select {|x| x < 0}.inject &:+) 21 | djs.each {|x| s += (-x)*(sum+x) if x < 0 } 22 | p s>>1 23 | -------------------------------------------------------------------------------- /kruskalmstrsub.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define REP(i, n) for (int i = 0; i < (n); i++) 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 3000; 18 | bool vis[N]; 19 | int g[N][N], d[N]; 20 | 21 | int main() 22 | { 23 | int n = ri(), m = ri(); 24 | memset(g, 0x3f, sizeof g); 25 | while (m--) { 26 | int u = ri()-1, v = ri()-1, w = ri(); 27 | g[v][u] = g[u][v] = min(g[u][v], w); 28 | } 29 | fill_n(d, n, INT_MAX); 30 | d[0] = 0; 31 | int ans = 0; 32 | for(;;) { 33 | int u = -1; 34 | REP(i, n) 35 | if (! vis[i] && (u < 0 || d[i] < d[u])) 36 | u = i; 37 | if (u < 0) break; 38 | ans += d[u]; 39 | vis[u] = true; 40 | REP(i, n) 41 | d[i] = min(d[i], g[u][i]); 42 | } 43 | printf("%d\n", ans); 44 | } 45 | -------------------------------------------------------------------------------- /kundu-and-tree.py: -------------------------------------------------------------------------------- 1 | def find(x): 2 | while uf[x] >= 0: 3 | if uf[uf[x]] >= 0: 4 | uf[x] = uf[uf[x]] 5 | x = uf[x] 6 | return x 7 | 8 | n = int(input()) 9 | uf = [-1]*n 10 | for i in range(n-1): 11 | x, y, col = input().split() 12 | if col == 'b': 13 | x, y = find(int(x)-1), find(int(y)-1) 14 | if uf[x] > uf[y]: 15 | x, y = y, x 16 | uf[x] += uf[y] 17 | uf[y] = x 18 | a, b, c = 0, 0, 0 19 | for i in range(n): 20 | if uf[i] < 0: 21 | c -= b*uf[i] 22 | b -= a*uf[i] 23 | a -= uf[i] 24 | print(c % int(1e9+7)) 25 | -------------------------------------------------------------------------------- /manasa-and-stones.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | n = gets.to_i 3 | a = gets.to_i 4 | b = gets.to_i 5 | a,b = b,a if a > b 6 | puts n.times.map {|i| a*(n-1-i)+b*i }.uniq.join ' ' 7 | } 8 | -------------------------------------------------------------------------------- /manipulative-numbers.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | #define se second 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 100; 17 | int a[N]; 18 | 19 | int main() 20 | { 21 | int n = ri(); 22 | REP(i, n) 23 | a[i] = ri(); 24 | int l = 0, h = 30; 25 | while (l < h) { 26 | map c; 27 | int mi = l+h >> 1; 28 | REP(i, n) 29 | c[a[i] >> mi]++; 30 | bool major = false; 31 | for (auto x: c) 32 | if (x.se > n/2) { 33 | major = true; 34 | break; 35 | } 36 | if (! major) 37 | l = mi+1; 38 | else 39 | h = mi; 40 | } 41 | printf("%d\n", l-1); 42 | } 43 | -------------------------------------------------------------------------------- /matrix.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | typedef pair pii; 9 | typedef pair pll; 10 | #define REP(i, n) for (int i = 0; i < (n); i++) 11 | #define eb emplace_back 12 | #define fi first 13 | #define se second 14 | 15 | int ri() 16 | { 17 | int x; 18 | scanf("%d", &x); 19 | return x; 20 | } 21 | 22 | const int N = 100000; 23 | bool a[N]; 24 | vector es[N]; 25 | 26 | pll dfs(int u, int p, int w) 27 | { 28 | ll f = 0, g = 0; 29 | for (auto e: es[u]) 30 | if (e.fi != p) { 31 | pll r = dfs(e.fi, u, e.se); 32 | f += r.fi; 33 | g = min(g, r.se-r.fi); 34 | } 35 | if (a[u]) 36 | g = f, f += w; 37 | else { 38 | g += f; 39 | f = min(f, g+w); 40 | } 41 | return {f, g}; 42 | } 43 | 44 | int main() 45 | { 46 | int n = ri(), k = ri(); 47 | REP(i, n-1) { 48 | int u = ri(), v = ri(), w = ri(); 49 | es[u].eb(v, w); 50 | es[v].eb(u, w); 51 | } 52 | REP(i, k) 53 | a[ri()] = true; 54 | printf("%lld\n", dfs(0, -1, 0).se); 55 | } 56 | -------------------------------------------------------------------------------- /maximizing-xor.rb: -------------------------------------------------------------------------------- 1 | a = gets.to_i 2 | b = gets.to_i 3 | 31.downto(0) {|i| 4 | if (a^b)>>i & 1 == 1 5 | p (1< 2 | #include 3 | #include 4 | using namespace std; 5 | #include 6 | #include 7 | using namespace __gnu_pbds; 8 | 9 | typedef pair pii; 10 | #define fi first 11 | 12 | int ri() 13 | { 14 | int x; 15 | scanf("%d", &x); 16 | return x; 17 | } 18 | 19 | int main() 20 | { 21 | typedef tree, rb_tree_tag, tree_order_statistics_node_update> mytree; 22 | mytree t; 23 | int id = 0; 24 | for (int n = ri(); n--; ) { 25 | char op; 26 | int x; 27 | scanf(" %c%d", &op, &x); 28 | do { 29 | if (op == 'a') 30 | t.insert(pii{x, id++}); 31 | else { 32 | auto it = t.lower_bound(pii{x, 0}); 33 | if (it == t.end() || it->fi != x || (t.erase(it), t.empty())) { 34 | puts("Wrong!"); 35 | break; 36 | } 37 | } 38 | double ans = (double(t.find_by_order(t.size()/2)->fi) + t.find_by_order(t.size()-1>>1)->fi) * 0.5; 39 | if (fmod(ans, 1.0) == 0) 40 | printf("%d\n", int(ans)); 41 | else 42 | printf("%.1lf\n", ans); 43 | } while (0); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /merge-two-sorted-linked-lists.py: -------------------------------------------------------------------------------- 1 | // Merge two sorted linked lists 2 | def MergeLists(a, b): 3 | h, t = None, None 4 | while a is not None and b is not None: 5 | if a.data < b.data: 6 | if h is None: 7 | h = t = a 8 | else: 9 | t.next = a 10 | t = t.next 11 | a = a.next 12 | else: 13 | if h is None: 14 | h = t = b 15 | else: 16 | t.next = b 17 | t = t.next 18 | b = b.next 19 | if h is None: 20 | h = b if a is None else a 21 | else: 22 | t.next = b if a is None else a 23 | return h 24 | -------------------------------------------------------------------------------- /merging-communities.py: -------------------------------------------------------------------------------- 1 | def find(x): 2 | while uf[x] >= 0: 3 | if uf[uf[x]] >= 0: 4 | uf[x] = uf[uf[x]] 5 | x = uf[x] 6 | return x 7 | 8 | n, m = map(int, input().split()) 9 | uf = [-1]*n 10 | for i in range(m): 11 | ls = input().split() 12 | if ls[0] == 'Q': 13 | print(- uf[find(int(ls[1])-1)]) 14 | else: 15 | x, y = find(int(ls[1])-1), find(int(ls[2])-1) 16 | if x != y: 17 | if uf[x] > uf[y]: 18 | x, y = y, x 19 | uf[x] += uf[y] 20 | uf[y] = x 21 | -------------------------------------------------------------------------------- /misc/delete-duplicate-value-nodes-from-a-sorted-linked-list.cc: -------------------------------------------------------------------------------- 1 | Node* RemoveDuplicates(Node *head) 2 | { 3 | for (auto p = head; p; p = p->next) { 4 | auto q = p->next; 5 | while (q && q->data == p->data) { 6 | p->next = q->next; 7 | delete q; 8 | q = p->next; 9 | } 10 | } 11 | return head; 12 | } 13 | -------------------------------------------------------------------------------- /misc/find-hackerrank.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | s = gets.strip 3 | r = 0 4 | r |= 2 if s.start_with? 'hackerrank' 5 | r |= 1 if s.end_with? 'hackerrank' 6 | p r > 0 ? 3-r : -1 7 | } 8 | -------------------------------------------------------------------------------- /misc/hackerrank-tweets.rb: -------------------------------------------------------------------------------- 1 | p gets.to_i.times.count { gets =~ /hackerrank/i } 2 | -------------------------------------------------------------------------------- /misc/trignometric-ratios.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times{x=gets.to_f;s=c=0;f=1;5.times{|i|j=i*2;s+=f*x/(j+1);c+=f;f*=-x*x/(j+1)/(j+2)};p s.round 3;p c.round 3} 2 | -------------------------------------------------------------------------------- /misc/valid-pan-format.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | if gets.strip =~ /^[A-Z]{5}\d{4}[A-Z]$/ 3 | puts 'YES' 4 | else 5 | puts 'NO' 6 | end 7 | } 8 | -------------------------------------------------------------------------------- /missile-defend.cc: -------------------------------------------------------------------------------- 1 | // Dilworth's theorem 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef pair pii; 8 | #define REP(i, n) for (int i = 0; i < (n); i++) 9 | #define se second 10 | 11 | int ri() 12 | { 13 | int x; 14 | scanf("%d", &x); 15 | return x; 16 | } 17 | 18 | const int N = 100000; 19 | pii a[N]; 20 | int b[N]; 21 | 22 | int main() 23 | { 24 | int n = ri(); 25 | REP(i, n) { 26 | int t = ri(), f = ri(); 27 | a[i] = {t+f, t-f}; // antichain: f1+t1 > f2+t2 && t1-f1 < t2-f2 28 | } 29 | sort(a, a+n); 30 | reverse(a, a+n); 31 | int m = 0; 32 | REP(i, n) { 33 | int j = lower_bound(b, b+m, a[i].se) - b; 34 | b[j] = a[i].se; 35 | if (j == m) 36 | m++; 37 | } 38 | printf("%d\n", m); 39 | } 40 | -------------------------------------------------------------------------------- /p-sequences.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | #define REP1(i, n) for (int i = 1; i <= (n); i++) 8 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int P = 1000000000, SP = 31624 /*floor(sqrt(P))+2*/, MOD = 1000000007; 18 | int f[2][SP], g[2][SP], cnt[SP]; 19 | 20 | int main() 21 | { 22 | int n = ri(), p = ri(), sp = p; 23 | for (int sq; (sq = (sp+p/sp)/2) < sp; sp = sq); 24 | REP1(i, sp) 25 | f[0][i] = i; 26 | int l = sp; 27 | ROF(i, 1, sp+1) { 28 | int t = p/i; 29 | cnt[i] = t-l; 30 | g[0][i] = (t-l+g[0][i+1])%MOD; 31 | l = t; 32 | } 33 | REP(i, n-1) { 34 | int x = i&1, y = i+1&1; 35 | REP1(j, sp) 36 | f[y][j] = (ll(f[y][j-1])+f[x][sp]+g[x][j])%MOD; 37 | ROF(j, 1, sp+1) 38 | g[y][j] = (g[y][j+1]+ll(cnt[j])*f[x][j])%MOD; 39 | } 40 | printf("%d\n", (f[n-1&1][sp]+g[n-1&1][1])%MOD); 41 | } 42 | -------------------------------------------------------------------------------- /palindrome-border.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 7 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 100000; 17 | char a[N+1]; 18 | struct Node { int suff, l, c[26], cnt; } b[N+2]; 19 | 20 | int getSuff(int i, int x) 21 | { 22 | while (i-1-b[x].l < 0 || a[i-1-b[x].l] != a[i]) 23 | x = b[x].suff; 24 | return x; 25 | } 26 | 27 | int main() 28 | { 29 | b[0].suff = 1; b[0].l = 0; 30 | b[1].suff = 1; b[1].l = -1; 31 | scanf("%s", a); 32 | int x = 1, y = 2; 33 | for (int i = 0; a[i]; i++) { 34 | x = getSuff(i, x); 35 | if (! b[x].c[a[i]-'a']) { 36 | b[y].l = b[x].l+2; 37 | b[y].suff = b[getSuff(i, b[x].suff)].c[a[i]-'a']; 38 | b[y].cnt = 0; 39 | b[x].c[a[i]-'a'] = y++; 40 | } 41 | x = b[x].c[a[i]-'a']; 42 | b[x].cnt++; 43 | } 44 | ROF(i, 0, y) 45 | b[b[i].suff].cnt += b[i].cnt; 46 | ll ans = 0; 47 | FOR(i, 2, y) { 48 | int c = b[i].cnt; 49 | ans += ll(c-1)*c/2; 50 | } 51 | printf("%lld\n", ans%1000000007); 52 | } 53 | -------------------------------------------------------------------------------- /play-game.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | n = gets.to_i 3 | a = gets.split.map &:to_i 4 | a << 0 << 0 5 | s = [0]*(n+3) 6 | (n-1).downto(0) {|i| 7 | s[i] = [a[i]-s[i+1], a[i]+a[i+1]-s[i+2], a[i]+a[i+1]+a[i+2]-s[i+3]].max 8 | } 9 | p a.inject(&:+)+s[0] >> 1 10 | } 11 | -------------------------------------------------------------------------------- /plus-minus.clj: -------------------------------------------------------------------------------- 1 | (import java.util.Scanner) 2 | 3 | (def scan (Scanner. *in*)) 4 | 5 | (let [n (.nextInt scan) 6 | nn (float n) 7 | a (repeatedly n #(.nextInt scan))] 8 | (printf "%f\n%f\n%f\n" (/ (count (filter #(> % 0) a)) nn) (/ (count (filter #(< % 0) a)) nn) (/ (count (filter zero? a)) nn))) 9 | -------------------------------------------------------------------------------- /poisonous-plants.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define REP(i, n) for (int i = 0; i < (n); i++) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 100000; 15 | int a[N], b[N], st[N], st2[N], uf[N]; 16 | 17 | int find(int x) 18 | { 19 | while (uf[x] != x) 20 | uf[x] = uf[uf[x]], x = uf[x]; 21 | return x; 22 | } 23 | 24 | int main() 25 | { 26 | int n = ri(), m = 0, m2 = 0; 27 | REP(i, n) { 28 | uf[i] = i; 29 | a[i] = ri(); 30 | while (m && a[st[m-1]] >= a[i]) 31 | m--; 32 | if (m) 33 | b[i] = b[find(st[m-1]+1)] + 1; 34 | st[m++] = i; 35 | while (m2 && b[st2[m2-1]] <= b[i]) 36 | uf[st2[--m2]] = i; 37 | st2[m2++] = i; 38 | } 39 | printf("%d\n", *max_element(b, b+n)); 40 | } 41 | -------------------------------------------------------------------------------- /print-the-elements-of-a-linked-list-in-reverse.py: -------------------------------------------------------------------------------- 1 | def ReversePrint(head): 2 | x = None 3 | while head is not None: 4 | head.next, x, head = x, head, head.next 5 | while x is not None: 6 | print(x.data) 7 | x = x.next 8 | -------------------------------------------------------------------------------- /print-the-elements-of-a-linked-list.py: -------------------------------------------------------------------------------- 1 | def print_list(head): 2 | while head: 3 | print(head.data) 4 | head = head.next 5 | -------------------------------------------------------------------------------- /qheap1.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE LambdaCase #-} 2 | import Control.Monad 3 | import Control.Monad.State 4 | import Data.Char 5 | import Data.Functor 6 | import Data.Maybe 7 | import qualified Data.Set as S 8 | import qualified Data.ByteString.Char8 as B 9 | 10 | int = state $ fromJust . B.readInt . B.dropWhile isSpace 11 | 12 | data Op = Add !Int | Del !Int | Min 13 | 14 | parse = do 15 | q <- int 16 | replicateM q $ do 17 | t <- int 18 | case t of 19 | 1 -> Add <$> int 20 | 2 -> Del <$> int 21 | 3 -> return Min 22 | 23 | main = do 24 | qs <- evalState parse <$> B.getContents 25 | foldM (\s -> \case 26 | Add x -> return $ S.insert x s 27 | Del x -> return $ S.delete x s 28 | Min -> print (fst . fromJust $ S.minView s) $> s 29 | ) S.empty qs 30 | -------------------------------------------------------------------------------- /quicksort1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 5 | #define REP(i, n) FOR(i, 0, n) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 1000; 15 | int a[N]; 16 | 17 | int inplace_stable_partition(int a[], int n, int p) 18 | { 19 | if (n == 1) 20 | return a[0] < p; 21 | int m = n/2, i = inplace_stable_partition(a, m, p), j = inplace_stable_partition(a+m, n-m, p); 22 | rotate(a+i, a+m, a+m+j); 23 | return i+j; 24 | } 25 | 26 | int main() 27 | { 28 | int n = ri(); 29 | REP(i, n) a[i] = ri(); 30 | inplace_stable_partition(a, n, a[0]); 31 | REP(i, n) printf("%d%c", a[i], i == n-1 ? '\n' : ' '); 32 | } 33 | -------------------------------------------------------------------------------- /red-john-is-back.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int P = 1822474; 16 | int prime[P]; 17 | 18 | int main() 19 | { 20 | int a[40]; 21 | fill_n(a, 4, 1); 22 | FOR(i, 4, 41) 23 | a[i] = a[i-1]+a[i-4]; 24 | fill_n(prime, P, 1); 25 | prime[0] = prime[1] = 0; 26 | for (int i = 2; i*i <= P; i++) 27 | if (prime[i]) 28 | for (int j = i*i; j < P; j += i) 29 | prime[j] = 0; 30 | partial_sum(prime, prime+P, prime); 31 | for (int cc = ri(); cc--; ) 32 | printf("%d\n", prime[a[ri()]]); 33 | } 34 | -------------------------------------------------------------------------------- /repeat-k-sums.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | ll a[100000]; 17 | multiset s; 18 | 19 | void go(int i, int c, ll sum) 20 | { 21 | if (! c) 22 | s.erase(s.find(sum)); 23 | else 24 | for (; i >= 0; i--) 25 | go(i, c-1, sum+a[i]); 26 | } 27 | 28 | int main() 29 | { 30 | for (int cc = ri(); cc--; ) { 31 | int n = ri(), k = ri(); 32 | char c; 33 | ll x; 34 | s.clear(); 35 | for(;;) { 36 | int g = scanf("%lld%c", &x, &c); 37 | s.insert(x); 38 | if (c == '\n' || g != 2) break; 39 | } 40 | a[0] = *s.begin()/k; 41 | s.erase(s.begin()); 42 | FOR(i, 1, n) { 43 | a[i] = *s.begin()-(k-1)*a[0]; 44 | if (i < n-1) 45 | go(i, k-1, a[i]); 46 | } 47 | REP(i, n) 48 | printf("%lld%c", a[i], i == n-1 ? '\n' : ' '); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /reverse-a-linked-list.py: -------------------------------------------------------------------------------- 1 | def Reverse(head): 2 | x = None 3 | while head is not None: 4 | head.next, x, head = x, head, head.next 5 | return x 6 | -------------------------------------------------------------------------------- /reverse-shuffle-merge.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | const int N = 10000, M = N*2+1; 9 | char a[M], ans[N+1]; 10 | int c[26], cc[26]; 11 | 12 | int main() 13 | { 14 | gets(a); 15 | int n = strlen(a); 16 | reverse(a, a+n); 17 | REP(i, n) c[a[i]-'a']++; 18 | REP(i, 26) c[i] /= 2; 19 | REP(i, n/2) 20 | REP(x, 26) { 21 | ans[i] = 'a'+x; 22 | int p = 0, l = 0; 23 | REP(j, n) 24 | if (ans[p] == a[j]) { 25 | l = j; 26 | if (++p > i) break; 27 | } 28 | if (p <= i) continue; 29 | copy(c, c+26, cc); 30 | REP(j, i+1) 31 | if (--cc[ans[j]-'a'] < 0) 32 | goto L1; 33 | FOR(j, l+1, n) 34 | cc[a[j]-'a']--; 35 | REP(j, 26) 36 | if (cc[j] > 0) 37 | goto L1; 38 | break; 39 | L1:; 40 | } 41 | puts(ans); 42 | } 43 | -------------------------------------------------------------------------------- /sam-and-substrings.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int MOD = 1000000007; 16 | 17 | int main() 18 | { 19 | string s; 20 | cin >> s; 21 | int l = s.size(); 22 | ll t = 0, ans = 0; 23 | REP(i, l) { 24 | t = (t*10+(i+1)*(s[i]-'0'))%MOD; 25 | ans = (ans+t)%MOD; 26 | } 27 | cout << ans << endl; 28 | } 29 | -------------------------------------------------------------------------------- /shell/awk-1.sh: -------------------------------------------------------------------------------- 1 | awk 'NF!=4 {print "Not all scores are available for "$1}' 2 | -------------------------------------------------------------------------------- /shell/awk-2.sh: -------------------------------------------------------------------------------- 1 | awk '{print $1 " : " ($2>=50&&$3>=50&&$4>=50 ? "Pass" : "Fail")}' 2 | -------------------------------------------------------------------------------- /shell/awk-3.sh: -------------------------------------------------------------------------------- 1 | awk '{s=$2+$3+$4; print $0 " : " (s>=240?"A":s>=180?"B":s>=150?"C":"FAIL")}' 2 | -------------------------------------------------------------------------------- /shell/awk-4.sh: -------------------------------------------------------------------------------- 1 | awk '{ORS = NR%2 ? ";" : "\n"}1' 2 | -------------------------------------------------------------------------------- /shell/bash-tutorials---compute-the-average.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read n 3 | x=$({ echo -n 'scale=4;(';{ cat;echo;}|tr -s '\n'|tr '\n' '+';echo "0)/$n";} | bc) 4 | (($(echo $x | sed 's/.*\....\(.\)/\1/') >= 5)) && x=$(echo "scale=3;($x+0.001)/1" | bc) || x=$(echo "scale=3;$x/1" | bc) 5 | echo $x 6 | -------------------------------------------------------------------------------- /shell/bash-tutorials---looping-with-numbers.sh: -------------------------------------------------------------------------------- 1 | printf '%d\n' {1..50} 2 | -------------------------------------------------------------------------------- /shell/bash-tutorials-concatenate-an-array-with-itself.sh: -------------------------------------------------------------------------------- 1 | while IFS= read -r line; do 2 | a=("${a[@]}" "$line") 3 | done 4 | a=("${a[@]}" "${a[@]}" "${a[@]}") 5 | printf '%s ' "${a[@]}" 6 | -------------------------------------------------------------------------------- /shell/bash-tutorials-count-the-number-of-elements-in-an-array.sh: -------------------------------------------------------------------------------- 1 | while IFS= read -r line; do 2 | a=("${a[@]}" "$line") 3 | done 4 | echo ${#a[@]} 5 | -------------------------------------------------------------------------------- /shell/bash-tutorials-display-the-third-element-of-an-array.sh: -------------------------------------------------------------------------------- 1 | while IFS= read -r line; do 2 | a=("${a[@]}" "$line") 3 | done 4 | printf %s\\n "${a[3]}" 5 | -------------------------------------------------------------------------------- /shell/bash-tutorials-filter-an-array-with-patterns.sh: -------------------------------------------------------------------------------- 1 | while read -r line; do 2 | a=(${a[@]} $line) 3 | done 4 | printf %s\\n ${a[@]/*[aA]*/} 5 | -------------------------------------------------------------------------------- /shell/bash-tutorials-read-in-an-array.sh: -------------------------------------------------------------------------------- 1 | declare -a a 2 | i=0 3 | while IFS= read -r line || [[ -n "$line" ]]; do 4 | a[i]=$line 5 | ((i++)) 6 | done 7 | printf '%s ' "${a[@]}" 8 | -------------------------------------------------------------------------------- /shell/bash-tutorials-remove-the-first-capital-letter-from-each-array-element.sh: -------------------------------------------------------------------------------- 1 | while IFS= read -r line; do 2 | a=("${a[@]}" "$line") 3 | done 4 | printf '%s ' "${a[@]/[A-Z]/.}" 5 | -------------------------------------------------------------------------------- /shell/bash-tutorials-slice-an-array.sh: -------------------------------------------------------------------------------- 1 | while IFS= read -r line; do 2 | a=("${a[@]}" "$line") 3 | done 4 | printf '%s ' "${a[@]:3:5}" 5 | -------------------------------------------------------------------------------- /shell/fractal-trees-all.sh: -------------------------------------------------------------------------------- 1 | declare -A a 2 | 3 | f() { 4 | local d=$1 l=$2 r=$3 c=$4 5 | [[ $d -eq 0 ]] && return 6 | for ((i=l; i; i--)); do 7 | a[$((r-i)).$c]=1 8 | done 9 | ((r -= l)) 10 | for ((i=l; i; i--)); do 11 | a[$((r-i)).$((c-i))]=1 12 | a[$((r-i)).$((c+i))]=1 13 | done 14 | f $((d-1)) $((l/2)) $((r-l)) $((c-l)) 15 | f $((d-1)) $((l/2)) $((r-l)) $((c+l)) 16 | } 17 | read n 18 | f $n 16 63 49 19 | for ((i=0; i<63; i++)); do 20 | for ((j=0; j<100; j++)); do 21 | if [[ ${a[$i.$j]} ]]; then 22 | printf 1 23 | else 24 | printf _ 25 | fi 26 | done 27 | echo 28 | done 29 | -------------------------------------------------------------------------------- /shell/lonely-integer-2.sh: -------------------------------------------------------------------------------- 1 | read 2 | read a 3 | s=0 4 | for i in $a; do 5 | ((s = s ^ i)) 6 | done 7 | echo $s 8 | -------------------------------------------------------------------------------- /shell/paste-1.sh: -------------------------------------------------------------------------------- 1 | paste -s -d\; 2 | -------------------------------------------------------------------------------- /shell/paste-2.sh: -------------------------------------------------------------------------------- 1 | paste -d\; - - - 2 | -------------------------------------------------------------------------------- /shell/paste-3.sh: -------------------------------------------------------------------------------- 1 | paste -sd\\t 2 | -------------------------------------------------------------------------------- /shell/paste-4.sh: -------------------------------------------------------------------------------- 1 | paste - - - 2 | -------------------------------------------------------------------------------- /shell/sed-command-4.sh: -------------------------------------------------------------------------------- 1 | sed 's/.\{14\}/**** **** ****/g' 2 | -------------------------------------------------------------------------------- /shell/sed-command-5.sh: -------------------------------------------------------------------------------- 1 | sed -r 's/(.{4}) (.{4}) (.{4}) (.{4})/\4 \3 \2 \1/g' 2 | -------------------------------------------------------------------------------- /shell/text-processing-cut-1.sh: -------------------------------------------------------------------------------- 1 | cut -c3 2 | -------------------------------------------------------------------------------- /shell/text-processing-cut-2.sh: -------------------------------------------------------------------------------- 1 | cut -c2,7 2 | -------------------------------------------------------------------------------- /shell/text-processing-cut-3.sh: -------------------------------------------------------------------------------- 1 | cut -c2-7 2 | -------------------------------------------------------------------------------- /shell/text-processing-cut-4.sh: -------------------------------------------------------------------------------- 1 | cut -c-4 2 | -------------------------------------------------------------------------------- /shell/text-processing-in-linux-the-grep-command-5.sh: -------------------------------------------------------------------------------- 1 | grep -P '(\d) *\1' 2 | -------------------------------------------------------------------------------- /shell/text-processing-in-linux-the-sed-command-2.sh: -------------------------------------------------------------------------------- 1 | sed 's/thy/your/ig' 2 | -------------------------------------------------------------------------------- /sherlock-and-anagrams.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | for _ in range(int(input())): 4 | a = input() 5 | n = len(a) 6 | c = defaultdict(int) 7 | for i in range(n): 8 | for j in range(i, n): 9 | c[''.join(sorted(list(a[i:j+1])))] += 1 10 | ans = 0 11 | for i in c.values(): 12 | ans += i*(i-1)//2 13 | print(ans) 14 | -------------------------------------------------------------------------------- /sherlock-and-cost.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 5 | #define REP(i, n) FOR(i, 0, n) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | int main() 15 | { 16 | for (int cc = ri(); cc--; ) { 17 | int n = ri(), bb, f = 0, g = 0; 18 | REP(i, n) { 19 | int b = ri(); 20 | if (i) { 21 | int ff = max(f, g+bb-1), 22 | gg = max(f+b-1, g+b-bb); 23 | f = ff; 24 | g = gg; 25 | } 26 | bb = b; 27 | } 28 | printf("%d\n", max(f, g)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /sherlock-and-gcd.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | n = gets.to_i 3 | if gets.split.map(&:to_i).inject(0) {|x,y| x, y = y, x%y while y > 0; x } == 1 4 | puts 'YES' 5 | else 6 | puts 'NO' 7 | end 8 | } 9 | -------------------------------------------------------------------------------- /sherlock-and-queries.rb: -------------------------------------------------------------------------------- 1 | MOD = 10**9+7 2 | n, m = gets.split.map &:to_i 3 | a = gets.split.map &:to_i 4 | b = gets.split.map &:to_i 5 | c = gets.split.map &:to_i 6 | b.zip(c).group_by(&:first).each {|b,bcs| 7 | prod = bcs.inject(1) {|prod,c| prod*c[1]%MOD } 8 | i = b 9 | while i <= n 10 | a[i-1] = a[i-1]*prod%1000000007 11 | i += b 12 | end 13 | } 14 | puts a.join ' ' 15 | -------------------------------------------------------------------------------- /sherlock-and-valid-string.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int N = 100000; 6 | char a[N+1]; 7 | int c[26]; 8 | 9 | int main() 10 | { 11 | scanf("%s", a); 12 | for (int i = 0; a[i]; i++) 13 | c[a[i]-'a']++; 14 | sort(c, c+26); 15 | int i = upper_bound(c, c+26, 0) - c, t; 16 | puts(c[i] == c[26-1] || c[i]+1 == c[26-1] && c[26-2] < c[26-1] || 1 == c[i] && c[i] < c[i+1] && c[i+1] == c[26-1] ? "YES" : "NO"); 17 | } 18 | -------------------------------------------------------------------------------- /simple-array-sum.clj: -------------------------------------------------------------------------------- 1 | (import 'java.util.Scanner) 2 | 3 | (def scan (Scanner. *in*)) 4 | 5 | (let [n (.nextInt scan)] 6 | (println (loop [i n sum 0] 7 | (if (= i 0) 8 | sum 9 | (recur (dec i) (+ sum (.nextInt scan))))))) 10 | -------------------------------------------------------------------------------- /sparse-arrays.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | c = Counter() 3 | for i in range(int(input())): 4 | c[input()] += 1 5 | for i in range(int(input())): 6 | print(c[input()]) 7 | -------------------------------------------------------------------------------- /square-subsequences.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 8 | 9 | int ri() 10 | { 11 | int x; 12 | scanf("%d", &x); 13 | return x; 14 | } 15 | 16 | const int N = 200, MOD = 1000000007; 17 | char a[N+1]; 18 | int s[N][N]; 19 | 20 | int main() 21 | { 22 | for (int cc = ri(); cc--; ) { 23 | scanf("%s", a); 24 | int n = strlen(a), ans = 0; 25 | FOR(i, 1, n) { 26 | reverse(a, a+i); 27 | REP(j, i) 28 | ROF(k, i, n) { 29 | s[j][k] = (j ? s[j-1][k] : 0) + (k+1 < n ? s[j][k+1] : 0); 30 | if (a[j] == a[k]) 31 | ++s[j][k] %= MOD; 32 | else if (j && k+1 < n) 33 | (s[j][k] -= s[j-1][k+1]) %= MOD; 34 | } 35 | (ans += s[i-1][i] - (i+1 < n ? s[i-1][i+1] : 0)) %= MOD; 36 | reverse(a, a+i); 37 | } 38 | (ans += MOD) %= MOD; 39 | printf("%d\n", ans); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /staircase.clj: -------------------------------------------------------------------------------- 1 | (import java.util.Scanner) 2 | 3 | (def scan (Scanner. *in*)) 4 | 5 | (let [n (.nextInt scan) 6 | s (apply str (concat (repeat (dec n) \space) (repeat n \#))) 7 | fmt (str "%" n "c\n")] 8 | (dotimes [i n] (printf fmt \#))) 9 | -------------------------------------------------------------------------------- /string-similarity.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 7 | #define REP(i, n) FOR(i, 0, n) 8 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int N = 100000; 18 | char a[N+1]; 19 | int z[N]; 20 | 21 | void calc_z(int n) 22 | { 23 | z[0] = n; 24 | for (int f, g = 0, i = 1; i < n; i++) 25 | if (i < g && z[i-f] != g-i) 26 | z[i] = min(z[i-f], g-i); 27 | else { 28 | f = i; 29 | g = max(g, f); 30 | while (g < n && a[g] == a[g-f]) 31 | g++; 32 | z[f] = g-f; 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | for (int cc = ri(); cc--; ) { 39 | scanf("%s", a); 40 | int n = strlen(a); 41 | calc_z(n); 42 | printf("%lld\n", accumulate(z, z+n, 0ll)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /subsequence-weighting.ml: -------------------------------------------------------------------------------- 1 | let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x) 2 | let read_int64 _ = Scanf.bscanf Scanf.Scanning.stdib " %Ld" (fun x -> x) 3 | 4 | let ( +% ) = Int64.add 5 | 6 | let fenwick_add n a i v = 7 | let rec go i = 8 | if i < n then ( 9 | if v > a.(i) then 10 | a.(i) <- v; 11 | go @@ i lor (i+1) 12 | ) 13 | in 14 | go i 15 | 16 | let fenwick_get_max a i = 17 | let rec go i mx = 18 | if i > 0 then 19 | go (i land (i-1)) (max mx a.(i-1)) 20 | else 21 | mx 22 | in 23 | go i 0L 24 | 25 | let () = 26 | for cc = 1 to read_int 0 do 27 | let n = read_int 0 in 28 | let a = Array.init n (fun i -> read_int 0, - i) in 29 | Array.sort compare a; 30 | let b = Array.init n read_int64 in 31 | let s = Array.make n 0L in 32 | for i = 0 to n-1 do 33 | let j = - snd a.(i) in 34 | let acc = fenwick_get_max s j +% b.(j) in 35 | fenwick_add n s j acc 36 | done; 37 | Printf.printf "%Ld\n" @@ fenwick_get_max s n 38 | done 39 | -------------------------------------------------------------------------------- /substring-diff.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 6 | #define REP(i, n) FOR(i, 0, n) 7 | 8 | int ri() 9 | { 10 | int x; 11 | scanf("%d", &x); 12 | return x; 13 | } 14 | 15 | const int N = 1500; 16 | char a[N+1], b[N+1]; 17 | int q[N]; 18 | 19 | int main() 20 | { 21 | for (int cc = ri(); cc--; ) { 22 | int s = ri(), ans = 0; 23 | scanf("%s%s", a, b); 24 | int n = (int)strlen(a); 25 | FOR(i, 1-n, n) { 26 | int *fo = q, *re = q; 27 | FOR(j, max(-i,0), min(n-i,n)) { 28 | if (a[j] != b[j+i]) 29 | *re++ = j; 30 | while (re-fo > s+1) 31 | fo++; 32 | ans = max(ans, j - (re-fo <= s ? max(-i,0)-1 : *fo)); 33 | } 34 | } 35 | printf("%d\n", ans); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /the-love-letter-mystery.rb: -------------------------------------------------------------------------------- 1 | gets.to_i.times { 2 | a = gets.strip 3 | p (a.size/2).times.map {|i| (a[i].ord-a[a.size-1-i].ord).abs }.inject &:+ 4 | } 5 | -------------------------------------------------------------------------------- /tree-height-of-a-binary-tree.cc: -------------------------------------------------------------------------------- 1 | int height(node * root) 2 | { 3 | if (! root) 4 | return 0; 5 | return max(height(root->left), height(root->right)) + 1; 6 | } 7 | -------------------------------------------------------------------------------- /tree-inorder-traversal.cc: -------------------------------------------------------------------------------- 1 | void Inorder(node *root) { 2 | if (root) { 3 | Inorder(root->left); 4 | printf("%d ", root->data); 5 | Inorder(root->right); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tree-postorder-traversal.cc: -------------------------------------------------------------------------------- 1 | void Postorder(node *x) { 2 | stack s; 3 | for(;;) { 4 | while (x) { 5 | s.push(x); 6 | x = x->left; 7 | } 8 | while (! s.empty() && s.top()->right == x) { 9 | x = s.top(); 10 | s.pop(); 11 | printf("%d ", x->data); 12 | } 13 | if (s.empty()) break; 14 | x = s.top()->right; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree-preorder-traversal.cc: -------------------------------------------------------------------------------- 1 | void Preorder(node *x) { 2 | stack s; 3 | for(;;) { 4 | while (x) { 5 | printf("%d ", x->data); 6 | if (x->right) 7 | s.push(x->right); 8 | x = x->left; 9 | } 10 | if (s.empty()) break; 11 | x = s.top(); 12 | s.pop(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /triplets.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | #define REP(i, n) for (int i = 0; i < (n); i++) 7 | 8 | const int N = 100000; 9 | unsigned a[N], b[N]; 10 | ll c[N], d[N], e[N]; 11 | 12 | ll getSum(ll fenwick[], int x) 13 | { 14 | ll s = 0; 15 | for (; x; x &= x-1) 16 | s += fenwick[x-1]; 17 | return s; 18 | } 19 | 20 | void set(ll fenwick[], int x, ll y) 21 | { 22 | y -= getSum(fenwick, x+1) - getSum(fenwick, x); 23 | for (; x < N; x |= x+1) 24 | fenwick[x] += y; 25 | } 26 | 27 | int main() 28 | { 29 | int n; 30 | scanf("%d", &n); 31 | REP(i, n) 32 | scanf("%u", a+i); 33 | copy_n(a, n, b); 34 | sort(b, b+n); 35 | REP(i, n) { 36 | int x = lower_bound(b, b+n, a[i]) - b; 37 | set(c, x, 1); 38 | set(d, x, getSum(c, x)); 39 | set(e, x, getSum(d, x)); 40 | } 41 | printf("%lld\n", getSum(e, N)); 42 | } 43 | -------------------------------------------------------------------------------- /two-strings.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import qualified Data.Set as S 3 | 4 | main = do 5 | n <- readLn 6 | replicateM_ n $ do 7 | a <- getLine 8 | b <- getLine 9 | putStrLn $ if S.null $ S.intersection (S.fromList a) (S.fromList b) then "NO" else "YES" 10 | -------------------------------------------------------------------------------- /xor-key_range_tree.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define ALL(x) (x).begin(), (x).end() 7 | #define REP(i, n) for (int i = 0; i < (n); i++) 8 | #define pb push_back 9 | 10 | int ri() 11 | { 12 | int x; 13 | scanf("%d", &x); 14 | return x; 15 | } 16 | 17 | const int A = 1 << 15; 18 | vector bag[2*A]; 19 | 20 | int main() 21 | { 22 | for (int cc = ri(); cc--; ) { 23 | REP(i, 2*A) 24 | bag[i].clear(); 25 | int n = ri(), q = ri(); 26 | REP(i, n) 27 | for (int x = A+ri(); x; x >>= 1) 28 | bag[x].pb(i); 29 | REP(i, 2*A) 30 | sort(ALL(bag[i])); 31 | while (q--) { 32 | int x = ri(), l = ri()-1, r = ri(), i = 1, j = 14, ans = 0; 33 | for (; j >= 0; j--) { 34 | i *= 2; 35 | int c = (x>>j&1)^1; 36 | auto it = lower_bound(ALL(bag[i+c]), l); 37 | if (it != bag[i+c].end() && *it < r) 38 | i += c, ans += 1 << j; 39 | else 40 | i += c^1; 41 | } 42 | printf("%d\n", ans); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /xoring-ninja.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (int i = 0; i < (n); i++) 5 | #define ROF(i, a, b) for (int i = (b); --i >= (a); ) 6 | 7 | int ri() 8 | { 9 | int x; 10 | scanf("%d", &x); 11 | return x; 12 | } 13 | 14 | const int N = 100000, MOD = 1000000007; 15 | int a[N]; 16 | 17 | int main() 18 | { 19 | for (int cc = ri(); cc--; ) { 20 | int n = ri(), s = 0; 21 | REP(i, n) 22 | a[i] = ri(); 23 | ROF(k, 0, 30) { 24 | int z0 = 1, z1 = 0, t; 25 | REP(i, n) 26 | if (a[i] >> k & 1) 27 | t = (z0+z1)%MOD, z0 = z1 = t; 28 | else 29 | z0 = 2*z0%MOD, z1 = 2*z1%MOD; 30 | s = (s*2LL+z1)%MOD; 31 | } 32 | printf("%d\n", s); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /xoring-ninja_formula.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define REP(i, n) for (int i = 0; i < (n); i++) 5 | 6 | int ri() 7 | { 8 | int x; 9 | scanf("%d", &x); 10 | return x; 11 | } 12 | 13 | const int MOD = 1000000007; 14 | 15 | int main() 16 | { 17 | for (int cc = ri(); cc--; ) { 18 | int n = ri(), s = 0; 19 | REP(i, n) 20 | s |= ri(); 21 | REP(i, n-1) 22 | s = (2*s)%MOD; 23 | printf("%d\n", s); 24 | } 25 | } 26 | --------------------------------------------------------------------------------