Posts

Showing posts from 2016

【UVa】11636 - Hello World!

Problem  here Solution water water water water ~~~ #include <iostream> using namespace std ; int main(){ int n; int kase = 1 ; while ( cin >> n){ if (n < 0 ) break ; int ans = 1 ; int cnt = 0 ; while (ans < n){ ans = ans * 2 ; cnt++; } cout << "Case " << kase++ << ": " << cnt << endl; } return 0 ; }

【UVa】10684 - The jackpot

Problem  here Problem As Manuel wants to get rich fast and without too much work, he decided to make a career in gambling. Initially, he plans to study the gains and losses of players, so that, he can identify patterns of consecutive wins and elaborate a win-win strategy. But Manuel, as smart as he thinks he is, does not know how to program computers. So he hired you to write programs that will assist him in elaborating his strategy. Your first task is to write a program that identifies the maximum possible gain out of a sequence of bets. A bet is an amount of money and is either winning (and this is recorded as a positive value), or losing (and this is recorded as a negative value). Input The input set consists of a positive number N ≤ 10000 , that gives the length of the sequence, followed by N integers. Each bet is an integer greater than 0 and less than 1000. The input is terminated with N = 0. Output For each given input set, the output will echo a line wi...

【POJ】Sumsets

Problem  here Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1  2) 1+1+1+1+1+2  3) 1+1+1+2+2  4) 1+1+1+4  5) 1+2+2+2  6) 1+2+4 Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000). Input A single line with a single integer, N. Output The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation). Sample Input 7 Sample Output 6 Source USACO 2005 January Silver Solution 當n為奇數時, dp[n] = dp[n-1]   當n為偶數且含有1時, dp[n] = dp[n-1]   當n為偶數且不含有1時, dp[n]=dp[n/2]   所以當n為偶數時, dp[i] = (dp[i-1] + dp[i/2])%1000000000 #include <iostream> #include <memory.h> using namespace std ; int dp[ 100000...

【POJ】Cow Bowling

Problem  here Description The cows don’t use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5  Then the other cows traverse the triangle starting from its tip and moving “down” to one of the two diagonally adjacent cows until the “bottom” row is reached. The cow’s score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable. Input Line 1: A single integer, N Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle. Output Line 1: The largest sum achievable using the traversal rules Sample Input 5  7  3 8  8 1 0  2 7 4 4  4 5 2 6 5 Sample Output 30 Hint...

【UVa】10298 - Power Strings

Problem  here Given two strings a and b we define a ∗ b to be their concatenation. For  example, if a = ‘abc’ and b = ‘def’ then a ∗ b = ‘abcdef’. If we think of  concatenation as multiplication, exponentiation by a non-negative integer  is defined in the normal way: a  0 = ‘’ (the empty string) and a  (n+1) =  a ∗ (a  n). Input Each test case is a line of input representing s, a string of printable characters.  The length of s will be at least 1 and will not exceed 1 million  characters. A line containing a period follows the last test case. Output For each s you should print the largest n such that s = a  n for some string  a. Sample Input abcd  aaaa  ababab  . Sample Output 1  4  3 Solution #include <iostream> #include <string> //#include <fstream> #define MAX_NUM 9999999 using namespace std ; //ifstream fin("10298.in"); //ofstream fout("10298.out"); ...

【UVa】455 - Periodic Strings

Problem  here   A character string is said to have period k if it can be formed by concatenating one or more repetitions  of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed  by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one  repetition of ”abcabcabcabc”).  Write a program to read a character string and determine its smallest period. Input The first line oif the input file will contain a single integer N indicating how many test case that your  program will test followed by a blank line. Each test case will contain a single character string of up  to 80 non-blank characters. Two consecutive input will separated by a blank line. Output An integer denoting the smallest period of the input string for each input. Two consecutive output are  separated by a blank line. Sample Input 1  HoHoHo Sample Output 2 Solution #inclu...

【TopCoder】ZigZag - 2003 TCCC Semifinals 3

Problem Statement A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.  For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.  Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order. Solution class ZigZag{ public : int longestZigZag( vector < int > n...

【POJ】Balance

Image
Problem  here Description Gigel has a strange “balance” and he wants to poise it. Actually, the device is different from any other ordinary balance.  It orders two arms of negligible weight and each arm’s length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights.  Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced. Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device.  It is guaranteed that will exist at least one solution for each test case at the evaluation. Input The input has the following structu...

【POJ】Painter

Problem  here Description The local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if you mix X ml each of any three different colors, you get X ml of gray. (The paints are thick and “airy”, almost like cake frosting, and when you mix them together the volume doesn’t increase, the paint just gets more dense.) None of the individual colors are gray; the only way to get gray is by mixing exactly three distinct colors, but it doesn’t matter which three. Your friend Emily is an elementary school teacher and every Friday she does a fingerpainting project with her class. Given the number of different colors needed, the amount of each color, and the amount of gray, your job is to calculate the number of kits needed for her class. Input The input consists of one or more test cases, followed by a line containing only zero that signals...

【CodeForces】〖 Educational Codeforces Round 16〗A. King Moves

Image
Problem  here Problem The only king stands on the standard chess board. You are given his position in format “cd”, where c is the column from ‘a’ to ‘h’ and d is the row from ‘1’ to ‘8’. Find the number of moves permitted for the king. Check the king’s moves here  https://en.wikipedia.org/wiki/King_(chess) . King moves from the position e4 Input The only line contains the king’s position in the format “cd”, where ‘c’ is the column from ‘a’ to ‘h’ and ‘d’ is the row from ‘1’ to ‘8’. Output Print the only integer x — the number of moves permitted for the king. Example input e4 output 8 Solution #include <iostream> #include <memory.h> using namespace std ; int main(){ char cc; int c, d, ans = 0 ; cin >> cc >> d; cc -= 48 ; c = cc - '0' ; if (c + 1 <= 8 && c + 1 > 0 ){ ans++; if (d+ 1 > 0 && d+ 1 <= 8 ) ans++; if (d- 1 > 0 ...