├── .gitignore ├── brspoj ├── obi │ ├── AERO.cpp │ ├── BAFO.cpp │ ├── BIT.cpp │ ├── BURACOS.cpp │ ├── CAIXEIRB.cpp │ ├── CALCULA.cpp │ ├── COFRE.cpp │ ├── CRIPTO.cpp │ ├── DENGUE.cpp │ ├── DESCULPA.cpp │ ├── DOBRA.cpp │ ├── DOMINO.cpp │ ├── DUENDE.cpp │ ├── ELASTICO.cpp │ ├── ELEICOES.cpp │ ├── ENERGIA.cpp │ ├── ESTAGIO.cpp │ ├── FATORIA2.cpp │ ├── FLIPERAM.cpp │ ├── FROTATAX.cpp │ ├── FUROS.cpp │ ├── JUNINA.cpp │ ├── LUA.cpp │ ├── MACACO.cpp │ ├── MANUT.cpp │ ├── METEORO.cpp │ ├── MINHOCA.cpp │ ├── MINHOCA2.cpp │ ├── MIOJO.cpp │ ├── NUMERDOS.cpp │ ├── OBIDOMIN.cpp │ ├── OBIHANOI.cpp │ ├── OBIPOKER.cpp │ ├── OBITETRI.cpp │ ├── ORKUT.cpp │ ├── PAL.cpp │ ├── PAR.cpp │ ├── PARPROX.cpp │ ├── PARQUE.cpp │ ├── PASSEIO.cpp │ ├── PEDAGIO.cpp │ ├── PRIMO.cpp │ ├── QUADRAD2.cpp │ ├── QUERM.cpp │ ├── REDOTICA.cpp │ ├── SALDO.cpp │ ├── SENHA.cpp │ ├── SOMA.cpp │ ├── SORVETE.cpp │ ├── SUPERMER.cpp │ ├── TESOURO.cpp │ ├── TRILHAS.cpp │ ├── TV.cpp │ └── VIVO.cpp ├── regional │ ├── AMIGOS.cpp │ ├── APAGA.cpp │ ├── BABEL.cpp │ ├── BALDES.cpp │ ├── BICHO.cpp │ ├── BOLHAS.cpp │ ├── CAIXAS.cpp │ ├── CIRCUITO.cpp │ ├── CLUBE.cpp │ ├── COPA.cpp │ ├── CUBOS.cpp │ ├── CURSO.cpp │ ├── DAMA.cpp │ ├── ENERGIAT.cpp │ ├── ESPACO.cpp │ ├── HIST.cpp │ ├── LOOPMUSI.cpp │ ├── MARIO.cpp │ ├── MEGADAMA.cpp │ ├── NLOGONIA.cpp │ ├── OLIMP.cpp │ ├── PARADA.cpp │ ├── PISCINA.cpp │ ├── ROTA.cpp │ ├── ROUBA.cpp │ ├── TECLE.cpp │ ├── TUBOS.cpp │ ├── VAMPIROS.cpp │ ├── VARETAS.cpp │ ├── VOLEI.cpp │ └── ZAK.cpp ├── seletiva │ ├── .CALCULADORA.cpp.swp │ ├── BAPOSTAS.cpp │ ├── BRAIN.cpp │ ├── BUGADA.cpp │ ├── CARDAPIO.cpp │ ├── CFATORES.cpp │ ├── CONTAGEM.cpp │ ├── CORRIDA.cpp │ ├── CRUZVERM.cpp │ ├── DINOSTRA.cpp │ ├── DIOFANTO.cpp │ ├── DVORONOI.cpp │ ├── EINSTEIN.cpp │ ├── ENCOTEL.cpp │ ├── ESPIRAL.cpp │ ├── F91.cpp │ ├── FATORIAL.cpp │ ├── FLECHAS.cpp │ ├── GENERAL.cpp │ ├── GORE.cpp │ ├── JASPION.cpp │ ├── KOCH.cpp │ ├── KOOPMANS.cpp │ ├── LKING.cpp │ ├── MARQUES.cpp │ ├── MESA.cpp │ ├── NOME.cpp │ ├── ONZE.cpp │ ├── PAPRIMAS.cpp │ ├── PARIDADE.cpp │ ├── PLACAR.cpp │ ├── POPULAR.cpp │ ├── POSLIVRE.cpp │ ├── PREEMPOS.cpp │ ├── PSQF.cpp │ ├── RECUPERA.cpp │ ├── RONTGEN.cpp │ ├── RUMO9S.cpp │ ├── SANDUBA.cpp │ ├── SEQUENCI.cpp │ ├── SETUN.cpp │ ├── SUDOIME.cpp │ ├── TIROS.cpp │ └── WCW.cpp └── sulamericana │ ├── BORABORA.cpp │ ├── CEBOLA.cpp │ ├── CPCARROS.cpp │ ├── DOCE.cpp │ ├── EMOTIC.cpp │ ├── FEYNMAN.cpp │ ├── GALOUVOL.cpp │ ├── IMPEDIDO.cpp │ ├── LOTERIA.cpp │ ├── MAPABOLH.cpp │ ├── ODDOREVE.cpp │ ├── PLACICPC.cpp │ ├── POLEPOS.cpp │ ├── POWERGEN.cpp │ ├── QUASEMEN.cpp │ ├── RAPREGUI.cpp │ └── RELATORI.cpp ├── cf ├── 001A.cpp ├── 001B.cpp ├── 002A.cpp ├── 002B.cpp ├── 003A.cpp ├── 003C.cpp ├── 003D.cpp ├── 004A.cpp ├── 004B.cpp ├── 004C.cpp ├── 004D.cpp ├── 005A.cpp ├── 005A.py ├── 005B.py ├── 005C.py ├── 006A.py ├── 006B.cpp ├── 006C.cpp ├── 006E.cpp ├── 007A.cpp ├── 008A.py ├── 008B.py ├── 008C.cpp ├── 009A.cpp ├── 009C.cpp ├── 009C.py ├── 011A.cpp ├── 011B.cpp ├── 012A.cpp ├── 013A.py ├── 016A.cpp ├── 017A.cpp ├── 020C.cpp ├── 022A.cpp ├── 023A.cpp ├── 024A.cpp ├── 025A.py ├── 025B.cpp ├── 026A.cpp ├── 026B.cpp ├── 027A.cpp ├── 027E.cpp ├── 029A.cpp ├── 029B.cpp ├── 029C.cpp ├── 029D.cpp ├── 031A.cpp ├── 032A.cpp ├── 033A.cpp ├── 033B.cpp ├── 033C.cpp ├── 033D.cpp ├── 033E.cpp ├── 034A.py ├── 034B.py ├── 036C.cpp ├── 036D.cpp ├── 037A.py ├── 038A.py ├── 038B.cpp ├── 038C.cpp ├── 041A.cpp ├── 041A.py ├── 042B.cpp ├── 042C.cpp ├── 042D.cpp ├── 043A.py ├── 043B.cpp ├── 043C.cpp ├── 046A.py ├── 046B.py ├── 047A.py ├── 047B.cpp ├── 048A.cpp ├── 049A.py ├── 050A.py ├── 050B.cpp ├── 052A.cpp ├── 052B.cpp ├── 053A.cpp ├── 053C.cpp ├── 054A.cpp ├── 055A.cpp ├── 056A.cpp ├── 056B.cpp ├── 056C.cpp ├── 056D.cpp ├── 057A.cpp ├── 057B.cpp ├── 057C.cpp ├── 058A.cpp ├── 058B.cpp ├── 058C.cpp ├── 058C.py ├── 058D.cpp ├── 059A.cpp ├── 059B.cpp ├── 059C.cpp ├── 059D.cpp ├── 059E.cpp ├── 060A.cpp ├── 060B.cpp ├── 060C.cpp ├── 066A.py ├── 066B.cpp ├── 066C.cpp ├── 066D.cpp ├── 156A.cpp ├── 156B.cpp ├── 156C.cpp ├── 157A.cpp ├── 157B.cpp ├── 158A.cpp ├── 158B.cpp ├── 158C.cpp ├── 158D.cpp ├── 191A.cpp ├── 191B.cpp ├── 191B.pl ├── 191B.py └── plist ├── codesprint ├── clique.cpp ├── complement.cpp └── insertion.cpp ├── euler ├── base_exp.txt ├── cipher1.txt ├── e001.cpp ├── e001.pl ├── e002.cpp ├── e002.pl ├── e003.cpp ├── e003.pl ├── e004.cpp ├── e004.pl ├── e005.cpp ├── e005.pl ├── e006.cpp ├── e006.pl ├── e007.cpp ├── e007.pl ├── e008.cpp ├── e009.cpp ├── e010.cpp ├── e011.cpp ├── e012.cpp ├── e013.cpp ├── e014.cpp ├── e015.cpp ├── e016.cpp ├── e017.cpp ├── e018.cpp ├── e019.cpp ├── e020.cpp ├── e021.cpp ├── e022.cpp ├── e023.cpp ├── e024.cpp ├── e025.cpp ├── e026.cpp ├── e027.cpp ├── e028.cpp ├── e029.cpp ├── e030.cpp ├── e031.cpp ├── e032.cpp ├── e033.cpp ├── e034.cpp ├── e035.cpp ├── e036.cpp ├── e037.cpp ├── e038.cpp ├── e039.cpp ├── e040.cpp ├── e041.cpp ├── e042.cpp ├── e043.cpp ├── e044.cpp ├── e045.cpp ├── e046.cpp ├── e047.cpp ├── e048.cpp ├── e049.cpp ├── e050.cpp ├── e051.cpp ├── e052.cpp ├── e053.cpp ├── e054.cpp ├── e055.cpp ├── e056.py ├── e057.py ├── e058.py ├── e059.py ├── e060.cpp ├── e061.cpp ├── e062.py ├── e063.py ├── e064.cpp ├── e065.py ├── e066.cpp ├── e067.cpp ├── e068.cpp ├── e069.py ├── e070.cpp ├── e071.py ├── e072.cpp ├── e073.cpp ├── e074.cpp ├── e075.cpp ├── e076.cpp ├── e077.cpp ├── e078.cpp ├── e079.cpp ├── e080.py ├── e081.cpp ├── e082.cpp ├── e083.cpp ├── e084.cpp ├── e085.cs ├── e086.cpp ├── e087.cpp ├── e088.cpp ├── e089.cs ├── e090.cs ├── e091.cs ├── e092.cs ├── e093.cs ├── e093.py ├── e094.cpp ├── e095.cpp ├── e096.cpp ├── e097.cpp ├── e098.cpp ├── e099.cpp ├── e100.cs ├── e102.cpp ├── e103.cpp ├── e104.cpp ├── e104.py ├── e105.cpp ├── e106.cpp ├── e108.cpp ├── e109.cpp ├── e110.cpp ├── e111.cpp ├── e112.cpp ├── e113.cpp ├── e114.cpp ├── e115.cpp ├── e116.cpp ├── e117.cpp ├── e118.cpp ├── e119.cpp ├── e120.cpp ├── e121.cpp ├── e122.cpp ├── e123.cpp ├── e124.cpp ├── e125.cpp ├── e127.cpp ├── e128.cpp ├── e129.cpp ├── e130.cpp ├── e131.cpp ├── e134.cpp ├── e135.cpp ├── e138.cpp ├── e139.cpp ├── e142.cpp ├── e149.cpp ├── e151.cpp ├── e155.cpp ├── e162.cpp ├── e164.cpp ├── e173.cpp ├── e174.cpp ├── e188.cpp ├── e197.cpp ├── e205.cpp ├── e214.cpp ├── e231.cpp ├── e329.cpp ├── e336.cpp ├── e357.cpp ├── e371.cpp ├── keylog.txt ├── matrix.txt ├── pell.txt ├── plist ├── roman.txt ├── sets.txt ├── skel.cpp ├── sudoku.txt ├── sudoku_solutions.txt ├── triangles.txt └── words.txt ├── la ├── 2006.cpp ├── 2007.cpp ├── 2008.cpp ├── 2013.cpp ├── 2014.cpp ├── 2015.cpp ├── 2017.cpp ├── 2021.cpp ├── 2068.cpp ├── 2069.cpp ├── 2071.cpp ├── 2072.cpp ├── 2678.cpp ├── 2681.cpp ├── 2888.cpp ├── 2926.cpp ├── 3151.cpp ├── 3152.cpp ├── 3153.cpp ├── 3154.cpp ├── 3279.cpp ├── 3280.cpp ├── 3281.cpp ├── 3283.cpp ├── 3284.cpp ├── 3373.cpp ├── 3375.cpp ├── 3377.cpp ├── 3378.cpp ├── 3389.cpp ├── 3392.cpp ├── 3409.cpp ├── 3410.cpp ├── 3411.cpp ├── 3413.cpp ├── 3414.cpp ├── 3415.cpp ├── 3418.cpp ├── 3468.cpp ├── 3469.cpp ├── 3470.cpp ├── 3471.cpp ├── 3473.cpp ├── 3474.cpp ├── 3475.cpp ├── 3498.cpp ├── 3571.cpp ├── 3572.cpp ├── 3642.cpp ├── 3646.cpp ├── 3651.cpp ├── 3652.cpp ├── 3654.cpp ├── 3655.cpp ├── 3656.cpp ├── 3657.cpp ├── 3658.cpp ├── 3708.cpp ├── 3711.cpp ├── 3712.cpp ├── 3910.cpp ├── 3911.cpp ├── 3929.cpp ├── 3934.cpp ├── 3935.cpp ├── 3953.cpp ├── 3956.cpp ├── 3958.cpp ├── 3960.cpp ├── 3971.cpp ├── 3972.cpp ├── 3973.cpp ├── 3977.cpp ├── 3978.cpp ├── 3979.cpp ├── 4043.cpp ├── 4051.cpp ├── 4183.cpp ├── 4184.cpp ├── 4185.cpp ├── 4187.cpp ├── 4188.cpp ├── 4189.cpp ├── 4190.cpp ├── 4191.cpp ├── 4210.cpp ├── 4212.cpp ├── 4213.cpp ├── 4215.cpp ├── 4216.cpp ├── 4219.cpp ├── 4286.cpp ├── 4289.cpp ├── 4292.cpp ├── 4294.cpp ├── 4296.cpp ├── 4298.cpp ├── 4299.cpp ├── 4304.cpp ├── 4361.cpp ├── 4362.cpp ├── 4364.cpp ├── 4365.cpp ├── 4366.cpp ├── 4367.cpp ├── 4368.cpp ├── 4370.cpp ├── 4403.cpp ├── 4405.cpp ├── 4407.cpp ├── 4408.cpp ├── 4472.cpp ├── 4473.cpp ├── 4474.cpp ├── 4475.cpp ├── 4476.cpp ├── 4477.cpp ├── 4478.cpp ├── 4479.cpp ├── 4480.cpp ├── 4481.cpp ├── 4482.cpp ├── 4512.cpp ├── 4552.cpp ├── 4553.cpp ├── 4554.cpp ├── 4556.cpp ├── 4788.cpp ├── 4805.cpp ├── 4806.cpp ├── 4807.cpp ├── 4808.cpp ├── 4809.cpp ├── 4810.cpp ├── 4812.cpp ├── 4813.cpp ├── 4814.cpp ├── 4816.cpp ├── 4820.cpp ├── 4825.cpp ├── 4944.cpp ├── 4946.cpp ├── 4947.cpp ├── 4954.cpp └── plist ├── leetcode ├── 00001_two_sum.py ├── 00002_add_two_numbers.py ├── 00003_longest_substring_without_repeating_characters.py ├── 00003_longest_substring_without_repeating_characters.rs ├── 00004_median_of_two_sorted_arrays.py ├── 00005_longest_palindromic_substring.py ├── 00006_zigag_conversion.py ├── 00007_reverse_integer.py ├── 00008_string_to_integer_atoi.py ├── 00009_palindrome_number.py ├── 00010_regular_expression_matching.py ├── 00011_container_with_most_water.py ├── 00012_integer_to_roman.py ├── 00013_roman_to_integer.py ├── 00014_longest_common_prefix.py ├── 00015_3sum.py ├── 00016_3sum_closest.py ├── 00017_letter_combinations_of_a_phone_number.py ├── 00018_4sum.py ├── 00019_remove_nth_node_from_end_of_list.py ├── 00020_valid_parentheses.py ├── 00021_merge_two_sorted_lists.py ├── 00022_generate_parentheses.py ├── 00023_merge_k_sorted_lists.py ├── 00024_swap_nodes_in_pairs.py ├── 00025_reverse_nodes_in_k_group.py ├── 00026_remove_duplicates_from_sorted_array.py ├── 00027_remove_element.py ├── 00028_implement_strstr.py ├── 00029_divide_two_integers.py ├── 00030_substring_with_concatenation_of_all_words.py ├── 00031_next_permutation.py ├── 00032_longest_valid_parentheses.py ├── 00033_search_in_rotated_sorted_array.py ├── 00034_find_first_and_last_position_of_element_in_sorted_array.py ├── 00035_search_insert_position.py ├── 00036_valid_sudoku.py ├── 00038_count_and_say.py ├── 00039_combination_sum.py ├── 00040_combination_sum_ii.py ├── 00041_first_missing_positive.py ├── 00042_trapping_rain_water.py ├── 00042_trapping_rain_water.rs ├── 00043_multiply_strings.py ├── 00046_permutations.py ├── 00047_permutations_ii.py ├── 00048_rotate_image.py ├── 00049_group_anagrams.py ├── 00050_powx_n.py ├── 00053_maximum_subarray.py ├── 00054_spiral_matrix.py ├── 00055_jump_game.py ├── 00056_merge_intervals.py ├── 00058_length_of_last_word.py ├── 00058_length_of_last_word.rs ├── 00059_spiral_matrix_ii.py ├── 00060_permutation_sequence.py ├── 00061_rotate_list.py ├── 00062_unique_paths.py ├── 00063_unique_paths_ii.py ├── 00064_minimum_path_sum.py ├── 00066_plus_one.py ├── 00067_add_binary.py ├── 00069_sqrtx.py ├── 00070_climbing_stairs.py ├── 00071_simplify_path.py ├── 00073_set_matrix_zeroes.py ├── 00074_search_a_2d_matrix.py ├── 00075_sort_colors.py ├── 00077_combinations.py ├── 00078_subsets.py ├── 00079_word_search.py ├── 00079_word_search.rs ├── 00080_remove_duplicates_from_sorted_array_ii.py ├── 00081_search_in_rotated_sorted_array_ii.py ├── 00082_remove_duplicates_from_sorted_list_ii.py ├── 00083_remove_duplicates_from_sorted_list.py ├── 00085_maximal_rectangle.rs ├── 00086_partition_list.py ├── 00088_merge_sorted_array.py ├── 00089_gray_code.py ├── 00092_reverse_linked_list_ii.py ├── 00098_validate_binary_search_tree.py ├── 00099_recover_binary_search_tree.py ├── 00100_same_tree.py ├── 00101_symmetric_tree.py ├── 00104_maximum_depth_of_binary_tree.py ├── 00105_construct_binary_tree_from_preorder_and_inorder_traversal.py ├── 00106_construct_binary_tree_from_inorder_and_postorder_traversal.py ├── 00110_balanced_binary_tree.py ├── 00111_minimum_depth_of_binary_tree.py ├── 001128_number_of_equivalent_domino_pairs.py ├── 00112_path_sum.py ├── 00113_path_sum_ii.py ├── 00115_distinct_subsequences.py ├── 00117_populating_next_right_pointers_in_each_node_ii.py ├── 00118_pascals_triangle.py ├── 00122_best_time_to_buy_and_sell_stock_ii.py ├── 00124_binary_tree_maximum_path_sum.py ├── 00125_valid_palindrome.py ├── 00129_sum_root_to_leaf_numbers.py ├── 00129_sum_root_to_leaf_numbers.rs ├── 00136_single_number.py ├── 00146_lru_cache.py ├── 00150_evaluate_reverse_polish_notation.py ├── 00155_min_stack.py ├── 00160_intersection_of_two_linked_lists.py ├── 00164_maximum_gap.py ├── 00165_compare_version_numbers.rs ├── 00169_majority_element.py ├── 00172_factorial_trailing_zeroes.py ├── 00181_employees_earning_more_than_their_managers.py ├── 00189_rotate_array.py ├── 00191_number_of_1_bits.py ├── 00192_word_frequency.sh ├── 00197_rising_temperature.sql ├── 00200_number_of_islands.py ├── 00200_number_of_islands.rs ├── 00201_bitwise_and_of_numbers_range.py ├── 00202_happy_number.py ├── 00204_count_primes.py ├── 00205_isomorphic_strings.rs ├── 00205_isomorphic_strings.ts ├── 00206_reverse_linked_list.rs ├── 00208_implement_trie_prefix_tree.py ├── 00210_course_schedule_ii.py ├── 00217_contains_duplicate.py ├── 00221_maximal_square.py ├── 00222_count_complete_tree_nodes.py ├── 00225_implement_stack_using_queues.py ├── 00228_summary_ranges.py ├── 00229_majority_element_ii.py ├── 00231_power_of_two.rs ├── 00236_lowest_common_ancestor_of_a_binary_tree.py ├── 00238_product_of_array_except_self.py ├── 00239_sliding_window_maximum.py ├── 00240_search_a_2d_matrix_ii.py ├── 00242_valid_anagram.py ├── 00268_missing_number.rs ├── 00278_first_bad_version.py ├── 00279_perfect_squares.rs ├── 00283_move_zeroes.py ├── 00287_find_the_duplicate_number.rs ├── 00299_bulls_and_cows.py ├── 00301_remove_invalid_parentheses.py ├── 00303_range_sum_query_immutable.py ├── 00310_minimum_height_trees.rs ├── 00316_remove_duplicate_letters.py ├── 00318_maximum_product_of_word_lengths.py ├── 00328_odd_even_linked_list.py ├── 00331_verify_preorder_serialization_of_a_binary_tree.py ├── 00332_reconstruct_itinerary.py ├── 00337_house_robber_iii.py ├── 00342_power_of_four.py ├── 00344_reverse_string.py ├── 00347_top_k_frequent_elements.py ├── 00349_intersection_of_two_arrays.py ├── 00367_valid_perfect_square.py ├── 00371_sum_of_two_integers.py ├── 00377_combination_sum_iv.py ├── 00378_kth_smallest_element_in_a_sorted_matrix.py ├── 00383_ransom_note.py ├── 00384_shuffle_an_array.py ├── 00385_mini_parser.py ├── 00386_lexicographical_numbers.py ├── 00387_first_unique_character_in_a_string.py ├── 00388_longest_absolute_file_path.py ├── 00389_find_the_difference.py ├── 00401_binary_watch.py ├── 00402_remove_k_digits.py ├── 00402_remove_k_digits.rs ├── 00404_sum_of_left_leaves.rs ├── 00406_queue_reconstruction_by_height.py ├── 00414_third_maximum_number.py ├── 00415_add_strings.py ├── 00416_partition_equal_subset_sum.py ├── 00437_path_sum_iii.py ├── 00438_find_all_anagrams_in_a_string.py ├── 00441_arranging_coins.py ├── 00442_find_all_duplicates_in_an_array.py ├── 00443_string_compression.py ├── 00449_serialize_and_deserialize_bst.py ├── 00452_minimum_number_of_arrows_to_burst_balloons.rs ├── 00455_assign_cookies.py ├── 00457_circular_array_loop.rs ├── 00463_island_perimeter.rs ├── 00464_can_i_win.py ├── 00473_matchsticks_to_square.py ├── 00476_number_complement.py ├── 00478_generate_random_point_in_a_circle.py ├── 00479_largest_palindrome_product.py ├── 00480_sliding_window_median.py ├── 00482_license_key_formatting.py ├── 00485_max_consecutive_ones.py ├── 00486_predict_the_winner.py ├── 00491_increasing_subsequences.py ├── 00495_teemo_attacking.py ├── 00496_next_greater_element_i.py ├── 00497_random_point_in_non_overlapping_rectangles.py ├── 00500_keyboard_row.py ├── 00514_freedom_trail.rs ├── 00515_find_largest_value_in_each_tree_row.py ├── 00518_coin_change_2.py ├── 00519_random_flip_matrix.py ├── 00520_detect_capital.py ├── 00525_contiguous_array.py ├── 00526_beautiful_arrangement.py ├── 00528_random_pick_with_weight.py ├── 00529_minesweeper.py ├── 00530_minimum_absolute_difference_in_bst.py ├── 00537_complex_number_multiplication.py ├── 00538_convert_bst_to_greater_tree.py ├── 00540_single_element_in_a_sorted_array.py ├── 00542_01_matrix.py ├── 00543_diameter_of_binary_tree.py ├── 00546_remove_boxes.py ├── 00551_student_attendance_record_i.py ├── 00557_reverse_words_in_a_string_iii.py ├── 00560_subarray_sum_equals_k.py ├── 00565_array_nesting.py ├── 00567_permutation_in_string.py ├── 00576_out_of_boundary_paths.py ├── 00589_n_ary_tree_preorder_traversal.py ├── 00590_n_ary_tree_postorder_traversal.py ├── 00592_fraction_addiction_and_subtraction.py ├── 00593_valid_square.py ├── 00611_valid_triangle_number.py ├── 00622_design_circular_queue.py ├── 00623_add_one_row_to_tree.rs ├── 00627_swap_salary.sql ├── 00640_solve_the_equation.py ├── 00650_2_keys_keyboard.py ├── 00652_find_duplicate_subtrees.py ├── 00653_two_sum_iv_input_is_a_bst.py ├── 00657_robot_return_to_origin.py ├── 00658_find_k_closest_elements.py ├── 00659_split_array_into_consecutive_subsequences.py ├── 00662_maximum_width_of_binary_tree.py ├── 00668_kth_smallest_number_in_multiplication_table.py ├── 00670_maximum_swap.py ├── 00671_second_minimum_node_in_a_binary_tree.py ├── 00672_bulb_switcher_ii.py ├── 00674_longest_continuous_increasing_subsequence.py ├── 00677_map_sum_pairs.py ├── 00678_valid_parenthesis_string.py ├── 00678_valid_parenthesis_string.rs ├── 00680_valid_palindrome_ii.py ├── 00687_longest_univalue_path.py ├── 00698_partition_to_k_equal_sum_subsets.py ├── 00699_falling_squares.py ├── 00700_search_in_a_binary_search_tree.py ├── 00709_to_lower_case.py ├── 00722_remove_comments.py ├── 00733_flood_fill.py ├── 00752_open_the_lock.rs ├── 00757_set_intersection_size_at_least_two.py ├── 00766_toeplitz_matrix.py ├── 00769_max_chunks_to_make_sorted.py ├── 00771_jewels_and_stones.py ├── 00773_sliding_puzzle.py ├── 00777_swap_adjacent_in_lr_string.py ├── 00778_swim_in_rising_water.py ├── 00780_reaching_points.py ├── 00794_valid_tic_tac_toe_state.py ├── 00797_all_paths_from_source_to_target.py ├── 00804_unique_morse_code_words.py ├── 00809_expressive_words.py ├── 00817_linked_list_components.py ├── 00824_goat_latin.py ├── 00829_consecutive_number_sum.py ├── 00830_positions_of_large_groups.py ├── 00834_sum_of_distances_in_tree.py ├── 00837_new_21_game.py ├── 00840_magic_squares_in_grid.py ├── 00844_backspace_string_compare.py ├── 00848_shifting_letters.py ├── 00849_maximize_distance_to_closest_person.py ├── 00864_shortest_path_to_get_all_keys.py ├── 00867_transpose_matrix.py ├── 00870_advantage_shuffle.py ├── 00871_minimum_number_of_refueling_stops.py ├── 00872_leaf_similar_trees.py ├── 00876_middle_of_the_linked_list.py ├── 00878_nth_magical_number.py ├── 00880_decoded_string_at_index.py ├── 00881_boats_to_save_people.rs ├── 00883_projection_area_of_3d_shapes.py ├── 00885_spiral_matrix_iii.py ├── 00891_sum_of_subsequence_widths.py ├── 00894_all_possible_full_binary_trees.py ├── 00896_monotonic_array.py ├── 00897_increasing_order_search_tree.py ├── 00897_increasing_order_search_tree_2.py ├── 00901_online_stock_span.py ├── 00908_smallest_range_i.py ├── 00909_snakes_and_ladders.py ├── 00918_maximum_sum_circular_subarray.py ├── 00919_complete_binary_tree_inserter.py ├── 00922_sort_array_by_parity_ii.py ├── 00923_3sum_with_multiplicity.py ├── 00925_long_pressed_name.py ├── 00933_number_of_recent_calls.py ├── 00935_knight_dialer.py ├── 00937_reorder_data_in_log_files.py ├── 00938_range_sum_of_bst.py ├── 00941_valid_mountain_array.py ├── 00946_validate_stack_sequences.py ├── 00949_largest_time_for_given_digits.py ├── 00950_reveal_cards_in_increasing_order.rs ├── 00954_array_of_doubled_pairs.py ├── 00957_prison_cells_after_n_days.py ├── 00959_regions_cut_by_slashes.py ├── 00967_numbers_with_same_consecutive_differences.py ├── 00971_flip_binary_tree_to_match_preorder_traversal.py ├── 00977_squares_of_a_sorted_array.py ├── 00987_vertical_order_traversal_of_a_binary_tree.py ├── 00988_smallest_string_starting_from_leaf.rs ├── 00991_broken_calculator.py ├── 00993_cousins_in_binary_tree.py ├── 00994_rotting_oranges.py ├── 00995_minimum_number_of_k_consecutive_bit_flips.py ├── 00997_find_the_town_judge.py ├── 01004_max_consecutive_ones_iii.py ├── 01006_clumsy_factorial.py ├── 01008_construct_binary_search_tree_from_preorder_traversal.py ├── 01009_complement_of_base_10_integer.py ├── 01010_pairs_of_songs_with_total_duration_divisible_by_60.py ├── 01023_camelcase_matching.py ├── 01035_uncrossed_lines.py ├── 01038_binary_search_tree_to_greater_sum_tree.py ├── 01041_robot_bounded_in_circle.py ├── 01046_last_stone_weight.py ├── 01049_last_stone_weight_ii.py ├── 01051_height_checker.py ├── 01052_grumpy_bookstore_owner.py ├── 01079_letter_tile_possibilities.py ├── 01090_largest_values_from_labels.py ├── 01104_path_in_zigzag_labelled_binary_tree.py ├── 01108_defanging_an_ip_address.py ├── 01115_print_foobar_alternately.py ├── 01123_lowest_common_ancestor_of_deepest_leaves.py ├── 01129_shortest_path_with_alternating_colors.py ├── 01131_maximum_of_absolute_value_expression.py ├── 01137_nth_tribonacci_number.rs ├── 01143_longest_common_subsequence.py ├── 01146_snapshot_array.py ├── 01147_longest_chunked_palindrome_decomposition.py ├── 01154_day_of_the_year.py ├── 01161_maximum_level_sum_of_a_binary_tree.py ├── 01169_invalid_transactions.py ├── 01171_remove_zero_sum_consecutive_nodes_from_linked_list.py ├── 01200_minimum_absolute_difference.py ├── 01201_ugly_number_iii.py ├── 01207_unique_number_of_occurrences.py ├── 01218_longest_arithmetic_subsequence_of_given_difference.py ├── 01219_path_with_maximum_gold.py ├── 01220_count_vowels_permutation.py ├── 01221_split_a_string_in_balanced_strings.py ├── 01226_the_dining_philosophers.py ├── 01227_airplane_seat_assignment_probability.py ├── 01228_missing_number_in_arithmetic_progression.py ├── 01229_meeting_scheduler.py ├── 01230_toss_strange_coins.py ├── 01231_divide_chocolate.py ├── 01232_check_if_it_is_a_straight_line.py ├── 01233_remove_sub_folders_from_the_file_system.py ├── 01234_replace_the_substring_for_balanced_string.py ├── 01235_maximum_profit_in_job_scheduling.py ├── 01237_find_positive_integer_solution_for_a_given_equation.py ├── 01239_maximum_length_of_a_concatenated_string_with_unique_characters.py ├── 01247_minimum_swaps_to_make_strings_equal.py ├── 01248_count_number_of_nice_subarrays.py ├── 01249_minimum_remove_to_make_valid_parentheses.py ├── 01249_minimum_remove_to_make_valid_parentheses.rs ├── 01250_check_if_it_is_a_good_array.py ├── 01252_cells_with_odd_values_in_a_matrix.py ├── 01253_reconstruct_a_2_row_binary_matrix.py ├── 01254_number_of_closed_islands.py ├── 01255_maximum_score_words_formed_by_letters.py ├── 01260_shift_2d_grid.py ├── 01261_find_elements_in_a_contaminated_binary_tree.py ├── 01262_greatest_sum_divisible_by_three.py ├── 01263_minimum_moves_to_move_a_box_to_their_target_location.py ├── 01275_find_winner_on_a_tic_tac_toe_game.py ├── 01276_number_of_burgers_with_no_waste_of_ingredients.py ├── 01277_count_square_submatrices_with_all_ones.py ├── 01278_palindrome_partitioning_iii.py ├── 01289_minimum_falling_path_sum_ii.rs ├── 01295_find_numbers_with_even_number_of_digits.py ├── 01337_the_k_weakest_rows_in_a_matrix.py ├── 01348_tweet_counts_per_frequency.py ├── 01386_cinema_seat_allocation.py ├── 01389_create_target_array_in_the_given_order.py ├── 01404_number_of_steps_to_reduce_a_number_in_binary_representation_to_one.py ├── 01410_html_entity_parser.py ├── 01436_destination_city.py ├── 01437_check_if_all_1s_are_at_least_length_k_places_away.py ├── 01438_longest_continuous_subarray_with_absolute_diff_less_than_or_equal_to_limit.py ├── 01439_find_the_kth_smallest_sum_of_a_matrix_with_sorted_rows.py ├── 01450_number_of_students_doing_homework_at_a_given_time.py ├── 01451_rearrange_words_in_a_sentence.py ├── 01452_people_whose_list_of_favorite_companies_is_not_a_subset_of_another_list.py ├── 01453_maximum_number_of_darts_inside_of_a_circular_dartboard.py ├── 01460_make_two_arrays_equal_by_reversing_subarrays.py ├── 01514_path_with_maximum_probability.rs ├── 01535_find_the_winner_of_an_array_game.rs ├── 01540_can_convert_string_in_k_moves.py ├── 01544_make_the_string_great.rs ├── 01584_min_cost_to_connect_all_points.py ├── 01614_maximum_nesting_depth_of_the_parentheses.rs ├── 01700_number_of_students_unable_to_eat_lunch.rs ├── 01720_decode_xored_array.py ├── 01818_minimum_absolute_sum_difference.py ├── 01903_largest_odd_number_in_string.py ├── 01915_number_of_wonderful_strings.rs ├── 01971_find_if_path_exists_in_graph.rs ├── 01992_find_all_groups_of_farmland.rs ├── 02000_reverse_prefix_of_word.rs ├── 02073_time_needed_to_buy_tickets.rs ├── 02108_find_first_palindromic_string_in_the_array.rs ├── 02125_number_of_laser_beams_in_a_bank.rs ├── 02126_destroying_asteroids.rs ├── 02370_longest_ideal_subsequence.rs ├── 02441_largest_positive_integer_that_exists_with_its_negative.rs ├── 02514_count_anagrams.rs ├── 02551_put_marbles_in_bags.rs ├── 02864_maximum_odd_binary_number.rs ├── 02997_minimum_number_of_operations_to_make_array_xor_equal_to_k.rs ├── 03099_harshad_number.rs ├── 03100_water_bottles_ii.rs └── 03101_count_alternating_subarrays.rs ├── lib ├── bigint ├── bigint.cpp ├── char.cpp ├── convex_hull.cpp ├── fastio.cpp ├── ford_fulkerson.cpp ├── geometry.cpp ├── kmp.cpp ├── number_theory.cpp ├── suffix.cpp ├── tarjan.cpp └── template.cpp ├── regional ├── 2004 │ ├── boxes.cpp │ ├── chemistry.cpp │ ├── crossword.cpp │ ├── drop.cpp │ ├── goeasy.cpp │ ├── hurry.cpp │ └── patrol.cpp ├── 2005 │ ├── bicho.cpp │ ├── cubos.cpp │ ├── curso.cpp │ ├── espaco.cpp │ ├── pena.cpp │ ├── piscina.cpp │ ├── regata.cpp │ ├── tecle.cpp │ └── tecle.mrn.cpp ├── 2006 │ ├── amigos.cpp │ ├── circuito.cpp │ ├── copa.cpp │ ├── damas.cpp │ ├── energia.cpp │ ├── paises.cpp │ ├── parada.cpp │ └── rota.cpp ├── 2007 │ ├── bolhas.cpp │ ├── caixas.cpp │ ├── hist.cpp │ ├── mario.cpp │ ├── olimp.cpp │ ├── rouba.cpp │ ├── tubos.cpp │ ├── varetas.cpp │ ├── volei.cpp │ └── zak.cpp └── 2008 │ ├── apagando.cpp │ ├── babel.cpp │ ├── bolhas.cpp │ ├── clube.cpp │ ├── dama.cpp │ ├── loop.cpp │ ├── nlog.cpp │ ├── seq.cpp │ └── vampiros.cpp ├── seletiva ├── IME-USP2008-1 │ ├── cruzverm.cpp │ ├── diofanto.cpp │ ├── einstein.cpp │ ├── gore.cpp │ ├── jaspion.cpp │ ├── king.cpp │ ├── koch.cpp │ ├── koopmans.cpp │ ├── marques.cpp │ ├── rontgen.cpp │ └── wcw.cpp ├── IME-USP2008-2 │ ├── calculadora.cpp │ ├── corrida.cpp │ ├── flechas.cpp │ ├── general.cpp │ ├── nomes.cpp │ ├── sanduba.cpp │ ├── setun.cpp │ ├── tiros.cpp │ └── voronoi.cpp └── IME-USP2009 │ ├── acampamento.cpp │ ├── asfalto.cpp │ ├── cinema.cpp │ ├── dias.cpp │ ├── estiagem.cpp │ ├── lasers.cpp │ └── montagem.cpp ├── spoj ├── classical │ ├── ABSYS.cpp │ ├── ACODE.cpp │ ├── ACPC10A.cpp │ ├── ADDREV.cpp │ ├── AE00.cpp │ ├── AE1B.cpp │ ├── AGGRCOW.cpp │ ├── ANARC08B.cpp │ ├── ANARC08D.cpp │ ├── ANARC08E.cpp │ ├── ANARC08F.cpp │ ├── ANARC08G.cpp │ ├── ANARC08H.cpp │ ├── ANARC09A.cpp │ ├── ARMY.cpp │ ├── BABTWR.cpp │ ├── BEADS.cpp │ ├── BINSTIRL.cpp │ ├── BISHOPS.cpp │ ├── BISHOPS.py │ ├── BITMAP.cpp │ ├── CANDY.cpp │ ├── CANDY3.cpp │ ├── CANTON.cpp │ ├── CERC07B.cpp │ ├── CERC07R.cpp │ ├── CHOCOLA.cpp │ ├── CMPLS.cpp │ ├── COINS.cpp │ ├── CPC.cpp │ ├── CPCRC1C.cpp │ ├── CPRMT.cpp │ ├── CYLINDER.cpp │ ├── DISUBSTR.cpp │ ├── DIV15.cpp │ ├── DIVSUM.cpp │ ├── DSUBSEQ.cpp │ ├── EASYPROB.cpp │ ├── EIGHTS.cpp │ ├── ELDORADO.cpp │ ├── EMP.cpp │ ├── FAMILYP.cpp │ ├── FASHION.cpp │ ├── FCTRL.cpp │ ├── FCTRL2.cpp │ ├── FENCE1.cpp │ ├── GNY07A.cpp │ ├── GNY07B.cpp │ ├── HANGOVER.cpp │ ├── HELLOKIT.cpp │ ├── HISTOGRA.cpp │ ├── HPYNOSII.cpp │ ├── HUBULLU.cpp │ ├── INCDSEQ.cpp │ ├── INCSEQ.cpp │ ├── JAVAC.cpp │ ├── JULKA.cpp │ ├── KGSS.cpp │ ├── LABYR1.cpp │ ├── LAGRANGE.cpp │ ├── LASTDIG.cpp │ ├── LITE.cpp │ ├── MARBLES.cpp │ ├── MAYA.cpp │ ├── MBIPALIN.cpp │ ├── MDIGITS.cpp │ ├── MFLAR10.cpp │ ├── MIRRORED.cpp │ ├── MIXTURES.cpp │ ├── MRECAMAN.cpp │ ├── MSE08H.cpp │ ├── MSTRMND.cpp │ ├── MTREE.cpp │ ├── MULTQ3.cpp │ ├── NEG2.cpp │ ├── NGM.cpp │ ├── NSTEPS.cpp │ ├── NSYSTEM.cpp │ ├── OFFSIDE.cpp │ ├── ONP.cpp │ ├── OPTSUB.cpp │ ├── PALIN.cpp │ ├── PANCAKES.cpp │ ├── PARTY.cpp │ ├── PERMUT1.cpp │ ├── PERMUT2.cpp │ ├── PIGBANK.cpp │ ├── PIR.cpp │ ├── POLEVAL.cpp │ ├── PRIME1.cpp │ ├── PT07Y.cpp │ ├── PT07Z.cpp │ ├── QKP.cpp │ ├── QUALITY.cpp │ ├── RESN04.cpp │ ├── ROADNET.cpp │ ├── SAMER08A.cpp │ ├── SAMER08C.cpp │ ├── SAMER08D.cpp │ ├── SAMER08E.cpp │ ├── SAMER08F.cpp │ ├── SAMER08G.cpp │ ├── SAMER08J.cpp │ ├── SBANK.cpp │ ├── SHOP.cpp │ ├── SHPATH.cpp │ ├── SQRBR.cpp │ ├── STAMPS.cpp │ ├── STPAR.cpp │ ├── SUBST1.cpp │ ├── SUMITR.cpp │ ├── TEST.cpp │ ├── TOANDFRO.cpp │ ├── TRICOUNT.cpp │ ├── TRT.cpp │ ├── TTREAT.cpp │ ├── TWOSQRS.cpp │ ├── UJ.py │ ├── WORDCNT.cpp │ ├── YAPP.cpp │ └── YODANESS.cpp └── plist ├── sulamericana ├── 2003 │ ├── child.cpp │ ├── club.cpp │ └── dice.cpp ├── 2004 │ ├── longnight.mrn.cpp │ ├── magic.cpp │ ├── petanque.cpp │ └── tax.cpp ├── 2005 │ ├── dj.cpp │ ├── fiber.cpp │ ├── genes.cpp │ ├── icpc.cpp │ ├── mission.cpp │ ├── pascal.cpp │ ├── pnets.cpp │ └── tornado.cpp ├── 2006 │ ├── frog.cpp │ ├── lottery.cpp │ ├── maps.cpp │ ├── odd.cpp │ ├── onion.cpp │ ├── power.cpp │ └── report.cpp └── status ├── tc ├── SRM325D1L1.cpp ├── SRM340D1L1.cpp ├── SRM352D1L1.cpp ├── SRM363D1L1.cpp ├── SRM366D1L1.cpp ├── SRM402D1L1.cpp ├── SRM411D1L1.cpp ├── SRM412D1L1.cpp ├── SRM422D1L1.cpp ├── SRM460D1L1.cpp ├── SRM486D1L2.cpp ├── SRM494D1L1.cpp ├── SRM495D2L1.cpp ├── SRM495D2L2.cpp ├── SRM495D2L3.cpp ├── SRM497D2L1.cpp ├── SRM497D2L2.cpp ├── SRM500D2L2.cpp └── plist ├── timus ├── T1000.cpp ├── T1001.cpp ├── T1005.cpp ├── T1009.cpp ├── T1012.cpp ├── T1013.cpp ├── T1014.cpp ├── T1017.cpp ├── T1018.cpp ├── T1025.cpp ├── T1029.cpp ├── T1031.cpp ├── T1036.cpp ├── T1039.cpp ├── T1049.cpp ├── T1068.cpp ├── T1073.cpp ├── T1078.cpp ├── T1081.cpp ├── T1083.cpp ├── T1086.cpp ├── T1091.cpp ├── T1104.cpp ├── T1118.cpp ├── T1146.cpp ├── T1224.cpp ├── T1225.cpp ├── T1423.cpp └── T1820.cpp └── uva ├── Volume C ├── 10000.cpp ├── 10003.cpp ├── 10004.cpp ├── 10005.cpp ├── 10006.cpp ├── 10007.cpp ├── 10008.cpp ├── 10009.cpp ├── 10010.cpp ├── 10012.cpp ├── 10013.cpp ├── 10015.cpp ├── 10018.cpp ├── 10019.cpp ├── 10020.cpp ├── 10025.cpp ├── 10026.cpp ├── 10032.cpp ├── 10033.cpp ├── 10034.cpp ├── 10035.cpp ├── 10036.cpp ├── 10037.cpp ├── 10038.cpp ├── 10041.cpp ├── 10042.cpp ├── 10044.cpp ├── 10048.cpp ├── 10049.cpp ├── 10050.cpp ├── 10051.cpp ├── 10055.cpp ├── 10056.cpp ├── 10060.cpp ├── 10062.cpp ├── 10065.cpp ├── 10066.cpp ├── 10067.cpp ├── 10069.cpp ├── 10070.cpp ├── 10071.cpp ├── 10074.cpp ├── 10077.cpp ├── 10078.cpp ├── 10079.cpp ├── 10080.cpp ├── 10081.cpp ├── 10082.cpp ├── 10093.cpp ├── 10098.cpp └── 10099.cpp ├── Volume CI ├── 10101.cpp ├── 10104.cpp ├── 10105.cpp ├── 10106.cpp ├── 10107.cpp ├── 10110.cpp ├── 10114.cpp ├── 10127.cpp ├── 10130.cpp ├── 10131.cpp ├── 10136.cpp ├── 10137.cpp ├── 10140.cpp ├── 10142.cpp ├── 10161.cpp ├── 10167.cpp ├── 10170.cpp ├── 10172.cpp ├── 10176.cpp ├── 10179.cpp ├── 10180.cpp ├── 10182.cpp ├── 10183.cpp ├── 10187.cpp ├── 10189.cpp ├── 10191.cpp ├── 10192.cpp ├── 10193.cpp ├── 10195.cpp ├── 10196.cpp └── 10198.cpp ├── Volume CII ├── 10200.cpp ├── 10201.cpp ├── 10202.cpp ├── 10205.cpp ├── 10209.cpp ├── 10219.cpp ├── 10220.cpp ├── 10221.cpp ├── 10222.cpp ├── 10223.cpp ├── 10226.cpp ├── 10227.cpp ├── 10229.cpp ├── 10233.cpp ├── 10235.cpp ├── 10242.cpp ├── 10245.cpp ├── 10249.cpp ├── 10250.cpp ├── 10252.cpp ├── 10258.cpp ├── 10260.cpp ├── 10263.cpp ├── 10267.cpp ├── 10268.cpp ├── 10276.cpp ├── 10279.cpp ├── 10281.cpp ├── 10282.cpp ├── 10284.cpp ├── 10285.cpp ├── 10286.cpp ├── 10290.cpp ├── 10295.cpp ├── 10298.cpp └── 10299.cpp ├── Volume CIII ├── 10300.cpp ├── 10301.cpp ├── 10302.cpp ├── 10303.cpp ├── 10304.cpp ├── 10305.cpp ├── 10306.cpp ├── 10309.cpp ├── 10310.cpp ├── 10313.cpp ├── 10315.cpp ├── 10323.cpp ├── 10324.cpp ├── 10327.cpp ├── 10330.cpp ├── 10334.cpp ├── 10336.cpp ├── 10337.cpp ├── 10338.cpp ├── 10340.cpp ├── 10341.cpp ├── 10344.cpp ├── 10346.cpp ├── 10360.cpp ├── 10361.cpp ├── 10363.cpp ├── 10364.cpp ├── 10369.cpp ├── 10370.cpp ├── 10371.cpp ├── 10377.cpp ├── 10382.cpp ├── 10387.cpp └── 10394.cpp ├── Volume CIV ├── 10400.cpp ├── 10404.cpp ├── 10405.cpp ├── 10407.cpp ├── 10408.cpp ├── 10409.cpp ├── 10420.cpp ├── 10424.cpp ├── 10427.cpp ├── 10432.cpp ├── 10440.cpp ├── 10450.cpp ├── 10451.cpp ├── 10452.cpp ├── 10465.cpp ├── 10469.cpp ├── 10473.cpp ├── 10474.cpp ├── 10491.cpp ├── 10496.cpp ├── 10497.cpp └── 10499.cpp ├── Volume CIX ├── 10901.cpp ├── 10903.cpp ├── 10905.cpp ├── 10916.cpp ├── 10921.cpp ├── 10922.cpp ├── 10924.cpp ├── 10926.cpp ├── 10929.cpp ├── 10931.cpp ├── 10935.cpp ├── 10943.cpp ├── 10945.cpp ├── 10954.cpp ├── 10959.cpp ├── 10970.cpp ├── 10986.cpp └── 10994.cpp ├── Volume CV ├── 10503.cpp ├── 10505.cpp ├── 10507.cpp ├── 10509.cpp ├── 10515.cpp ├── 10530.cpp ├── 10533.cpp ├── 10534.cpp ├── 10539.cpp ├── 10550.cpp ├── 10557.cpp ├── 10570.cpp ├── 10579.cpp ├── 10583.cpp ├── 10589.cpp └── 10591.cpp ├── Volume CVI ├── 10608.cpp ├── 10616.cpp ├── 10633.cpp ├── 10635.cpp ├── 10642.cpp ├── 10646.cpp ├── 10650.cpp ├── 10656.cpp ├── 10660.cpp ├── 10662.cpp ├── 10663.cpp ├── 10664.cpp ├── 10667.cpp ├── 10670.cpp ├── 10678.cpp ├── 10679.cpp ├── 10682.cpp ├── 10684.cpp ├── 10696.cpp └── 10699.cpp ├── Volume CVII ├── 10700.cpp ├── 10701.cpp ├── 10703.cpp ├── 10714.cpp ├── 10720.cpp ├── 10721.cpp ├── 10724.cpp ├── 10731.cpp ├── 10733.cpp ├── 10738.cpp ├── 10763.cpp ├── 10773.cpp ├── 10783.cpp ├── 10784.cpp ├── 10789.cpp ├── 10790.cpp └── 10793.cpp ├── Volume CVIII ├── 10801.cpp ├── 10803.cpp ├── 10812.cpp ├── 10813.cpp ├── 10815.cpp ├── 10819.cpp ├── 10827.cpp ├── 10830.cpp ├── 10849.cpp ├── 10852.cpp ├── 10874.cpp ├── 10878.cpp ├── 10888.cpp ├── 10892.cpp └── 10895.cpp ├── Volume CX ├── 11000.cpp ├── 11003.cpp ├── 11034.cpp ├── 11044.cpp ├── 11045.cpp ├── 11054.cpp ├── 11057.cpp ├── 11059.cpp ├── 11060.cpp ├── 11067.cpp ├── 11068.cpp ├── 11069.cpp ├── 11080.cpp ├── 11082.cpp ├── 11085.cpp └── 11096.cpp ├── Volume CXI ├── 11101.cpp ├── 11107.cpp ├── 11110.cpp ├── 11111.cpp ├── 11121.cpp ├── 11130.cpp ├── 11137.cpp ├── 11138.cpp ├── 11150.cpp ├── 11151.cpp ├── 11152.cpp ├── 11157.cpp ├── 11161.cpp ├── 11172.cpp ├── 11185.cpp ├── 11192.cpp └── 11195.cpp ├── Volume CXII ├── 11200.cpp ├── 11203.cpp ├── 11205.cpp ├── 11207.cpp ├── 11219.cpp ├── 11220.cpp ├── 11221.cpp ├── 11222.cpp ├── 11223.cpp ├── 11225.cpp ├── 11227.cpp ├── 11231.cpp ├── 11233.cpp ├── 11235.cpp ├── 11236.cpp ├── 11242.cpp ├── 11244.cpp ├── 11254.cpp ├── 11258.cpp ├── 11286.cpp ├── 11287.cpp ├── 11292.cpp └── 11296.cpp ├── Volume CXIII ├── 11309.cpp ├── 11310.cpp ├── 11321.cpp ├── 11327.cpp ├── 11332.cpp ├── 11340.cpp ├── 11360.cpp ├── 11362.cpp ├── 11364.cpp ├── 11369.cpp ├── 11371.cpp ├── 11375.cpp ├── 11385.cpp ├── 11387.cpp ├── 11388.cpp ├── 11389.cpp └── 11398.cpp ├── Volume CXIV ├── 11401.cpp ├── 11402.2.cpp ├── 11402.cpp ├── 11408.cpp ├── 11413.cpp ├── 11417.cpp ├── 11428.cpp ├── 11437.cpp ├── 11452.cpp ├── 11455.cpp ├── 11456.cpp ├── 11459.cpp ├── 11461.cpp ├── 11462.cpp ├── 11463.cpp ├── 11466.cpp ├── 11470.cpp ├── 11475.cpp ├── 11479.cpp ├── 11480.cpp ├── 11481.cpp ├── 11483.cpp ├── 11489.cpp ├── 11491.cpp ├── 11492.cpp ├── 11493.cpp ├── 11494.cpp ├── 11495.cpp ├── 11496.cpp └── 11498.cpp ├── Volume CXIX ├── 11900.cpp ├── 11902.cpp ├── 11909.cpp ├── 11917.cpp ├── 11926.cpp ├── 11930.cpp ├── 11933.cpp ├── 11934.cpp ├── 11935.cpp ├── 11936.cpp ├── 11942.cpp ├── 11945.cpp ├── 11946.cpp ├── 11947.cpp ├── 11953.cpp ├── 11955.cpp ├── 11956.cpp ├── 11958.cpp ├── 11960.cpp ├── 11965.cpp ├── 11970.cpp ├── 11984.cpp ├── 11988.cpp ├── 11991.cpp └── 11995.cpp ├── Volume CXV ├── 11500.cpp ├── 11503.cpp ├── 11504.cpp ├── 11505.cpp ├── 11507.cpp ├── 11508.cpp ├── 11512.cpp ├── 11514.cpp ├── 11515.cpp ├── 11517.cpp ├── 11518.cpp ├── 11519.cpp ├── 11520.cpp ├── 11525.cpp ├── 11526.cpp ├── 11530.cpp ├── 11541.cpp ├── 11547.cpp ├── 11553.cpp ├── 11554.cpp ├── 11556.cpp ├── 11559.cpp ├── 11565.cpp ├── 11567.cpp ├── 11576.cpp ├── 11577.cpp ├── 11581.cpp ├── 11587.cpp └── 11588.cpp ├── Volume CXVI ├── 11608.cpp ├── 11610.cpp ├── 11629.cpp ├── 11636.cpp ├── 11650.cpp ├── 11658.cpp ├── 11660.cpp ├── 11661.cpp ├── 11666.cpp ├── 11677.cpp ├── 11678.cpp ├── 11679.cpp ├── 11680.cpp ├── 11683.cpp ├── 11685.cpp ├── 11686.cpp ├── 11687.cpp ├── 11688.cpp ├── 11689.cpp ├── 11690.cpp ├── 11691.cpp ├── 11692.cpp ├── 11693.cpp ├── 11697.cpp └── 11699.cpp ├── Volume CXVII ├── 11701.cpp ├── 11702.cpp ├── 11703.cpp ├── 11705.cpp ├── 11708.cpp ├── 11709.cpp ├── 11710.cpp ├── 11711.cpp ├── 11712.cpp ├── 11713.cpp ├── 11714.cpp ├── 11715.cpp ├── 11716.cpp ├── 11717.cpp ├── 11718.cpp ├── 11721.cpp ├── 11722.cpp ├── 11723.cpp ├── 11727.cpp ├── 11728.cpp ├── 11729.cpp ├── 11730.cpp ├── 11731.cpp ├── 11732.cpp ├── 11733.cpp ├── 11734.cpp ├── 11735.cpp ├── 11736.cpp ├── 11737.cpp ├── 11742.cpp ├── 11743.cpp ├── 11744.cpp ├── 11745.cpp ├── 11747.cpp ├── 11748.cpp ├── 11749.cpp ├── 11752.cpp ├── 11753.cpp ├── 11760.cpp ├── 11762.cpp ├── 11764.cpp ├── 11770.cpp ├── 11774.cpp ├── 11776.cpp ├── 11777.cpp ├── 11780.cpp ├── 11783.cpp ├── 11786.cpp ├── 11787.cpp ├── 11790.cpp ├── 11792.cpp ├── 11795.cpp ├── 11797.cpp └── 11799.cpp ├── Volume CXVIII ├── 10872.cpp ├── 11800.cpp ├── 11804.cpp ├── 11805.cpp ├── 11824.cpp ├── 11827.cpp ├── 11830.cpp ├── 11831.cpp ├── 11832.cpp ├── 11833.cpp ├── 11834.cpp ├── 11835.cpp ├── 11837.cpp ├── 11838.cpp ├── 11839.cpp ├── 11843.cpp ├── 11847.cpp ├── 11849.cpp ├── 11850.cpp ├── 11854.cpp ├── 11858.cpp ├── 11875.cpp ├── 11876.cpp ├── 11877.cpp ├── 11878.cpp ├── 11879.cpp ├── 11889.cpp └── 11892.cpp ├── Volume CXX ├── 12001.cpp ├── 12004.cpp ├── 12015.cpp ├── 12019.cpp ├── 12024.cpp ├── 12028.cpp ├── 12032.cpp ├── 12036.cpp ├── 12043.cpp ├── 12049.cpp ├── 12060.cpp ├── 12068.cpp ├── 12077.cpp ├── 12078.cpp ├── 12079.cpp ├── 12081.cpp ├── 12082.cpp └── 12083.cpp ├── Volume CXXI ├── 12124.cpp ├── 12125.cpp ├── 12126.cpp ├── 12130.cpp ├── 12131.cpp ├── 12132.cpp ├── 12136.cpp ├── 12144.cpp ├── 12146.cpp ├── 12147.cpp ├── 12148.cpp ├── 12149.cpp ├── 12150.cpp ├── 12153.cpp ├── 12154.cpp ├── 12155.cpp ├── 12157.cpp ├── 12159.cpp ├── 12160.cpp ├── 12186.cpp ├── 12187.cpp ├── 12188.cpp ├── 12189.cpp ├── 12190.cpp ├── 12191.cpp ├── 12192.cpp ├── 12193.cpp ├── 12194.cpp ├── 12195.cpp ├── 12196.cpp └── 12198.cpp ├── Volume CXXII ├── 12202.cpp ├── 12205.cpp ├── 12210.cpp ├── 12238.cpp ├── 12239.cpp ├── 12240.cpp ├── 12241.cpp ├── 12242.cpp ├── 12243.cpp ├── 12245.cpp ├── 12246.cpp ├── 12247.cpp ├── 12248.cpp ├── 12250.cpp ├── 12256.cpp ├── 12259.cpp ├── 12261.cpp ├── 12269.cpp ├── 12279.cpp ├── 12289.cpp └── 12293.cpp ├── Volume CXXIII ├── 12300.cpp ├── 12317.cpp ├── 12318.cpp ├── 12319.cpp ├── 12321.cpp ├── 12324.cpp ├── 12335.cpp ├── 12337.cpp ├── 12338.cpp ├── 12342.cpp ├── 12346.cpp ├── 12347.cpp ├── 12356.cpp ├── 12357.cpp ├── 12358.cpp ├── 12360.cpp ├── 12363.cpp ├── 12364.cpp ├── 12365.cpp ├── 12366.cpp ├── 12372.cpp ├── 12376.cpp ├── 12382.cpp ├── 12385.cpp ├── 12397.cpp └── 12398.cpp ├── Volume CXXIV ├── 12403.cpp ├── 12405.cpp ├── 12406.cpp ├── 12407.cpp ├── 12414.cpp ├── 12416.cpp ├── 12435.cpp ├── 12442.cpp ├── 12455.cpp ├── 12459.cpp ├── 12463.cpp ├── 12468.cpp ├── 12482.cpp ├── 12483.cpp ├── 12484.cpp ├── 12485.cpp ├── 12487.cpp ├── 12488.cpp ├── 12490.cpp ├── 12491.cpp ├── 12492.cpp ├── 12493.cpp └── 12498.cpp ├── Volume CXXV ├── 12502.cpp ├── 12503.cpp ├── 12504.cpp ├── 12526.cpp ├── 12527.cpp ├── 12529.cpp ├── 12531.cpp ├── 12532.cpp ├── 12537.cpp ├── 12541.cpp ├── 12542.cpp ├── 12543.cpp └── 12545.cpp ├── Volume CXXVI ├── 12602.cpp ├── 12608.cpp ├── 12611.cpp ├── 12614.cpp ├── 12626.cpp └── 12630.cpp ├── Volume CXXVII ├── 12700.cpp ├── 12704.cpp ├── 12708.cpp └── 12709.cpp ├── Volume I ├── 00100.cpp ├── 00101.cpp ├── 00102.cpp ├── 00103.cpp ├── 00104.cpp ├── 00105.cpp ├── 00106.cpp ├── 00107.cpp ├── 00108.cpp ├── 00109.cpp ├── 00110.cpp ├── 00111.cpp ├── 00112.cpp ├── 00113.cpp ├── 00114.cpp ├── 00115.cpp ├── 00116.cpp ├── 00117.cpp ├── 00118.cpp ├── 00119.cpp ├── 00120.cpp ├── 00121.cpp ├── 00122.cpp ├── 00123.cpp ├── 00124.cpp ├── 00125.cpp ├── 00126.cpp ├── 00127.cpp ├── 00128.cpp ├── 00129.cpp ├── 00130.cpp ├── 00133.cpp ├── 00136.cpp ├── 00138.cpp ├── 00139.cpp ├── 00140.cpp ├── 00141.cpp ├── 00144.cpp ├── 00145.cpp ├── 00146.cpp ├── 00147.cpp ├── 00148.cpp ├── 00151.cpp ├── 00152.cpp ├── 00153.cpp ├── 00154.cpp ├── 00155.cpp ├── 00156.cpp ├── 00160.cpp ├── 00161.cpp ├── 00162.cpp ├── 00166.cpp ├── 00167.cpp ├── 00168.cpp ├── 00170.cpp ├── 00181.cpp ├── 00184.cpp ├── 00186.cpp ├── 00187.cpp ├── 00190.cpp ├── 00191.cpp ├── 00193.cpp ├── 00195.cpp └── 00196.cpp ├── Volume II ├── 00200.cpp ├── 00201.cpp ├── 00202.cpp ├── 00213.cpp ├── 00216.cpp ├── 00218.cpp ├── 00220.cpp ├── 00225.cpp ├── 00227.cpp ├── 00231.cpp ├── 00232.cpp ├── 00247.cpp ├── 00253.cpp ├── 00255.cpp ├── 00256.cpp ├── 00259.cpp ├── 00260.cpp ├── 00263.cpp ├── 00264.cpp ├── 00270.cpp ├── 00271.cpp ├── 00272.cpp ├── 00275.cpp ├── 00278.cpp ├── 00280.cpp ├── 00291.cpp ├── 00294.cpp ├── 00297.cpp └── 00299.cpp ├── Volume III ├── 00300.cpp ├── 00305.cpp ├── 00311.cpp ├── 00315.cpp ├── 00324.cpp ├── 00333.cpp ├── 00336.cpp ├── 00340.cpp ├── 00343.cpp ├── 00344.cpp ├── 00347.cpp ├── 00348.cpp ├── 00350.cpp ├── 00352.cpp ├── 00355.cpp ├── 00357.cpp ├── 00369.cpp ├── 00371.cpp ├── 00374.cpp ├── 00378.cpp ├── 00380.cpp ├── 00382.cpp ├── 00383.cpp ├── 00386.cpp ├── 00389.cpp └── 00392.cpp ├── Volume IV ├── 00400.cpp ├── 00401.cpp ├── 00402.cpp ├── 00406.cpp ├── 00408.cpp ├── 00409.cpp ├── 00410.cpp ├── 00412.cpp ├── 00414.cpp ├── 00416.cpp ├── 00417.cpp ├── 00423.cpp ├── 00424.cpp ├── 00428.cpp ├── 00435.cpp ├── 00437.cpp ├── 00438.cpp ├── 00439.cpp ├── 00440.cpp ├── 00441.cpp ├── 00442.cpp ├── 00443.cpp ├── 00444.cpp ├── 00445.cpp ├── 00446.cpp ├── 00452.cpp ├── 00454.cpp ├── 00455.cpp ├── 00458.cpp ├── 00459.cpp ├── 00462.cpp ├── 00465.cpp ├── 00469.cpp ├── 00474.cpp ├── 00476.cpp ├── 00477.cpp ├── 00478.cpp ├── 00481.cpp ├── 00482.cpp ├── 00483.cpp ├── 00484.cpp ├── 00485.cpp ├── 00488.cpp ├── 00489.cpp ├── 00490.cpp ├── 00492.cpp ├── 00494.cpp ├── 00495.cpp ├── 00496.cpp ├── 00497.cpp ├── 00498.cpp └── 00499.cpp ├── Volume IX ├── 00900.cpp ├── 00902.cpp ├── 00913.cpp ├── 00939.cpp ├── 00948.cpp ├── 00957.cpp ├── 00967.cpp ├── 00978.cpp ├── 00990.cpp ├── 00991.cpp └── 00993.cpp ├── Volume V ├── 00501.cpp ├── 00505.cpp ├── 00507.cpp ├── 00514.cpp ├── 00515.cpp ├── 00516.cpp ├── 00524.cpp ├── 00530.cpp ├── 00531.cpp ├── 00532.cpp ├── 00534.cpp ├── 00536.cpp ├── 00537.cpp ├── 00541.cpp ├── 00543.cpp ├── 00544.cpp ├── 00545.cpp ├── 00551.cpp ├── 00555.cpp ├── 00556.cpp ├── 00562.cpp ├── 00563.cpp ├── 00567.cpp ├── 00568.cpp ├── 00572.cpp ├── 00573.cpp ├── 00574.cpp ├── 00575.cpp ├── 00576.cpp ├── 00579.cpp ├── 00583.cpp ├── 00584.cpp ├── 00591.cpp └── 00594.cpp ├── Volume VI ├── 00607.cpp ├── 00608.cpp ├── 00612.cpp ├── 00614.cpp ├── 00616.cpp ├── 00621.cpp ├── 00623.cpp ├── 00624.cpp ├── 00628.cpp ├── 00630.cpp ├── 00634.cpp ├── 00636.cpp ├── 00637.cpp ├── 00639.cpp ├── 00640.cpp ├── 00642.cpp ├── 00644.cpp ├── 00647.cpp ├── 00657.cpp ├── 00661.cpp ├── 00663.cpp ├── 00673.cpp ├── 00674.cpp ├── 00675.cpp ├── 00679.cpp ├── 00681.cpp ├── 00686.cpp ├── 00694.cpp └── 00696.cpp ├── Volume VII ├── 00706.cpp ├── 00713.cpp ├── 00714.cpp ├── 00719.cpp ├── 00725.cpp ├── 00727.cpp ├── 00729.cpp ├── 00739.cpp ├── 00750.cpp ├── 00755.cpp ├── 00756.cpp ├── 00763.cpp └── 00793.cpp ├── Volume VIII ├── 00820.cpp ├── 00821.cpp ├── 00833.cpp ├── 00834.cpp ├── 00836.cpp ├── 00843.cpp ├── 00846.cpp ├── 00847.cpp ├── 00855.cpp ├── 00884.cpp ├── 00893.cpp └── 00895.cpp ├── Volume X ├── 01009.cpp ├── 01016.cpp └── 01056.cpp ├── Volume XII ├── 01203.cpp ├── 01208.cpp ├── 01210.cpp ├── 01213.cpp └── 01225.cpp └── plist /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /brspoj/obi/BAFO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int a, b, n, tnum = 1, v1, v2; 4 | while(scanf("%d",&n) && n){ 5 | a = b = 0; 6 | printf("Teste %d\n",tnum++); 7 | while(n--){ 8 | scanf("%d%d",&v1,&v2); 9 | a += v1; b += v2; 10 | } 11 | if(a > b) puts("Aldo\n"); 12 | else puts("Beto\n"); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /brspoj/obi/BIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int u[4]; 4 | for(int a = 1, n; scanf("%d",&n) && n; a++){ 5 | u[0] = u[1] = u[2] = u[3] = 0; 6 | if(n>49) u[0] = n/50, n -= u[0]*50; 7 | if(n>9) u[1] = n/10, n -= u[1]*10; 8 | if(n>4) u[2] = n/5, n -= u[2]*5; 9 | u[3] = n; 10 | printf("Teste %d\n%d %d %d %d\n",a,u[0],u[1],u[2],u[3]); 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /brspoj/obi/COFRE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int a, b, dep, diff, teste = 1; 4 | while(scanf("%d",&dep) && dep){ 5 | diff = 0; 6 | printf("Teste %d\n",teste++); 7 | while(dep--){ 8 | scanf("%d%d",&a,&b); 9 | diff = diff + (a - b); 10 | printf("%d\n",diff); 11 | } 12 | putchar('\n'); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /brspoj/obi/DOBRA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int k, n, t; 4 | for(t = 1; scanf("%d",&n) && n != -1 ; t++) 5 | printf("Teste %d\n%d\n\n", t,(k = (1< 2 | struct A { 3 | int id, nota; 4 | }aln[1032]; 5 | int main(void){ 6 | for(int i, max, n, t = 1; scanf("%d",&n) && n; t++){ 7 | for(max = i = 0; i < n; i++){ 8 | scanf("%d%d",&aln[i].id,&aln[i].nota); 9 | if(aln[i].nota > max) max = aln[i].nota; 10 | } 11 | printf("Turma %d\n",t); 12 | for(i = 0; i < n; i++) 13 | if(aln[i].nota == max) printf("%d ",aln[i].id); 14 | printf("\n\n"); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /brspoj/obi/FATORIA2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int fat[13] = {1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600}; 3 | int main(void){ 4 | int n; 5 | scanf("%d",&n); 6 | printf("%d\n",fat[n]); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /brspoj/obi/FLIPERAM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int v[10000], i, m, n; 4 | bool inv(int a, int b){ 5 | return a > b; 6 | } 7 | int main(void){ 8 | scanf("%d%d",&n,&m); 9 | for(i = 0; i < n; i++) scanf("%d",&v[i]); 10 | std::sort(v,v+n,inv); 11 | for(i = 0; i < m; i++) printf("%d\n",v[i]); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /brspoj/obi/FROTATAX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | float a, g, ra, rg; 4 | scanf("%f%f%f%f",&a,&g,&ra,&rg); 5 | if((ra/a) > (rg/g)) puts("A"); 6 | else puts("G"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /brspoj/obi/METEORO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int c[4], meteor, hazard, teste = 1, x, y; 4 | while(scanf("%d%d%d%d",&c[0],&c[1],&c[2],&c[3]) 5 | && (c[0] || c[1] || c[2] || c[3])){ 6 | hazard = 0; 7 | scanf("%d",&meteor); 8 | while(meteor--){ 9 | scanf("%d%d",&x,&y); 10 | if(x >= c[0] && x <= c[2] 11 | && y <= c[1] && y >= c[3]) 12 | hazard++; 13 | } 14 | printf("Teste %d\n%d\n\n",teste++,hazard); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /brspoj/obi/OBIHANOI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n, tnum = 1; 4 | while(scanf("%d",&n) && n) printf("Teste %d\n%d\n\n",tnum++,(1< 2 | int main(void){ 3 | char name[2][15]; 4 | int n, a, b, tnum = 1; 5 | while(scanf("%d",&n) && n){ 6 | scanf("%s%s",&name[0],&name[1]); 7 | printf("Teste %d\n",tnum++); 8 | while(n--){ 9 | scanf("%d%d",&a,&b); 10 | puts(name[(a+b)&1]); 11 | } 12 | putchar('\n'); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /brspoj/obi/PRIMO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int n; 4 | int main(void){ 5 | scanf("%d",&n); 6 | if(n < 0) n = -n; 7 | int i, lim = (int)(sqrt(n)+1); 8 | bool primo = !(n == 1 || (!(n&1) && n != 2)); 9 | for(i = 3; primo && i < lim; i+=2) 10 | primo = n%i; 11 | if(primo) puts("sim"); 12 | else puts("nao"); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /brspoj/obi/QUADRAD2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; 4 | scanf("%d",&n); 5 | printf("%d\n",n*n); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /brspoj/obi/QUERM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | char line[90000]; 5 | int main(void){ 6 | int n; 7 | for(int a = 1, n; scanf("%d\n",&n) && n; a++){ 8 | gets(line); 9 | char *ptr = strtok(line," "); 10 | int i = 0; 11 | while(ptr!=NULL){ 12 | int k = atoi(ptr); 13 | if(k == ++i) break; 14 | ptr = strtok(NULL," "); 15 | } 16 | printf("Teste %d\n%d\n\n",a,i); 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /brspoj/obi/SOMA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n, sum = 0, v; 4 | scanf("%d",&n); 5 | while(n--) 6 | scanf("%d",&v), sum += v; 7 | printf("%d\n",sum); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /brspoj/obi/SUPERMER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int i, n; 4 | int vx[1000] ,vy[1000]; 5 | int main(void){ 6 | for(int t = 1; scanf("%d",&n) && n; t++){ 7 | for(i = 0; i < n; i++) scanf("%d%d",&vx[i],&vy[i]); 8 | std::sort(vx,vx+n); 9 | std::sort(vy,vy+n); 10 | n >>= 1; 11 | printf("Teste %d\n%d %d\n\n",t,vx[n],vy[n]); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /brspoj/regional/COPA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int T, N; 4 | 5 | int main(void){ 6 | char ign[128]; 7 | while(scanf("%d %d", &T, &N) && T){ 8 | int E = 0, p; 9 | for(int i = 0; i < T; ++i) scanf("%s %d", &ign, &p), E += p; 10 | printf("%d\n", N + (N << 1) - E); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /brspoj/regional/DAMA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int xi,yi,xf,yf; 4 | bool a,b; 5 | int main(void){ 6 | while(scanf("%d %d %d %d\n",&xi,&yi,&xf,&yf) && xi){ 7 | a = xi == xf; 8 | b = yi == yf; 9 | if(a && b) putchar('0'); 10 | else if(a || b || (abs(xf-xi) == abs(yf-yi))) putchar('1'); 11 | else putchar('2'); 12 | putchar('\n'); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /brspoj/regional/HIST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int lastexe[1000001], i, k, n, total; 4 | int main(void){ 5 | while(scanf("%d",&n) && n){ 6 | memset(lastexe,-1,sizeof(lastexe)); 7 | for(i = total = 0; i < n; i++){ 8 | scanf("%d",&k); 9 | if(lastexe[k]!=-1) total += (i-lastexe[k]); 10 | else total += k+i; 11 | lastexe[k] = i; 12 | } 13 | printf("%d\n",total); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /brspoj/regional/NLOGONIA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int k,cx,cy,x,y; 3 | char c[3]; 4 | int main(void){ 5 | while(scanf("%d",&k)){ 6 | if(k==0) break; 7 | scanf("%d %d",&cx,&cy); 8 | for(int i = 0; i < k; i++){ 9 | scanf("%d %d",&x,&y); 10 | if(x == cx || y == cy) printf("divisa\n"); 11 | else{ 12 | c[0] = (y > cy? 'N':'S'); 13 | c[1] = (x > cx? 'E':'O'); 14 | printf("%s\n",c); 15 | } 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /brspoj/regional/VAMPIROS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | double ev1,ev2,at,d,p,res; 4 | int main(void){ 5 | double p; 6 | scanf("%d",&ev1); 7 | while(ev1){ 8 | scanf("%lf %lf %lf", &ev2,&at,&d); 9 | ev1 = ceil(ev1/d); 10 | ev2 = ceil(ev2/d); 11 | d = 1; 12 | p = at/6; 13 | if(at == 3) res = ev1/(ev1+ev2); 14 | else res = (1 - pow((1-p)/p,ev1)) / (1 - pow((1-p)/p,ev1+ev2)); 15 | printf("%.1lf\n",res*100); 16 | scanf("%lf",&ev1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /brspoj/regional/VARETAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int c, n, pares, v; 3 | int main(void){ 4 | while(scanf("%d",&n) && n){ 5 | pares = 0; 6 | while(n--){ 7 | scanf("%d%d",&c,&v); 8 | pares += (v>>1); 9 | } 10 | printf("%d\n",pares>>1); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /brspoj/seletiva/.CALCULADORA.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ajmarin/coding/417d4a3987126fc458747c391ca18843eee74257/brspoj/seletiva/.CALCULADORA.cpp.swp -------------------------------------------------------------------------------- /brspoj/seletiva/BAPOSTAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n, max; scanf("%d",&n) && n; ){ 4 | max = -1; 5 | for(int i = 0, x, sum = 0; i < n; i++){ 6 | scanf("%d",&x); 7 | sum += x; 8 | if(sum > max) max = sum; 9 | else if(sum < 0) sum = 0; 10 | } 11 | if(max > 0) printf("The maximum winning streak is %d.\n",max); 12 | else printf("Losing streak.\n"); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /brspoj/seletiva/F91.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int n; 3 | int main(void){ 4 | scanf("%d",&n); 5 | while(n){ 6 | printf("f91(%d) = %d\n",n,(n<102)?91:n-10); 7 | scanf("%d",&n); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /brspoj/seletiva/FATORIAL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000001 3 | #define K 20000 4 | long long int i, r[MAX]; 5 | int inum = 1; 6 | int main(void){ 7 | r[0] = r[1] = 1; 8 | for(i = 2; i < MAX; i++){ 9 | r[i] = r[i-1]*i; 10 | while(r[i]%10 == 0) r[i] /= 10; 11 | r[i] %= 20000; 12 | } 13 | while(scanf("%lld",&i)!= EOF) 14 | printf("Instancia %d\n%lld\n\n",inum++,r[i]%10); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /brspoj/seletiva/ONZE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char num[1005]; 3 | unsigned short mod11,i; 4 | int main(void){ 5 | while(scanf("%s\n",&num)!=EOF){ 6 | if(num[0] == '0' && !num[1]) return 0; 7 | for(mod11 = i = 0; num[i]; i++) mod11 = (mod11*10+num[i]-'0')%11; 8 | if(mod11) printf("%s is not a multiple of 11.\n"); 9 | else printf("%s is a multiple of 11.\n"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /brspoj/seletiva/PARIDADE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | unsigned int n; 3 | char ans[35],i,par; 4 | int main(void){ 5 | ans[34] = 0; 6 | while(scanf("%u",&n) && n){ 7 | i = 33; par = 0; 8 | do { 9 | ans[i--] = (n&1)+'0'; par += n&1; n>>=1; 10 | } while(n); 11 | printf("The parity of %s is %u (mod 2).\n",ans+i+1,par); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /brspoj/seletiva/POPULAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int i, j, max, n, v[100]; 3 | int main(void){ 4 | register char c; 5 | while(scanf("%d\n",&n) && n){ 6 | for(i = 0; i < n; i++) 7 | for(j = 0; j < n; j++){ 8 | c = getchar(); 9 | getchar(); 10 | if(c == '1') v[j]++; 11 | } 12 | for(max = i = 0; i < n; v[i] = 0, i++) 13 | if(v[i] > max) max = v[i], j = i; 14 | printf("%d\n",max); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /brspoj/seletiva/RECUPERA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int ans, i, n, sum, v[100]; 3 | int main(void){ 4 | for(int inst = 1; scanf("%d",&n)==1; inst++){ 5 | printf("Instancia %d\n",inst); 6 | ans = -1; 7 | for(i = 0; i < n; i++) scanf("%d",&v[i]); 8 | for(sum = i = 0; i < n; i++){ 9 | if(sum == v[i]){ ans = i; break; } 10 | sum += v[i]; 11 | } 12 | if(ans == -1) puts("nao achei\n"); 13 | else printf("%d\n\n",v[ans]); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /brspoj/seletiva/SANDUBA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int c, i, j, n; 4 | long long acc, sum, v[100025]; 5 | int main(void){ 6 | scanf("%d",&c); 7 | while(c--){ 8 | sum = 0; 9 | scanf("%d",&n); 10 | for(i = 0; i < n; i++) scanf("%lld",&v[i]), acc += v[i]; 11 | for(i = 0; i < n; i++){ 12 | sum += ((acc%1300031)*(v[i]%1300031))%1300031; 13 | acc -= v[i]; 14 | } 15 | printf("%lld\n",sum%1300031); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /brspoj/seletiva/WCW.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int aux, cn, i, n, swap, v[10001]; 3 | int main(void){ 4 | scanf("%d",&cn); 5 | while(cn--){ 6 | scanf("%d",&n); swap = 0; 7 | for(i = 1; i <= n; i++) scanf("%d",&v[i]); 8 | for(i = 1; i <= n; i++) 9 | while(v[i] != i){ 10 | aux = v[i]; 11 | v[i] = v[v[i]]; 12 | v[aux] = aux; 13 | swap++; 14 | } 15 | printf("%d\n",swap); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /brspoj/sulamericana/FEYNMAN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; 4 | while(scanf("%d",&n) && n) printf("%d\n",n*(n+1)*(2*n+1)/6); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /brspoj/sulamericana/ODDOREVE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int i, k, n, v1, v2; 5 | while(scanf("%d",&n) && n){ 6 | for(v1 = i = 0; i < n; i++) scanf("%d",&k), v1 += k&1; 7 | for(v2 = i = 0; i < n; i++) scanf("%d",&k), v2 += !(k&1); 8 | printf("%d\n",abs(v1-v2)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /cf/001A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void){ 4 | long long a, m, n; cin >> m >> n >> a; 5 | cout << ((m+a-1)/a) * ((n+a-1)/a) << endl; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /cf/004A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n; scanf("%d", &n); 5 | puts(n < 3 || n & 1 ? "NO" : "YES"); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /cf/005A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char l[128]; 3 | int main(void){ 4 | int ans = 0, c = 0; 5 | while(fgets(l, 128, stdin)){ 6 | if(*l == '+' || *l == '-') c += ((*l == '+') << 1) - 1; 7 | else { 8 | char *p = l, *q; 9 | while(*p && *p++ != ':'); q = p; while(*p++ > 30); 10 | ans += (p - q - 1) * c; 11 | } 12 | } 13 | printf("%d\n", ans); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /cf/005A.py: -------------------------------------------------------------------------------- 1 | import sys 2 | a, c = 0, 0 3 | for s in sys.stdin: 4 | if s[0] == '+' or s[0] == '-': c += ((s[0] == '+') << 1) - 1 5 | else: a += c * (len(s) - s.index(':') - 2) 6 | print a 7 | -------------------------------------------------------------------------------- /cf/005B.py: -------------------------------------------------------------------------------- 1 | import sys 2 | s = [z.strip() for z in sys.stdin] 3 | L = max(map(len,s)) 4 | print '*' * (L + 2) 5 | l = 0; 6 | for z in s: 7 | sp = L - len(z) 8 | spl = (sp + l) >> 1; spr = sp - spl 9 | print '*' + ' ' * spl + z + ' ' * spr + '*' 10 | if sp & 1: l = 1 - l 11 | print '*' * (L + 2) 12 | -------------------------------------------------------------------------------- /cf/005C.py: -------------------------------------------------------------------------------- 1 | b, big, cnt, s, z = -1, 0, 1, [], raw_input() 2 | for i in range(len(z)): 3 | if z[i] == '(': s.append(i) 4 | elif s: 5 | s.pop() 6 | L = i - s[-1] if len(s) else i - b 7 | if L > big: big = L; cnt = 1; 8 | else: cnt += L == big 9 | else: b = i 10 | print big, cnt 11 | 12 | -------------------------------------------------------------------------------- /cf/006A.py: -------------------------------------------------------------------------------- 1 | a = sorted(map(int, raw_input().split())) 2 | m = max(a[0]+a[1]-a[2],a[1]+a[2]-a[3]) 3 | print ["IMPOSSIBLE", "SEGMENT", "TRIANGLE"][(m>=0)+(m>0)] 4 | -------------------------------------------------------------------------------- /cf/008A.py: -------------------------------------------------------------------------------- 1 | import re 2 | r = raw_input 3 | o, p = r(), r()+'.*'+r() 4 | x = re.compile(p) 5 | f = x.search(o) > 0 6 | b = x.search(o[::-1]) > 0 7 | print ["fantasy","backward","forward","both"][b + (f << 1)] 8 | -------------------------------------------------------------------------------- /cf/009A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int W, Y; scanf("%d %d", &Y, &W); 5 | int n = 7 - (W > Y? W : Y), d = 6; 6 | if(!(n % 3)) n /= 3, d = 2; d >>= !(n & 1); n >>= !(n & 1); 7 | printf("%d/%d\n", n, d); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /cf/009C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char c, p = '0'; 5 | int x = 0; 6 | for(c = getc(stdin); isdigit(c); p = c > p ? c : p, c = getc(stdin)) 7 | x = (x << 1) + (c > '0' || p > '1'); 8 | printf("%d\n", x); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /cf/009C.py: -------------------------------------------------------------------------------- 1 | n, x, z = raw_input(), 0, '0' 2 | for c in n: 3 | x = (x << 1) + (c > '0' or z > '1') 4 | z = max(z, c) 5 | print x 6 | -------------------------------------------------------------------------------- /cf/011A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int add = 0, pv, v = 0; 4 | int n, d; scanf("%d %d", &n, &d); 5 | for(int i = 0; i < n; ++i){ 6 | pv = v; 7 | scanf("%d", &v); 8 | if(v <= pv){ 9 | int c = (pv - v) / d + 1; 10 | v += c * d; 11 | add += c; 12 | } 13 | } 14 | printf("%d\n", add); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /cf/012A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dx[4] = {-1, 0, 1, -1}; 4 | int dy[4] = {-1, -1, -1, 0}; 5 | char s[3][16]; 6 | int main(void){ 7 | bool symmetric = true; 8 | for(int i = 0; i < 3; ++i) scanf("%s", s[i]); 9 | for(int d = 0; d < 4; ++d) 10 | symmetric &= s[1 + dx[d]][1 + dy[d]] == s[1 - dx[d]][1 - dy[d]]; 11 | puts(symmetric ? "YES" : "NO"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /cf/013A.py: -------------------------------------------------------------------------------- 1 | import fractions 2 | n, s = input(), 0 3 | for i in range(2, n): 4 | x = n 5 | while x: s += x % i; x /= i 6 | g = fractions.gcd(s, n-2) 7 | print str(s/g)+'/'+str((n-2)/g) 8 | -------------------------------------------------------------------------------- /cf/022A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, t, x = 128, y = 128; 5 | scanf("%d", &n); 6 | while(n--){ 7 | scanf("%d", &t); 8 | if(t < x) y = x, x = t; 9 | else if(t != x && t < y) y = t; 10 | } 11 | if(y == 1 << 7) puts("NO"); 12 | else printf("%d\n", y); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /cf/025A.py: -------------------------------------------------------------------------------- 1 | input() 2 | x = map(lambda x: int(x)&1, raw_input().split()) 3 | print x.index(x.count(0) > 1) + 1 4 | 5 | -------------------------------------------------------------------------------- /cf/025B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int c = 0, n; scanf("%d\n", &n); 4 | while(n--){ 5 | putc(getc(stdin), stdout); 6 | if(++c == 2 && n>1) c = 0, putc('-', stdout); 7 | } 8 | putc(10, stdout); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /cf/026A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | bool ap(int x){ 3 | int div = !(x & 1); 4 | while(!(x&1)) x >>= 1; 5 | for(int i = 3; x != 1; i += 2){ 6 | div += !(x % i); 7 | while(!(x%i)) x /= i; 8 | if(i * i > x) break; 9 | } 10 | div += x != 1; 11 | return div == 2; 12 | } 13 | int main(void){ 14 | int ans = 0, n; scanf("%d", &n); 15 | for(int i = 6; i <= n; ++i) ans += ap(i); 16 | printf("%d\n", ans); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /cf/026B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int ans = 0, bal = 0; 4 | for(char c = getc(stdin); c == '(' || c == ')'; c = getc(stdin)){ 5 | if(c == '(') ++ans, ++bal; 6 | if(c == ')' && bal) ++ans, --bal; 7 | } 8 | printf("%d\n", ans - bal); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /cf/027A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | bool used[3072]; 4 | int main(void){ 5 | memset(used, 0, 3072); 6 | int a, n; scanf("%d", &n); 7 | while(n--){ 8 | scanf("%d", &a); 9 | used[a] = 1; 10 | } 11 | for(int i = 1; ; ++i) 12 | if(!used[i]){ printf("%d\n", i); break; } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /cf/029A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x[128], d[128]; 4 | 5 | int main(void){ 6 | int n; 7 | scanf("%d", &n); 8 | for(int i = 0; i < n; ++i){ 9 | scanf("%d %d", &x[i], &d[i]); 10 | } 11 | bool y = 0; 12 | for(int i = 0; i < n; ++i){ 13 | for(int j = i + 1; j < n; ++j){ 14 | int s1 = x[i] + d[i]; 15 | int s2 = x[j] + d[j]; 16 | y |= x[i] == s2 && x[j] == s1; 17 | } 18 | } 19 | if(y) puts("YES"); 20 | else puts("NO"); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /cf/029B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int d, g, l, r, ttl, v; 6 | double eta; 7 | scanf("%d %d %d %d %d", &l, &d, &v, &g, &r); 8 | eta = l /(double) v; 9 | ttl = d % ((g + r) * v); 10 | if(ttl >= g * v) eta += ((r + g) * v - ttl)/(double)v; 11 | printf("%.8lf\n", eta); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /cf/032A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[1000]; 3 | int abs(int a){ return a < 0 ? -a : a; } 4 | int main(void){ 5 | int ans = 0, d, n; scanf("%d %d", &n, &d); 6 | for(int i = 0; i < n; ++i){ 7 | scanf("%d", &v[i]); 8 | for(int j = 0; j < i; ++j) if(abs(v[j] - v[i]) <= d) ans += 2; 9 | } 10 | printf("%d\n", ans); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cf/033A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int t[1005], r[1005]; 4 | int main(void){ 5 | int k, m, n, x, y; scanf("%d %d %d", &n, &m, &k); 6 | for(int i = 0; i < 1005; ++i) r[i] = 0; 7 | while(n--){ 8 | scanf("%d %d", &x, &y); 9 | if(!r[x]) t[x] = y; 10 | else if(y < t[x]) t[x] = y; 11 | r[x]++; 12 | } 13 | x = 0; 14 | for(int i = 1; i <= m; ++i) x += t[i]; 15 | printf("%d\n", x > k ? k : x); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /cf/034A.py: -------------------------------------------------------------------------------- 1 | n, h = input(), map(int, raw_input().split()) 2 | idx, mind = -1, 1024 3 | for i in range(n): 4 | t = abs(h[i] - h[i - 1]) 5 | if t < mind: mind = t; idx = i 6 | print idx+1, 1 + (idx + n - 1) % n 7 | -------------------------------------------------------------------------------- /cf/034B.py: -------------------------------------------------------------------------------- 1 | n, m = map(int,raw_input().split()) 2 | x, s = sorted(map(int,raw_input().split())), 0 3 | for i in range(m): 4 | s += -x[i] if x[i] < 0 else 0 5 | print s 6 | -------------------------------------------------------------------------------- /cf/036D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | #ifdef ONLINE_JUDGE 4 | freopen("input.txt", "r", stdin); 5 | freopen("output.txt", "w", stdout); 6 | #endif 7 | const char *ans[2] = {"-", "+"}; 8 | int t, k; scanf("%d %d", &t, &k); ++k; 9 | for(int m, n; t-- && scanf("%d %d", &m, &n) == 2; ){ 10 | if(n > m) n ^= m ^= n ^= m; 11 | puts(ans[!(n % k) || ((n / k) & k > 2)^((m + n) & 1)]); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /cf/037A.py: -------------------------------------------------------------------------------- 1 | n, x = input(), map(int,raw_input().split()) 2 | c, s = [0] * 1024, 0 3 | for k in x: s += not c[k]; c[k] += 1 4 | print max(c),s 5 | -------------------------------------------------------------------------------- /cf/038A.py: -------------------------------------------------------------------------------- 1 | n, x = input(), map(int,raw_input().split()) 2 | a, b = map(int, raw_input().split()) 3 | print sum(x[a-1:b-1]) 4 | -------------------------------------------------------------------------------- /cf/038C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[128]; 3 | int main(void){ 4 | int ans = 0, n, l; scanf("%d %d", &n, &l); 5 | for(int i = 0; i < n; ++i) scanf("%d", &v[i]); 6 | for(int i = l; i < 101; ++i){ 7 | int c = 0; 8 | for(int j = 0; j < n; ++j) 9 | c += v[j] / i; 10 | if(c * i > ans) ans = c * i; 11 | if(!c) break; 12 | } 13 | printf("%d\n", ans); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cf/041A.py: -------------------------------------------------------------------------------- 1 | r = raw_input 2 | a, b = r()[::-1], r() 3 | print["NO","YES"][a==b] 4 | -------------------------------------------------------------------------------- /cf/043A.py: -------------------------------------------------------------------------------- 1 | score = [0, 1] 2 | n = input(); 3 | team1, team2 = raw_input(), "" 4 | for i in range(n-1): 5 | s = raw_input() 6 | score[s == team1] += 1 7 | if s != team1: team2 = s 8 | print team1 if score[1] > score[0] else team2 9 | 10 | -------------------------------------------------------------------------------- /cf/043B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define IN getc(stdin) 3 | #define READL(x) for(char c = IN; c != EOF && c != 10; c = IN) 4 | int cnt[128]; 5 | int main(void){ 6 | bool yes = true; 7 | for(int i = 0; i < 128; ++i) cnt[i] = 0; 8 | READL(c) ++cnt[c]; 9 | READL(c) yes &= c == 32 || cnt[c]--; 10 | puts(yes ? "YES":"NO"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cf/043C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int mod[3]; 3 | int main(void){ 4 | int n, t; scanf("%d", &n); 5 | mod[0] = mod[1] = mod[2] = 0; 6 | while(n--){ scanf("%d", &t); ++mod[t % 3]; } 7 | printf("%d\n", (mod[0] >> 1)+ (mod[1] < mod[2] ? mod[1] : mod[2])); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /cf/046A.py: -------------------------------------------------------------------------------- 1 | x, n = 0, input() 2 | for i in range(1,n): 3 | x = (x + i) % n 4 | print x+1, 5 | print 6 | -------------------------------------------------------------------------------- /cf/046B.py: -------------------------------------------------------------------------------- 1 | size = ['S', 'M', 'L', 'XL', 'XXL'] 2 | have = map(int, raw_input().split()) 3 | n = input() 4 | for i in range(n): 5 | s = raw_input() 6 | x = size.index(s) 7 | for i in range(5): 8 | if x + i < 5 and have[x + i]: 9 | print size[x + i] 10 | have[x + i] -= 1 11 | break 12 | if x - i > -1 and have[x - i]: 13 | print size[x - i] 14 | have[x - i] -= 1 15 | break 16 | -------------------------------------------------------------------------------- /cf/047A.py: -------------------------------------------------------------------------------- 1 | r = (1 + 8*input())**.5 2 | print ["NO","YES"][int(r) == r and not ((int(r)-1) & 1)] 3 | -------------------------------------------------------------------------------- /cf/049A.py: -------------------------------------------------------------------------------- 1 | def is_letter(x): return 'a' <= x <= 'z' or 'A' <= x <= 'Z' 2 | def is_vowel(x): return x in ['A','E','I','O','U','Y'] 3 | s = raw_input()[::-1].upper() 4 | for c in s: 5 | if is_letter(c): 6 | print "YES" if is_vowel(c) else "NO" 7 | break 8 | -------------------------------------------------------------------------------- /cf/050A.py: -------------------------------------------------------------------------------- 1 | m, n = map(int,raw_input().split()) 2 | print m*n/2 3 | -------------------------------------------------------------------------------- /cf/050B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long cnt[128]; 4 | int main(void){ 5 | long long ans = 0; 6 | for(int i = 0; i < 128; ++i) cnt[i] = 0; 7 | for(char c = getc(stdin); c != EOF; c = getc(stdin)) ++cnt[c]; 8 | for(char c = '0'; c <= '9'; ++c) ans += cnt[c]*cnt[c]; 9 | for(char c = 'a'; c <= 'z'; ++c) ans += cnt[c]*cnt[c]; 10 | std::cout << ans << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cf/053C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; scanf("%d", &n); 4 | int jump = n - 1; 5 | for(int i = 1; ; ){ 6 | printf("%d%c", i, jump ? ' ' : '\n'); 7 | if(!jump) break; 8 | i += jump; 9 | jump = -jump + (jump > 0) - (jump < 0); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cf/054A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int v[512]; 4 | int main(void){ 5 | int c, k, n; scanf("%d %d %d", &n, &k, &c); 6 | int idx = 0, lp = 0, pc = 0; 7 | for(int i = 0; i < c; ++i) 8 | scanf("%d", &v[i]); 9 | for(int i = 1; i <= n; ++i) 10 | if(i - lp == k || v[idx] == i){ 11 | idx += v[idx] == i; 12 | lp = i; 13 | ++pc; 14 | } 15 | printf("%d\n", pc); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /cf/055A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool v[1024]; 4 | int main(void){ 5 | int n; scanf("%d", &n); 6 | for(int i = 0; i < n; ++i) v[i] = 0; 7 | int vc = 0; 8 | for(int at = 0, c = 0; c < n; ++c){ 9 | vc += !v[at]; 10 | v[at] = 1; 11 | at = (at + c + 1) % n; 12 | } 13 | puts(vc == n ? "YES" : "NO"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cf/058A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char h[16] = "hello"; 4 | char s[128]; 5 | int main(void){ 6 | scanf("%s", s); 7 | int j = 0; 8 | for(int i = 0; s[i]; ++i){ 9 | if(s[i] == h[j]) ++j; 10 | } 11 | if(j == 5) puts("YES"); 12 | else puts("NO"); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /cf/058B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int reduce(int n){ 4 | if(!(n & 1)) return n >> 1; 5 | if(!(n % 3)) return n / 3; 6 | for(int i = 6; ; i += 6){ 7 | int t1 = i + 1, t2 = i - 1; 8 | if(!(n % t2)) return n / t2; 9 | if(!(n % t1)) return n / t1; 10 | if(t1 * t1 > n) return 1; 11 | } 12 | } 13 | int main(void){ 14 | int n; scanf("%d", &n); 15 | while(n != 1){ 16 | printf("%d ", n); 17 | n = reduce(n); 18 | } 19 | printf("1\n"); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /cf/058C.py: -------------------------------------------------------------------------------- 1 | x=[0 for i in range(2**17)] 2 | n=input() 3 | t=map(int,raw_input().split()) 4 | for i in range(n): 5 | d=t[i]-min(i,n-i-1) 6 | if d>0:x[d]+=1; 7 | print n-max(x) 8 | -------------------------------------------------------------------------------- /cf/059B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int m = 128, n, s = 0, t; scanf("%d", &n); 4 | while(n--){ 5 | scanf("%d", &t); 6 | s += t; 7 | if(t & 1 && t < m) m = t; 8 | } 9 | if(!(s & 1)) s -= m & 1 ? m : s; 10 | printf("%d\n", s); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cf/066A.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n < -9223372036854775808 or n > 9223372036854775807: 3 | print("BigInteger") 4 | elif n < -2147483648 or n > 2147483647: 5 | print("long") 6 | elif n < -32768 or n > 32767: 7 | print("int") 8 | elif n < -128 or n > 127: 9 | print("short") 10 | else: 11 | print("byte") 12 | -------------------------------------------------------------------------------- /cf/066B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int h[1024]; 3 | int main(void){ 4 | int maxw = 0, n; 5 | scanf("%d", &n); 6 | for(int i = 0; i < n; ++i) scanf("%d", h + i); 7 | for(int i = 0; i < n; ++i){ 8 | int l, r; 9 | for(l = i; l > 0 && h[l - 1] <= h[l]; --l); 10 | for(r = i; r < n-1 && h[r + 1] <= h[r]; ++r); 11 | if(r - l + 1 > maxw) maxw = r - l + 1; 12 | } 13 | printf("%d\n", maxw); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /cf/156C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define FOR(i,s,n) for(int i(s); i 2 | 3 | int main(void){ 4 | int v[64]; 5 | for(int k, n; scanf("%d %d", &n, &k) == 2; ){ 6 | int ans = 0; 7 | for(int i = 0; i < n; ++i) scanf("%d", v + i); 8 | for(int i = 0; i < n; ++i) ans += v[i] && v[i] >= v[k - 1]; 9 | printf("%d\n",ans); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cf/191B.pl: -------------------------------------------------------------------------------- 1 | ($n,$k)=split(/ /,<>); 2 | $m=int <>; 3 | @c=map{int $_} split(/ /,<>); 4 | @z=sort {$a<=>$b}@c[0..$n-2]; 5 | $s += $z[$n-$_-1] for(1 .. $k); 6 | $x=0; 7 | print "$n\n" if $s <= $m; 8 | for(0 .. $n-1){ 9 | $x = $_ + 1 if $s - $z[$n-$k-1] + $c[$_] > $m; 10 | last if $x; 11 | } 12 | print "$x\n" if $s > $m; 13 | -------------------------------------------------------------------------------- /cf/191B.py: -------------------------------------------------------------------------------- 1 | R=lambda:map(int,raw_input().split()) 2 | n,k=R() 3 | m=R()[0] 4 | c=R() 5 | z=sorted(c[:-1]) 6 | s=sum(z[-k:]) 7 | print n if s <= m else 1+min(i for i,j in enumerate(c) if s-z[-k]+j>m) 8 | -------------------------------------------------------------------------------- /euler/e001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int sum = 0; 7 | for(int i = 3; i < 1000; ++i) sum += (!(i % 3) || !(i % 5) ? i : 0); 8 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 9 | printf("Answer: %d\n", sum); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /euler/e001.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | $sum = 0; 4 | 5 | for($i = 3; $i < 1000; $i++){ 6 | if(not($i % 3) or not($i % 5)){ 7 | $sum += $i; 8 | } 9 | } 10 | 11 | print "$sum\n" 12 | -------------------------------------------------------------------------------- /euler/e002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int f1, f2, f3, sum = 0; 7 | f1 = f2 = 1; 8 | while(f3 < 4000000){ 9 | f3 = f1 + f2; 10 | if(!(f3 & 1)) sum += f3; 11 | f1 = f2; 12 | f2 = f3; 13 | } 14 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 15 | printf("Answer: %d\n", sum); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /euler/e002.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | 5 | my @fib = (0, 1); 6 | my $sum = 0; 7 | 8 | for(my $i = 2; ; ++$i){ 9 | $fib[$i] = $fib[-1] + $fib[-2]; 10 | if($fib[-1] > 4000000){ last; } 11 | if(not($fib[-1] & 1)){ $sum += $fib[-1]; } 12 | } 13 | print("$sum\n"); 14 | -------------------------------------------------------------------------------- /euler/e003.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | 5 | my $n = 600851475143; 6 | 7 | sub factor { 8 | while(!($_[0] & 1)){ $_[0] >>= 1; } 9 | while(!($_[0] % 3)){ $_[0] /= 3; } 10 | for(my $i = 5; ; ++$i){ 11 | my $j = $i + 2; 12 | while(!($_[0] % $i)){ $_[0] /= $i; } 13 | while(!($_[0] % $j)){ $_[0] /= $j; } 14 | last unless($j ** 2 < $_[0]); 15 | } 16 | } 17 | factor $n; 18 | print "$n\n" 19 | 20 | -------------------------------------------------------------------------------- /euler/e004.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | 5 | my $ans = 0; 6 | 7 | sub palindrome { 8 | my $num = $_[0] . ''; 9 | return reverse($num) eq $num; 10 | } 11 | 12 | for(my $i = 100; $i < 1000; ++$i){ 13 | for(my $j = $i; $j < 1000; ++$j){ 14 | my $p = $i * $j; 15 | if(palindrome($p) and $p > $ans){ 16 | $ans = $p; 17 | } 18 | } 19 | } 20 | 21 | print "$ans\n"; 22 | -------------------------------------------------------------------------------- /euler/e005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 7 | printf("Answer: %d\n", 2*2*2*2*3*3*5*7*11*13*17*19); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /euler/e005.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | 5 | my $ans = 1; 6 | 7 | sub gcd { 8 | my ($a, $b) = @_; 9 | return $b ? gcd($b, $a % $b) : $a; 10 | } 11 | 12 | for(my $i = 2; $i <= 20; ++$i){ 13 | $ans = $ans * $i / gcd($ans, $i); 14 | } 15 | 16 | print("$ans\n"); 17 | 18 | -------------------------------------------------------------------------------- /euler/e006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define n 100 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int sqsum = (n * (n + 1) * (2 * n + 1)) / 6, sumsq = (1 + n) * n / 2; 8 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 9 | printf("Answer: %d\n", sumsq*sumsq - sqsum); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /euler/e006.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | 5 | # SQ(SUM[1..n]) - SUM(SQ[1..n]) = 6 | # = ((n * (n + 1)) / 2)^2 - (n * (n + 1) * (2n + 1)) / 6 7 | # = (n^4 + 2n^3 + n^2) / 4 - (2n^3 + 3n^2 + n) / 6 8 | # = (3n^4 + 6n^3 + 3n^2 - 4n^3 - 6n^2 - 2n) / 12 9 | # = (3n^4 + 2n^3 - 3n^2 - 2n) / 12 10 | 11 | my $n = 100; 12 | my $ans = (3 * $n ** 4 + 2 * $n ** 3 - 3 * $n ** 2 - 2 * $n) / 12; 13 | 14 | print "$ans\n"; 15 | -------------------------------------------------------------------------------- /euler/e008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int v[1000]; 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = 0; 8 | for(int i = 0; i < 1000; ++i) scanf("%1d", &v[i]); 9 | for(int i = 0; i < 996; ++i){ 10 | int p = v[i] * v[i + 1] * v[i + 2] * v[i + 3] * v[i + 4]; 11 | if(p > ans) ans = p; 12 | } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /euler/e009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int ans = 0; 7 | for(int n = 1; !ans && n < 499; ++n) 8 | for(int m = n + 1; !ans && m < 499; ++m){ 9 | int a = 2 * m * n, b = m * m - n * n, c = m * m + n * n; 10 | if(a + b + c == 1000) ans = a * b * c; 11 | } 12 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 13 | printf("Answer: %d\n", ans); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /euler/e015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | long long ans = 1; 7 | for(int c = 40, d = 1; c > 20; c--, d++) ans = (ans * c)/d; 8 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 9 | printf("Answer: %lld\n", ans); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /euler/e028.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define LIM 1001 6 | int main(void){ 7 | clock_t ini = clock(); 8 | int ans = 1, n = 1, inc = 2; 9 | for(int i = 3; i <= LIM; i += 2, inc += 2) 10 | for(int c = 0; c < 4; ++c) 11 | ans += (n += inc); 12 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 13 | printf("Answer: %d\n", ans); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /euler/e048.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | long long ans = 0; 8 | for(int i = 1; i < 1001; ++i){ 9 | long long c = i, r = i; 10 | for(int j = 1; j < i; ++j) r = (r * c) % 10000000000LL; 11 | ans = (ans + r) % 10000000000LL; 12 | } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %lld\n", ans); 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /euler/e053.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = 0; 8 | for(int i = 23; i < 101; ++i) 9 | for(int d = 1, n = i, v = 1; n > i / 2; n--, d++) 10 | if((v = v*n/d) > 1000000){ ans += (i + 1) - (d << 1); break; } 11 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 12 | printf("Answer: %d\n", ans); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /euler/e056.py: -------------------------------------------------------------------------------- 1 | maxs = 0 2 | for i in range(1, 100): 3 | for j in range(1, 100): 4 | maxs = max(maxs, sum(int(x) for x in str(i ** j))) 5 | print(maxs) 6 | -------------------------------------------------------------------------------- /euler/e057.py: -------------------------------------------------------------------------------- 1 | from math import * 2 | ans, d, n = 0, 1, 1 3 | for i in range(999): 4 | d, n = n + d, n + (d << 1) 5 | ans += floor(log10(n)) > floor(log10(d)) 6 | print(ans) 7 | 8 | -------------------------------------------------------------------------------- /euler/e062.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | ans, d, f, s = '', {}, {}, set() 3 | for i in range(1, 10000): 4 | nums = ''.join(sorted(str(i ** 3))) 5 | l = len(s) 6 | s.add(nums) 7 | if len(s) == l: 8 | d[nums] += 1 9 | else: 10 | d[nums] = 1 11 | f[nums] = i ** 3 12 | if d[nums] == 5: 13 | print('Answer is', f[nums]) 14 | break 15 | 16 | -------------------------------------------------------------------------------- /euler/e063.py: -------------------------------------------------------------------------------- 1 | from math import * 2 | cnt = 0 3 | for b in range(1, 10): 4 | cnt += int(1 / (1 - log10(b))) 5 | print('Answer is', cnt) 6 | -------------------------------------------------------------------------------- /euler/e065.py: -------------------------------------------------------------------------------- 1 | d, dsum, n = 1, 0, 0 2 | for i in range(1, 101): 3 | if not(i % 3): x = int(2 * i / 3) 4 | else: x = 1 + (i == 1) 5 | d, n = x * d + n, d 6 | dsum = 0 7 | sn = str(d) 8 | for x in str(d): 9 | dsum += int(x) 10 | print(dsum) 11 | -------------------------------------------------------------------------------- /euler/e069.py: -------------------------------------------------------------------------------- 1 | primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 2 | ans = 1 3 | for i in primes: 4 | if ans * i > 1000000: 5 | print('Answer is', ans) 6 | break 7 | else: 8 | ans *= i 9 | -------------------------------------------------------------------------------- /euler/e071.py: -------------------------------------------------------------------------------- 1 | a, b, c, d = 2, 5, 3, 7 2 | while b + d < 1000000: 3 | a, b = a + c, b + d 4 | print('Answer is {}, from fraction {}/{}'.format(a,a,b)) 5 | -------------------------------------------------------------------------------- /euler/e076.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int ans = 0; 5 | void r(int s, int m){ 6 | if(s == 100){ 7 | ans++; 8 | return; 9 | } 10 | int lim = 101 - s; 11 | for(int i = m; i < lim; ++i) r(s + i, i); 12 | } 13 | int main(void){ 14 | clock_t ini = clock(); 15 | for(int i = 1; i < 51; ++i) r(i, i); 16 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 17 | printf("Answer: %d\n", ans); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /euler/e117.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | long long w[51]; 6 | int main(void){ 7 | clock_t ini = clock(); 8 | w[0] = w[1] = 1; w[2] = 2; w[3] = 4; 9 | for(int i = 4; i < 51; ++i) 10 | w[i] = w[i - 1] + w[i - 2] + w[i - 3] + w[i - 4]; 11 | long long ans = w[50]; 12 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 13 | printf("Answer: %lld\n", ans); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /euler/e120.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = 0; 8 | for(int i = 3; i < 1001; ++i) ans += (i << 1) * ((i - 1) >> 1); 9 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 10 | printf("Answer: %d\n", ans); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /euler/e129.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int getk(int n){ 5 | for(int x = 1, k = 1; ; x = (x * 10 + 1) % n, ++k) 6 | if(!x) return k; 7 | } 8 | int main(void){ 9 | clock_t ini = clock(); 10 | int ans = -1; 11 | for(int i = 1000001; ; i += 2) 12 | if((i % 5) && getk(i) > 1000000){ ans = i; break; } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /euler/e371.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int NN = 1024; 5 | 6 | double p[NN], p500[NN]; 7 | 8 | int main(void){ 9 | p500[499] = 2; 10 | p[499] = (1000 + p500[499]) / 500; 11 | for(int i = 499; i--; ){ 12 | p500[i] = (1000 + (998 - (i << 1)) * p500[i + 1]) / (999 - i); 13 | p[i] = (1000 + (998 - (i << 1)) * p[i + 1] + p500[i]) / (999 - i); 14 | } 15 | printf("%.8lf\n", p[0]); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /euler/keylog.txt: -------------------------------------------------------------------------------- 1 | 319 2 | 680 3 | 180 4 | 690 5 | 129 6 | 620 7 | 762 8 | 689 9 | 762 10 | 318 11 | 368 12 | 710 13 | 720 14 | 710 15 | 629 16 | 168 17 | 160 18 | 689 19 | 716 20 | 731 21 | 736 22 | 729 23 | 316 24 | 729 25 | 729 26 | 710 27 | 769 28 | 290 29 | 719 30 | 680 31 | 318 32 | 389 33 | 162 34 | 289 35 | 162 36 | 718 37 | 729 38 | 319 39 | 790 40 | 680 41 | 890 42 | 362 43 | 319 44 | 760 45 | 316 46 | 729 47 | 380 48 | 319 49 | 728 50 | 716 51 | -------------------------------------------------------------------------------- /euler/pell.txt: -------------------------------------------------------------------------------- 1 | Pell equations 2 | 3 | Pell's equation is any Diophantine equation of the form 4 | 5 | x^2 - n y^2 = 1 6 | 7 | (x/y) ~ sqrt(n) when x-> inf 8 | 9 | After finding the fundamental solution, new solutions can be generated: 10 | 11 | x_k = x_1 * x_k-1 + n * y_1 * y_k-1 12 | y_k = x_1 * y_k-1 + y_1 * x_k-1 13 | -------------------------------------------------------------------------------- /euler/skel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 8 | printf("Answer: %d\n", ans); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /la/2681.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char line[1024]; 4 | int main(void){ 5 | while(fgets(line, 1024, stdin)){ 6 | printf("%s", line); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /la/3279.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int c[2]; 4 | char name1[32], name2[32], x[32]; 5 | 6 | int main(void){ 7 | while(scanf("%s %s %s", name1, name2, x)){ 8 | if(name1[0] == '#') break; 9 | int n, z; scanf("%d", &n); 10 | c[0] = c[1] = 0; 11 | while(n--){ 12 | scanf("%d", &z); 13 | ++c[z & 1]; 14 | } 15 | printf("%s %d %s %d\n", name1, c[1 - (x[0]=='o')], name2, c[x[0]=='o']); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /la/3389.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int N, T; 5 | scanf("%d", &T); 6 | for(int cnum = 0; cnum < T; ){ 7 | scanf("%d",&N); 8 | int q, d, n, p; 9 | q = N/25; 10 | N%= 25; 11 | d = N/10; 12 | N%= 10; 13 | n = N/5; 14 | N%= 5; 15 | p = N; 16 | printf("%d %d QUARTER(S), %d DIME(S), %d NICKEL(S), %d PENNY(S)\n", 17 | ++cnum, q, d, n, p); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /la/3392.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NN 302 4 | long long tri[NN]; 5 | int main(void){ 6 | for(int i = 1; i < NN; ++i) tri[i] = (i*(i+1))/2; 7 | for(int i = 2; i < NN; ++i) tri[i] *= (i-1); 8 | int T, N; 9 | scanf("%d",&T); 10 | for(int cnum = 0; cnum < T; ){ 11 | scanf("%d",&N); 12 | long long acc = 0; 13 | for(int i = 1; i <= N; ++i) acc += tri[i+1]; 14 | printf("%d %d %d\n",++cnum,N,acc); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /la/3469.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ajmarin/coding/417d4a3987126fc458747c391ca18843eee74257/la/3469.cpp -------------------------------------------------------------------------------- /la/3470.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const char *y = "yes", *n = "no"; 4 | int main(void){ 5 | register int i, j, D, N; 6 | register bool w[100], x; 7 | while(scanf("%d %d\n",&N,&D) && N){ 8 | memset(w,1,N); 9 | for(x = i = 0; i < D; i++) 10 | for(j = 0; j < N; getc(stdin), j++) 11 | w[j] &= (getc(stdin) - '0'); 12 | for(i = 0; i < N; i++) x |= w[i]; 13 | ((x)?(puts(y)):(puts(n))); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /la/3656.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int i, k, n, v1, v2; 5 | while(scanf("%d",&n) && n){ 6 | for(v1 = i = 0; i < n; i++) scanf("%d",&k), v1 += k&1; 7 | for(v2 = i = 0; i < n; i++) scanf("%d",&k), v2 += !(k&1); 8 | printf("%d\n",abs(v1-v2)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /la/3910.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define IN getc( stdin ) 5 | 6 | int main(void){ 7 | int k, N, T; 8 | register char c; 9 | scanf("%d", &T); 10 | for(int cnum = 0; cnum < T; ){ 11 | scanf("%d ", &N); 12 | printf("%d ", ++cnum); 13 | for(c = IN; !isupper(c); c = IN); 14 | ungetc(c, stdin); 15 | for(k = 0, c = IN; isupper(c); c = IN) 16 | if(++k != N) putc(c, stdout); 17 | putc(10, stdout); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /la/4191.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | LL f(LL t, LL c, LL n){ 4 | return t * (c - t * n); 5 | } 6 | int main(void){ 7 | for(int c, n; scanf("%d %d", &n, &c) == 2; ){ 8 | LL t; 9 | if(n) { 10 | t = (c / (2 * n)); 11 | while(f(t + 1, c, n) > f(t, c, n)) t++; 12 | } 13 | printf("%d\n", n ? int(t) : 0); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /la/4215.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; 4 | while(scanf("%d",&n) && n) printf("%d\n",n*(n+1)*(2*n+1)/6); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /la/4365.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int dp[11][11]; 5 | for(int i = 0; i < 11; ++i) for(int j = 0; j < 11; ++j) 6 | dp[i][j] = (i ? dp[i - 1][j] : 0) + (j ? dp[i][j - 1] : 0) + (!i && !j); 7 | for(int a, b; scanf("%d %d", &a, &b) == 2 && a >= 0; ) 8 | if(dp[a][b] == a + b) printf("%d+%d=%d\n", a, b, a + b); 9 | else printf("%d+%d!=%d\n", a, b, a + b); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /la/4368.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[1048576]; 4 | int main(void){ 5 | for(int n, k; scanf("%d %d", &n, &k) == 2 && (n || k); ){ 6 | dp[1] = 0; 7 | for(int i = 2; i <= n; ++i) 8 | dp[i] = (dp[i - 1] + k) % i; 9 | printf("%d %d %d\n", n, k, 1 + dp[n]); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /la/4552.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int z; scanf("%d", &z); 5 | for(int c; scanf("%d", &c) == 1; ){ 6 | int f, n, s, t; 7 | f = s = t = -1; 8 | for(int i = 0; i < 10; ++i){ 9 | scanf("%d", &n); 10 | if(n >= f) t = s, s = f, f = n; 11 | else if(n >= s) t = s, s = n; 12 | else if(n >= t) t = n; 13 | } 14 | printf("%d %d\n", c, t); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /la/4556.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | char number[128]; 6 | int main(void){ 7 | int z; scanf("%d", &z); 8 | for(int c; scanf("%d %s", &c, number) == 2; ){ 9 | if(next_permutation(number, number + strlen(number))) 10 | printf("%d %s\n", c, number); 11 | else printf("%d BIGGEST\n", c); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /la/4810.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char line[2048]; 6 | int main(void){ 7 | while(fgets(line, 2048, stdin) && line[0] != '*'){ 8 | char *p = strtok(line, " "); 9 | char letter = tolower(line[0]); 10 | while(p != NULL){ 11 | if(tolower(*p) != letter) break; 12 | p = strtok(NULL, " "); 13 | } 14 | if(p == NULL) puts("Y"); 15 | else puts("N"); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /la/4820.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const double lim = 1 - (1e-9); 4 | int main(void){ 5 | for(int n; scanf("%d", &n) == 1; ){ 6 | if(n == 0) { puts("0"); continue; } 7 | int cnt = 1; double w, z; scanf("%lf", &w); 8 | for(int i = 1; i < n; ++i){ 9 | scanf("%lf", &z); 10 | if(z - w > lim) w = z, ++cnt; 11 | } 12 | printf("%d\n", cnt); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /la/4825.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n, s; scanf("%d %d", &n, &s) == 2; ) printf("%d\n", s / (n + 1)); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /leetcode/00001_two_sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | index = {val: index for index, val in enumerate(nums)} 4 | for i in range(len(nums)): 5 | x = index.get(target - nums[i]) 6 | if x != i and x is not None: 7 | return [i, x] 8 | return [-1, -1] -------------------------------------------------------------------------------- /leetcode/00007_reverse_integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverse(self, x: int) -> int: 3 | negative = x < 0 4 | x = -x if negative else x 5 | res = 0 6 | while x: 7 | res = res * 10 + (x % 10) 8 | x = x // 10 9 | res = -res if negative else res 10 | return res if -2**31 <= res and res <= 2**31 - 1 else 0 -------------------------------------------------------------------------------- /leetcode/00009_palindrome_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | if x < 0: 4 | return False 5 | return str(x) == str(x)[::-1] -------------------------------------------------------------------------------- /leetcode/00014_longest_common_prefix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestCommonPrefix(self, strs: List[str]) -> str: 3 | if not strs: 4 | return "" 5 | lcp = strs[0] 6 | for s in strs: 7 | while lcp: 8 | if s.startswith(lcp): 9 | break 10 | lcp = lcp[:-1] 11 | return lcp -------------------------------------------------------------------------------- /leetcode/00020_valid_parentheses.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValid(self, s: str) -> bool: 3 | expect = [] 4 | for c in s: 5 | if c == '(': 6 | expect.append(')') 7 | elif c == '[': 8 | expect.append(']') 9 | elif c == '{': 10 | expect.append('}') 11 | elif len(expect) == 0 or expect.pop() != c: 12 | return False 13 | return len(expect) == 0 -------------------------------------------------------------------------------- /leetcode/00026_remove_duplicates_from_sorted_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | if len(nums) <= 1: 4 | return len(nums) 5 | left, right = 0, 0 6 | while right < len(nums): 7 | if nums[right] != nums[left]: 8 | nums[left + 1] = nums[right] 9 | left += 1 10 | right += 1 11 | return left + 1 -------------------------------------------------------------------------------- /leetcode/00027_remove_element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeElement(self, nums: List[int], val: int) -> int: 3 | index = 0 4 | for i in range(len(nums)): 5 | if nums[i] != val: 6 | nums[i], nums[index] = nums[index], nums[i] 7 | index += 1 8 | return index -------------------------------------------------------------------------------- /leetcode/00028_implement_strstr.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def strStr(self, haystack: str, needle: str) -> int: 3 | if not needle: 4 | return 0 5 | for start in range(len(haystack) - len(needle) + 1): 6 | if haystack[start:].startswith(needle): 7 | return start 8 | return -1 -------------------------------------------------------------------------------- /leetcode/00035_search_insert_position.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchInsert(self, nums: List[int], target: int) -> int: 3 | left = 0 4 | right = len(nums) 5 | while left < right: 6 | mid = (left + right) >> 1 7 | if nums[mid] < target: 8 | left = mid + 1 9 | else: 10 | right = mid 11 | return left -------------------------------------------------------------------------------- /leetcode/00042_trapping_rain_water.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trap(self, height: List[int]) -> int: 3 | base_area = sum(height) 4 | l = height 5 | r = height[::-1] 6 | n = len(height) 7 | lmax, rmax = 0, 0 8 | for i in range(n): 9 | lmax = l[i] = max(l[i], lmax) 10 | rmax = r[i] = max(r[i], rmax) 11 | return sum(map(min, zip(l, r[::-1]))) - base_area 12 | 13 | -------------------------------------------------------------------------------- /leetcode/00048_rotate_image.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, matrix: List[List[int]]) -> None: 3 | """ 4 | Do not return anything, modify matrix in-place instead. 5 | """ 6 | n = len(matrix) 7 | for i in range(n): 8 | for j in range(i + 1, n): 9 | matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] 10 | for i in range(n): 11 | matrix[i].reverse() -------------------------------------------------------------------------------- /leetcode/00049_group_anagrams.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def groupAnagrams(self, strs: List[str]) -> List[List[str]]: 3 | result = collections.defaultdict(list) 4 | for s in strs: 5 | result[tuple(sorted(s))].append(s) 6 | return result.values() -------------------------------------------------------------------------------- /leetcode/00050_powx_n.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myPow(self, x: float, n: int) -> float: 3 | power, absx = abs(n), abs(x) 4 | res = 1 5 | while power: 6 | if power & 1: 7 | res *= absx 8 | absx = absx * absx 9 | power = power >> 1 10 | if (x < 0) and (n & 1): 11 | res = -res 12 | if n < 0: 13 | res = 1 / res 14 | return res -------------------------------------------------------------------------------- /leetcode/00053_maximum_subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSubArray(self, nums: List[int]) -> int: 3 | best = float('-inf') 4 | current = 0 5 | for n in nums: 6 | current += n 7 | if current > best: 8 | best = current 9 | if current < 0: 10 | current = 0 11 | return best -------------------------------------------------------------------------------- /leetcode/00055_jump_game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | max_reached = 0 4 | for i in range(len(nums)): 5 | if i > max_reached: 6 | return False 7 | max_reached = max(max_reached, i + nums[i]) 8 | return True -------------------------------------------------------------------------------- /leetcode/00058_length_of_last_word.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | parts = s.split() 4 | return len(parts[-1]) if parts else 0 -------------------------------------------------------------------------------- /leetcode/00058_length_of_last_word.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn length_of_last_word(s: String) -> i32 { 3 | s.split_whitespace().last().unwrap().len() as i32 4 | } 5 | } 6 | 7 | -------------------------------------------------------------------------------- /leetcode/00062_unique_paths.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniquePaths(self, m: int, n: int) -> int: 3 | if m <= 0 or n <= 0: 4 | return 0 5 | ways = 1 6 | moves = m + n - 2 7 | stop = min(m, n) - 1 8 | for i in range(stop): 9 | ways *= moves - i 10 | ways //= i + 1 11 | return ways -------------------------------------------------------------------------------- /leetcode/00066_plus_one.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def plusOne(self, digits: List[int]) -> List[int]: 3 | pos = len(digits) - 1 4 | carry = 1 5 | while pos > -1 and carry: 6 | carry = carry + digits[pos] 7 | digits[pos] = carry % 10 8 | carry //= 10 9 | pos -= 1 10 | if carry: 11 | digits.insert(0, carry) 12 | return digits -------------------------------------------------------------------------------- /leetcode/00069_sqrtx.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mySqrt(self, x: int) -> int: 3 | if x < 2: 4 | return x 5 | left = 0 6 | right = x 7 | while left < right: 8 | mid = (left + right) >> 1 9 | if mid*mid <= x: 10 | left = mid + 1 11 | else: 12 | right = mid 13 | return left - 1 -------------------------------------------------------------------------------- /leetcode/00070_climbing_stairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def climbStairs(self, n: int) -> int: 3 | a, b = 1, 1 4 | for i in range(n - 1): 5 | a, b = b, a + b 6 | return b -------------------------------------------------------------------------------- /leetcode/00089_gray_code.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def grayCode(self, n: int) -> List[int]: 3 | return [x ^ (x >> 1) for x in range(1< int: 10 | return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) if root else 0 -------------------------------------------------------------------------------- /leetcode/001128_number_of_equivalent_domino_pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int: 3 | dominoes = (10 * a + b if a < b else 10 * b + a for a, b in dominoes) 4 | return sum(x * (x - 1) >> 1 for x in collections.Counter(dominoes).values()) -------------------------------------------------------------------------------- /leetcode/00118_pascals_triangle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def generate(self, numRows: int) -> List[List[int]]: 3 | answer, r = [], [] 4 | for i in range(numRows): 5 | r = [x + (r[j - 1] if j else 0) for j, x in enumerate(r)] + [1] 6 | answer.append(r) 7 | return answer -------------------------------------------------------------------------------- /leetcode/00122_best_time_to_buy_and_sell_stock_ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | answer = 0 4 | prev = prices[0] 5 | for price in prices: 6 | if price > prev: 7 | answer += price - prev 8 | prev = price 9 | return answer -------------------------------------------------------------------------------- /leetcode/00125_valid_palindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s: str) -> bool: 3 | s = list(map(lambda x: x.lower(), filter(lambda c: c.isalpha() or c.isdigit(), s))) 4 | return all(c == s[~i] for i, c in enumerate(s)) -------------------------------------------------------------------------------- /leetcode/00136_single_number.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def singleNumber(self, nums: List[int]) -> int: 4 | return next(k for k, v in Counter(nums).items() if v == 1) -------------------------------------------------------------------------------- /leetcode/00169_majority_element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def majorityElement(self, nums: List[int]) -> int: 3 | count, answer = 0, 0 4 | for n in nums: 5 | if not count: 6 | answer, count = n, 1 7 | else: 8 | count += (n == answer) - (n != answer) 9 | return answer -------------------------------------------------------------------------------- /leetcode/00172_factorial_trailing_zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trailingZeroes(self, n: int) -> int: 3 | answer, div = 0, 5 4 | while div <= n: 5 | answer += n // div 6 | div *= 5 7 | return answer -------------------------------------------------------------------------------- /leetcode/00181_employees_earning_more_than_their_managers.py: -------------------------------------------------------------------------------- 1 | SELECT E.Name as 'Employee' FROM Employee AS E JOIN Employee AS M WHERE E.ManagerId = M.Id AND E.Salary > M.Salary -------------------------------------------------------------------------------- /leetcode/00189_rotate_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | n = len(nums) 4 | start = 0 5 | start, n = 0, len(nums) 6 | k %= n 7 | while k: 8 | j = n - k 9 | for i in range(start, start + k): 10 | nums[i], nums[j] = nums[j], nums[i] 11 | j += 1 12 | start += k 13 | k %= (n - start) -------------------------------------------------------------------------------- /leetcode/00191_number_of_1_bits.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hammingWeight(self, n): 3 | ans = 0 4 | while n: 5 | ans += n & 1 6 | n >>= 1 7 | return ans -------------------------------------------------------------------------------- /leetcode/00192_word_frequency.sh: -------------------------------------------------------------------------------- 1 | # Read from the file words.txt and output the word frequency list to stdout. 2 | sed 's/ /\n/g;' words.txt | sed '/^\s*$/D' | sort | uniq -c | sort -nr | awk '{print $2 " " $1}' -------------------------------------------------------------------------------- /leetcode/00197_rising_temperature.sql: -------------------------------------------------------------------------------- 1 | SELECT b.Id 2 | FROM Weather AS a JOIN Weather AS b 3 | ON datediff(b.RecordDate, a.RecordDate) = 1 4 | WHERE b.Temperature > a.Temperature -------------------------------------------------------------------------------- /leetcode/00201_bitwise_and_of_numbers_range.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rangeBitwiseAnd(self, m: int, n: int) -> int: 3 | answer = m & n 4 | isize = n - m + 1 5 | unset = 1 6 | while unset < isize: 7 | answer &= ~unset 8 | unset <<= 1 9 | return answer -------------------------------------------------------------------------------- /leetcode/00202_happy_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def squareSum(self, n: int) -> int: 3 | return 0 if n == 0 else self.squareSum(n // 10) + (n % 10) ** 2 4 | 5 | def isHappy(self, n: int, seen: set = None) -> bool: 6 | if seen is None: 7 | seen = set() 8 | if n in seen: 9 | return False 10 | seen.add(n) 11 | return n == 1 or self.isHappy(self.squareSum(n), seen) -------------------------------------------------------------------------------- /leetcode/00206_reverse_linked_list.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn reverse_list(head: Option>) -> Option> { 3 | let (mut prev, mut head) = (None, head); 4 | while let Some(mut node) = head { 5 | head = node.next; 6 | node.next = prev; 7 | prev = Some(node) 8 | } 9 | prev 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /leetcode/00217_contains_duplicate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def containsDuplicate(self, nums: List[int]) -> bool: 3 | return len(set(nums)) != len(nums) -------------------------------------------------------------------------------- /leetcode/00231_power_of_two.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn is_power_of_two(n: i32) -> bool { 3 | n > 0 && (n & (n - 1) == 0) 4 | } 5 | } 6 | 7 | -------------------------------------------------------------------------------- /leetcode/00238_product_of_array_except_self.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def productExceptSelf(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | prod = [1] * n 5 | for i in range(1, n): 6 | prod[i] = prod[i - 1] * nums[i - 1] 7 | acc = 1 8 | for i in range(n - 1, -1, -1): 9 | prod[i] = prod[i] * acc 10 | acc *= nums[i] 11 | return prod -------------------------------------------------------------------------------- /leetcode/00239_sliding_window_maximum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: 3 | if not nums: 4 | return [] 5 | window = sorted(nums[:k]) 6 | maxes = [window[-1]] 7 | for i, n in enumerate(nums[k:]): 8 | window.remove(nums[i]) 9 | bisect.insort(window, n) 10 | maxes.append(window[-1]) 11 | return maxes -------------------------------------------------------------------------------- /leetcode/00240_search_a_2d_matrix_ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchMatrix(self, matrix, target): 3 | if not matrix or not matrix[0]: 4 | return False 5 | for row in matrix: 6 | if row[0] > target: 7 | break 8 | index = bisect.bisect(row, target) 9 | if index and row[index - 1] == target: 10 | return True 11 | return False -------------------------------------------------------------------------------- /leetcode/00242_valid_anagram.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAnagram(self, s: str, t: str) -> bool: 3 | return Counter(s) == Counter(t) -------------------------------------------------------------------------------- /leetcode/00268_missing_number.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn missing_number(nums: Vec) -> i32 { 3 | let len = nums.len() as i32; 4 | len * (len + 1) / 2 - nums.iter().fold(0, |sum, n| sum + n) 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /leetcode/00278_first_bad_version.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstBadVersion(self, n): 3 | left, right = 1, n + 1 4 | while left < right: 5 | mid = (left + right) >> 1 6 | if not isBadVersion(mid): 7 | left = mid + 1 8 | else: 9 | right = mid 10 | return left -------------------------------------------------------------------------------- /leetcode/00283_move_zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def moveZeroes(self, n: List[int]) -> None: 3 | zero = None 4 | for i, x in enumerate(n): 5 | if (zero is None) ^ (x != 0): 6 | if x: 7 | n[i], n[zero] = n[zero], n[i] 8 | while n[zero]: 9 | zero += 1 10 | else: 11 | zero = i -------------------------------------------------------------------------------- /leetcode/00287_find_the_duplicate_number.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn find_duplicate(nums: Vec) -> i32 { 3 | let mut c = vec![0; nums.len()]; 4 | nums.iter().for_each(|n| c[*n as usize] += 1); 5 | c.iter().enumerate().find(|t| *t.1 > 1).unwrap().0 as i32 6 | } 7 | } 8 | 9 | -------------------------------------------------------------------------------- /leetcode/00303_range_sum_query_immutable.py: -------------------------------------------------------------------------------- 1 | class NumArray: 2 | def __init__(self, nums: List[int]): 3 | self.nums = nums 4 | for i in range(1, len(nums)): 5 | nums[i] += nums[i - 1] 6 | 7 | def sumRange(self, i: int, j: int) -> int: 8 | return self.nums[j] - (self.nums[i - 1] if i else 0) -------------------------------------------------------------------------------- /leetcode/00318_maximum_product_of_word_lengths.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProduct(self, words: List[str]) -> int: 3 | N = len(words) 4 | chars = list(map(set, words)) 5 | lens = list(map(len, words)) 6 | m = 0 7 | for i in range(N): 8 | for j in range(i + 1, N): 9 | prod = lens[i] * lens[j] 10 | if prod > m and not len(chars[i] & chars[j]): 11 | m = prod 12 | return m -------------------------------------------------------------------------------- /leetcode/00331_verify_preorder_serialization_of_a_binary_tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValidSerialization(self, preorder: str) -> bool: 3 | count = 0 4 | for node in preorder.split(','): 5 | if count == -1: 6 | return False 7 | count += (node != '#') - (node == '#') 8 | return count == -1 -------------------------------------------------------------------------------- /leetcode/00342_power_of_four.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfFour(self, num: int) -> bool: 3 | while num: 4 | if num == 1: 5 | return True 6 | if num & 3: 7 | break 8 | num >>= 2 9 | return False -------------------------------------------------------------------------------- /leetcode/00344_reverse_string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseString(self, s: List[str]) -> None: 3 | """ 4 | Do not return anything, modify s in-place instead. 5 | """ 6 | left, right = 0, len(s) - 1 7 | while left < right: 8 | s[left], s[right] = s[right], s[left] 9 | left += 1 10 | right -= 1 -------------------------------------------------------------------------------- /leetcode/00347_top_k_frequent_elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def topKFrequent(self, nums: List[int], k: int) -> List[int]: 3 | buckets = [[] for _ in nums] 4 | for n, f in collections.Counter(nums).items(): 5 | buckets[-f].append(n) 6 | q = collections.deque() 7 | for b in filter(None, buckets): 8 | q.extend(b[:k-len(q)]) 9 | if len(q) == k: 10 | break 11 | return q -------------------------------------------------------------------------------- /leetcode/00349_intersection_of_two_arrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 | return list(set(nums1) & set(nums2)) -------------------------------------------------------------------------------- /leetcode/00367_valid_perfect_square.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPerfectSquare(self, num: int) -> bool: 3 | guess = num 4 | while guess * guess > num: 5 | guess = (guess + num // guess) >> 1 6 | return guess * guess == num -------------------------------------------------------------------------------- /leetcode/00371_sum_of_two_integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSum(self, a: int, b: int) -> int: 3 | mask = 0xffffffff 4 | maxv = 0x0fffffff 5 | while b: 6 | a, b = (a ^ b) & mask, ((a & b) << 1) & mask 7 | return a if a <= maxv else ~(a ^ mask) -------------------------------------------------------------------------------- /leetcode/00377_combination_sum_iv.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum4(self, nums: List[int], target: int) -> int: 3 | target += 1 4 | dp = [0] * (target) 5 | dp[target - 1] = 1 6 | for i in range(target - 1, -1, -1): 7 | for n in nums: 8 | if n <= i: 9 | dp[i - n] += dp[i] 10 | return dp[0] -------------------------------------------------------------------------------- /leetcode/00378_kth_smallest_element_in_a_sorted_matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kthSmallest(self, matrix: List[List[int]], k: int) -> int: 3 | low, high = matrix[0][0], matrix[-1][-1] 4 | while low < high: 5 | mid = (low + high) // 2 6 | if sum(bisect.bisect(row, mid) for row in matrix) < k: 7 | low = mid + 1 8 | else: 9 | high = mid 10 | return low -------------------------------------------------------------------------------- /leetcode/00383_ransom_note.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canConstruct(self, ransomNote: str, magazine: str) -> bool: 3 | mag_count = collections.Counter(magazine) 4 | return all(v <= mag_count[k] for k, v in collections.Counter(ransomNote).items()) -------------------------------------------------------------------------------- /leetcode/00387_first_unique_character_in_a_string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstUniqChar(self, s: str) -> int: 3 | cnt = Counter(s) 4 | return s.find(next((c for c in cnt if cnt[c] == 1), '$')) -------------------------------------------------------------------------------- /leetcode/00389_find_the_difference.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def findTheDifference(self, s: str, t: str) -> str: 5 | diff = Counter(t) - Counter(s) 6 | return next(iter(diff)) 7 | -------------------------------------------------------------------------------- /leetcode/00402_remove_k_digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeKdigits(self, num: str, k: int) -> str: 3 | s = deque() 4 | for n in num: 5 | while s and k and n < s[-1]: 6 | s.pop() 7 | k -= 1 8 | s.append(n) 9 | while k: 10 | s.pop() 11 | k -= 1 12 | return ''.join(s).lstrip('0') or '0' -------------------------------------------------------------------------------- /leetcode/00414_third_maximum_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def thirdMax(self, nums: List[int]) -> int: 3 | top3 = set() 4 | for n in nums: 5 | top3.add(n) 6 | if len(top3) == 4: 7 | top3.remove(min(top3)) 8 | return min(top3) if len(top3) == 3 else max(top3) -------------------------------------------------------------------------------- /leetcode/00441_arranging_coins.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | ''' 3 | x * (x + 1) / 2 <= n 4 | x**2 + x - 2 * n <= 0 5 | x = floor((-1 + sqrt(1 + 8n))/2) 6 | ''' 7 | def arrangeCoins(self, n: int) -> int: 8 | return int(.5 * ((1 + (n << 3))**.5 - 1)) -------------------------------------------------------------------------------- /leetcode/00442_find_all_duplicates_in_an_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDuplicates(self, nums: List[int]) -> List[int]: 3 | ans = [] 4 | for n in nums: 5 | i = abs(n) - 1 6 | nums[i] = -nums[i] 7 | if nums[i] > 0: 8 | ans.append(i + 1) 9 | return ans -------------------------------------------------------------------------------- /leetcode/00452_minimum_number_of_arrows_to_burst_balloons.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn find_min_arrow_shots(mut points: Vec>) -> i32 { 3 | points.sort_unstable(); 4 | points.iter().fold((1, points[0][1]), |(arrows, reach), p| { 5 | if p[0] > reach { (arrows + 1, p[1]) } 6 | else { (arrows, reach.min(p[1])) } 7 | }).0 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /leetcode/00455_assign_cookies.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findContentChildren(self, g: List[int], s: List[int]) -> int: 3 | g.sort() 4 | gi = 0 5 | for size in sorted(s): 6 | gi += size >= g[gi] 7 | if gi == len(g): 8 | break 9 | return gi -------------------------------------------------------------------------------- /leetcode/00476_number_complement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findComplement(self, num: int) -> int: 3 | pow2 = 2 4 | while pow2 <= num: 5 | pow2 <<= 1 6 | return pow2 - num - 1 -------------------------------------------------------------------------------- /leetcode/00478_generate_random_point_in_a_circle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def __init__(self, radius: float, x_center: float, y_center: float): 3 | self.r = radius 4 | self.x = x_center 5 | self.y = y_center 6 | 7 | def randPoint(self) -> List[float]: 8 | r = self.r * math.sqrt(random.random()) 9 | a = math.pi * 2 * random.random() 10 | return [self.x + r * math.cos(a), self.y + r * math.sin(a)] -------------------------------------------------------------------------------- /leetcode/00482_license_key_formatting.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def licenseKeyFormatting(self, S: str, K: int) -> str: 3 | tmp = ''.join(S.upper().split('-')) 4 | mod = len(tmp) % K 5 | return '-'.join(filter(None, [tmp[:mod]] + list(tmp[i:i + K] for i in range(mod, len(tmp), K)))) -------------------------------------------------------------------------------- /leetcode/00485_max_consecutive_ones.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 3 | ans = count = 0 4 | for n in nums: 5 | if n == 0: 6 | ans = max(ans, count) 7 | count = 0 8 | else: 9 | count += 1 10 | return max(ans, count) -------------------------------------------------------------------------------- /leetcode/00495_teemo_attacking.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: 3 | start, end = -1, -1 4 | poisoned = 0 5 | for t in timeSeries: 6 | if t >= end: 7 | poisoned += end - start 8 | start = t 9 | end = t + duration 10 | if end != -1: 11 | poisoned += end - start 12 | return poisoned 13 | -------------------------------------------------------------------------------- /leetcode/00496_next_greater_element_i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 | ans = [] 4 | for n in nums1: 5 | index = nums2.index(n) 6 | for j in range(index, len(nums2)): 7 | if nums2[j] > n: 8 | ans.append(nums2[j]) 9 | break 10 | else: 11 | ans.append(-1) 12 | return ans -------------------------------------------------------------------------------- /leetcode/00500_keyboard_row.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | LINES = list(map(set, ["qwertyuiop", "asdfghjkl", "zxcvbnm"])) 3 | def findWords(self, words: List[str]) -> List[str]: 4 | return [w for w in words if any(len(set(w.lower()) - line) == 0 for line in self.LINES)] -------------------------------------------------------------------------------- /leetcode/00518_coin_change_2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def change(self, amount: int, coins: List[int]) -> int: 3 | amount += 1 4 | dp = [0] * amount 5 | dp[0] = 1 6 | for c in coins: 7 | for i in range(c, amount): 8 | dp[i] += dp[i - c] 9 | return dp[-1] -------------------------------------------------------------------------------- /leetcode/00520_detect_capital.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def detectCapitalUse(self, word: str) -> bool: 3 | caps = lambda c: 'A' <= c <= 'Z' 4 | cnt = len(list(filter(caps, word))) 5 | return cnt == 0 or cnt == len(word) or cnt == 1 and caps(word[0]) -------------------------------------------------------------------------------- /leetcode/00528_random_pick_with_weight.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def __init__(self, w: List[int]): 3 | self.w = w 4 | for i in range(1, len(w)): 5 | w[i] += w[i - 1] 6 | self.total = w[-1] 7 | 8 | def pickIndex(self) -> int: 9 | return bisect.bisect(self.w, int(self.total * random.random())) -------------------------------------------------------------------------------- /leetcode/00537_complex_number_multiplication.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def parse(self, s: str) -> (int, int): 3 | return map(int, s[:-1].split('+')) 4 | def complexNumberMultiply(self, a: str, b: str) -> str: 5 | a1, b1 = self.parse(a) 6 | a2, b2 = self.parse(b) 7 | return f"{a1*a2-b1*b2}+{a2*b1+b2*a1}i" -------------------------------------------------------------------------------- /leetcode/00540_single_element_in_a_sorted_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNonDuplicate(self, nums: List[int]) -> int: 3 | l, r = 0, len(nums) - 1 4 | while l < r: 5 | mid = (l + r) >> 1 6 | if nums[mid] == nums[mid ^ 1]: 7 | l = mid + 1 8 | else: 9 | r = mid 10 | return nums[l] -------------------------------------------------------------------------------- /leetcode/00551_student_attendance_record_i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkRecord(self, s: str) -> bool: 3 | return s.count('A') <= 1 and 'LLL' not in s -------------------------------------------------------------------------------- /leetcode/00557_reverse_words_in_a_string_iii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | return ' '.join(map(lambda part: part[::-1], s.split(' '))) -------------------------------------------------------------------------------- /leetcode/00560_subarray_sum_equals_k.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def subarraySum(self, nums: List[int], k: int) -> int: 4 | answer = 0 5 | count = Counter([0]) 6 | s = 0 7 | for n in nums: 8 | s += n 9 | answer += count[s - k] 10 | count[s] += 1 11 | return answer -------------------------------------------------------------------------------- /leetcode/00565_array_nesting.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrayNesting(self, nums: List[int]) -> int: 3 | max_count = 0 4 | for index in range(len(nums)): 5 | count = 0 6 | while nums[index] is not None: 7 | tmp = nums[index] 8 | nums[index] = None 9 | index = tmp 10 | count += 1 11 | max_count = max(max_count, count) 12 | return max_count -------------------------------------------------------------------------------- /leetcode/00590_n_ary_tree_postorder_traversal.py: -------------------------------------------------------------------------------- 1 | """ 2 | # Definition for a Node. 3 | class Node: 4 | def __init__(self, val, children): 5 | self.val = val 6 | self.children = children 7 | """ 8 | class Solution: 9 | def postorder(self, root: 'Node') -> List[int]: 10 | if not root: 11 | return [] 12 | return list(itertools.chain(*[self.postorder(c) for c in root.children])) + [root.val] -------------------------------------------------------------------------------- /leetcode/00627_swap_salary.sql: -------------------------------------------------------------------------------- 1 | UPDATE salary SET sex = (CASE WHEN sex = 'f' THEN 'm' WHEN sex ='m' THEN 'f' END) -------------------------------------------------------------------------------- /leetcode/00650_2_keys_keyboard.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, n: int) -> int: 3 | n += 1 4 | dp = [1000] * n 5 | dp[1] = 0 6 | for i in range(1, n): 7 | inc = 2 8 | for j in range(2 * i, n, i): 9 | dp[j] = min(dp[j], dp[i] + inc) 10 | inc += 1 11 | return dp[n - 1] -------------------------------------------------------------------------------- /leetcode/00657_robot_return_to_origin.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def judgeCircle(self, moves: str) -> bool: 3 | c = collections.Counter(moves) 4 | return c['U'] == c['D'] and c['L'] == c['R'] -------------------------------------------------------------------------------- /leetcode/00668_kth_smallest_number_in_multiplication_table.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthNumber(self, m: int, n: int, k: int) -> int: 3 | class SmallerThan: 4 | def __getitem__(self, x): 5 | return sum(min(x // row, n) for row in range(1, m + 1)) 6 | return bisect.bisect_left(SmallerThan(), k, 1, m * n) -------------------------------------------------------------------------------- /leetcode/00674_longest_continuous_increasing_subsequence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLengthOfLCIS(self, nums: List[int]) -> int: 3 | solution, size = 0, 1 4 | for i in range(len(nums)): 5 | if i and nums[i] > nums[i - 1]: 6 | size += 1 7 | else: 8 | size = 1 9 | solution = max(solution, size) 10 | return solution -------------------------------------------------------------------------------- /leetcode/00677_map_sum_pairs.py: -------------------------------------------------------------------------------- 1 | class MapSum: 2 | def __init__(self): 3 | self.values = {} 4 | 5 | def insert(self, key: str, val: int) -> None: 6 | self.values[key] = val 7 | 8 | def sum(self, prefix: str) -> int: 9 | return sum(v for k, v in self.values.items() if k.startswith(prefix)) -------------------------------------------------------------------------------- /leetcode/00678_valid_parenthesis_string.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn check_valid_string(s: String) -> bool { 3 | let mut low = 0; 4 | let mut high = 0; 5 | for c in s.chars() { 6 | low += 1 - 2 * (c != '(') as i8; 7 | high += 1 - 2 * (c == ')') as i8; 8 | if high < 0 { return false; } 9 | low = low.max(0); 10 | } 11 | low == 0 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /leetcode/00709_to_lower_case.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def toLowerCase(self, s: str) -> str: 3 | return s.lower() -------------------------------------------------------------------------------- /leetcode/00771_jewels_and_stones.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numJewelsInStones(self, J: str, S: str) -> int: 3 | return len([c for c in S if c in J]) -------------------------------------------------------------------------------- /leetcode/00780_reaching_points.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool: 3 | while sx < tx and sy < ty: 4 | if ty > tx: ty %= tx 5 | else: tx %= ty 6 | return ( 7 | sx <= tx and (tx - sx) % sy == 0 and 8 | sy <= ty and (ty - sy) % sx == 0 9 | ) -------------------------------------------------------------------------------- /leetcode/00824_goat_latin.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def toGoatLatin(self, S: str) -> str: 3 | transform = lambda s, a: (s if s[0].lower() in 'aeiou' else s[1:] + s[0]) + 'ma' + 'a' * a 4 | return ' '.join(transform(word, i) for i, word in enumerate(S.split(), 1)) -------------------------------------------------------------------------------- /leetcode/00830_positions_of_large_groups.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largeGroupPositions(self, S: str) -> List[List[int]]: 3 | left = 0 4 | answer = collections.deque() 5 | for c, g in itertools.groupby(S): 6 | cnt = len(list(g)) 7 | if cnt >= 3: 8 | answer.append((left, left + cnt - 1)) 9 | left += cnt 10 | return answer -------------------------------------------------------------------------------- /leetcode/00844_backspace_string_compare.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def backspaceCompare(self, S: str, T: str) -> bool: 3 | def process(s: str): 4 | r = [] 5 | for c in s: 6 | if c != '#': 7 | r.append(c) 8 | elif r: 9 | r.pop() 10 | return ''.join(r) 11 | return process(S) == process(T) -------------------------------------------------------------------------------- /leetcode/00848_shifting_letters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def shiftingLetters(self, S: str, shifts: List[int]) -> str: 3 | answer = ['a'] * len(S) 4 | orda = ord('a') 5 | total_shifts = 0 6 | for i in range(len(S) - 1, -1, -1): 7 | total_shifts = (total_shifts + shifts[i]) % 26 8 | answer[i] = chr(orda + (ord(S[i]) - orda + total_shifts) % 26) 9 | return ''.join(answer) -------------------------------------------------------------------------------- /leetcode/00867_transpose_matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def transpose(self, A: List[List[int]]) -> List[List[int]]: 3 | return [[A[i][j] for i in range(len(A))] for j in range(len(A[0]))] -------------------------------------------------------------------------------- /leetcode/00876_middle_of_the_linked_list.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def middleNode(self, head: ListNode) -> ListNode: 3 | count, node = 0, head 4 | while node: 5 | count += 1 6 | node = node.next 7 | count >>= 1 8 | goal, node = 0, head 9 | while goal < count: 10 | goal += 1 11 | node = node.next 12 | return node -------------------------------------------------------------------------------- /leetcode/00878_nth_magical_number.py: -------------------------------------------------------------------------------- 1 | import bisect, math 2 | 3 | class Solution: 4 | def nthMagicalNumber(self, n: int, a: int, b: int) -> int: 5 | lcm = (a * b) // math.gcd(a, b) 6 | 7 | class MagicalCount: 8 | def __getitem__(self, z): 9 | return z // a + z // b - z // lcm 10 | 11 | return bisect.bisect_left(MagicalCount(), n, 0, 10**14) % (10 ** 9 + 7) 12 | 13 | -------------------------------------------------------------------------------- /leetcode/00896_monotonic_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isMonotonic(self, A: List[int]) -> bool: 3 | L = len(A) 4 | inc = all(A[i] >= A[i - 1] for i in range(1, L)) 5 | dec = all(A[i] <= A[i - 1] for i in range(1, L)) 6 | return inc or dec -------------------------------------------------------------------------------- /leetcode/00901_online_stock_span.py: -------------------------------------------------------------------------------- 1 | class StockSpanner: 2 | def __init__(self): 3 | self.stack = deque() 4 | 5 | def next(self, price: int) -> int: 6 | count = 1 7 | while self.stack and self.stack[-1][0] <= price: 8 | count += self.stack.pop()[1] 9 | self.stack.append((price, count)) 10 | return count -------------------------------------------------------------------------------- /leetcode/00908_smallest_range_i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestRangeI(self, A: List[int], K: int) -> int: 3 | return max(0, max(A) - min(A) - 2 * K) -------------------------------------------------------------------------------- /leetcode/00922_sort_array_by_parity_ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortArrayByParityII(self, A: List[int]) -> List[int]: 3 | odd = 1 4 | for even in range(0, len(A), 2): 5 | if A[even] & 1: 6 | while A[odd] & 1: odd += 2 7 | A[even], A[odd] = A[odd], A[even] 8 | even += 2 9 | return A -------------------------------------------------------------------------------- /leetcode/00925_long_pressed_name.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isLongPressedName(self, name: str, typed: str) -> bool: 3 | i, j = 0, 0 4 | while i < len(name) and j < len(typed): 5 | if name[i] == typed[j]: 6 | i += 1 7 | j += 1 8 | elif i and typed[j] == name[i - 1]: 9 | j += 1 10 | else: 11 | return False 12 | return i == len(name) -------------------------------------------------------------------------------- /leetcode/00933_number_of_recent_calls.py: -------------------------------------------------------------------------------- 1 | class RecentCounter: 2 | def __init__(self): 3 | self.pings = collections.deque() 4 | 5 | def ping(self, t: int) -> int: 6 | mint = t - 3000 7 | self.pings.append(t) 8 | while self.pings[0] < mint: 9 | self.pings.popleft() 10 | return len(self.pings) -------------------------------------------------------------------------------- /leetcode/00949_largest_time_for_given_digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestTimeFromDigits(self, A: List[int]) -> str: 3 | ans = () 4 | for hl, hr, ml, mr in itertools.permutations(A): 5 | hours = 10 * hl + hr 6 | mins = 10 * ml + mr 7 | if 0 <= hours <= 23 and 0 <= mins <= 59: 8 | ans = max(ans, (hours, mins)) 9 | return f'{ans[0]:02d}:{ans[1]:02d}' if ans else '' -------------------------------------------------------------------------------- /leetcode/00954_array_of_doubled_pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canReorderDoubled(self, A: List[int]) -> bool: 3 | A = list(map(abs,A)) 4 | A.sort() 5 | count = collections.Counter(A) 6 | for n in A: 7 | if not count[n]: 8 | continue 9 | count[n] -= 1 10 | if count[n << 1]: 11 | count[n << 1] -= 1 12 | continue 13 | return False 14 | return True -------------------------------------------------------------------------------- /leetcode/00977_squares_of_a_sorted_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortedSquares(self, A: List[int]) -> List[int]: 3 | return sorted(map(lambda n: n*n, A)) -------------------------------------------------------------------------------- /leetcode/00991_broken_calculator.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def brokenCalc(self, X: int, Y: int) -> int: 3 | steps = 0 4 | while X < Y: 5 | steps += 1 + (Y & 1) 6 | Y = (Y + (Y & 1)) >> 1 7 | return steps + X - Y -------------------------------------------------------------------------------- /leetcode/00997_find_the_town_judge.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findJudge(self, N: int, trust: List[List[int]]) -> int: 3 | t = collections.Counter() 4 | for a, b in trust: 5 | t[b] += 1 6 | t[a] -= 1 7 | for i in range(1, N + 1): 8 | if t[i] == N - 1: 9 | return i 10 | return -1 -------------------------------------------------------------------------------- /leetcode/01004_max_consecutive_ones_iii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestOnes(self, A: List[int], K: int) -> int: 3 | N = len(A) 4 | best, left = 0, -1 5 | for right, is_one in enumerate(A): 6 | K -= not is_one 7 | if is_one or K > -1: 8 | best = max(best, right - left) 9 | else: 10 | left += 1 11 | while A[left]: 12 | left += 1 13 | return best -------------------------------------------------------------------------------- /leetcode/01006_clumsy_factorial.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clumsy(self, N: int) -> int: 3 | def helper(N: int): 4 | if N >= 3: 5 | return N * (N - 1) // (N - 2) 6 | if N == 2: 7 | return N * (N - 1) 8 | return N 9 | answer = helper(N) 10 | N -= 3 11 | while N > 0: 12 | answer += N 13 | answer -= helper(N - 1) 14 | N -= 4 15 | return answer -------------------------------------------------------------------------------- /leetcode/01009_complement_of_base_10_integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bitwiseComplement(self, N: int) -> int: 3 | pow2 = 2 4 | while pow2 <= N: 5 | pow2 <<= 1 6 | return pow2 - N - 1 -------------------------------------------------------------------------------- /leetcode/01010_pairs_of_songs_with_total_duration_divisible_by_60.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numPairsDivisibleBy60(self, time: List[int]) -> int: 3 | c = collections.Counter(map(lambda t: t % 60, time)) 4 | tri = lambda x: x * (x - 1) // 2 5 | return tri(c[0]) + tri(c[30]) + sum((c[x] * c[60 - x]) for x in range(1, 30)) -------------------------------------------------------------------------------- /leetcode/01035_uncrossed_lines.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: 3 | lena, lenb = len(A), len(B) 4 | dp = [0] * (lenb + 1) 5 | for i, a in enumerate(A): 6 | prev = dp[0] 7 | for j, b in enumerate(B): 8 | tmp = dp[j + 1] 9 | dp[j + 1] = max(dp[j + 1], dp[j], prev + (a == b)) 10 | prev = tmp 11 | return dp[lenb] -------------------------------------------------------------------------------- /leetcode/01046_last_stone_weight.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smash(self, stones: List[int]) -> List[int]: 3 | y = max(stones) 4 | stones.remove(y) 5 | x = max(stones) 6 | stones.remove(x) 7 | if x != y: 8 | stones.append(y - x) 9 | 10 | def lastStoneWeight(self, stones: List[int]) -> int: 11 | while len(stones) > 1: 12 | self.smash(stones) 13 | return stones[0] if stones else 0 -------------------------------------------------------------------------------- /leetcode/01049_last_stone_weight_ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lastStoneWeightII(self, stones: List[int]) -> int: 3 | total = sum(stones) 4 | half = total // 2 5 | dp = [0] * (half + 1) 6 | dp[0] = 1 7 | for s in stones: 8 | for i in range(half, s - 1, -1): 9 | dp[i] |= dp[i - s] 10 | return next(total - 2 * t for t in range(half, -1, -1) if dp[t]) -------------------------------------------------------------------------------- /leetcode/01051_height_checker.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def heightChecker(self, heights: List[int]) -> int: 3 | return sum(a != b for a, b in zip(sorted(heights), heights)) -------------------------------------------------------------------------------- /leetcode/01079_letter_tile_possibilities.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numTilePossibilities(self, tiles: str) -> int: 3 | answer = set() 4 | for N in range(1, len(tiles) + 1): 5 | for j in itertools.permutations(tiles, N): 6 | answer.add(j) 7 | return len(answer) -------------------------------------------------------------------------------- /leetcode/01104_path_in_zigzag_labelled_binary_tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pathInZigZagTree(self, label: int) -> List[int]: 3 | answer = [] 4 | n = 1 5 | while n <= label: 6 | n <<= 1 7 | while label: 8 | answer.append(label) 9 | label = ((n - label - 1) >> 1) + (n >> 2) 10 | n >>= 1 11 | return answer[::-1] -------------------------------------------------------------------------------- /leetcode/01108_defanging_an_ip_address.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def defangIPaddr(self, address: str) -> str: 3 | return '[.]'.join(address.split('.')) -------------------------------------------------------------------------------- /leetcode/01131_maximum_of_absolute_value_expression.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int: 3 | n = len(arr1) 4 | a = [arr1[i] + arr2[i] + i for i in range(n)] 5 | b = [arr1[i] + arr2[i] - i for i in range(n)] 6 | c = [arr1[i] - arr2[i] + i for i in range(n)] 7 | d = [arr1[i] - arr2[i] - i for i in range(n)] 8 | return max(max(x) - min(x) for x in (a,b,c,d)) -------------------------------------------------------------------------------- /leetcode/01137_nth_tribonacci_number.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn tribonacci(n: i32) -> i32 { 3 | let n = n as usize; 4 | let mut ans = vec![1; n + 1]; 5 | ans[0] = 0; 6 | (3..=n).for_each(|i| { ans[i] = ans[i - 1] + ans[i - 2] + ans[i - 3]; }); 7 | ans[n] 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /leetcode/01147_longest_chunked_palindrome_decomposition.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestDecomposition(self, text: str) -> int: 3 | n = len(text) 4 | for i in range(1, 1 + (n >> 1)): 5 | if text[:i] == text[n - i:]: 6 | return 2 + self.longestDecomposition(text[i:n-i]) 7 | return 1 if text else 0 -------------------------------------------------------------------------------- /leetcode/01154_day_of_the_year.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 3 | DAYS_BEFORE = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] 4 | def dayOfYear(self, date: str) -> int: 5 | y, m, d = map(int, date.split('-')) 6 | add_one_for_leap = m > 2 and not y % 4 and (y % 100 != 0 or not y % 400) 7 | return d + self.DAYS_BEFORE[m] + add_one_for_leap -------------------------------------------------------------------------------- /leetcode/01200_minimum_absolute_difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]: 3 | arr.sort() 4 | min_diff = min(arr[i + 1] - x for i, x in enumerate(arr[:-1])) 5 | return [[n, arr[i + 1]] for i, n in enumerate(arr[:-1]) if arr[i + 1] - n == min_diff] -------------------------------------------------------------------------------- /leetcode/01207_unique_number_of_occurrences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueOccurrences(self, arr: List[int]) -> bool: 3 | c = collections.Counter(arr) 4 | return len(c) == len(set(c.values())) -------------------------------------------------------------------------------- /leetcode/01218_longest_arithmetic_subsequence_of_given_difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestSubsequence(self, arr: List[int], difference: int) -> int: 3 | z = collections.defaultdict(int) 4 | for i in arr: 5 | z[i + difference] = z[i] + 1 6 | return max(z.values()) -------------------------------------------------------------------------------- /leetcode/01221_split_a_string_in_balanced_strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def balancedStringSplit(self, s: str) -> int: 3 | balanced, count = 0, 0 4 | for c in s: 5 | if c == 'R': 6 | count += 1 7 | else: 8 | count -= 1 9 | if count == 0: 10 | balanced += 1 11 | return balanced -------------------------------------------------------------------------------- /leetcode/01227_airplane_seat_assignment_probability.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nthPersonGetsNthSeat(self, n: int) -> float: 3 | return 1 - (n != 1) / 2 -------------------------------------------------------------------------------- /leetcode/01228_missing_number_in_arithmetic_progression.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missingNumber(self, arr: List[int]) -> int: 3 | r = (arr[len(arr) - 1] - arr[0]) // len(arr) 4 | expected = arr[0] 5 | for x in arr: 6 | if x != expected: 7 | break 8 | expected += r 9 | return expected -------------------------------------------------------------------------------- /leetcode/01230_toss_strange_coins.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def probabilityOfHeads(self, prob: List[float], target: int) -> float: 3 | dp = [1] + [0] * target 4 | for p in prob: 5 | for h in range(target, -1, -1): 6 | dp[h] = (1 - p) * dp[h] + (p if h else 0) * dp[h - 1] 7 | return dp[target] -------------------------------------------------------------------------------- /leetcode/01232_check_if_it_is_a_straight_line.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkStraightLine(self, coordinates: List[List[int]]) -> bool: 3 | dx, dy = coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1] 4 | for i in range(2, len(coordinates)): 5 | ddx, ddy = coordinates[i][0] - coordinates[0][0], coordinates[i][1] - coordinates[0][1] 6 | if ddx * dy != ddy * dx: 7 | return False 8 | return True -------------------------------------------------------------------------------- /leetcode/01250_check_if_it_is_a_good_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isGoodArray(self, nums: List[int]) -> bool: 3 | def gcd(a, b): 4 | if b == 0: 5 | return a 6 | return gcd(b, a % b) 7 | n = nums[0] 8 | for i in range(1, len(nums)): 9 | n = gcd(n, nums[i]) 10 | return n == 1 -------------------------------------------------------------------------------- /leetcode/01252_cells_with_odd_values_in_a_matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: 3 | matrix = [[0 for _ in range(m)] for _ in range(n)] 4 | for row, col in indices: 5 | for j in range(m): 6 | matrix[row][j] += 1 7 | for i in range(n): 8 | matrix[i][col] += 1 9 | return sum(matrix[i][j] & 1 for i in range(n) for j in range(m)) -------------------------------------------------------------------------------- /leetcode/01276_number_of_burgers_with_no_waste_of_ingredients.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: 3 | jumbo = (tomatoSlices - 2 * cheeseSlices) // 2 4 | small = cheeseSlices - jumbo 5 | return [] if jumbo < 0 or small < 0 or 4 * jumbo + 2 * small != tomatoSlices else [jumbo, small] -------------------------------------------------------------------------------- /leetcode/01295_find_numbers_with_even_number_of_digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findNumbers(self, nums: List[int]) -> int: 3 | def digits(n: int) -> int: 4 | return 1 + (n > 9) + (n > 99) + (n > 999) + (n > 9999) + (n > 99999) 5 | return len(list(filter(lambda n: not digits(n) & 1, nums))) -------------------------------------------------------------------------------- /leetcode/01337_the_k_weakest_rows_in_a_matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: 3 | power = list(map(sum, mat)) 4 | return sorted(range(len(mat)), key=power.__getitem__)[:k] -------------------------------------------------------------------------------- /leetcode/01389_create_target_array_in_the_given_order.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: 3 | result = [] 4 | for index, value in zip(index, nums): 5 | result.insert(index, value) 6 | return result 7 | 8 | -------------------------------------------------------------------------------- /leetcode/01410_html_entity_parser.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | replacements = [ 3 | (""", "\""), 4 | ("&apos", "'"), 5 | (">", ">"), 6 | ("<", "<"), 7 | ("&frasl", "/"), 8 | ("&", "&"), 9 | ] 10 | 11 | def entityParser(self, text: str) -> str: 12 | for find, replace in self.replacements: 13 | text = text.replace(find + ";", replace) 14 | return text -------------------------------------------------------------------------------- /leetcode/01436_destination_city.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def destCity(self, paths: List[List[str]]) -> str: 3 | cities, sources = set(), set() 4 | for a, b in paths: 5 | cities.add(a) 6 | cities.add(b) 7 | sources.add(a) 8 | return (cities - sources).pop() -------------------------------------------------------------------------------- /leetcode/01437_check_if_all_1s_are_at_least_length_k_places_away.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kLengthApart(self, nums: List[int], k: int) -> bool: 3 | x = tuple(i for i, j in enumerate(nums) if j) 4 | return all(x[i] - x[i - 1] > k for i in range(1, len(x))) -------------------------------------------------------------------------------- /leetcode/01450_number_of_students_doing_homework_at_a_given_time.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int: 3 | return sum(startTime[i] <= queryTime <= endTime[i] for i in range(len(startTime))) -------------------------------------------------------------------------------- /leetcode/01451_rearrange_words_in_a_sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrangeWords(self, text: str) -> str: 3 | pieces = text.lower().split(' ') 4 | pieces.sort(key=lambda s: len(s)) 5 | pieces[0] = pieces[0][0].upper() + pieces[0][1:] 6 | return ' '.join(pieces) -------------------------------------------------------------------------------- /leetcode/01460_make_two_arrays_equal_by_reversing_subarrays.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def canBeEqual(self, target: List[int], arr: List[int]) -> bool: 5 | return Counter(target) == Counter(arr) 6 | 7 | -------------------------------------------------------------------------------- /leetcode/01614_maximum_nesting_depth_of_the_parentheses.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn max_depth(s: String) -> i32 { 3 | s.chars().fold((0, 0), |(d, md), c| { 4 | let d = d + (c == '(') as i32 - (c == ')') as i32; 5 | (d, if d > md { d } else { md }) 6 | }).1 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /leetcode/01720_decode_xored_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def decode(self, encoded: List[int], first: int) -> List[int]: 3 | ans = [first] 4 | for n in encoded: 5 | ans.append(ans[-1] ^ n) 6 | return ans 7 | 8 | -------------------------------------------------------------------------------- /leetcode/01903_largest_odd_number_in_string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestOddNumber(self, num: str) -> str: 3 | for i in range(len(num) - 1, -1, -1): 4 | if int(num[i]) & 1: 5 | return num[:i + 1] 6 | return "" 7 | 8 | -------------------------------------------------------------------------------- /leetcode/01915_number_of_wonderful_strings.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn wonderful_substrings(word: String) -> i64 { 3 | let mut cnt = [0; 1 << 10]; 4 | let mut m = 0; 5 | cnt[0] = 1; 6 | word.bytes().fold(0_i64, |ans, b| { 7 | m ^= 1 << (b - b'a') as usize; 8 | cnt[m] += 1; 9 | ans + cnt[m] - 1 + (0..10).fold(0, |r, i| r + cnt[m ^ (1 << i)]) 10 | }) 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /leetcode/02000_reverse_prefix_of_word.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn reverse_prefix(word: String, ch: char) -> String { 3 | if let Some((head, tail)) = word.split_once(ch) { 4 | let ans: String = ch.into(); 5 | return ans + &head.chars().rev().collect::() + tail; 6 | } 7 | word 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /leetcode/02073_time_needed_to_buy_tickets.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn time_required_to_buy(tickets: Vec, k: i32) -> i32 { 3 | let target = tickets[k as usize]; 4 | tickets.iter().enumerate().fold(0, |ans, (index, &t)| { 5 | match index <= k as usize { 6 | true => ans + target.min(t), 7 | false => ans + t.min(target - 1), 8 | } 9 | }) 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /leetcode/02108_find_first_palindromic_string_in_the_array.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn first_palindrome(words: Vec) -> String { 3 | fn is_palindrome(s: &[u8]) -> bool { 4 | s.len() <= 1 || s[0] == s[s.len() - 1] && is_palindrome(&s[1..s.len() - 1]) 5 | } 6 | match words.iter().find(|w| is_palindrome(w.as_bytes())) { 7 | Some(s) => s, 8 | None => "" 9 | }.to_string() 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /leetcode/02125_number_of_laser_beams_in_a_bank.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn number_of_beams(bank: Vec) -> i32 { 3 | let mut ans = 0; 4 | let mut p = 0; 5 | bank.iter().for_each(|row| { 6 | let ones = row.chars().filter(|c| *c == '1').count() as i32; 7 | ans += ones * p; 8 | if ones > 0 { p = ones; } 9 | }); 10 | ans 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /leetcode/02126_destroying_asteroids.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn asteroids_destroyed(mass: i32, mut asteroids: Vec) -> bool { 3 | let mut mass = mass as i64; 4 | asteroids.sort_unstable(); 5 | 6 | for a in asteroids { 7 | let a64 = a as i64; 8 | if a64 > mass { return false; } 9 | mass += a64; 10 | } 11 | true 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /leetcode/02370_longest_ideal_subsequence.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn longest_ideal_string(s: String, k: i32) -> i32 { 3 | let (k, mut dp) = (k as usize, vec![0; 256]); 4 | for b in s.bytes() { 5 | let b = b as usize; 6 | dp[b] = 1 + ((b - k)..=(b + k)).fold(0, |val, i| val.max(dp[i])); 7 | } 8 | *dp.iter().max().unwrap() 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /leetcode/02551_put_marbles_in_bags.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn put_marbles(mut w: Vec, k: i32) -> i64 { 3 | (0..w.len() - 1).for_each(|i| { w[i] += w[i + 1]; }); 4 | w.pop(); 5 | w.sort_unstable(); 6 | (0..k as usize - 1) 7 | .fold(0_i64, |ans, i| ans + w[w.len() - i - 1] as i64 - w[i] as i64) 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /leetcode/02864_maximum_odd_binary_number.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn maximum_odd_binary_number(s: String) -> String { 3 | let ones = s.chars().filter(|c| *c == '1').count(); 4 | "1".repeat(ones - 1) + &"0".repeat(s.len() - ones) + "1" 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /leetcode/02997_minimum_number_of_operations_to_make_array_xor_equal_to_k.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn min_operations(nums: Vec, k: i32) -> i32 { 3 | nums.iter().fold(k, |r, n| r ^ n).count_ones() as i32 4 | } 5 | } 6 | 7 | -------------------------------------------------------------------------------- /leetcode/03099_harshad_number.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn sum_of_the_digits_of_harshad_number(x: i32) -> i32 { 3 | let mut digit_sum = 0; 4 | let mut num = x; 5 | while num > 0 { 6 | digit_sum += num % 10; 7 | num /= 10; 8 | } 9 | if x % digit_sum != 0 { return -1 } 10 | return digit_sum; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /leetcode/03100_water_bottles_ii.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn max_bottles_drunk(num_bottles: i32, num_exchange: i32) -> i32 { 3 | let mut drunk = 0; 4 | let mut cost = num_exchange; 5 | let mut bottles_left = num_bottles; 6 | while bottles_left >= cost { 7 | drunk += cost; 8 | bottles_left -= (cost - 1); 9 | cost += 1; 10 | } 11 | return drunk + bottles_left; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /lib/bigint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ajmarin/coding/417d4a3987126fc458747c391ca18843eee74257/lib/bigint -------------------------------------------------------------------------------- /regional/2006/copa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n, t, npontos, temp; 4 | char nome[32]; 5 | 6 | int main(void) { 7 | while(scanf("%d %d",&t,&n)) { 8 | if(n == 0 && t == 0) break; 9 | npontos = 0; 10 | for(int i=0;i < t; i++) { 11 | scanf("%s %d",nome,&temp); 12 | npontos+=temp; 13 | } 14 | printf("%d\n",3*n-npontos); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /regional/2007/hist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int lastexe[1000001], i, k, n, total; 4 | int main(void){ 5 | while(scanf("%d",&n) && n){ 6 | memset(lastexe,-1,sizeof(lastexe)); 7 | for(i = total = 0; i < n; i++){ 8 | scanf("%d",&k); 9 | if(lastexe[k]!=-1) total += (i-lastexe[k]); 10 | else total += k+i; 11 | lastexe[k] = i; 12 | } 13 | printf("%d\n",total); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /regional/2007/varetas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int c, n, pares, v; 3 | int main(void){ 4 | while(scanf("%d",&n) && n){ 5 | pares = 0; 6 | while(n--){ 7 | scanf("%d%d",&c,&v); 8 | pares += (v>>1); 9 | } 10 | printf("%d\n",pares>>1); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /seletiva/IME-USP2008-1/wcw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int aux, cn, i, n, swap, v[10001]; 3 | int main(void){ 4 | scanf("%d",&cn); 5 | while(cn--){ 6 | scanf("%d",&n); swap = 0; 7 | for(i = 1; i <= n; i++) scanf("%d",&v[i]); 8 | for(i = 1; i <= n; i++) 9 | while(v[i] != i){ 10 | aux = v[i]; 11 | v[i] = v[v[i]]; 12 | v[aux] = aux; 13 | swap++; 14 | } 15 | printf("%d\n",swap); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /seletiva/IME-USP2008-2/sanduba.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int c, i, j, n; 4 | long long acc, sum, v[100025]; 5 | int main(void){ 6 | scanf("%d",&c); 7 | while(c--){ 8 | sum = 0; 9 | scanf("%d",&n); 10 | for(i = 0; i < n; i++) scanf("%lld",&v[i]), acc += v[i]; 11 | for(i = 0; i < n; i++){ 12 | sum += ((acc%1300031)*(v[i]%1300031))%1300031; 13 | acc -= v[i]; 14 | } 15 | printf("%lld\n",sum%1300031); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /seletiva/IME-USP2009/cinema.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int N, M; 3 | int main(void){ 4 | int i, M, N, T; 5 | scanf("%d",&T); 6 | while(T--){ 7 | scanf("%d%d",&N,&M); 8 | long long res = 1; 9 | for(i = 0; i < M; i++){ 10 | long long x = N-M-i; 11 | res = (res*x)%1300031LL; 12 | } 13 | for(i = 0; i < M; i++){ 14 | res = (res<<1)%1300031LL; 15 | } 16 | printf("%lld\n",res); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /seletiva/IME-USP2009/dias.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | long long gcd(long long a, long long b) { 5 | if(a % b == 0) return b; 6 | return gcd(b, a % b); 7 | } 8 | int main(void){ 9 | long long a, b; 10 | while(scanf("%lld%lld",&a,&b) && a){ 11 | printf("%lld\n",((a*b)/gcd(a,b))%1713371337LL); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /spoj/classical/ACODE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long w[5005]; 4 | int main(void){ 5 | for(char c, p; (c = getc(stdin) - '0') > 0; ){ 6 | w[0] = w[1] = 1; 7 | int i; 8 | for(i = 1, p = c; (c = getc(stdin) - '0') > -1; p = c, ++i) 9 | w[i + 1] = (c ? w[i] : 0) + (p && 10*p+c < 27 ? w[i - 1]: 0); 10 | printf("%lld\n", w[i]); 11 | c += '0'; 12 | while(c!=10) c = getc(stdin); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /spoj/classical/ACPC10A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, b, c; scanf("%d %d %d", &a, &b, &c) == 3; ){ 5 | if(!(a || b || c)) break; 6 | if(c - b == b - a) printf("AP %d\n", (c<<1) - b); 7 | else printf("GP %d\n", (c * c) / b); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /spoj/classical/ADDREV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template _T reverse(_T a){ 4 | for(_T r = 0; ; a /= 10) 5 | if(a) r = r * 10 + (a % 10); 6 | else return r; 7 | } 8 | int main(void){ 9 | int a, b, t; scanf("%d", &t); 10 | while(t--){ 11 | scanf("%d %d", &a, &b); 12 | printf("%d\n", reverse(reverse(a) + reverse(b))); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/classical/AE00.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int ans[10048]; 4 | int main(void){ 5 | ans[0] = 0; 6 | for(int i = 1; i < 10001; ++i){ 7 | ans[i] = ans[i - 1]; 8 | for(int j = 1; j * j <= i; ++j) ans[i] += !(i % j); 9 | } 10 | for(int n; scanf("%d", &n) == 1; printf("%d\n", ans[n])); 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /spoj/classical/AE1B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int x[1024]; 4 | int main(void){ 5 | int k, n, s; scanf("%d %d %d", &n, &k, &s); 6 | int i, need = k * s; 7 | for(i = s = 0; i < n; ++i) scanf("%d", x + i), s += x[i]; 8 | std::sort(x, x + n); 9 | for(i = 0; s - x[i] >= need; s -= x[i++]); 10 | printf("%d\n", n - i); 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /spoj/classical/ANARC08E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int a, b; scanf("%d %d", &a, &b) == 2 && (a > -1 || b > -1); ) 4 | printf("%d+%d%s%d\n", a, b, a == 1 || b == 1 ? "=" : "!=", a + b); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /spoj/classical/ANARC08H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[1048576]; 4 | int main(void){ 5 | dp[1] = 0; 6 | for(int d, i, n; scanf("%d %d", &n, &d) == 2 && (n || d); ){ 7 | for(i = 2; i <= n; ++i) dp[i] = (dp[i - 1] + d) % i; 8 | printf("%d %d %d\n", n, d, dp[n] + 1); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spoj/classical/ANARC09A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char input[2048]; 3 | int main(void){ 4 | for(int cnum = 0; scanf("%s", input) && input[0] != '-'; ){ 5 | int b = 0, c = 0; 6 | for(int i = 0; input[i]; ++i){ 7 | b += (input[i] == '{') - (input[i] == '}'); 8 | if(b < 0) b += 2, ++c; 9 | } 10 | printf("%d. %d\n", ++cnum, c + (b >> 1)); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/BINSTIRL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int m, n, t; 4 | for(scanf("%d", &t); t-- && scanf("%d %d", &n, &m); ) 5 | printf("%d\n", 0 == ((n - m) & ((m - 1) >> 1))); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /spoj/classical/BISHOPS.py: -------------------------------------------------------------------------------- 1 | import sys 2 | for s in sys.stdin: 3 | x = int(s) - 1 4 | print(x << 1 if x else 1) 5 | -------------------------------------------------------------------------------- /spoj/classical/CANDY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int x[10005]; 3 | int main(void){ 4 | for(int mean, n, s = 0, t; scanf("%d", &n) && n != -1; ){ 5 | s = 0; 6 | for(int i = 0; i < n; ++i) scanf("%d", x + i), s += x[i]; 7 | mean = s / n; 8 | if(!(s % n)){ 9 | mean = s / n; s = 0; 10 | for(int i = 0; i < n; ++i) s += x[i] > mean ? x[i] - mean : 0; 11 | printf("%d\n", s); 12 | } else puts("-1"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/classical/CANDY3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int i, n, t; scanf("%d", &t); 5 | long long s, z; 6 | while(t--){ 7 | scanf("%d", &n); 8 | for(i = s = 0; i < n; ++i) scanf("%lld", &z), s = (s + z) % n; 9 | puts(s ? "NO" : "YES"); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /spoj/classical/CANTON.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int n, t; scanf("%d", &t); 5 | while(t--){ 6 | scanf("%d", &n); 7 | int top = int(ceil(-.5 + sqrt(1 + (n << 3))/2.)); 8 | int u = (top * (top + 1)) >> 1; 9 | if(top & 1) printf("TERM %d IS %d/%d\n", n, 1 + u - n, n - u + top); 10 | else printf("TERM %d IS %d/%d\n", n, n - u + top, 1 + u - n); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /spoj/classical/COINS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | std::map < unsigned, unsigned > m; 4 | unsigned exchange(unsigned n){ 5 | unsigned &r = m[n]; 6 | if(r) return r; 7 | unsigned sum = (n/2) + (n/3) + (n/4); 8 | if(sum > n) return r = exchange(n/2) + exchange(n/3) + exchange(n/4); 9 | return r = n; 10 | } 11 | int main(void){ 12 | for(int n; scanf("%d", &n) == 1; ) printf("%u\n", exchange(n)); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /spoj/classical/CPRMT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int c[128][2]; 4 | char s[1024], t[1024]; 5 | int main(void){ 6 | while(scanf("%s %s", s, t) == 2){ 7 | for(int i = 'a'; i <= 'z'; ++i) c[i][0] = c[i][1] = 0; 8 | for(char *p = s; *p; ) ++c[*p++][0]; 9 | for(char *p = t; *p; ) ++c[*p++][1]; 10 | for(int i = 'a'; i <= 'z'; ++i) 11 | for(int &c0 = c[i][0], &c1 = c[i][1]; c0-- && c1--; ) 12 | putc(i, stdout); 13 | puts(""); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /spoj/classical/EIGHTS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int end[4] = {192, 442, 692, 942}; 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | for(long long n; t-- && scanf("%lld", &n); ){ 6 | int idx = --n & 3; 7 | if(n >>= 2) printf("%lld", n); 8 | printf("%d\n", end[idx]); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spoj/classical/EMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int m, n; 4 | scanf("%d %d", &m, &n); 5 | printf("%d\n", m + n); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /spoj/classical/FAMILYP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | for(int n, t; scanf("%d", &n) == 1; ){ 5 | t = ceil(sqrt(1 + (n<<3)) / 2 - .5); 6 | printf("TERM %d is %c\n", n, ('A' + (t-1) % 26)); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /spoj/classical/FCTRL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, t; scanf("%d", &t); 5 | while(t--){ 6 | int ans = 0; 7 | scanf("%d", &n); 8 | while(n) ans += n /= 5; 9 | printf("%d\n", ans); 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /spoj/classical/FENCE1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const double c = .5/acos(-1); 4 | int main(void){ 5 | for(int L; scanf("%d", &L) == 1 && L; ) 6 | printf("%.2lf\n", L * L * c); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/classical/GNY07A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int i, k, t; scanf("%d", &t); 5 | for(int c = 0; c++ < t; ){ 6 | scanf("%d\n", &k); i = 0; 7 | printf("%d ", c); 8 | for(char c; (c = getc(stdin)) != 10 && c != EOF; ) 9 | if(++i != k) putc(c, stdout); 10 | putc(10, stdout); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /spoj/classical/GNY07B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | char unit[8]; 4 | float v; 5 | int t; scanf("%d", &t); 6 | for(int c = 0; c++ < t; ){ 7 | scanf("%f %s", &v, unit); 8 | if(*unit == 'k') printf("%d %.4lf lb\n", c, v * 2.2046); 9 | else if(*unit == 'g') printf("%d %.4lf l\n", c, v * 3.7854); 10 | else if(*(unit+1) == 'b') printf("%d %.4lf kg\n", c, v * 0.4536); 11 | else printf("%d %.4lf g\n", c, v * 0.2642); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spoj/classical/HANGOVER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float p[512]; 4 | int main(void){ 5 | p[0] = 0; 6 | for(int i = 1; i < 512; ++i) p[i] = p[i - 1] + 1. / (i + 1); 7 | for(float x; scanf("%f", &x) && x; ){ 8 | int i; for(i = 0; p[i] < x; ++i); 9 | printf("%d card(s)\n", i); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /spoj/classical/HELLOKIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char w[1024]; 5 | for(int l, n, p; scanf("%s %d", w, &n) == 2; ){ 6 | p = (l = strlen(w))*n; 7 | for(int i = 0; i < l; ++i){ 8 | for(int j = 0; j < p; ++j) putc(w[(i+j)%l], stdout); 9 | puts(""); 10 | } 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/HUBULLU.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int s, t; 5 | for(scanf("%d", &t); t-- && scanf("%*d %d", &s); ) 6 | puts(1-s ? "Airborne wins." : "Pagfloyd wins."); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/classical/LASTDIG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | while(t--){ 6 | int a, b, r = 1; scanf("%d %d", &a, &b); a %= 10; 7 | while(b){ 8 | if(b & 1) r = (r * a) % 10; 9 | a = (a * a) % 10; 10 | b >>= 1; 11 | } 12 | printf("%d\n", r); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /spoj/classical/MARBLES.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, k, t; scanf("%d", &t); 5 | while(t--){ 6 | scanf("%d %d", &n, &k); --n; --k; 7 | long long r = 1; 8 | if(n - k < k) k = n - k; 9 | for(int i = 0; i < k; ) r *= n--, r /= ++i; 10 | printf("%lld\n", r); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/MFLAR10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char line[2048]; 6 | int main(void){ 7 | while(fgets(line, 2048, stdin) && line[0] != '*'){ 8 | char *p = strtok(line, " "); 9 | char letter = tolower(line[0]); 10 | while(p != NULL){ 11 | if(tolower(*p) != letter) break; 12 | p = strtok(NULL, " "); 13 | } 14 | if(p == NULL) puts("Y"); 15 | else puts("N"); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /spoj/classical/MIRRORED.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int f[128]; 4 | char s[16]; 5 | int main(void){ 6 | for(int i = 0; i < 128; ++i) f[i] = 0; 7 | f['p'] = f['q'] = 1; 8 | f['b'] = f['d'] = 2; 9 | puts("Ready"); 10 | while(fgets(s, 16, stdin) && (*s != ' ' || *(s+1) != ' ')) 11 | puts(f[*s] && f[*s] == f[*(s+1)] ? "Mirrored pair" : "Ordinary pair"); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/NEG2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char s[128]; 4 | int main(void){ 5 | int d, n, t = 0; scanf("%d", &n); 6 | if(n == 0) puts("0"); 7 | else { 8 | while(n){ 9 | s[t++] = '0' + (n & 1); 10 | n = (n - (n < 0)) / -2; 11 | } 12 | while(t--) putc(s[t], stdout); 13 | putc(10, stdout); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /spoj/classical/NGM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n; scanf("%d", &n); 5 | if(n % 10) printf("%d\n%d\n", 1, n % 10); 6 | else puts("2"); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/classical/NSTEPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int t, x, y; scanf("%d", &t); 4 | while(t--){ 5 | scanf("%d %d", &x, &y); 6 | if(x != y && x - y != 2) puts("No Number"); 7 | else printf("%d\n", x + y - (y & 1)); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /spoj/classical/PERMUT2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x[1<<17], y[1<<17]; 4 | int main(void){ 5 | for(int n; scanf("%d", &n) && n; ){ 6 | bool ambiguous = true; 7 | for(int i = 0; i < n; ++i){ 8 | scanf("%d", x + i); 9 | y[x[i]-1] = i + 1; 10 | } 11 | for(int i = 0; i < n; ++i) ambiguous &= x[i] == y[i]; 12 | puts(ambiguous ? "ambiguous" : "not ambiguous"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/classical/RESN04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, s, t, z; scanf("%d", &t); 5 | while(t-- && scanf("%d", &n) == 1){ 6 | s = 0; 7 | for(int i = 1; i <= n; ++i){ 8 | scanf("%d", &z); 9 | s += z / i; 10 | } 11 | puts(s & 1 ? "ALICE" : "BOB"); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spoj/classical/SAMER08F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", (n*(n+1)*(2*n+1))/6)); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /spoj/classical/SUMITR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define F(i,n) for(i=1;i<=n;++i) 3 | #define R(x) scanf("%d",&x) 4 | int main(void){ 5 | int i,j,*k,m[108][108],n,t,z; R(t); 6 | while(t--){ 7 | R(n);z=0; 8 | F(i,n)F(j,i) 9 | R(m[i][j]), 10 | k=m[i-1], 11 | z=(m[i][j]+=k[j-1]>k[j]?k[j-1]:k[j])>z?m[i][j]:z; 12 | printf("%d\n",z); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spoj/classical/TEST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1 && n != 42; printf("%d\n", n)); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /spoj/classical/TOANDFRO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | char s[256]; 4 | for(int c; scanf("%d", &c) && c; ){ 5 | int l, t = c << 1; 6 | scanf("%s", s); 7 | for(l = -1; s[++l]; ); 8 | for(int i = 0; i < c; ++i){ 9 | int j = 1 + (i << 1); 10 | for(int p = i; p < l; p += j = t - j) 11 | putc(s[p], stdout); 12 | } 13 | puts(""); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /spoj/classical/TRICOUNT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | for(long long n; scanf("%lld", &n) == 1; ) 6 | printf("%lld\n", (n * (2*n+1) * (n + 2)) >> 3); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/classical/UJ.py: -------------------------------------------------------------------------------- 1 | import sys 2 | while True: 3 | b, e = map(int,input().split()); 4 | if not(b or e): break 5 | print(b**e) 6 | 7 | -------------------------------------------------------------------------------- /sulamericana/2005/mission.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ajmarin/coding/417d4a3987126fc458747c391ca18843eee74257/sulamericana/2005/mission.cpp -------------------------------------------------------------------------------- /sulamericana/2005/pascal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const char *y = "yes", *n = "no"; 4 | int main(void){ 5 | register int i, j, D, N; 6 | register bool w[100], x; 7 | while(scanf("%d %d\n",&N,&D) && N){ 8 | memset(w,1,N); 9 | for(x = i = 0; i < D; i++) 10 | for(j = 0; j < N; getc(stdin), j++) 11 | w[j] &= (getc(stdin) - '0'); 12 | for(i = 0; i < N; i++) x |= w[i]; 13 | ((x)?(puts(y)):(puts(n))); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /sulamericana/2006/odd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int i, k, n, v1, v2; 5 | while(scanf("%d",&n) && n){ 6 | for(v1 = i = 0; i < n; i++) scanf("%d",&k), v1 += k&1; 7 | for(v2 = i = 0; i < n; i++) scanf("%d",&k), v2 += !(k&1); 8 | printf("%d\n",abs(v1-v2)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /timus/T1000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, b; scanf("%d %d", &a, &b)==2; printf("%d\n", a+b)); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /timus/T1001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | stack < double > s; 6 | int main(void){ 7 | for(long long n; scanf("%lld", &n) == 1; s.push(sqrt((double)n))); 8 | while(!s.empty()){ 9 | printf("%.4lf\n", s.top()); 10 | s.pop(); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /timus/T1025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int v[128]; 5 | int main(void){ 6 | for(int k; scanf("%d", &k) == 1; ){ 7 | int ans = 0; 8 | for(int i = 0; i < k; ++i) scanf("%d", v + i); 9 | sort(v, v + k); 10 | for(int i = 0; i < (k + 1) >>1; ++i) ans += (1 + v[i]) >> 1; 11 | printf("%d\n", ans); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /timus/T1068.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1; ){ 5 | bool neg = n < 1; 6 | if(n < 0) n = -n; 7 | if(neg) printf("%d\n", 1 - (n * n + n) / 2); 8 | else printf("%d\n", (n * n + n) / 2); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /timus/T1073.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int N = 60048; 4 | int dp[N]; 5 | int min(int a, int b){ return a < b ? a : b; } 6 | int main(void){ 7 | int i, isq, j; 8 | for(i = 0; i < N; ++i) dp[i] = i; 9 | for(i = 1; i*i <= N; ++i) 10 | for(j = isq = i * i; j <= N; ++j) 11 | dp[j] = min(dp[j], dp[j-isq] + 1); 12 | for(int n; scanf("%d", &n) == 1; ) printf("%d\n", dp[n]); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /timus/T1083.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char f[32]; 5 | for(int n; scanf("%d%s", &n, f) == 2; ){ 6 | int r = 1, k = strlen(f); 7 | while(n > 0) r *= n, n -= k; 8 | printf("%d\n", r); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /timus/T1224.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int m, n; scanf("%d %d", &n, &m) == 2; ) 5 | if(n <= m) printf("%u\n", (n << 1) - 2); 6 | else printf("%u\n", (m << 1) - 1); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /timus/T1225.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[45][2]; 4 | int main(void){ 5 | dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = 1; 6 | for(int i = 2; i < 45; ++i){ 7 | dp[i][0] = dp[i - 1][1] + dp[i - 2][1]; 8 | dp[i][1] = dp[i - 1][0] + dp[i - 2][0]; 9 | } 10 | for(int n; scanf("%d", &n) == 1; ) 11 | printf("%u\n", dp[n-1][0] + dp[n-1][1]); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /timus/T1820.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n, k; scanf("%d %d", &n, &k) == 2; ){ 5 | if(k >= n){ 6 | puts("2"); 7 | } else { 8 | printf("%d\n", (2 * n + k - 1) / k); 9 | } 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /uva/Volume C/10055.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10055 - Hashmat the Brave Warrior 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | register unsigned long long a,b; 7 | while(scanf("%llu %llu",&a,&b)!=EOF) 8 | printf("%llu\n",(b>a)?b-a:a-b); 9 | } 10 | -------------------------------------------------------------------------------- /uva/Volume C/10056.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int cases; 6 | for(scanf("%d", &cases); cases--; ){ 7 | int n, k; 8 | double p; 9 | scanf("%d %lf %d", &n, &p, &k); 10 | printf("%.4lf\n", p ? pow(1.0 - p, k - 1) * p / (1 - pow(1.0 - p, n)) : 0); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume C/10071.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10071 - Back to High School Physics 3 | ///////////////////////////////// 4 | #include 5 | int a, b; 6 | int main(){ 7 | while(scanf("%d %d",&a,&b)!=EOF){ 8 | printf("%d\n",2*a*b); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /uva/Volume C/10079.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10079 - Pizza Cutting 3 | ///////////////////////////////// 4 | #include 5 | int k; 6 | long long int n; 7 | int main(void){ 8 | scanf("%d",&k); 9 | while(k >= 0){ 10 | n = k; 11 | n = n*(n+1)/2+1; 12 | printf("%lld\n",n); 13 | scanf("%d",&k); 14 | } 15 | } -------------------------------------------------------------------------------- /uva/Volume CI/10105.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long f[14]; 4 | 5 | int main(void){ 6 | int k, K, N; 7 | f[0] = 1; 8 | for(k = 1; k < 14; ++k) 9 | f[k] = k * f[k - 1]; 10 | while(scanf("%d%d",&K,&N) == 2){ 11 | long long r = f[K]; 12 | for(int i = 0; i < N; ++i){ 13 | scanf("%d",&k); 14 | r /= f[k]; 15 | } 16 | printf("%lld\n", r); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CI/10110.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10110 - Light, More Light 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | long long i,n,root; 7 | 8 | bool on; 9 | int main(void){ 10 | scanf("%lld",&n); 11 | while(n){ 12 | on = 0; 13 | root = (int)sqrt(n); 14 | on = (root*root == n); 15 | if(on) printf("yes\n"); 16 | else printf("no\n"); 17 | scanf("%lld",&n); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume CI/10170.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long long T(long long n){ 5 | return (n * (n + 1)) >> 1; 6 | } 7 | 8 | int main(void){ 9 | for(long long d, s; scanf("%lld%lld", &s, &d) == 2; ){ 10 | printf("%d\n", int(ceil(-.5 + sqrt(1 + (d + T(s - 1)) * 8) * .5))); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CI/10176.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int mod; 5 | for(char c = 0; ; c = 0){ 6 | while(c != EOF && c != '0' && c != '1') c = getc(stdin); 7 | if(c == EOF) break; 8 | for(mod = 0; c != '#'; c = getc(stdin)) 9 | mod = ((mod << 1) | (c == '1')) % 131071; 10 | puts(mod ? "NO" : "YES"); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CII/10209.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10209 - Is This Integration ? 3 | ///////////////////////////////// 4 | #include 5 | double a,a1,a2,a3; 6 | int main(void){ 7 | while(scanf("%lf",&a)!=EOF){ 8 | a*=a; 9 | a1 = 0.315146743627720*a; 10 | a2 = 0.511299166334351*a; 11 | a3 = 0.173554090037927*a; 12 | printf("%.3lf %.3lf %.3lf\n",a1,a2,a3); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CII/10219.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long max(long long a, long long b){ return a > b ? a : b; } 4 | int main(void){ 5 | for(long long k, n; scanf("%lld %lld", &n, &k) == 2; ){ 6 | double r = 0; 7 | k = max(k, n - k); 8 | for(long long i = 1, j = k + 1; j <= n; ++j, ++i) 9 | r += log10(j) - log10(i); 10 | printf("%.0lf\n", floor(r) + 1); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /uva/Volume CII/10250.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | long double x1, x2, xm, y1, y2, ym; 5 | while(scanf("%Lf %Lf %Lf %Lf", &x1, &y1, &x2, &y2) == 4){ 6 | xm = (x1 + x2) * .5; 7 | ym = (y1 + y2) * .5; 8 | printf("%Lf %Lf %Lf %Lf\n", xm - ym + y1, ym + xm - x1, xm + ym - y1, ym - xm + x1); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CII/10286.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double pi = acos(-1); 5 | const double cv = sin((108 * pi)/ 180) / sin((63 * pi)/180); 6 | 7 | int main(void){ 8 | double n; 9 | while(scanf("%lf",&n)==1) printf("%.10lf\n",n * cv); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CIII/10302.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10302 - Summation of Polynomials 3 | ///////////////////////////////// 4 | #include 5 | unsigned long long int n; 6 | int main(void){ 7 | while(scanf("%llu",&n)==1) printf("%llu\n",n*n*(n+1)*(n+1)/4); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /uva/Volume CIII/10346.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10346 - Peter's Smoke 3 | ///////////////////////////////// 4 | #include 5 | int ciggs,n,k,butts; 6 | int main(void){ 7 | while(scanf("%d %d",&n,&k)!=EOF){ 8 | ciggs = n; 9 | while(n >= k){ 10 | butts = n%k; 11 | n/=k; 12 | ciggs+= n; 13 | n+=butts; 14 | } 15 | printf("%d\n",ciggs); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /uva/Volume CIV/10407.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(void){ 5 | for(int ans, n, p; scanf("%d", &n) && n; ){ 6 | scanf("%d", &p); 7 | ans = abs(p - n); 8 | while(scanf("%d", &p) && p) ans = __gcd(ans, abs(p - n)); 9 | printf("%d\n", ans); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /uva/Volume CIV/10432.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10432 - Polygon Inside A Circle 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | const double PI2 = 4*acos(0); 7 | unsigned int n; 8 | double R; 9 | int main(void){ 10 | while(scanf("%lf %u\n",&R,&n)!=EOF) printf("%.3lf\n",sin(PI2/n)*n*R*R/2); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume CIV/10451.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double PI = 2 * acos(0); 5 | const double PI2 = 2 * PI; 6 | int main(void){ 7 | int n; 8 | double A; 9 | for(int c = 1; (scanf("%d %lf", &n, &A) && n > 2); ++c){ 10 | double R = (A * 2.) / (n * sin(PI2 / n)); 11 | double r = A / (n * tan(PI / n)); 12 | printf("Case %d: %.5lf %.5lf\n", c, PI * R - A, A - PI * r); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CIV/10473.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10473 - Simple Base Conversion 3 | ///////////////////////////////// 4 | #include 5 | char line[30]; 6 | unsigned int n; 7 | int main(void){ 8 | while(fgets(line,30,stdin)){ 9 | if(line[0]=='-') return 0; 10 | if(line[1]!='x') sscanf(line,"%u",&n), printf("0x%X\n",n); 11 | else sscanf(line+2,"%X",&n), printf("%u\n",n); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /uva/Volume CIV/10491.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int NC, NW, NS; 5 | while(scanf("%d %d %d", &NW, &NC, &NS) == 3){ 6 | int T = NW + NC; 7 | printf("%.5lf\n", (NC * (T - 1)/(double)(T * (T - NS - 1)))); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /uva/Volume CIV/10499.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10499 - The Land of Justice 3 | ///////////////////////////////// 4 | #include 5 | long long int n; 6 | int main(void){ 7 | while(scanf("%lld\n",&n) && n>0) 8 | if(n!=1) printf("%llu%%\n",n*25); 9 | else printf("0%%\n"); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CIX/10931.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10931 - Parity 3 | ///////////////////////////////// 4 | #include 5 | unsigned int n; 6 | char ans[35],i,par; 7 | int main(void){ 8 | ans[34] = 0; 9 | while(scanf("%u",&n) && n){ 10 | i = 33; par = 0; 11 | do { 12 | ans[i--] = (n&1)+'0'; par += n&1; n>>=1; 13 | } while(n); 14 | printf("The parity of %s is %u (mod 2).\n",ans+i+1,par); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CIX/10994.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long int LL; 4 | LL f(LL x){ 5 | if(!x) return 0; 6 | LL m = x % 10; 7 | return (x / 10) * 45 + (m * m + m) / 2 + f(x / 10); 8 | } 9 | 10 | int main(void){ 11 | for(LL p, q; scanf("%lld%lld", &p, &q) == 2; ){ 12 | if(p < 0 && q < 0) break; 13 | printf("%lld\n", f(q) - f(p - 1)); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume CV/10509.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10509 - R U Kidding Mr. Feynman! 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define EPS 1e-6 7 | double dx,n, div = 1.0/3; 8 | int a; 9 | int main(void){ 10 | while(scanf("%lf",&n) && n){ 11 | a = pow(n+EPS,div); 12 | dx = (n-a*a*a)/(3*a*a); 13 | printf("%.4lf\n",dx+a); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume CVI/10633.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(long long z; scanf("%lld", &z) == 1 && z; ){ 5 | long long n = z + z/9; 6 | bool first = true; 7 | for(long long i = n - 10; i <= n + 10; ++i){ 8 | if(i - i/10 == z){ 9 | if(!first) putc(32, stdout); 10 | printf("%lld", i); 11 | first = false; 12 | } 13 | } 14 | puts(""); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CVI/10656.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int v[1024]; 4 | int main(void){ 5 | for(int n; scanf("%d", &n) == 1 && n; ){ 6 | bool showed = false; 7 | for(int i = 0; i < n; ++i){ 8 | scanf("%d", v + i); 9 | if(v[i] > 0){ 10 | if(showed) putc(32, stdout); 11 | printf("%d", v[i]); 12 | showed = true; 13 | } 14 | } 15 | if(!showed) putc('0', stdout); 16 | puts(""); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CVI/10678.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const double hhpi = atan(1); 4 | int sqr(int x){ return x * x; } 5 | int main(void){ 6 | int D, L, z; scanf("%d", &z); 7 | while(scanf("%d %d", &D, &L) == 2) 8 | printf("%.3lf\n", hhpi * L * sqrt(sqr(L) - sqr(D))); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /uva/Volume CVI/10696.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10696 - f91 3 | ///////////////////////////////// 4 | #include 5 | int n; 6 | int main(void){ 7 | scanf("%d",&n); 8 | while(n){ 9 | printf("f91(%d) = %d\n",n,(n<102)?91:n-10); 10 | scanf("%d",&n); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /uva/Volume CVII/10721.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define FORI(i,s,n) for(int i = s; i < n; ++i) 4 | long long dp[64][64]; 5 | int main(void){ 6 | for(int n, k, m; scanf("%d %d %d", &n, &k, &m) == 3; ){ 7 | FORI(i, 0, n+1) FORI(j, 0, k+1) dp[i][j] = 0; 8 | dp[0][0] = 1; 9 | FORI(i, 0, n+1) FORI(j, 1, k+1) 10 | for(int v = 1; v <= i && v <= m; ++v) 11 | dp[i][j] += dp[i - v][j - 1]; 12 | printf("%lld\n", dp[n][k]); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CVII/10733.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned long long ULL; 4 | 5 | ULL P(ULL n, int x){ 6 | ULL ret = 1, b = n; 7 | while(x){ 8 | if(x & 1) ret *= b; 9 | b *= b; 10 | x >>= 1; 11 | } 12 | return ret; 13 | } 14 | 15 | int main(void){ 16 | for(ULL n; scanf("%llu", &n) == 1 && n; ) 17 | printf("%llu\n", (P(n, 6) + 3 * P(n, 4) + 12 * P(n, 3) + 8 * P(n, 2)) / 24); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CVII/10773.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int cases; 6 | scanf("%d", &cases); 7 | for(int c = 0, d, v, u; c++ < cases && scanf("%d %d %d", &d, &v, &u) == 3; ){ 8 | printf("Case %d: ", c); 9 | if(v < u && u && v) printf("%.3lf\n", d / sqrt(u*u - v*v) - d / double(u)); 10 | else puts("can\'t determine"); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CVII/10783.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10783 - Odd Sum 3 | ///////////////////////////////// 4 | #include 5 | int a,b,c,ncase,sum; 6 | int main(void){ 7 | while(scanf("%d",&c)!=EOF){ 8 | ncase =1; 9 | while(c){ 10 | scanf("%d\n%d",&a,&b); 11 | c--; 12 | if(a%2==0) a++; 13 | for(sum = 0; a <= b; sum+=a,a+=2); 14 | printf("Case %d: %d\n",ncase,sum); 15 | ncase++; 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume CVII/10784.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int cs = 1; 5 | for(long long k; scanf("%lld", &k) && k; ++cs) 6 | printf("Case %d: %.0lf\n", cs, ceil(1.5 + sqrt(9 + (k << 3))/2)); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /uva/Volume CVII/10790.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int cn = 0; 5 | for(long long a, b; scanf("%lld%lld", &a, &b) == 2 && (a || b); ) 6 | printf("Case %d: %lld\n", ++cn, (a*a - a) * (b*b - b) >> 2); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /uva/Volume CVIII/10830.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int c = 0, n; scanf("%d", &n) == 1 && n; ){ 5 | long long ans = 0; 6 | int i, y, z; 7 | for(i = 2, y = n; i <= n / i; ++i){ 8 | z = y; 9 | y = n / i; 10 | ans += i * (y - 1); 11 | ans += (long long)(i - 2) * (y + 1 + z) * (z - y) / 2; 12 | } 13 | if(i <= y) ans += i * (n / i - 1); 14 | printf("Case %d: %lld\n", ++c, ans); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CX/11000.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11000 - Bee 3 | ///////////////////////////////// 4 | #include 5 | unsigned int f[45],i,m[45]; 6 | int k; 7 | int main(void){ 8 | m[0] = 0; f[0] = 1; 9 | for(i = 1; i < 45; i++) 10 | m[i] = f[i-1], f[i] = m[i-1]+f[i-1]+1; 11 | while(scanf("%d",&k) && k != -1) 12 | printf("%u %u\n",m[k],f[k]); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CX/11044.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11044 - Searching for Nessy 3 | ///////////////////////////////// 4 | #include 5 | unsigned int m,n,cnum; 6 | int main(void){ 7 | scanf("%u\n",&cnum); 8 | while(cnum--){ 9 | scanf("%u %u\n",&m,&n); 10 | m = (m/3)*(n/3); 11 | printf("%u\n",m); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /uva/Volume CX/11054.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) && n; ){ 5 | long long balance = 0, mov = 0, x; 6 | while(n--){ 7 | scanf("%lld", &x); 8 | x = x + balance; 9 | mov += (x < 0 ? -x:x); 10 | balance = x; 11 | } 12 | printf("%lld\n", mov); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CX/11069.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11069 - A Graph Problem 3 | ///////////////////////////////// 4 | #include 5 | unsigned int ways[77],i; 6 | int main(void){ 7 | ways[1] = 1; 8 | ways[2] = ways[3] = 2; 9 | for(i = 4; i != 77; i++) 10 | ways[i] = ways[i-2]+ways[i-3]; 11 | while(scanf("%u",&i)!=EOF) printf("%u\n",ways[i]); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXI/11172.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11172 - Relational Operator 3 | ///////////////////////////////// 4 | #include 5 | unsigned int a,b,cnum; 6 | int main(void){ 7 | scanf("%u\n",&cnum); 8 | while(cnum--){ 9 | scanf("%u %u\n",&a,&b); 10 | if(a < b) printf("<\n"); 11 | else if(a > b) printf(">\n"); 12 | else printf("=\n"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume CXI/11185.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11185 - Ternary 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define MAXCAND 8 7 | int n; 8 | char ans[30],idx; 9 | int main(void){ 10 | ans[29] = 0; 11 | while(scanf("%d",&n) && n >= 0){ 12 | idx = 29; 13 | do{ 14 | ans[--idx] = ('0'+n%3); 15 | n/=3; 16 | } while(n); 17 | puts(ans+idx); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXII/11231.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n, m, c; scanf("%d %d %d", &n, &m, &c) == 3 && n; ){ 5 | int even = (m - 7 + c) >> 1, odd = m - 7 - even; 6 | printf("%d\n", even * ((n - 6) / 2) + odd * ((n - 7) / 2)); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXII/11296.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(long long n; scanf("%lld", &n) == 1; n = (n+2)>>1, printf("%lld\n", ((n+1)*n) >> 1)); 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /uva/Volume CXIII/11310.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long s[41]; 4 | int main(void){ 5 | int N, T; 6 | s[1] = 1; s[2] = 5; s[3] = 11; 7 | for(int k = 4; k < 41; ++k) 8 | s[k] = s[k-1] + (s[k - 2] << 2) + (s[k - 3] << 1); 9 | scanf("%d",&T); 10 | while(T--){ 11 | scanf("%d", &N); 12 | printf("%lld\n",s[N]); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXIII/11388.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11388 - GCD LCM 3 | ///////////////////////////////// 4 | #include 5 | unsigned int a,b,tnum; 6 | int main(void){ 7 | scanf("%u",&tnum); 8 | while(tnum--){ 9 | scanf("%u %u",&a,&b); 10 | if(!(b%a)) printf("%u %u\n",a,b); 11 | else printf("-1\n"); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /uva/Volume CXIV/11480.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11480 - Jimmy's Balls 3 | ///////////////////////////////// 4 | #include 5 | long long n; 6 | int cnum = 0; 7 | int main(void){ 8 | while(scanf("%lld",&n) && n) 9 | if(n%6) printf("Case %d: %lld\n", ++cnum, ((n%6) + 3 * ((n-7)/6)) * ((n-1)/6)); 10 | else printf("Case %d: %lld\n", ++cnum, 3 * ((n-7)/6) * ((n-1)/6) + n-5); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /uva/Volume CXIX/11900.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | int cnum = 0; 6 | for(int n, p, q, z; t-- && scanf("%d %d %d", &n, &p, &q) == 3; ){ 7 | int ans = 0; 8 | for(int i = 0; i < n; ++i){ 9 | scanf("%d", &z); 10 | if(p && q >= z){ 11 | --p; 12 | q -= z; 13 | ++ans; 14 | } 15 | } 16 | printf("Case %d: %d\n", ++cnum, ans); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11909.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double conv = acos(-1)/180; 5 | int main(void){ 6 | 7 | for(int h, l, t, w; scanf("%d %d %d %d", &l, &w, &h, &t) == 4; ){ 8 | double trad = t * conv, trih = l * tan(trad); 9 | if(trih > h) printf("%.3lf mL\n", .5*h*h*w/tan(trad)); 10 | else printf("%.3lf mL\n", l*w*(h - .5*l*tan(t*conv))); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11933.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) && n; ){ 5 | int a = 0, b = 0; 6 | for(int i = 0; i < 32; ++i) 7 | if(n & (1< b) b |= (1 << i); 9 | else a |= (1 << i); 10 | } 11 | printf("%d %d\n", a, b); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11934.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a, b, c; 4 | int f(int x){ 5 | return (a * x + b) * x + c; 6 | } 7 | 8 | int main(void){ 9 | for(int d, L; scanf("%d %d %d %d %d", &a, &b, &c, &d, &L) == 5; ){ 10 | int ans = 0; 11 | if(!(a || b || c || d || L)) break; 12 | for(int i = 0; i <= L; ++i) 13 | ans += !(f(i) % d); 14 | printf("%d\n", ans); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11936.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | for(int a, b, c; t-- && scanf("%d %d %d", &a, &b, &c) == 3; ) 6 | puts(a+b <= c || a+c <= b || b+c <= a ? "Wrong!!" : "OK"); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11942.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | puts("Lumberjacks:"); 6 | for(int c, p; t--; ){ 7 | scanf("%d", &p); 8 | bool asc = true, desc = true; 9 | for(int i = 1; i < 10; ++i){ 10 | scanf("%d", &c); 11 | asc &= c > p; 12 | desc &= c < p; 13 | p = c; 14 | } 15 | puts(asc || desc ? "Ordered" : "Unordered"); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11945.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int T; 5 | scanf("%d", &T); 6 | setlocale(LC_ALL, "en_US.UTF-8"); 7 | for(int cnum = 0; cnum++ < T; ){ 8 | double r = 0, z; 9 | for(int i = 0; i < 12; ++i, r += z) scanf("%lf", &z); 10 | printf("%d $%'.2lf\n", cnum, r / 12.0); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11946.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char map[128]; 4 | int main(void){ 5 | int t; scanf("%d\n", &t); 6 | for(unsigned c = 0; c < 128; ++c) map[c] = c; 7 | map['0'] = 'O'; map['1'] = 'I'; map['2'] = 'Z'; 8 | map['3'] = 'E'; map['4'] = 'A'; map['5'] = 'S'; 9 | map['6'] = 'G'; map['7'] = 'T'; map['8'] = 'B'; 10 | map['9'] = 'P'; 11 | for(char c; (c = getchar()) != EOF; putc(*(map + c), stdout)); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXIX/11984.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int cnum = 0, t; scanf("%d", &t); 5 | for(int c, inc; t-- && scanf("%d %d", &c, &inc) == 2; ) 6 | printf("Case %d: %.2lf\n", ++cnum, c + inc / 1.8); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /uva/Volume CXV/11526.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int t; scanf("%d", &t); 6 | for(int n; t-- && scanf("%d", &n) == 1; ){ 7 | long long s = 0; 8 | if(n > 0){ 9 | int L = floor(sqrt(n)); 10 | for(int i = 1, q, pq = n; i <= L; ++i){ 11 | q = n / i; 12 | s += q + (pq - q) * (i - 1); 13 | pq = q; 14 | } 15 | if(n / L > L) s += L; 16 | } 17 | printf("%lld\n", s); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXV/11547.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11547 - Automatic Answer 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define pos(a) (a < 0? -a: a) 7 | int n,c; 8 | int main(void){ 9 | scanf("%d",&c); 10 | while(c--){ 11 | scanf("%d",&n); 12 | printf("%d\n",pos((315*n+36962)%100/10)); 13 | } 14 | } -------------------------------------------------------------------------------- /uva/Volume CXV/11556.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11556 - Best Compression Ever 3 | ///////////////////////////////// 4 | #include 5 | unsigned long long int a,b; 6 | const char *y = "yes", *n = "no"; 7 | int main(void){ 8 | while(scanf("%llu %llu",&a,&b)!=EOF) 9 | if(a < 1LLU<<(b+1)) puts(y); 10 | else puts(n); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11608.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int w[16]; 3 | int main(void){ 4 | for(int c = 0, n, z; scanf("%d", &n) == 1 && n >= 0; ){ 5 | printf("Case %d:\n", ++c); 6 | for(int i = 0; i < 12; ++i) scanf("%d", &w[i]); 7 | for(int i = 0; i < 12; ++i){ 8 | scanf("%d", &z); 9 | if(n >= z) n -= z, puts("No problem! :D"); 10 | else puts("No problem. :("); 11 | n += w[i]; 12 | } 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11636.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int c = 0, n; scanf("%d", &n) == 1 && n > 0; ){ 4 | int ans = !!(n & (n - 1)); 5 | while(n > 1) ++ans, n >>= 1; 6 | printf("Case %d: %d\n", ++c, ans); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11666.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int L, h, n; 6 | while(scanf("%d",&n) && n){ 7 | L = 0; 8 | h = n>>1; 9 | while(pow(M_E, L) < h) L++; 10 | printf("%d %.8lf\n",L, 1. - (double)n/pow(M_E,L)); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11677.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int h1, m1, h2, m2; 5 | while(scanf("%d %d %d %d",&h1,&m1,&h2,&m2)){ 6 | if((h1 + h2 + m1 + m2) == 0) break; 7 | if(h2 < h1 || (h2 == h1 && m2 < m1)) h2 += 24; 8 | printf("%d\n",(h2-h1)*60+(m2-m1)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11689.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int e, f, c, T; 5 | int drank, empty, more; 6 | scanf("%d",&T); 7 | while(T--){ 8 | scanf("%d%d%d",&e,&f,&c); 9 | drank = 0; 10 | empty = f+e; 11 | while(empty >= c){ 12 | more = empty/c; 13 | empty = empty%c; 14 | drank += more; 15 | empty += more; 16 | } 17 | printf("%d\n",drank); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11714.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int n; 6 | while(scanf("%d", &n) == 1){ 7 | int ans = --n + (int)log2(n); 8 | printf("%d\n", ans); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11723.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int R, N; 5 | for(int cnum = 0; (scanf("%d %d", &R, &N) && R); ){ 6 | printf("Case %d: ", ++cnum); 7 | int s = (R - 1)/N; 8 | if(s > 26) printf("impossible\n"); 9 | else printf("%d\n", s); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11727.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int a, ans, b, c, cnum = 0, t; 5 | scanf("%d", &t); 6 | while(t--){ 7 | scanf("%d %d %d", &a, &b, &c); 8 | if(a > b){ 9 | if(b > c) ans = b; 10 | else if(a > c) ans = c; 11 | else ans = a; 12 | } else if(a > c) ans = a; 13 | else if(b > c) ans = c; 14 | else ans = b; 15 | printf("Case %d: %d\n", ++cnum, ans); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11764.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int h, l, n, p, r, t; 5 | scanf("%d", &t); 6 | for(int cnum = 0; cnum < t; ){ 7 | scanf("%d", &n); 8 | h = l = 0; 9 | for(int i = 0; i < n; ++i){ 10 | p = r; 11 | scanf("%d", &r); 12 | if(i){ 13 | h += r > p; 14 | l += r < p; 15 | } 16 | } 17 | printf("Case %d: %d %d\n", ++cnum, h, l); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11799.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int k, n, t, x; scanf("%d", &t); 5 | for(int cnum = 0; cnum++ < t; ){ 6 | x = 0; 7 | scanf("%d", &n); 8 | while(n--) scanf("%d", &k), x = x > k ? x : k; 9 | printf("Case %d: %d\n", cnum , x); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/10872.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int cnum = 0; 5 | for(long long n; scanf("%lld", &n) == 1 && n; ){ 6 | if(n & 1) n += 3; 7 | printf("Case %d: %lld\n", ++cnum, (n*n+24) / 48); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11805.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int k, n, p, t; 3 | int main(void){ 4 | scanf("%d", &t); 5 | for(int cnum = 0; cnum < t; ){ 6 | scanf("%d %d %d", &n, &k, &p); 7 | k--; 8 | printf("Case %d: %d\n", ++cnum, ((k + p) % n) + 1); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11839.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, v; 5 | while(scanf("%d", &n) && n){ 6 | for(int k = 0; k < n; ++k){ 7 | char ans = 0; 8 | for(int i = 0; i < 5; ++i){ 9 | scanf("%d", &v); 10 | ans = v < 128 ? ans ? '*' : i + 'A' : ans; 11 | } 12 | if(ans) putc(ans, stdout); 13 | else putc('*', stdout); 14 | putc(10, stdout); 15 | } 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11847.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; ){ 4 | int ans = 0; 5 | while(n > 1) ++ans, n >>= 1; 6 | printf("%d\n", ans); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11854.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char *ans[2] = {"wrong", "right"}; 4 | int sqr(int x){ return x * x; } 5 | int main(void){ 6 | int a, b, c; 7 | bool r; 8 | while(scanf("%d %d %d", &a, &b, &c) == 3 && (a || b || c)){ 9 | if(c > b && c > a) r = sqr(a) + sqr(b) == sqr(c); 10 | if(b > c && b > a) r = sqr(a) + sqr(c) == sqr(b); 11 | if(a > b && a > c) r = sqr(b) + sqr(c) == sqr(a); 12 | puts(ans[r]); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11875.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[128]; 3 | int main(void){ 4 | int n, t; scanf("%d", &t); 5 | for(int c = 0; c < t; ++c){ 6 | scanf("%d", &n); 7 | for(int i = 0; i < n; ++i) scanf("%d", &v[i]); 8 | printf("Case %d: %d\n", c + 1, v[n >> 1]); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11877.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", n >> 1)); 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11878.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int ans = 0; char op, x; 5 | int a, b, c; 6 | while(scanf("%d%c%d=", &a, &op, &b) == 3){ 7 | x = getc(stdin); 8 | if(x == '?') continue; 9 | ungetc(x, stdin); 10 | scanf("%d", &c); 11 | if(op == '-' && a - b == c) ans++; 12 | if(op == '+' && a + b == c) ans++; 13 | } 14 | printf("%d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11879.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char num[128]; 4 | int main(void){ 5 | while(scanf("%s", num) == 1 && num[0] != '0'){ 6 | int mod = 0; 7 | for(char *p = num; *p; ++p) mod = (10 * mod + *p - '0') % 17; 8 | printf("%d\n", !mod); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11892.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char *ans[2] = {"piloop", "poopi"}; 4 | int main(void){ 5 | int c, i, n, t, x; scanf("%d", &t); 6 | while(t--){ 7 | scanf("%d", &n); 8 | for(c = i = 0; i < n; ++i) scanf("%d", &x), c += x == 1; 9 | puts(ans[n - c || c & 1]); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /uva/Volume CXX/12015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char url[16][128]; 4 | int v[16]; 5 | int main(void){ 6 | int t; scanf("%d", &t); 7 | for(int ncase = 1; t--; ++ncase){ 8 | int maxv = -1; 9 | for(int i = 0; i < 10; ++i){ 10 | scanf("%s %d", url[i], v + i); 11 | if(v[i] > maxv) maxv = v[i]; 12 | } 13 | printf("Case #%d:\n", ncase); 14 | for(int i = 0; i < 10; ++i) if(v[i] == maxv) puts(url[i]); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXXI/12149.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", (n*(n+1)*(2*n+1))/6)); 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /uva/Volume CXXII/12243.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char line[2048]; 6 | int main(void){ 7 | while(fgets(line, 2048, stdin) && line[0] != '*'){ 8 | char *p = strtok(line, " "); 9 | char letter = tolower(line[0]); 10 | while(p != NULL){ 11 | if(tolower(*p) != letter) break; 12 | p = strtok(NULL, " "); 13 | } 14 | if(p == NULL) puts("Y"); 15 | else puts("N"); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume CXXII/12256.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long ans[62]; 4 | int main(void){ 5 | ans[1] = ans[2] = ans[3] = 1; 6 | for(int i = 4; i < 62; ++i) 7 | ans[i] = ans[i - 1] + ans[i - 2] + ans[i - 3]; 8 | for(int c = 0, n; scanf("%d", &n) == 1 && n; ) 9 | printf("Case %d: %lld\n", ++c, ans[n]); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXXII/12279.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int c = 0, n; scanf("%d", &n) == 1 && n; ){ 5 | int cnt = 0; 6 | for(int i = 0, z; i < n; ++i){ 7 | scanf("%d", &z); 8 | cnt += !z; 9 | } 10 | printf("Case %d: %d\n", ++c, n - (cnt << 1)); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXXII/12293.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1 && n; puts(n & (n + 1) ? "Alice" : "Bob")); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /uva/Volume CXXIII/12372.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define GOOD(a,b,c) (a < 21 && b < 21 && c < 21) 4 | const char *ans[2] = {"bad", "good"}; 5 | 6 | int main(void){ 7 | scanf("%*d"); 8 | for(int a, b, c, z = 0; scanf("%d%d%d", &a, &b, &c) == 3; ) 9 | printf("Case %d: %s\n", ++z, ans[GOOD(a,b,c)]); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXXIII/12397.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int c[16] = { 4, 6, 3, 5, 2, 4, 2, 4, 2, 3, 2, 3, 1 }; 4 | int v[16] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; 5 | int main(void){ 6 | for(int cnt(0), n; scanf("%d", &n) == 1; cnt = 0){ 7 | for(int i = 0; n; ++i) 8 | while(n >= v[i]){ 9 | cnt += c[i]; 10 | n -= v[i]; 11 | } 12 | printf("%d\n", cnt); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12403.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int t; scanf("%d", &t) == 1; ){ 5 | int amount, balance = 0; 6 | char operation[16]; 7 | while(t--){ 8 | scanf("%s", operation); 9 | if(*operation == 'r') printf("%d\n", balance); 10 | else scanf("%d", &amount), balance += amount; 11 | } 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12405.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(void){ 5 | scanf("%*d"); 6 | for(int cnum = 0, n; scanf("%d\n", &n) == 1; ){ 7 | int scarecrows = 0; 8 | char c, p = '#'; 9 | while(n--){ 10 | c = getc( stdin ); 11 | scarecrows += p == '.'; 12 | if(p == '.') c = 'S'; 13 | else if (p == 'S') c = '#'; 14 | p = c; 15 | } 16 | printf("Case %d: %d\n", ++cnum, scarecrows + (p == '.')); 17 | 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12416.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(char c = getc( stdin ); c != EOF; c = getc( stdin )){ 5 | int steps, z; 6 | for(int cnt = z = 0; c != 10; c = getc( stdin )){ 7 | if(c == ' ') ++cnt; 8 | else { 9 | z = cnt > z ? cnt : z; 10 | cnt = 0; 11 | } 12 | } 13 | for(steps = z > 1; (z = (z + 1) >> 1) > 1; ++steps); 14 | printf("%d\n", steps); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12459.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long int ans[128]; 4 | int main(void){ 5 | ans[0] = 1; ans[1] = 1; 6 | for(int i = 2; i < 81; ++i) ans[i] = ans[i - 1] + ans[i - 2]; 7 | for(int n; scanf("%d", &n) == 1 && n; printf("%lld\n", ans[n])); 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12463.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, b, c, d, e; scanf("%d%d%d%d%d", &a, &b, &c, &d, &e) == 5; ){ 5 | if((a + b + c + d + e)) printf("%d\n", a * b * c * d * d * e * e); 6 | else break; 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12468.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, b, z; scanf("%d %d", &a, &b) == 2 && (a >= 0); ){ 5 | z = (b - a + 100) % 100; 6 | printf("%d\n", z > 50 ? 100 - z : z); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12482.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int N, L, C; scanf("%d %d %d", &N, &L, &C) == 3; ){ 5 | int c = 0; 6 | int l = 1, p = 1, z; 7 | for(int i = 0; i < N; ++i){ 8 | scanf(" "); 9 | scanf("%*s%n", &z); 10 | if(c + z > C){ 11 | c = 0; 12 | if(++l > L) l = 1, ++p; 13 | } 14 | c += z + 1; 15 | } 16 | printf("%d\n", p); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXXIV/12485.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int v[10048]; 4 | int main(void){ 5 | for(int n; scanf("%d", &n) == 1; ){ 6 | int ans = 1, sum = 0; 7 | for(int i = 0; i < n; ++i){ 8 | scanf("%d", v + i); 9 | sum += v[i]; 10 | } 11 | if(!(sum % n)){ 12 | sum /= n; 13 | for(int i = 0; i < n; ++i) 14 | ans += v[i] < sum ? sum - v[i] : 0; 15 | printf("%d\n", ans); 16 | } else puts("-1"); 17 | 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXXV/12502.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | scanf("%*d"); 5 | for(int x, y, z; scanf("%d%d%d", &x, &y, &z) == 3; printf("%d\n", z * (2 * x - y) / (x + y))); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /uva/Volume CXXV/12527.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool safe(int x){ 4 | for(int mask = 0; x; mask |= (1 << (x % 10)), x /= 10) 5 | if(mask & (1 << (x % 10))) return false; 6 | return true; 7 | } 8 | 9 | int s[5001]; 10 | 11 | int main(void){ 12 | s[0] = 1; 13 | for(int i = 1; i <= 5000; ++i) s[i] = s[i - 1] + safe(i); 14 | for(int a, b; scanf("%d %d", &a, &b) == 2; printf("%d\n", s[b] - s[a - 1])); 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume CXXVI/12602.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | char p[16]; 6 | int n; scanf("%d", &n); 7 | for(int i = 0; i < n; ++i){ 8 | scanf("%s", p); 9 | int first = (p[0] - 'A') * 676 + (p[1] - 'A') * 26 + (p[2] - 'A'), second; 10 | sscanf(p + 4, "%d", &second); 11 | puts(abs(first - second) <= 100 ? "nice" : "not nice"); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CXXVI/12611.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n; scanf("%d", &n); 5 | int L, W, xL, xR, yB, yT; 6 | for(int c = 0, r; c++ < n; ){ 7 | scanf("%d", &r); 8 | L = 5 * r; 9 | W = (L * 3) / 5; 10 | xR = (L * 55) / 100; 11 | xL = xR - L; 12 | yT = W >> 1; 13 | yB = -yT; 14 | printf("Case %d:\n%d %d\n%d %d\n%d %d\n%d %d\n", c, xL, yT, xR, yT, xR, yB, xL, yB); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXXVI/12614.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(void){ 6 | int n; scanf("%d", &n); 7 | for(int b, c = 0, y, z; c++ < n; ){ 8 | scanf("%d", &z); 9 | for(b = 0; z--; scanf("%d", &y), b = max(b, y)); 10 | printf("Case %d: %d\n", c, b); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CXXVI/12630.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", (n + 2) * (n + 1) * n * (n - 1) / 24)); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /uva/Volume CXXVII/12704.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int t; scanf("%d", &t); 6 | for(int r, x, y; t--; ){ 7 | scanf("%d %d %d", &x, &y, &r); 8 | double s = r - sqrt(x * x + y * y); 9 | printf("%.2lf %.2lf\n", s, 2.0 * r - s); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /uva/Volume CXXVII/12708.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | for(long long n; t-- && scanf("%lld", &n) == 1; printf("%lld\n", n >> 1)); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /uva/Volume CXXVII/12709.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1 && n; ){ 5 | int maxHeight = 0, volume = 0; 6 | for(int i = 0, h, l, v, w; i < n; ++i){ 7 | scanf("%d %d %d", &l, &w, &h); 8 | v = l * w * h; 9 | if(h > maxHeight){ 10 | maxHeight = h; 11 | volume = v; 12 | } else if(h == maxHeight && v > volume) 13 | volume = v; 14 | } 15 | printf("%d\n", volume); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume I/00113.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00113 - Power of Cryptography 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | double a,b; 7 | int main(void){ 8 | while(scanf("%lf %lf",&a,&b)!=EOF) 9 | printf("%.0lf\n",exp(log(b)/a)); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume I/00136.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00 136 - Ugly Numbers 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | printf("The 1500'th ugly number is 859963392.\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /uva/Volume I/00146.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00146 - ID Codes 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char c[55]; 7 | int i; 8 | int main(void){ 9 | while(gets(c)){ 10 | if(c[0] == '#') break; 11 | for(i = 0; c[i]; i++); 12 | if(std::next_permutation(c,c+i)) printf("%s\n",c); 13 | else printf("No Successor\n"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume II/00271.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const char *r[2] = {"NO", "YES"}; 4 | char s[1024]; 5 | int c, l, ok; 6 | int main(void){ 7 | while(scanf("%s", s) == 1){ 8 | c = 0; ok = 1; 9 | for(l = 0; s[l]; ++l); 10 | while(ok && l--){ 11 | if(s[l] == 'N') ok &= c > 0; 12 | else if(islower(s[l])) c++; 13 | else ok &= c-- > 1; 14 | } 15 | ok &= c == 1; 16 | puts(r[ok]); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume II/00272.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00272 - TEX Quotes 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | char c; 7 | for (int count=0; (c = getchar())!=EOF;){ 8 | if(c=='"') printf((++count&1)? "``":"''" ); 9 | else putchar(c); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume II/00278.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00278 - Chess 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | float a,b; 7 | char c; 8 | int n; 9 | int main(void){ 10 | scanf("%d\n",&n); 11 | while(n){ 12 | n--; 13 | scanf("%c %f %f\n",&c,&a,&b); 14 | if(c == 'k') printf("%.0f\n",ceil(a*b/2.0)); 15 | else if(c == 'K') printf("%.0f\n",(ceil(a/2.0)*ceil(b/2.0))); 16 | else printf("%.0f\n",(a < b? a:b)); 17 | } 18 | } -------------------------------------------------------------------------------- /uva/Volume III/00386.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00386 - Perfect Cubes 3 | ///////////////////////////////// 4 | #include 5 | unsigned int i,j,k,l; 6 | int main(void){ 7 | for(i = 6; i < 201; i++) 8 | for(j = 2; j < i-2; j++) 9 | for(k = j+1; k < i-1; k++) 10 | for(l = k+1; l < i; l++) 11 | if(j*j*j+k*k*k+l*l*l == i*i*i) 12 | printf("Cube = %u, Triple = (%u,%u,%u)\n",i,j,k,l); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume IV/00458.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00458 - The Decoder 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | char c; 7 | while((c = getchar())!=EOF) putchar(c=='\n'?'\n': (char)(c-7)); 8 | } 9 | -------------------------------------------------------------------------------- /uva/Volume IV/00474.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const long double log10_2 = log10(2); 5 | int main(void){ 6 | for(int n; scanf("%d", &n) == 1; ){ 7 | long double t = - log10_2 * n; 8 | int e = int(floor(t)); 9 | printf("2^-%d = %.3lfe%d\n", n, pow(10, t - e), e); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /uva/Volume IX/00900.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long f[55]; 4 | int main(void){ 5 | f[1] = 1; f[2] = 2; 6 | for(int k = 3; k < 55; ++k) f[k] = f[k - 1] + f[k - 2]; 7 | for(int n; scanf("%d",&n)==1 && n; ) printf("%lld\n",f[n]); 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume IX/00913.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00913 - Joana and the Odd Numbers 3 | ///////////////////////////////// 4 | #include 5 | unsigned long long int k; 6 | int main(void){ 7 | while(scanf("%llu",&k)==1){ 8 | k = (k*(k+2)-5)/2*3; 9 | printf("%llu\n",k); 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /uva/Volume IX/00957.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int NN = 100048; 4 | int p[NN]; 5 | 6 | int main(void){ 7 | for(int b, n, y, z; scanf("%d %d", &y, &n) == 2; ){ 8 | for(int i = 0, l = z = 0; i < n; ++i){ 9 | scanf("%d", p + i); 10 | while(p[i] - p[l] >= y) ++l; 11 | if(i - l > z){ 12 | z = i - l; 13 | b = l; 14 | } 15 | } 16 | printf("%d %d %d\n", z + 1, p[b], p[b + z]); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume IX/00991.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int catalan[10] = {1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796}; 4 | int main(void){ 5 | for(int n, p = 0; scanf("%d", &n) == 1; ){ 6 | if(p++) putc(10, stdout); 7 | printf("%d\n", catalan[n - 1]); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /uva/Volume V/00530.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00530 - Binomial Showdown 3 | ///////////////////////////////// 4 | #include 5 | long double n,k,sum,c; 6 | int main(void){ 7 | while(scanf("%Lf %Lf",&n,&k)){ 8 | if(n==0) break; 9 | if(k> n-k) k=n-k; 10 | for(sum=1,c=k;c>0;c--,n--) sum*=n; 11 | for(;k>1;k--) sum/=k; 12 | printf("%.0Lf\n",sum); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uva/Volume V/00545.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | double log10_2 = log10(2); 6 | int main(void){ 7 | int t; scanf("%d", &t); 8 | for(int n; t-- && scanf("%d", &n) == 1; ){ 9 | int x = floor(-n * log10_2); 10 | double k = pow(10, -n*log10_2 - x); 11 | printf("2^-%d = %.3lfE%d\n", n, k, x); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume V/00568.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00568 - Just the Facts 3 | ///////////////////////////////// 4 | #include 5 | unsigned int i,n,last; 6 | int main(void){ 7 | while(scanf("%d",&n)==1){ 8 | for(last = i = 1; !(i>n); ++i){ 9 | last *= i; 10 | while(!(last%10)) last/=10; 11 | if(last>20000) last%=20000; 12 | } 13 | printf("%5u -> %u\n",n,last%10); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /uva/Volume V/00579.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00579 - ClockHands 3 | ///////////////////////////////// 4 | #include 5 | int a,b; 6 | float res; 7 | int main(void){ 8 | scanf("%d:%d",&a,&b); 9 | while(a){ 10 | a%=12; 11 | res = 30*a-5.5*b; 12 | if(res < 0) res = -res; 13 | if(res > 180) res = 360-res; 14 | printf("%.3lf\n",res); 15 | scanf("%d:%d",&a,&b); 16 | } 17 | } -------------------------------------------------------------------------------- /uva/Volume VI/00616.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | for(int i, n, p, z; scanf("%d", &n) == 1 && n >= 0; ){ 6 | for(p = 9; p >= 2; --p){ 7 | z = n; 8 | for(i = 0; i < p && z % p == 1; ++i) 9 | z = z - z/p - 1; 10 | if(i == p && z % p == 0) break; 11 | } 12 | printf("%d coconuts, ", n); 13 | if(p > 1) printf("%d people and 1 monkey\n", p); 14 | else puts("no solution"); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume VI/00679.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int d, n, t; 5 | for(scanf("%d", &t); t-- && scanf("%d %d", &d, &n) == 2; ){ 6 | int x = 1; --n; 7 | for(int i = 1; i < d; n >>= 1, ++i) x = (x << 1) | (n & 1); 8 | printf("%d\n", x); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume VIII/00847.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const char *w[2] = {"Stan wins.","Ollie wins."}; 3 | unsigned int a, div[2] = {2,9}; 4 | int main(void){ 5 | for(bool x; scanf("%u",&a)!=EOF; puts(w[x])) 6 | for(x = 1; a > 1; x=!x) 7 | a = (a+div[x]-1)/div[x]; 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume XII/01225.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define FOR(i,n) for(int i = 0; i < n; ++i) 4 | int ans[16]; 5 | int main(void){ 6 | int t; scanf("%d", &t); 7 | for(int n, z; t-- && scanf("%d", &n) == 1; ){ 8 | FOR(i, 10) ans[i] = 0; 9 | FOR(i, n){ 10 | z = i + 1; 11 | while(z) ++ans[z % 10], z /= 10; 12 | } 13 | FOR(i, 10) printf("%d%c", ans[i], i == 9 ? 10 : 32); 14 | } 15 | return 0; 16 | } 17 | 18 | --------------------------------------------------------------------------------