task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Positive, negative or zero (PRF0101) Write a program that allows user to enter any integer named a and then checks that the number entered is positive, negative, or zero. Example 1: Please enter any integer: 2 2 is a positive number Example 2: Please enter any integer: -9 -9 is a negative number Example 3: Please enter any integer: 0 The number entered is zero   Input Allow user input a number t. Constraints -100 < t ≀ 100 Output check t is positive, negative or zero, show the result Example Input: 1 Output: Please enter any integer: 1 is a positive number
35,200
sqrt (PRF0102) Write a program that allows user to enter any number named n and then calculates square root of the number entered. Before calculating the square root, the program needs to check whether the input number is negative or not. Hint: use the function double sqrt (double n) of the math.h library to calculate the square root of the parameter n. Example 1: Please enter any number: -2 Accept positive number only! Example 2: Please enter any number: 15 Square root of 15.000000 is 3.872983 Example 3: Please enter any number: 0 Square root of 0.000000 is 0.000000 Input Allow user entry a number Output Calculates square root of the number entered and show result Example Input: 6 Output: Square root of 6.000000 is 2.449490
35,201
Division (PRF0103) Write a program that allows user to enter two integers called m and n then calculates the value of m divided by n . Before dividing, the program must check whether the denominator is zero. Example 1: Please enter numerator: 7 Please enter denominator: 0 The denominator can’t be zero! Example 2: Please enter numerator: -8 Please enter denominator: -5 Result: -8 / -5 = 1.600000 Example 3: Please enter numerator: 10 Please enter denominator: -2 Result: 10 / -2 = -5.000000
35,202
IF (PRF01IF01) Write a program that allows user enter an even number. The program must check and show result. Example format: ouput input   Example 1: Please enter an even number: 2 Thank you. Example 2: Please enter an even number: 1   1 isn't an even number. Example 3: Success #stdin #stdout 0s 4504KB Please enter an even number:Thank you. Success #stdin #stdout 0s 4500KB Please enter an even number:1 isn't an even number. Success #stdin #stdout 0s 4176KB Please enter an even number:0 is zero. Please enter an even number: 0 0 is zero.
35,203
IF (PRF01IF02) Write a program that allows user enter an odd number. The program must check and show result. Example format: ouput  input   Example 1: Please enter an  odd  number: 1 Thank you. Example 2: Please enter an  odd  number: 2   2 isn't an odd number. Example 3: Please enter an odd number: 0 0 is zero.
35,204
IF (PRF01IF03) Write a program that allows user enter a number divisible by 5 but not divisible by 3. The program must check and show result. Example format: ouput  input   Example 1: Please enter a number divisible by 5 but not divisible by 3:15 15 isn't a number divisible by 5 but not divisible by 3. Please enter a number divisible by 5 but not divisible by 3:5 Thank you. Please enter a number divisible by 5 but not divisible by 3:1 1 isn't a number divisible by 5 but not divisible by 3. Please enter a number divisible by 5 but not divisible by 3:0 0 isn't a number divisible by 5 but not divisible by 3. Please enter a number divisible by 5 but not divisible by 3: 15 15 isn't a number divisible by 5 but not divisible by 3. Example 2: Please enter a number divisible by 5 but not divisible by 3: 5 Thank you. Example 3: Please enter a number divisible by 5 but not divisible by 3: 1 1 isn't a number divisible by 5 but not divisible by 3. Example 4: Please enter a number divisible by 5 but not divisible by 3: 0 0 isn't a number divisible by 5 but not divisible by 3.
35,205
IF (PRF01IF04) Write a program that allows user enter a number divisible by 5 and divisible by 2. The program must check and show result. Example format: ouput  input   Example 1: Please enter a number divisible by 5 but not divisible by 3:15 0 isn't a number divisible by 5 but not divisible by 3. Please enter a number divisible by 5 and divisible by 2: 15 15 isn't a number divisible by 5 and divisible by 2. Example 2: Please enter a number divisible by 5 and divisible by 2: 10 Thank you. Example 3: Please enter a number divisible by 5 and divisible by 2: 1 1 isn't a number divisible by 5 and divisible by 2. Example 4: Please enter a number divisible by 5 and divisible by 2: 0 0 isn't a number divisible by 5 and divisible by 2.
35,206
LOOP (PRF02LOOP01) Write a program that prints the alphabet using uppercase characters. Example Output: Alphabet A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
35,207
LOOP (PRF02LOOP02) Write a program that prints the alphabet using lowercase characters. Example Output: alphabet a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
35,208
LOOP (PRF02LOOP03) Write a program that displays the number-triangle with N lines and is formatted as below. Example 1: Please enter the positive integer N: 0 N must be greater than 0! Example 2: Please enter the positive integer N: 5 The number-triangle that has 5 lines is: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Note by Editorial Board This program outputs prompts as though it is running in an interactive environment. SPOJ isn't interactive, and you'll get a better idea of what the real output should look like if you run your program in a similar non-interactive environment, such as ideone . I've done this and added the output below to give an alternative view of how the output should be formatted. Note the lack of newlines after the prompts. It looks like the problem setter has chosen the exact judge, so your output must match exactly. Example Input: 0 Output: Please enter the positive integer N:N must be greater than 0! Input: 5 Output: Please enter the positive integer N:The number-triangle that has 5 lines is: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
35,209
LOOP (PRF02LOOP04) Write a program that displays the number-triangle with N lines and is formatted as below. Example 1: Please enter the positive integer N: 0 N must be greater than 0! Example 2: Please enter the positive integer N: 4 The number-triangle that has 4 lines is: 1 1 2 1 2 3 1 2 3 4 Note by Editorial Board This program outputs prompts as though it is running in an interactive environment. SPOJ isn't interactive, and you'll get a better idea of what the real output should look like if you run your program in a similar non-interactive environment, such as ideone . I've done this and added the output below to give an alternative view of how the output should be formatted. Note the lack of newlines after the prompts. It looks like the problem setter has chosen the exact judge, so your output must match exactly. Example Input: 0 Output: Please enter the positive integer N:N must be greater than 0! Input: 4 Output: Please enter the positive integer N:The number-triangle that has 4 lines is: 1 1 2 1 2 3 1 2 3 4
35,210
ARRAY (INPUT,OUTPUT) (PRF03ARRAY0101) Write a program that allows users to enter a numeric array with N elements. The program will display them. Example 1 How many element of numeric array: 5 The value of a[1] is: 2 The value of a[2] is: -1 The value of a[3] is: 8 The value of a[4] is: 3 The value of a[5] is: 0   5 element of numeric array: The value of a[1] is:2 The value of a[2] is:-1 The value of a[3] is:8 The value of a[4] is:3 The value of a[5] is:0 Example 2 How many element of numeric array: 3 The value of a[1] is: 2 The value of a[2] is: 1 The value of a[3] is: 3   3 element of numeric array: The value of a[1] is:2 The value of a[2] is:1 The value of a[3] is:3 Note by Editorial Board This program outputs prompts as though it is running in an interactive environment. SPOJ isn't interactive, and you'll get a better idea of what the real output should look like if you run your program in a similar non-interactive environment, such as ideone . I've done this and added the output below to give an alternative view of how the output should be formatted. Note the lack of newlines after the prompts. It looks like the problem setter has chosen the exact judge, so your output must match exactly. Example Input: 5 2 -1 8 3 0 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is:The value of a[4] is:The value of a[5] is: 5 element of numeric array: The value of a[1] is:2 The value of a[2] is:-1 The value of a[3] is:8 The value of a[4] is:3 The value of a[5] is:0 Input: 3 2 1 3 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is: 3 element of numeric array: The value of a[1] is:2 The value of a[2] is:1 The value of a[3] is:3
35,211
ARRAY (SORT) (PRF03ARRAY0102) Write a program that allows users to enter a numeric array with N elements. The program will sort and display them. Example 1 How many element of numeric array: 5 The value of a[1] is: 2 The value of a[2] is: -1 The value of a[3] is: 8 The value of a[4] is: 3 The value of a[5] is: 0   ASC: 5 element of numeric array: The value of a[1] is:-1 The value of a[2] is:0 The value of a[3] is:2 The value of a[4] is:3 The value of a[5] is:8 Example 2 How many element of numeric array: 3 The value of a[1] is: 2 The value of a[2] is: 1 The value of a[3] is: 3   ASC: 3 element of numeric array: The value of a[1] is:1 The value of a[2] is:2 The value of a[3] is:3 Note by Editorial Board This program outputs prompts as though it is running in an interactive environment. SPOJ isn't interactive, and you'll get a better idea of what the real output should look like if you run your program in a similar non-interactive environment, such as ideone . I've done this and added the output below to give an alternative view of how the output should be formatted. Note the lack of newlines after the prompts. It looks like the problem setter has chosen the exact judge, so your output must match exactly. Example Input: 5 2 -1 8 3 0 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is:The value of a[4] is:The value of a[5] is: ASC: 5 element of numeric array: The value of a[1] is:-1 The value of a[2] is:0 The value of a[3] is:2 The value of a[4] is:3 The value of a[5] is:8 Input: 3 2 1 3 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is: ASC: 3 element of numeric array: The value of a[1] is:1 The value of a[2] is:2 The value of a[3] is:3
35,212
ARRAY (SORT) (PRF03ARRAY0103) Write a program that allows users to enter a numeric array with N elements. The program will sort and display them. Example 1 How many element of numeric array: 5 The value of a[1] is: 2 The value of a[2] is: -1 The value of a[3] is: 8 The value of a[4] is: 3 The value of a[5] is: 0   DESC: 5 element of numeric array: The value of a[1] is:8 The value of a[2] is:3 The value of a[3] is:2 The value of a[4] is:0 The value of a[5] is:-1 Example 2 How many element of numeric array: 3 The value of a[1] is: 2 The value of a[2] is: 1 The value of a[3] is: 3   DESC: 3 element of numeric array: The value of a[1] is:3 The value of a[2] is:2 The value of a[3] is:1 Note by Editorial Board This program outputs prompts as though it is running in an interactive environment. SPOJ isn't interactive, and you'll get a better idea of what the real output should look like if you run your program in a similar non-interactive environment, such as ideone . I've done this and added the output below to give an alternative view of how the output should be formatted. Note the lack of newlines after the prompts. It looks like the problem setter has chosen the exact judge, so your output must match exactly. Example Input: 5 2 -1 8 3 0 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is:The value of a[4] is:The value of a[5] is: DESC: 5 element of numeric array: The value of a[1] is:8 The value of a[2] is:3 The value of a[3] is:2 The value of a[4] is:0 The value of a[5] is:-1 Input: 3 2 1 3 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is: DESC: 3 element of numeric array: The value of a[1] is:3 The value of a[2] is:2 The value of a[3] is:1
35,213
ARRAY (MIN) (PRF03ARRAY0104) Write a program that allows users to enter a numeric array with N elements. The program will display minimum of the entered array values. Example 1 How many element of numeric array: 5 The value of a[1] is: 2 The value of a[2] is: -1 The value of a[3] is: 8 The value of a[4] is: 3 The value of a[5] is: 0   Min:-1 Example 2 How many element of numeric array: 3 The value of a[1] is: 2 The value of a[2] is: 1 The value of a[3] is: 3   Min:1 Note by Editorial Board This program outputs prompts as though it is running in an interactive environment. SPOJ isn't interactive, and you'll get a better idea of what the real output should look like if you run your program in a similar non-interactive environment, such as ideone . I've done this and added the output below to give an alternative view of how the output should be formatted. Note the lack of newlines after the prompts. It looks like the problem setter has chosen the exact judge, so your output must match exactly. Example Input: 5 2 -1 8 3 0 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is:The value of a[4] is:The value of a[5] is: Min:-1 Input: 3 2 1 3 Output: How many element of numeric array:The value of a[1] is:The value of a[2] is:The value of a[3] is: Min:1
35,214
String - Input/Output (PRF04STRING01) Create a program that allows user to enter a text and print it out. Input text Output text Example1 Input: Lap trinh can ban C. Output: Lap trinh can ban C. Example2 Input: Cong nghe thong tin. Output: Cong nghe thong tin. Example3 Input: Infomation Technology. Output: Infomation Technology.
35,215
String - lTrim (PRF04STRING02) Create a program that allows user to enter a text, then removes whitespace from the left side of a string and print it out. Input              text Output text Example1 Input: Lap trinh can ban C Output: Lap trinh can ban C Example2 Input: Information Technology Output: Information Technology Example1 Input: Cong nghe thong tin Output: Cong nghe thong tin
35,216
String - rTrim (PRF04STRING03) Create a program that allows user to enter a text, then removes whitespace from the right side of a string and print it out. Input text                   Output text Example 1 Input: Lap trinh can ban C Output: Lap trinh can ban C Example 2 Input: Information Technology Output: Information Technology Example 3 Input: Cong nghe thong tin Output: Cong nghe thong tin
35,217
String - trim (PRF04STRING04) Create a program that allows user to enter a text, then removes whitespace from the left side and right size of a string, removes all 2 whitespaces from inside that string and print it out. Input              text           text             Output text text Example 1 Input: Lap trinh can ban C Output: Lap trinh can ban C Example 2 Input: Information Technology Output: Information Technology Example 3 Input: Cong nghe thong tin Output: Cong nghe thong tin
35,218
String - Limit (PRF04STRING05) Allows the user to enter a string of up to 10 characters, and output the result. Input text. Output The first 10 characters of the input. Example Input: 123456789011 Output: 1234567890
35,219
InputOutut Array 2D (AR1NHAPINMANG) Allow user enter size n x m of array, then entry each element in array Input n m pt00 pt01 .... pt10 pt11 .... Output pt00 pt01 .... (use tab) pt10 pt11 .... Example Input: 2 3 1 2 3 4 5 6 Output: 1 2 3 4 5 6
35,220
Input Array 2D and cal AVG (AR2AVG) Allow user enter size n x m of array, then entry each element in array, show agv value Input n m pt00 pt01 .... pt10 pt11 .... Output avg Example Input: 2 3 1 2 3 4 5 6 Output: 3.500000
35,221
Nhap Mang Va Tim Max (AR2MAX) Allow the user to enter the size of the array n × m, then enter the value of each element in the array. Find and print the maximum value. Input n m pt00 pt01 .... pt10 pt11 .... Output The maximum value from the array Example Input: 2 3 1 2 3 4 5 6 Output: 6
35,222
Nhap Mang Va Tim Min (AR2MIN) Allow the user to enter the size of the array n × m, then enter the value of each element in the array. Find and print the minimum value. Input n m pt00 pt01 .... pt10 pt11 .... Output The minimum value from the array. Example Input: 2 3 1 2 3 4 5 6 Output: 1
35,223
Nhap Mang Va Tinh Tong (AR2SUM) Allow the user to enter the size of the array n × m, then enter the value of each element. Calculate and print the sum of the values. Input n m pt00 pt01 .... pt10 pt11 .... Output print the sum of the array elements. Example Input: 2 3 1 2 3 4 5 6 Output: 21
35,224
Book (Struct) (AR2BOOK) Build a struct that stores the book information in a structure: title: string author: string subject: string book_id: integer Allow users to enter and display information 1 book. Input title author subject book_id Output title : author : subject : book_id Example Input: nghin le mot dem khong tac gia co tich 1 Output: nghin le mot dem : khong tac gia : co tich : 1
35,225
Books (Struct) (ARBOOK2) Build a struct that stores book information in a structure: title: string author: string subject: string book_id: integer Allow the user to specify the number of books to save, enter information about the books, then display a list. Input n title1 author1 subject1 book_id1 title2 author2 subject2 ..... Output title1 : author1 : subject1 : book_id1 title2 : author2 : subject2 : book_id2 ..... Example Input: 2 thang quy nho nguyen nhat anh thanh xuan vuon truong 2 bo cau khong dua thu nguyen nhat anh thanh xuan vuon truong 1 Output: thang quy nho : nguyen nhat anh : thanh xuan vuon truong : 2 bo cau khong dua thu : nguyen nhat anh : thanh xuan vuon truong : 1
35,226
Employee (Struct) (AREMPLOYEE) Build a struct that stores employee information in a structure: id: string name: string gender: string salary: integer Allow user to enter information for one employee, then display it. Input ID name gender salary Output ID : name : gender : salary Example Input: NV02 Tran Thanh Hung M 1500 Output: NV02 : Tran Thanh Hung : M : 1500
35,227
Employees (Struct) (AR2EMPLOYEES) Build a struct that stores employee information in a structure: id: string name: string gender: string salary: integer Allow user to enter the number of employees and information for each one, then display a list. Input n ID1 name1 gender1 salary1 ID2 name2 gender2 salary2 ..... Output ID1 : name1 : gender1 : salary1 ID2 : name2 : gender2 : salary2 ..... Example Input: 3 NV01 Tran Thanh Hung F 1500 NV02 Nguyen Ngoc Bich M 2000 NV03 Tran Trung Hau F 1700 Output: NV01 : Tran Thanh Hung : F : 1500 NV02 : Nguyen Ngoc Bich : M : 2000 NV03 : Tran Trung Hau : F : 1700
35,228
Suma de Numeros (SIS110G401) Cuando comenzamos a sumar utilizamos las manos contamos un numero con la mano izquierda y otroa con la mano derecha, Vamos a sumar Entrada Un numero entero que representa la mano izquierda y otra que representa la mando derecha Salida Sume los dedos de ambas manos Ejemplo Entrada: 4 4 Salida: 8
35,229
ICPC (THR18A) The Iranian ChamPions Cup (ICPC), the most prestigious football league in Iran, is reaching its end, and people are eagerly waiting for the finals, which happened to be between the two most popular Iranian teams, Persepolis and Esteghlal. The ICPC finals consist of two matches, with each team competing as the home team in one match. The winning team is determined by aggregate score, the sum of the scores of the two matches. For example, if the scores of the two matches are Persepolis 6–0 Esteghlal in the first match, and Esteghlal 3–1 Persepolis in the second match, then the aggregate score will be Persepolis 7–3 Esteghlal, meaning that Persepolis is the winner. If aggregates are equal, the away goals rule is used to determine the winner, in which case the winner is the team that scored the most goals in the match it played away from home. If the result is still equal, a penalty shootout is required. Hana, an avid football fan, is trying to figure out various scenarios in which her favorite team wins the finals. To this end, she aims to write a program that gets as input the number of goals in the two matches, and decides which team is the winner if it can be derived from the aggregate scores and the away goals rule, otherwise declares that the match goes to penalty kicks. You are going to help Hana write such a program. Input The first line of the input contains two space-separated integers p 1 and s 1 , where p 1 and s 1 are the number of goals scored by Persepolis and Esteghlal, respectively, in the first match in which Persepolis is the home team. The second line contains two space-separated integers s 2 and p 2 , where s 2 and p 2 are the number of goals scored by Esteghlal and Persepolis, respectively, in the second match in which Esteghlal is the home team. All input integers are between 0 and 20, inclusively. Output In the output, print the name of the winning team, either Persepolis or Esteghlal, if the winner can be determined by the aggregate scores and the away goals rule. Otherwise, print Penalty. Example Input 1: 3 0 2 1 Output 1: Persepolis Input 2: 3 1 2 0 Output 2: Esteghlal Input 3: 2 0 2 0 Output 3: Penalty
35,230
Chaarshanbegaan at Cafebazaar (THR18B) Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbegaan. As a techie organizing a game for techies, you would rather use a smart screen and write a program to calculate the scores instead of hanging a traditional dartboard and scoring the shots manually. Your program must get the coordinates of dart shots for a player and calculate his/her total score. The score for each dart shot (at point (x, y)) is calculated based on its distance from the center of the dartboard (point (0, 0)). If the distance is d millimeters, the score is calculated based on the following table: Input The first line of the input contains a single integer N as the number of dart shots for a player (1 ≀ N ≀ 100). Each of the next N lines contains two space-separated integers as the coordinates (x, y) of a dart shot. The coordinates are in millimeters and their absolute values will not be greater than 300. Output Print a single line containing the total score of the player. Example Input 1: 2 4 7 -31 -5 Output 1: 18 Input 2: 3 12 -16 -180 100 152 10 Output 2: 11
35,231
Denver and Emma (DNEQ) Denver really likes Emma and he wants to impress her but Emma only like those boys who are good in problem solving. So Emma gave a problem to Denver and told him that she will only be impressed by him if he is able to solve the problem. As Denver is very bad in problem solving so he wants your help him. The problem statement is... There are N men and their weight is increased by A kg each, when one of the men whose weight is B kg is replaced by a new man so you need to find the weight of new men. Input The first line of the input consist of a single integer number t which determines the number of test cases. In each of next t lines there are three integer number N , A and B denoting the number of men which is N , the weight increased by A amount and the weight of the men which is replaced i.e. B kg. Constraints 1 ≀ t ≀ 10 1 ≀ N ≀ 10 2 1 ≀ A ≀ 99 1 ≀ B ≀ 99 Example Input: 2 8 1.5 65 6 3 80 Output: 77 98 Explanation: In test case 1 st there are 8 men and their weight is increased by 1.5kg so total weight increased = (8 × 1.5)kg = 12kg The weight of the new men = (12 + 65) = 77kg.
35,232
Count Set Bits (CSETB) You are given a number n and you need to find the total number of set bits in the binary representation of all integers from 1 to n inclusive. Input The first line of input contains t denoting the number of test cases. Next t lines contain a single integer n . Constraints 1 ≀ t ≀ 20 1 ≀ n ≀ 10 4 Output Output a single integer which is the total number of set bits in the binary representation of all integers from 1 to n inclusive. Example Input: 5 7 4 10 9 6 Output: 12 5 17 15 9
35,233
simple palindromes (FIZBBZIF) A palindrome is a word or phrase which is the same backwards and forwards.  Capitalisation and punctuation don't count. Input A list of words or phrases one at a time Output Each word, followed by " is a palindrome" or " is not a palindrome" Example Input: dad Hannah A man, a plan, a canal, Panama! Output: dad is a palindrome Hannah is a palindrome A man, a plan, a canal, Panama! is a palindrome
35,234
Eratosthenes Prime numbers (SIOENES) Given an integer 'N', print count all the prime numbers which are less than or equal to 'N'. Input description First line of input is an integer T (number of testcases) and integer T will be followed by T lines, where each line will contain an integer N. Output description Output will consists of T lines. Each line will have count of prime numbers not greater than 'N'. Constraint 1<=T<=10000 1<=N<=1000000 Example Input: 2 9 11 output 4 5
35,235
Kevin likes array (KVAR) Kevin likes array. This time he has a array A[1], A[2] ... A[N] . He calls a array beautiful, if the number of inversions is equal to the number of its local inversions. The number of inversion is equal to the number of pairs (i;j) such that 1 ≀ i ≀ j ≀ N and A[i] > A[j] and number of its local inversions is the number of integers i such that 1 ≀ i < N and A[i] > A[i+1] . So help him to find whether the array is beautiful or not. Print YES for a corresponding test case if it is beautiful and NO otherwise. Constraints 1 ≀ T ≀ 10 1 ≀ N ≀ 100 1 ≀ A[i] ≀ 100 Example Input: 4 1 1 2 2 1 3 3 2 1 4 1 3 2 4 Output: YES YES NO YES Explanation Case 1.Here N = 1, so we have no pairs (i; j) with 1 ≀ i < j ≀ N. So the number of inversions is equal to zero. The number of local inversion is also equal to zero. Hence this array is beautiful. Case 2. Here N = 2, and we have one pair (i; j) with 1 ≀ i < j ≀ N, the pair (1; 2). Since A[1] = 2 and A[2] = 1 then A[1] > A[2] and the number of inversions is equal to 1. The number of local inversion is also equal to 1 since we have one value of i for which 1 ≀ i < N (the value i = 1) and A[i] > A[i+1] for this value of i since A[1] > A[2]. Hence this permutation is also good. Case 3. Here N = 3, and we have three pairs (i; j) with 1 ≀ i < j ≀ N. We have A[1] = 3, A[2] = 2, A[3] = 1. Hence A[1] > A[2], A[1] > A[3] and A[2] > A[3]. So the number of inversions is equal to 3. To count the number of local inversion we should examine inequalities A[1] > A[2] and A[2] > A[3]. They both are satisfied in our case, so we have 2 local inversions. Since 2 ≠ 3 this array is not beautiful. Case 4. Here we have only one inversion and it comes from the pair (2; 3) since A[2] = 3 > 2 = A[3]. This pair gives also the only local inversion in this permutation. Hence the number of inversions equals to the number of local inversions and equals to one. So this array is beautiful
35,236
Star Justify (STARJUST) Pad strings with asterisks on either side to fill a given size of space. The string should appear in the centre, but if equal padding is not possible then the shorter padding should appear on the right of the string. Input The first line of the input contains the number of test cases. Each consecutive pair of lines provides an integer representing the size of space to fill and a string that needs to be padded. Output One string per line, which should be the asterisk-padded strings Example Input: 3 10 hello 10 milk 30 to be, or not to be Output: ***hello** ***milk*** ******to be, or not to be*****
35,237
Clock hands on a different planet (ALIENCLK) On planet Aberus, the day is broken into 16 hours. Each hour contains 80 minutes. An Aberitian child is using the clock face to learn about angles in her maths class. The teacher asks her to work out what angle is held between the two hands of the clock at different times of day, and report the answer in degrees. She'd rather use a program to calculate this, can you help her? Input The first input line contains a positive integer representing the number of test instances. Each subsequent line contains a clock time in the format HH:MM. Output For each of the clock times given, report the angle (between 0 and 359 degrees) that is held between the two hands of the clock, rounded to one decimal place. Note: If the angle is more than 180, subtract the angle from 360. Example Input: 3 12:00 06:00 13:15 Output: 90.0 135.0 130.8
35,238
Patio paving slabs (PATIOSLB) You are building a patio, which is made up of stone paving slabs and grout (the material between the slabs which holds the patio together). You need to lay a row of paving slabs to start the construction of a patio. You have bought a set of slabs of various different lengths. Which slabs should you choose from the set in order to fill the row perfectly? Remember that you'll need to leave a 1 cm gap for grout between each slab, and also a 1 cm gap for grout at either end of the row. Input The first line of input gives you the number of test cases. For each subsequent test case, there are three lines of input. In the first line you are given a positive integer representing the total length in cm of the row that you need to fill. The next line gives you a positive integer stating the number of patio slabs available. The third line gives the lengths in cm of the patio slabs that you have available, separated by spaces. Output Output the set of slab lengths that you have chosen, sorted in descending order, and separated by spaces. If there is no solution, output -1. If there are multiple solutions, output the solution that makes use of the largest of the slabs. Example Input: 2 100 9 20 40 30 24 31 18 55 40 23 50 4 20 20 30 40 Output: 55 23 18 -1
35,239
Factorising odd numbers (FCTORISE) Any odd integer, m, greater than 1, can be factorised by expressing it as the difference of two squares, since if m = x^2 - y^2 then m = (x+y)*(x-y), so m has factors x+y and x-y. For example, 15 = 4^2 - 1^2 = (4+1)*(4-1) = 5*3. Rearranging the first equation above gives m + y^2 = x^2, so we can find x and y by evaluating m + y^2 for y = 0, 1, 2 ... until the perfect square x^2 is found. Write a program to factorise an odd integer greater than 1 by expressing it as the difference of two squares. Input The first line of the input is the number of cases (a positive integer, n, on one line). This is followed by n odd integers greater than 1, each on a line of its own. Output The output should consist of: m = x^2 - y^2 = (x+y)*(x-y) (with the actual values for m, x, and y). Example Input: 2 15 9929 Output: 15 = 4^2 - 1^2 = 5*3 9929 = 4965^2 - 4964^2 = 9929*1
35,240
Timing Morse (TIMORSE) The International Morse Code specifies that the duration of a dash is three times that of a dot; the duration of the interval between the dots and dashes in a single character is the same as the duration of a dot; the duration of the interval between characters in a word is three times the duration of a dot; and the duration of the interval between words is seven times the duration of a dot. The Morse Code length of a message is the duration of the message as transmitted in Morse code, expressed as a multiple of the duration of a dot. For example, the Morse Code length of the message "hi" (····  ··) is 7 + 3 + 3 = 13.  The Morse Code length of the message "joe" (·–––  –––  ·) is 13 + 3 + 11 + 3 + 1 = 31, and the Morse Code length of the message "hi joe" is 13 + 7 + 31 = 51. Write a program to calculate the Morse Code length of messages. For ease of reference, the Morse code you will need (the alphabet) is: a ·– j ·––– s ··· b –··· k –·– t – c –·–· l ·–·· u ··– d –·· m –– v ···– e · n –· w ·–– f ··–· o ––– x –··– g ––· p ·––· y –·–– h ···· q ––·– z ––·· i ·· r ·–· Input The input to the program should consist of a positive integer, n, on one line, followed by n messages, each on a line of its own. Each message can consist of the letters "a" to "z" plus the space character (any other characters can be ignored). Output The output should consist of the n messages each followed by ": length = L", where L is the Morse Code length of the message. Example Input: 2 hi joe sos Output: hi joe: length = 51 sos: length = 27
35,241
Ascii Art Darkness Calculator (AARTDARK) In simple ASCII images a dark pixel is represented as an asterisk * and a bright pixel as a full stop . The darkness of an ASCII art image is defined as (number of * characters) / (number of characters in total). Input The first line of input contains the number of images. Subsequent lines of input contain either an integer defining the size of an "image", or image data. All images are square, and the size of the image is the length of a side. Output For each input image, output the darkness of that image, one per line. Example Input: 2 2 .. .* 8 ........ ........ ..*..*.. ........ .*....*. ..****.. ........ ........ Output: 0.25 0.125
35,242
Cinema seating, covid 19 version (CINECOVI) This challenge involves writing a simple cinema seat booking system.  Cinemas are opening up post lockdown, but they have to seat customers differently. No longer is it OK to try and fill all seats, or allow customers to choose their own.   Each group who book through the booking office need to sit together, and there needs to be an empty seat between groups on all sides (left, right, infront and behind). The seats in the cinema are filled from the bottom left, in order of booking. If a row fills up, we start filling on the left hand side of the next row back. (For the purposes of illustration, imagine that the screen is at the base of the grid and that we fill the front row first). So, if we have a cinema with just three rows, with 10 seats per row, and the following group sizes in our booking 5 4 3 2 1 we will be able to seat the first group of 5 at bottom left, then the group of 4 next to them after a gap of one seat. We'll leave a row behind, then the back row will fit the 3, 2 and 1. The seats would look like this: Seats: 1110110100 0000000000 1111101111 If there were more bookings and instead we had the group sizes 5 4 3 2 1 1 2 3 4 5 we would have to turn away some groups. In this case, the seats would look like this: Seats: 1110110101 0000000000 1111101111 Failed to seat: 2 3 4 5 Input The first line of your input contains two integers, which specify the size of the cinema in terms of number of rows and number of seats per row. Output Your output  should be the word "Seats:" on a line of its own, followed by an array of 1s and 0s represented the seats in the cinema (1=full, 0=empty). If you find that there are groups who cannot be seated in the cinema because it is too full, output the phrase "Failed to seat:" on the next line, followed by the sizes of the unseated groups in order, one per line. Example Input: 5 20 7 9 8 7 6 5 4 3 2 1 9 2 11 4 8 Output: Seats: 11111101111101111000 00000000000000000000 11111111011111110000 00000000000000000000 11111110111111111000 Failed to seat: 3 2 1 9 2 11 4 8
35,243
Tiebreaker (TIEBRKR) Are you the winner? Let's find out in this tiebreaker. Given a list of ticket numbers and the status of WIN or NOWIN, identify the winning tickets and output them in order from lowest to highest. Input An integer t, 1 <= t <= 100, denoting the number of tickets to check. For each ticket, there are two lines with information. The first line is an integer n, which is the ticket number. The second line is s, the status of the ticket number. The ticket status is either WIN or NOWIN. Output One line that lists the number of winning tickets. One line that lists the ticket numbers, sorted from highest to lowest. Each number is separated by a space. Example Input: 5 5011 WIN 4000 NOWIN 101 WIN 500 NOWIN 501 WIN Output: 3 101 501 5011
35,244
Fun With Fibonacci (DCP) The  Fibonacci sequence  is a set of numbers that starts with a  zero , followed by a one, and proceeds based on the rule that, each number ( called a Fibonacci number ) is equal to the sum of the previous two numbers. If the Fibonacci sequence is denoted  F(n) , where  n  is the term in the sequence, Now your task is to find the total number of odd Fibonacci numbers and total number of even Fibonacci numbers between a range ( consider 0 as an even number ). Input First Line of the input contains  T,  representing the number of test case  (1 <= T <= 50) . For each test case contains two integers N and M where (1 <= N <= 10 18 ) and (1 <= M <= 10 18 ) . Output You have to calculate total number of odd  Fibonacci numbers and total number of even  Fibonacci numbers between N th  Fibonacci number and M th  Fibonacci number. For each test case print case number with the desire answer as show sample output. Example Input: 2 2 6 1 5 Output: Case 1: Odd = 4 Even = 1 Case 2: Odd = 3 Even = 2
35,245
Pavement Blood (PAVEMENT) You live in a dangerous neighbourhood. Each night you walk home, the path home consists of a N by 3 tiled path (N rows). Recently there have been many murders, and the tiles have been stained with blood. Your feet are large, each step you take on the path home will cover a 2 by 2 grid of tiles, gathering some of the blood that has been left on the tiles onto your boots. To make things simpler, you have marked each tile with a number representing the amount of blood on the tile, the i-th row and j-th column having value a[i][j]. Each step will add an amount of blood onto your boots (equal to the sum of the 2×2 grid of the tiles you step on). Moreover, some tiles may have soap on them and such, which previous murderers have dropped whilst trying to clean up after themselves, these will have negative numbers representing how much they clean your boots. Once you step on a tile, to get home as quick as possible, you cannot step on the same tile again. You are smart, you know that the more blood you gather onto your boots, the less likely people are to attack you. You wish to gather as much blood as possible onto your boots by the end of your journey across the pavement. Your legs are long and you can skip over as many tiles as you wish on your way home. Given the values of all the tiles, determine the maximum amount of blood you can get on your boots. NOTE: Should you take a step, it must cover a 2×2 grid of tiles, you can't have half a foot out of the path or otherwise. Input The first line contains a single integer n, the length of the path home. The next N lines contain 3 integers each, representing the values of tiles that lead home. 1 <= N <= 100,000 -1,000,000,000 <= a[i][j] <= 1,000,000,000 Output A single integer representing the maximum amount of you can pick up on your boots. Example Input: 7 7 1 11 6 3 9 -4 -2 8 -8 -10 17 5 4 -3 -1 2 0 3 9 -3 Output: 50
35,246
Pan-da (PANANDDA) You are given a string, which only consists of the letters P, A, N and D. In one operation, you can insert a letter somewhere into the string. Determine the minimum number of insertions/operations so that if you cut the string into pieces, there is a possible cut so that all the pieces are either "DA" or "PAN". Input Your first and only line will contain the string (of length N, 1 <= N <= 1e5). Output Output a single integer representing the minimum number of insertions needed to be able to cut the string into substrings of only PAN and DA. Example Input 1: PANDA Output 1: 0 Input 2: DPANA Output 2: 2
35,247
Score : 1100 points Problem Statement Let f(A, B) , where A and B are positive integers, be the string satisfying the following conditions: f(A, B) has length A + B ; f(A, B) contains exactly A letters A and exactly B letters B ; The length of the longest substring of f(A, B) consisting of equal letters (ex., AAAAA or BBBB ) is as small as possible under the conditions above; f(A, B) is the lexicographically smallest string satisfying the conditions above. For example, f(2, 3) = BABAB , and f(6, 4) = AABAABAABB . Answer Q queries: find the substring of f(A_i, B_i) from position C_i to position D_i (1-based). Constraints 1 \leq Q \leq 10^3 1 \leq A_i, B_i \leq 5 \times 10^8 1 \leq C_i \leq D_i \leq A_i + B_i D_i - C_i + 1 \leq 100 All input values are integers. Partial Score 500 points will be awarded for passing the testset satisfying 1 \leq A_i, B_i \leq 10^3 . Input Input is given from Standard Input in the following format: Q A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 : A_Q B_Q C_Q D_Q Output For each query i in order of input, print a line containing the substring of f(A_i, B_i) from position C_i to position D_i (1-based). Sample Input 1 5 2 3 1 5 6 4 1 10 2 3 4 4 6 4 3 7 8 10 5 8 Sample Output 1 BABAB AABAABAABB A BAABA ABAB
35,248
Score : 200 points Problem Statement The development of algae in a pond is as follows. Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000 , the following formula holds: x_{i+1} = rx_i - D You are given r , D and x_{2000} . Calculate x_{2001} , ... , x_{2010} and print them in order. Constraints 2 ≀ r ≀ 5 1 ≀ D ≀ 100 D < x_{2000} ≀ 200 All values in input are integers. Input Input is given from Standard Input in the following format: r D x_{2000} Output Print 10 lines. The i -th line ( 1 ≀ i ≀ 10 ) should contain x_{2000+i} as an integer. Sample Input 1 2 10 20 Sample Output 1 30 50 90 170 330 650 1290 2570 5130 10250 For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50 . Sample Input 2 4 40 60 Sample Output 2 200 760 3000 11960 47800 191160 764600 3058360 12233400 48933560
35,249
Quick Sort Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode: Partition(A, p, r) 1 x = A[r] 2 i = p-1 3 for j = p to r-1 4 do if A[j] <= x 5 then i = i+1 6 exchange A[i] and A[j] 7 exchange A[i+1] and A[r] 8 return i+1 Quicksort(A, p, r) 1 if p < r 2 then q = Partition(A, p, r) 3 run Quicksort(A, p, q-1) 4 run Quicksort(A, q+1, r) Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers. Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance). Input The first line contains an integer n , the number of cards. n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space. Output In the first line, print the stability (" Stable " or " Not stable ") of this output. In the following lines, print the arranged cards in the same manner of that of the input. Constraints 1 ≀ n ≀ 100,000 1 ≀ the number of a card ≀ 10 9 There are no identical card in the input Sample Input 1 6 D 3 H 2 D 1 S 3 D 2 C 1 Sample Output 1 Not stable D 1 C 1 D 2 H 2 D 3 S 3 Sample Input 2 2 S 1 H 1 Sample Output 2 Stable S 1 H 1
35,250
Max Score: $1400$ Points Problem Statement E869120 defined a sequence $a$ like this: $a_1=a_2=1$, $a_{k+2}=a_{k+1}+a_k \ (k \ge 1)$ He also defined sequences $d_1, d_2, d_3, \dots , d_n$, as the following recurrence relation : $d_{1, j} = a_j$ $d_{i, j} = \sum_{k = 1}^j d_{i - 1, k} \ (i \ge 2)$ You are given integers $n$ and $m$. Please calculate the value of $d_{n, m}$. Since the answer can be large number, print the answer modulo $998,244,353$. Can you solve this problem??? Input The input is given from standard input in the following format. $n \quad m$ Output Print $d_{n, m}$ modulo $998,244,353$. Constraints $1 \le n \le 200,000$ $1 \le m \le 10^{18}$ Subtasks Subtask 1 [ $100$ points ] The testcase in this subtask satisfies $1 \le n, m \le 3,000$. Subtask 2 [ $170$ points ] The testcase in this subtask satisfies $1 \le m \le 200,000$. Subtask 3 [ $230$ points ] The testcase in this subtask satisfies $1 \le n \le 3$. Subtask 4 [ $420$ points ] The testcase in this subtask satisfies $1 \le n \le 1000$. Subtask 5 [ $480$ points ] There are no additional constraints. Sample Input 1 4 7 Sample Output 1 176 The sequence is following: $d_{i, 1}$ $d_{i, 2}$ $d_{i, 3}$ $d_{i, 4}$ $d_{i, 5}$ $d_{i, 6}$ $d_{i, 7}$ $d_1$ 1 1 2 3 5 8 13 $d_2$ 1 2 4 7 12 20 33 $d_3$ 1 3 7 14 26 46 79 $d_4$ 1 4 11 25 51 97 176 As a result, the answer is $d_{4, 7} = 176$. Sample Input 2 12 20 Sample Output 2 174174144 Interesting number. It only contains $1$, $4$ and $7$. Sample Input 3 16 30 Sample Output 3 102292850 You can calculate that $d_{16, 30} = 1,193,004,294,685$. Please don't forget printing the answer mod $998,244,353$. Writersquare1001
35,251
D: パスワヌド 問題 AOR むカちゃんは英小文字のみからなる匷力なパスワヌドを䜜りたいず思っおいたす。 友達から $N$ 個の危険なパスワヌドの䟋を教えおもらった AOR むカちゃんは、 以䞋の条件を党お満たすようなパスワヌドを䜜るこずにしたした。 長さは1文字以䞊である。 どの危険なパスワヌドの、どの連続した郚分文字列ずも異なる。 1, 2 の条件を満たした䞭で、最も短い文字列である。 1,2,3の条件を満たした䞭で、蟞曞順に䞊べたずき先頭に来る文字列である。 AOR むカちゃんに代わっお匷力なパスワヌドを生成するプログラムを曞いおください。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $S_1$ $\vdots$ $S_N$ 1 行目には、文字列の数を衚す敎数 $N$ が䞎えられる。 2 行目からの $N$ 行には、文字列 $S_i$ が䞎えられる。 $|S_i|$ ずは文字列の長さであり、1 文字以䞊である。 $1 \le N \le 100,000$ を満たす。 $1 \le \sum_{1\le i \le N} |S_i| \le 400,000$ を満たす。 文字列は英小文字のみを含む。 出力 答えを 1 行で出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 5 password login admin root master サンプル出力 1 b サンプル入力 2 3 abcdefghijklmnopqrstuvwxy qwertyuiopasdfghjklxcvbnm qawsxedcrfvtgbyhnujmikolp サンプル出力 2 z サンプル入力 3 1 abcdefghijklmnopqrstuvwxyz サンプル出力 3 aa
35,252
Score : 100 points Problem Statement An uppercase or lowercase English letter \alpha will be given as input. If \alpha is uppercase, print A ; if it is lowercase, print a . Constraints \alpha is an uppercase ( A - Z ) or lowercase ( a - z ) English letter. Input Input is given from Standard Input in the following format: α Output If \alpha is uppercase, print A ; if it is lowercase, print a . Sample Input 1 B Sample Output 1 A B is uppercase, so we should print A . Sample Input 2 a Sample Output 2 a a is lowercase, so we should print a .
35,253
Balls and Boxes 2 Balls Boxes Any way At most one ball At least one ball Distinguishable Distinguishable 1 2 3 Indistinguishable Distinguishable 4 5 6 Distinguishable Indistinguishable 7 8 9 Indistinguishable Indistinguishable 10 11 12 Problem You have $n$ balls and $k$ boxes. You want to put these balls into the boxes. Find the number of ways to put the balls under the following conditions: Each ball is distinguished from the other. Each box is distinguished from the other. Each ball can go into only one box and no one remains outside of the boxes. Each box can contain at most one ball. Note that you must print this count modulo $10^9+7$. Input $n$ $k$ The first line will contain two integers $n$ and $k$. Output Print the number of ways modulo $10^9+7$ in a line. Constraints $1 \le n \le 1000$ $1 \le k \le 1000$ Sample Input 1 2 3 Sample Output 1 6 Sample Input 2 3 2 Sample Output 2 0 Sample Input 3 100 100 Sample Output 3 437918130
35,254
Is-Convex For a given polygon g , print "1" if g is a convex polygon, "0" otherwise. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees. g is represented by a sequence of points p 1 , p 2 ,..., p n where line segments connecting p i and p i+1 (1 ≀ i ≀ n-1 ) are sides of the polygon. The line segment connecting p n and p 1 is also a side of the polygon. Input g is given by coordinates of the points p 1 ,..., p n in the following format: n x 1 y 1 x 2 y 2 : x n y n The first integer n is the number of points. The coordinate of a point p i is given by two integers x i and y i . The coordinates of points are given in the order of counter-clockwise visit of them. Output Print "1" or "0" in a line. Constraints 3 ≀ n ≀ 100 -10000 ≀ x i , y i ≀ 10000 No point of the polygon will occur more than once. Two sides of the polygon can intersect only at a common endpoint. Sample Input 1 4 0 0 3 1 2 3 0 3 Sample Output 1 1 Sample Input 2 5 0 0 2 0 1 1 2 2 0 2 Sample Output 2 0
35,255
Score : 500 points Problem Statement A cheetah and a cheater are going to play the game of Nim. In this game they use N piles of stones. Initially the i -th pile contains a_i stones. The players take turns alternately, and the cheetah plays first. In each turn, the player chooses one of the piles, and takes one or more stones from the pile. The player who can't make a move loses. However, before the game starts, the cheater wants to cheat a bit to make sure that he can win regardless of the moves by the cheetah. From each pile, the cheater takes zero or one stone and eats it before the game. In case there are multiple ways to guarantee his winning, he wants to minimize the number of stones he eats. Compute the number of stones the cheater will eat. In case there is no way for the cheater to win the game even with the cheating, print -1 instead. Constraints 1 ≀ N ≀ 10^5 2 ≀ a_i ≀ 10^9 Input The input is given from Standard Input in the following format: N a_1 : a_N Output Print the answer. Sample Input 1 3 2 3 4 Sample Output 1 3 The only way for the cheater to win the game is to take stones from all piles and eat them. Sample Input 2 3 100 100 100 Sample Output 2 -1
35,256
Score : 400 points Problem Statement An SNS has N users - User 1 , User 2 , \cdots , User N . Between these N users, there are some relationships - M friendships and K blockships . For each i = 1, 2, \cdots, M , there is a bidirectional friendship between User A_i and User B_i . For each i = 1, 2, \cdots, K , there is a bidirectional blockship between User C_i and User D_i . We define User a to be a friend candidate for User b when all of the following four conditions are satisfied: a \neq b . There is not a friendship between User a and User b . There is not a blockship between User a and User b . There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a , c_L = b , and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1 . For each user i = 1, 2, ... N , how many friend candidates does it have? Constraints All values in input are integers. 2 ≀ N ≀ 10^5 0 \leq M \leq 10^5 0 \leq K \leq 10^5 1 \leq A_i, B_i \leq N A_i \neq B_i 1 \leq C_i, D_i \leq N C_i \neq D_i (A_i, B_i) \neq (A_j, B_j) (i \neq j) (A_i, B_i) \neq (B_j, A_j) (C_i, D_i) \neq (C_j, D_j) (i \neq j) (C_i, D_i) \neq (D_j, C_j) (A_i, B_i) \neq (C_j, D_j) (A_i, B_i) \neq (D_j, C_j) Input Input is given from Standard Input in the following format: N M K A_1 B_1 \vdots A_M B_M C_1 D_1 \vdots C_K D_K Output Print the answers in order, with space in between. Sample Input 1 4 4 1 2 1 1 3 3 2 3 4 4 1 Sample Output 1 0 1 0 1 There is a friendship between User 2 and 3 , and between 3 and 4 . Also, there is no friendship or blockship between User 2 and 4 . Thus, User 4 is a friend candidate for User 2 . However, neither User 1 or 3 is a friend candidate for User 2 , so User 2 has one friend candidate. Sample Input 2 5 10 0 1 2 1 3 1 4 1 5 3 2 2 4 2 5 4 3 5 3 4 5 Sample Output 2 0 0 0 0 0 Everyone is a friend of everyone else and has no friend candidate. Sample Input 3 10 9 3 10 1 6 7 8 2 2 5 8 4 7 3 10 9 6 4 5 8 2 6 7 5 3 1 Sample Output 3 1 3 5 4 3 3 3 3 1 0
35,257
Score : 300 points Problem Statement You are given a string S . Takahashi can insert the character A at any position in this string any number of times. Can he change S into AKIHABARA ? Constraints 1 \leq |S| \leq 50 S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into AKIHABARA , print YES ; otherwise, print NO . Sample Input 1 KIHBR Sample Output 1 YES Insert one A at each of the four positions: the beginning, immediately after H , immediately after B and the end. Sample Input 2 AKIBAHARA Sample Output 2 NO The correct spell is AKIHABARA . Sample Input 3 AAKIAHBAARA Sample Output 3 NO
35,258
Score : 300 points Problem Statement You are given a sequence of positive integers of length N , a = (a_1, a_2, ..., a_N) . Your objective is to remove some of the elements in a so that a will be a good sequence . Here, an sequence b is a good sequence when the following condition holds true: For each element x in b , the value x occurs exactly x times in b . For example, (3, 3, 3) , (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not. Find the minimum number of elements that needs to be removed so that a will be a good sequence. Constraints 1 \leq N \leq 10^5 a_i is an integer. 1 \leq a_i \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of elements that needs to be removed so that a will be a good sequence. Sample Input 1 4 3 3 3 3 Sample Output 1 1 We can, for example, remove one occurrence of 3 . Then, (3, 3, 3) is a good sequence. Sample Input 2 5 2 4 1 4 2 Sample Output 2 2 We can, for example, remove two occurrences of 4 . Then, (2, 1, 2) is a good sequence. Sample Input 3 6 1 2 2 3 3 3 Sample Output 3 0 Sample Input 4 1 1000000000 Sample Output 4 1 Remove one occurrence of 10^9 . Then, () is a good sequence. Sample Input 5 8 2 7 1 8 2 8 1 8 Sample Output 5 5
35,259
Score : 100 points Problem Statement There are N slimes lining up in a row. Initially, the i -th slime from the left has a size of a_i . Taro is trying to combine all the slimes into a larger slime. He will perform the following operation repeatedly until there is only one slime: Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y , where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes. Find the minimum possible total cost incurred. Constraints All values in input are integers. 2 \leq N \leq 400 1 \leq a_i \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 \ldots a_N Output Print the minimum possible total cost incurred. Sample Input 1 4 10 20 30 40 Sample Output 1 190 Taro should do as follows (slimes being combined are shown in bold): ( 10 , 20 , 30, 40) → ( 30 , 30, 40) ( 30 , 30 , 40) → ( 60 , 40) ( 60 , 40 ) → ( 100 ) Sample Input 2 5 10 10 10 10 10 Sample Output 2 120 Taro should do, for example, as follows: ( 10 , 10 , 10, 10, 10) → ( 20 , 10, 10, 10) (20, 10 , 10 , 10) → (20, 20 , 10) (20, 20 , 10 ) → (20, 30 ) ( 20 , 30 ) → ( 50 ) Sample Input 3 3 1000000000 1000000000 1000000000 Sample Output 3 5000000000 The answer may not fit into a 32-bit integer type. Sample Input 4 6 7 6 8 6 1 1 Sample Output 4 68 Taro should do, for example, as follows: (7, 6, 8, 6, 1 , 1 ) → (7, 6, 8, 6, 2 ) (7, 6, 8, 6 , 2 ) → (7, 6, 8, 8 ) ( 7 , 6 , 8, 8) → ( 13 , 8, 8) (13, 8 , 8 ) → (13, 16 ) ( 13 , 16 ) → ( 29 )
35,260
ダンゞョン 問題 あなたはあるダンゞョンの地䞋 N 階にある財宝を手に入れたいず思っおいる.最初,あなたは地䞋 1 階におり,あなたの䜓力は H(H は正敎数) である.䞋の階に降りるずきに,䜓力が消費される.あらかじめ各階における䞋の階に降りるずきに消費される䜓力が分かっおいる.䞀方,各階には 1 ぀の回埩の泉があり,泉を 1 回䜿うごずに回埩できる䜓力がそれぞれ定たっおいる.䜓力が 0 以䞋になるずあなたは死んでしたう.たた,䜓力が H よりも高くなるこずはない.回埩の泉は䜕回でも䜿うこずができるが,回埩には時間がかかるので,あなたは泉の䜿甚回数をできるだけ少なくしたいず考えおいる. N ,H ,各階の䞋の階に降りるずきに消費される䜓力,そしお各階で回埩の泉を 1 回䜿甚したずきに回埩する䜓力が䞎えられたずき,䜓力を 0 以䞋にするこずなく地䞋 N 階たで到達するために必芁な泉の䜿甚回数の最小倀を求めるプログラムを䜜成せよ. たた,䞀床䞋の階に降りるず,財宝を手に入れるたで䞊の階に戻るこずはできない. 入力 入力は N 行からなる. 入力の 1 行目には 2 ぀の敎数 N, H(2 ≀ N ≀ 100000 = 10 5 , 1 ≀ H ≀ 10000000 = 10 7 ) が空癜を区切りずしお曞かれおいる.N は地䞋 N 階に財宝があるずいうこずを衚し,H は初期䜓力 ( ダンゞョンの地䞋 1 階に到着した段階の䜓力 ) であり,か぀䜓力の最倧倀である ( 回埩によっお䜓力が H より倧きくなるこずはない ) . 続く N − 1 行には,2 ぀の敎数が空癜を区切りずしお曞かれおいる.1 + i 行目(1 ≀ i ≀ N − 1) の敎数 di , hi に぀いお, di は地䞋 i 階から地䞋 i + 1 階に降りる際に消費される䜓力を,hi は地䞋 i 階で泉を 1 回䜿甚したずきに回埩する䜓力を衚す(0 ≀ di < H, 1 ≀ hi < H). どの採点甚デヌタにおいおも,地䞋 N 階にたどり着く方法が存圚する.採点甚デヌタのうち,配点の 10%分に぀いおは,N ≀ 1000 か぀ H ≀ 1000 を満たす.配点の30%分に぀いおは,N ≀1000 を満たす.配点の 50%分に぀いおは,泉の䜿甚回数の最小倀が 10 6 を超えない. 出力 出力は,䜓力を 0 以䞋にするこずなく地䞋 N 階に到達するために必芁な,泉の䜿甚回数の最小倀を含む 1 行からなる. 泚意 この問題では,扱う敎数の範囲が 32bit に収たらない可胜性があるこずに泚意せよ. 入出力䟋 入力䟋 1 10 10 4 2 2 5 6 1 7 3 6 4 9 6 0 8 4 1 9 4 出力䟋 1 10 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,261
Dropping Ink As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by ( x , y ) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall ( x , y ) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≀ x < 10, 0 ≀ y < 10. Input x 1 , y 1 , s 1 x 2 , y 2 , s 2 : : ( x i , y i ) represents the position of the i -th drop and s i denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Sample Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output for the Sample Input 77 5
35,262
Problem A: Luck Manipulator Nathan O. Davis くんはあるゲヌムを攻略䞭であり非垞にレアなアむテムを手に入れようず四苊八苊しおいたこのレアなアむテムはカゞノのスロットマシヌンで特殊な絵柄を䞀列に揃えたずきに手に入れるこずができるスロットマシヌンには N 個のリヌルがありボタンを䞀回抌すず珟圚回転しおいる最も巊偎のリヌルが停止するそのためこのレアアむテムを手に入れるためには N 回ボタンを抌す必芁があるたた特殊な絵柄で停止させるためにはあるタむミングでボタンを抌す必芁があるずころがこのボタンを抌すタむミングは非垞にシビアであるためなかなか䞊手くいかずに困っおいたそこでNathan くんはメモリビュヌアを利甚しおゲヌム䞭におけるメモリの倀を確認しながらゲヌムを解析するこずにした Nathan くんの解析の結果その特殊な絵柄でリヌルを停止させるためにはボタンが抌された時にメモリの 007E0D1F 番地に曞き蟌たれた「乱数」が特定の倀になっおいる必芁があるこずを突き止めた乱数の倀は 1 フレヌム毎に線圢合同法によっお倉化しおおりたたボタンが抌されたかどうかの刀定は 1 フレヌム毎に 1 回行われおいるこずも分かったここで線圢合同法ずは擬䌌乱数を生成するための方法の䞀぀であり以䞋の匏によっお倀を決定する x' = ( A × x + B ) mod C ここで x は珟圚の乱数の倀 x' は次の乱数の倀 A , B , C は䜕らかの定数であるたた y mod z は y を z で割ったずきの䜙りを衚す 䟋えば2 個のリヌルを持぀スロットマシヌンで A = 5 B = 7 C = 11 で最初の「乱数」の倀が 10 であったずするそしお特殊な絵柄でリヌルを止めるための条件は巊偎のリヌルが 2右偎のリヌルが 4 であったずするこのずき1 フレヌム目における乱数の倀は (5 × 10 + 7) mod 11 = 2 であるため1 フレヌム目にボタンを抌せば巊偎のリヌルは特殊な絵柄で停止する続く 2 フレヌム目での乱数の倀は (5 × 2 + 7) mod 11 = 6 であるから2 フレヌム目にボタンを抌しおも右偎のリヌルは特殊な絵柄では停止しない続く 3 フレヌム目では乱数の倀が (5 × 6 + 7 ) mod 11 = 4 になるので3 フレヌム目にボタンを抌せば右偎のリヌルは特殊な絵柄で停止するよっお最短 3 フレヌムで党おのリヌルを特殊な絵柄で停止されるこずができレアなアむテムを手に入れるこずができる あなたの仕事は最短で䜕フレヌム目に党おのリヌルを特殊な絵柄で停止させるこずができるかを求めるプログラムを曞いおNathan くんがレアなアむテムを手に入れられるよう助けおあげるこずである Input 入力は耇数のデヌタセットからなる各デヌタセットは次の圢匏で䞎えられる N A B C X Y 1 Y 2 ... Y N 最初の行には 5 ぀の敎数 N (1 ≀ N ≀ 100)  A , B (0 ≀ A , B ≀ 10,000) C (1 ≀ C ≀ 10,000)  X (0 ≀ X < C ) がそれぞれ 1 ぀の空癜文字で区切られお䞎えられるここで X は最初の乱数の倀を衚す続く行では N 個の敎数 Y 1 , Y 2 , ... , Y N (0 ≀ Y i ≀ 10,000) が 1 ぀の空癜文字で区切られお䞎えられるこれらはリヌルを特定の絵柄で止めるための条件を衚しおおりその第 i 番目の数 Y i は巊から i 番目のリヌルを特殊な絵柄で停止するためには乱数の倀が Y i である必芁があるいう意味である 入力の終わりは空癜で区切られた 5 ぀の 0 を含む 1 行で瀺される Output 各デヌタセットに぀いお特殊な絵柄で停止させるために必芁ずなる最短のフレヌム数を 1 行に出力せよなお10,000 フレヌム以内に停止させるこずができない堎合はフレヌム数の代わりに -1 を出力せよ出力に䜙蚈な空癜や改行を含めおはならない Sample Input 1 5 7 11 10 10 2 5 7 11 10 2 4 2 1 1 256 0 128 255 2 0 0 1 0 1234 5678 2 1 1 100 0 99 98 2 1 1 100 0 99 99 2 1 1 10000 0 1 0 2 1 1 10000 0 2 1 0 0 0 0 0 Output for the Sample Input 0 3 255 -1 198 199 10000 -1
35,263
Problem D: Change ICPC World Finals 2日目 ティヌ氏らは空枯のタヌミナルに぀いた。 これから飛行機を乗り継ぎ敵地R囜に乗り蟌むのである。 我々はD囜を経由するため、手持ちのお金を2皮類の通貚に䞡替せねばならない。 ケヌ氏「ティヌさんはどのように䞡替したすか」 ティヌ氏「ハフハフハフハフハフハフハフハフ」 ケヌ氏「なるほど。私も20,000円をD囜甚に5,000円、R囜甚に15,000円割り振ろうず思いたす」 ティヌ氏「ハフハフハフハフハフハフハフハフ」 ケヌ氏「あれ倉ですね。䞡替結果が私ず違いたすね 。」 問題 ある旅行者は、日本を旅立ち、D囜通貚単䜍D、R囜通貚単䜍Rを芳光し、日本に垰っおくるこずを考えおいる。 今、\( M \)円持っおいる。 \( c_{D} \)[D]、\( c_{R} \)[R]を各囜で消費するこずが分かっおいるので、 お金が䞍足しないように䞡替したい。 \( x \)円をD囜のお金に䞡替するず\( \lfloor \frac{r_{D}x}{100} \rfloor \)[D]に、 \( x \)円をR囜のお金に䞡替するず\( \lfloor \frac{r_{R}x}{100} \rfloor \)[R]になる。 䟋えば、 \( r_{D}=11, c_{D}=10 \)の時に 150円をD囜のお金に䞡替するず\( \lfloor \frac{11 \times 150}{100} \rfloor = \)16[D]になるためお金は䞍足しない。 しかし、50円しか䞡替しないず5[D]になり10[D]に満たないためお金が䞍足する。 たた、垰囜時には手持ちのお金を党お日本円に䞡替する。 \( x \)[D]を日本円に䞡替するず\( \lfloor \frac{100x}{r_{D}} \rfloor \)円に、 \( x \)[R]を日本円に䞡替するず\( \lfloor \frac{100x}{r_{R}} \rfloor \)円になる。 先の1぀目の䟋では、 埗られた16[D]のうち10[D]を消費するため6[D]䜙る。 これを日本円に䞡替するず\( \lfloor \frac{100 \times 6}{11} \rfloor = 54\)円になる。 出囜時に日本円の最適な䞡替を行った時、垰囜時に最終的に手元に戻っおくる日本円の最倧額を求めよ。 いかなる䞡替を行なっおも、D囜たたはR囜のいずれかでお金が䞍足する堎合には"-1"ず出力せよ。 ※\( \lfloor x \rfloor \)は、\(x\)を超えない最倧の敎数を衚す。 入力 M r D r R c D c R 1行目に 珟圚手持ちの日本円の額\(M\)、 日本円ずD囜の通貚単䜍ずの䞡替レヌト\( r_{D} \)、 日本円ずR囜の通貚単䜍ずの䞡替レヌト\( r_{R} \)、 D囜でのお金の消費額\( c_{D} \)、 R囜でのお金の消費額\( c_{R} \)が空癜区切りで䞎えられる。 出力 手元に戻っおくる日本円の最倧額を1行に出力せよ。 いかなる䞡替を行なっおも、D囜たたはR囜のいずれかでお金が䞍足する堎合は"-1"を出力せよ。 制玄 \( 0 \leq M \leq 10^{12}(= 1000000000000) \) \( 1 \leq r_{D}, r_{R} \leq 100 \) \( 0 \leq c_{D}, c_{R} \leq 10^{12}(= 1000000000000) \) 入出力䟋 入力1 20000 3 1 20 100 出力1 9333 667円をD囜のお金に、10,000円をR囜のお金に倉換すれば、それぞれ20[D]、100[R]が埗られる。 入力2 1000000000000 3 1 20 100 出力2 999999989333 この旅行者は倧金持ちである。 入力3 0 3 1 20 100 出力3 -1 この旅行者は䞀文無しであり、どこにも行けない。
35,264
Score : 400 points Problem Statement There are N people standing in a queue from west to east. Given is a string S of length N representing the directions of the people. The i -th person from the west is facing west if the i -th character of S is L , and east if that character of S is R . A person is happy if the person in front of him/her is facing the same direction. If no person is standing in front of a person, however, he/she is not happy. You can perform the following operation any number of times between 0 and K (inclusive): Operation: Choose integers l and r such that 1 \leq l \leq r \leq N , and rotate by 180 degrees the part of the queue: the l -th, (l+1) -th, ... , r -th persons. That is, for each i = 0, 1, ..., r-l , the (l + i) -th person from the west will stand the (r - i) -th from the west after the operation, facing east if he/she is facing west now, and vice versa. What is the maximum possible number of happy people you can have? Constraints N is an integer satisfying 1 \leq N \leq 10^5 . K is an integer satisfying 1 \leq K \leq 10^5 . |S| = N Each character of S is L or R . Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of happy people after at most K operations. Sample Input 1 6 1 LRLRRL Sample Output 1 3 If we choose (l, r) = (2, 5) , we have LLLRLL , where the 2 -nd, 3 -rd, and 6 -th persons from the west are happy. Sample Input 2 13 3 LRRLRLRRLRLLR Sample Output 2 9 Sample Input 3 10 1 LLLLLRRRRR Sample Output 3 9 Sample Input 4 9 2 RRRLRLRLL Sample Output 4 7
35,265
Problem B: Miscalculation Bob is an elementary schoolboy, not so good at mathematics. He found Father's calculator and tried cheating on his homework using it. His homework was calculating given expressions containing multiplications and additions. Multiplications should be done prior to additions, of course, but the calculator evaluates the expression from left to right, neglecting the operator precedence. So his answers may be the result of either of the following two calculation rules. Doing multiplication before addition Doing calculation from left to right neglecting the operator precedence Write a program that tells which of the rules is applied from an expression and his answer. An expression consists of integers and operators. All the integers have only one digit, from 0 to 9. There are two kinds of operators + and * , which represent addition and multiplication, respectively. The following is an example expression. 1+2*3+4 Calculating this expression with the multiplication-first rule, the answer is 11, as in Sample Input 1. With the left-to-right rule, however, the answer will be 13 as shown in Sample Input 2. There may be cases in which both rules lead to the same result and you cannot tell which of the rules is applied. Moreover, Bob sometimes commits miscalculations. When neither rules would result in Bob’s answer, it is clear that he actually did. Input The input consists of a single test case specified with two lines. The first line contains the expression to be calculated. The number of characters of the expression is always odd and less than or equal to 17. Each of the odd-numbered characters in the expression is a digit from ' 0 ' to ' 9 '. Each of the even-numbered characters is an operator ' + ' or ' * '. The second line contains an integer which ranges from 0 to 999999999, inclusive. This integer represents Bob's answer for the expression given in the first line. Output Output one of the following four characters: M When only the multiplication-first rule results Bob's answer. L When only the left-to-right rule results Bob's answer. U When both of the rules result Bob's answer. I When neither of the rules results Bob's answer. Sample Input 1 1+2*3+4 11 Sample Output 1 M Sample Input 2 1+2*3+4 13 Sample Output 2 L Sample Input 3 3 3 Sample Output 3 U Sample Input 4 1+2*3+4 9 Sample Output 4 I
35,266
Problem B: 宝くじチェッカヌ あなたは無事進孊振り分けを終え、駒堎近蟺から本郷近蟺ぞ匕っ越す準備の最䞭であった。 家の䞭を敎理しおいたずころ去幎賌入した宝くじが倧量に芋぀かった。 亀換期限を確認したずころ明日たでに換金しなければならないのだが、匕っ越しの準備がただ党く終わっおいないため少額の圓遞金のために宝くじ売り堎たで行くのは面倒である。 そこであなたはいくら圓遞金が埗られるかを調べるこずにした。 宝くじは8桁の数字からなる。圓遞番号は同じく8桁で数字ず'*'から構成され、持っおいる宝くじず数字郚分が䞀臎した堎合、圓遞番号に応じた圓遞金が支払われる。 䟋えば所持しおいる宝くじの番号が "12345678" の堎合、圓遞番号が "*******8" や "****5678" の堎合は圓遞ずなるが、圓遞番号が "****4678" のずきは圓遞ずはならない。圓遞番号は耇数あり埗お、それぞれ独立に圓遞金が支払われる。ただし、圓遞番号は1枚の宝くじが2぀以䞊の圓遞番号に圓たらないように遞ばれる。たずえば圓遞番号が "*******8", "****5678" の2぀であった堎合、 "12345678" は䞡方に圓遞しおしたうため、このような圓遞番号の遞ばれ方がされるこずはない。 あなたの仕事は、入力ずしお䞎えられる所持しおいる宝くじの番号ず宝くじの圓遞番号に察しお、いくらの圓遞金が埗られるかを出力するプログラムを曞くこずである。 Input 入力は以䞋のような圢匏で䞎えられる。 n m N 1 M 1 ... N n M n B 1 ... B m 1行目では圓遞番号の数 n (1 ≀ n ≀ 100)ず所持しおいる宝くじの枚数 m (1 ≀ m ≀ 1000)がそれぞれ敎数で䞎えられる。 続く n 行では、各行に圓遞番号 N i ず圓遞金 M i (1 ≀ M i ≀ 1000000)が䞎えられる。圓遞金は敎数である。圓遞番号の圢匏は問題文䞭で述べたずおりである。 続く m 行では所持しおいる宝くじの番号が䞎えられる。 Output 圓遞金の合蚈額を出力せよ。 Notes on Test Cases 䞊蚘入力圢匏で耇数のデヌタセットが䞎えられたす。各デヌタセットに察しお䞊蚘出力圢匏で出力を行うプログラムを䜜成しお䞋さい。 n が 0 のずき入力の終わりを瀺したす。 Sample Input 3 3 *******1 100 ******22 1000 11111112 1000000 01203291 02382022 11111111 10 10 ****3228 149416 ****3992 198635 ****4286 77783 ****4843 225244 ***49835 231046 ***59393 379996 *5763748 437345 *6726222 58054 *8117882 16375 *9244339 537727 77885716 96726222 26971031 66652868 89599648 37772338 64679621 65479161 92959393 57855682 0 0 Output for Sample Input 1200 438050
35,267
A: A-Z Cat / A-Z キャット 物語 あいずにゃんは若ヶ束高校のプログラミングコンテスト郚通称ぷろこん郚に所属する2幎生であるずお぀もなくかわいいあいずにゃんは友達のゞョむに頌たれお猫を預かるこずになったA-Z キャットず呌ばれる珍皮の猫であるあいずにゃんは預かった A-Z キャットを「あいずにゃん 2 号」ず (勝手に) 名付け倧局可愛がった A-Z キャットは特定の条件を満たす文字列を奜むずされるそこであいずにゃんが詊しに文字列を䞎えおみるずあいずにゃん 2 号は文字列の䞀郚を爪で切り取った埌満足げな衚情を浮かべたどうやら奜みの文字列に曞き換えようずしおいるようだ 倩䜿のごずくかわいいあいずにゃんにぞっこんである D のひずはあいずにゃんのために A-Z キャットが奜む文字列の条件を芋぀け出したそれは 'A' ず 'Z' が亀互に繰り返される文字列でありか぀ 'A' から始たり 'Z' で終わる文字列であるA-Z キャットはずおも賢いので最小限の文字消去で奜みの文字列に倉換しようずするあいずにゃんにいい栌奜をしたい D のひずは䞎えられた 'A' ず 'Z' のみからなる文字列を A-Z キャットがどのような文字列に倉換するか求めるプログラムを曞くこずにした 問題 英倧文字のみからなる文字列 S が䞎えられる S の任意の文字を奜きなだけ削陀するこずで'A' ず 'Z' が亀互に出珟しか぀ 'A' から始たり 'Z' で終わる文字列を䜜る削陀回数を最小化したずき埗られる文字列を求めよ 入力圢匏 入力ずしお文字列 S が1行に䞎えられる S は英倧文字のみからなり 1 ≀ |S| ≀ 20 を満たす 出力圢匏 S に察し最小回数の文字削陀を行っお埗られる'A' ず'Z' が亀互に出珟しか぀ 'A' から始たり 'Z' で終わる文字列を 1 行に出力せよどのような文字削陀を行っおも条件を満たす文字列が埗られない堎合は -1 を 1 行に出力せよ 入力䟋1 AIZUNYANPEROPERO 出力䟋1 AZ 入力䟋2 AZAZ 出力䟋2 AZAZ 入力䟋3 ZDDYAZAWABDDZAZPIDDA 出力䟋3 AZAZAZ 入力䟋4 ZZZZAAAAAA 出力䟋4 -1
35,268
Problem G: The Morning after Halloween You are working for an amusement park as an operator of an obakeyashiki , or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts. In this problem, you are asked to write a program that, given a floor map of a house, finds the smallest number of steps to move all ghosts to the positions where they are supposed to be. A floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can. At each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions: No more than one ghost occupies one position at the end of the step. No pair of ghosts exchange their positions one another in the step. For example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign (' # ) represents a wall cell and 'a', 'b', and 'c' ghosts. #### ab# #c## #### The following four maps show the only possible positions of the ghosts after one step. #### #### #### #### ab# a b# acb# ab # #c## #c## # ## #c## #### #### #### #### Input The input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows. w h n c 11 c 12 ... c 1 w c 21 c 22 ... c 2 w . . .. . .. . . .. . c h 1 c h 2 ... c h w w , h and n in the first line are integers, separated by a space. w and h are the floor width and height of the house, respectively. n is the number of ghosts. They satisfy the following constraints. 4 ≀ w ≀ 16 4 ≀ h ≀ 16 1 ≀ n ≀ 3 Subsequent h lines of w characters are the floor map. Each of c ij is either: a ' # ' representing a wall cell, a lowercase letter representing a corridor cell which is the initial position of a ghost, an uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or a space representing a corridor cell that is none of the above. In each map, each of the first n letters from a and the first n letters from A appears once and only once. Outermost cells of a map are walls; i.e. all characters of the first and last lines are sharps; and the first and last characters on each line are also sharps. All corridor cells in a map are connected; i.e. given a corridor cell, you can reach any other corridor cell by following corridor cells in the 4-neighborhoods. Similarly, all wall cells are connected. Any 2 × 2 area on any map has at least one sharp. You can assume that every map has a sequence of moves of ghosts that restores all ghosts to the positions where they are supposed to be. The last dataset is followed by a line containing three zeros separated by a space. Output For each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces. Sample Input 5 5 2 ##### #A#B# # # #b#a# ##### 16 4 3 ################ ## ########## ## # ABCcba # ################ 16 16 3 ################ ### ## # ## ## # ## # c# # ## ########b# # ## # # # # # # ## # # ## ## a# # # # # ### ## #### ## # ## # # # # # ##### # ## ## #### #B# # # ## C# # ### # # # ####### # # ###### A## # # # ## ################ 0 0 0 Output for the Sample Input 7 36 77
35,269
Problem F: Marked Ancestor You are given a tree T that consists of N nodes. Each node is numbered from 1 to N , and node 1 is always the root node of T . Consider the following two operations on T : M v : (Mark) Mark node v . Q v : (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q , which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≀ N ≀ 100000 and 1 ≀ Q ≀ 100000. The following N - 1 lines describe the configuration of the tree T . Each line contains a single integer p i ( i = 2, ... , N ), which represents the index of the parent of i -th node. The next Q lines contain operations in order. Each operation is formatted as " M v " or " Q v ", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Sample Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output for the Sample Input 4
35,270
気象予報士 (Weather Forecaster) 問題 JOI 垂は南北方向に H キロメヌトル東西方向に W キロメヌトルの長方圢の圢をしおおりH × W 個の 1 キロメヌトル四方の小区画に区切られおいる北から i 番目西から j 番目の小区画を (i, j) ず衚す 各小区画は䞊空に雲があるか雲がないかのどちらかであるすべおの雲は1 分経぀ごずに 1 キロメヌトル東に移動する今日は実に倩気が良いためJOI 垂の倖から JOI 垂内に雲が移動しおくるこずはない 今各小区画の䞊空に雲があるかないかがわかっおいる気象予報士であるあなたは各小区画に぀いお今から䜕分埌に初めおその小区画の䞊空に雲が来るかを予枬するこずになった 各小区画に぀いお今から䜕分埌に初めおその小区画の䞊空に雲が来るか求めよ 入力 入力は 1 + H 行からなる 1 行目には敎数 H, W (1 ≀ H ≀ 100, 1 ≀ W ≀ 100) が空癜を区切りずしお曞かれおいるこれはJOI 垂が H × W 個の 1 キロメヌトル四方の小区画に区切られおいるこずを衚す 続く H 行のうちの i 行目 (1 ≀ i ≀ H) には W 文字からなる文字列が曞かれおいるW 文字のうちの j 文字目 (1 ≀ j ≀ W) は小区画 (i, j) の䞊空に今雲があるかどうかを衚す雲がある堎合は文字 'c' (英小文字) が雲がない堎合は文字 '.' (ピリオド) が曞かれおいる 出力 出力は H 行からなりそれぞれの行は空癜を区切りずした W 個の敎数からなる出力の i 行目の j 番目の敎数 (1 ≀ i ≀ H, 1 ≀ j ≀ W) は今から䜕分埌に初めお小区画 (i, j) の䞊空に雲が来るかを衚さなければならないただし今すでに小区画 (i, j) の䞊空に雲がある堎合は 0 を䜕分経っおも小区画 (i, j) の䞊空に雲が来ない堎合は -1 を出力せよ 出力の各行の行頭ず行末には䜙蚈な空癜を入れないこず 入出力䟋 入力䟋 1 3 4 c..c ..c. .... 出力䟋 1 0 1 2 0 -1 -1 0 1 -1 -1 -1 -1 入力䟋 2 6 8 .c...... ........ .ccc..c. ....c... ..c.cc.. ....c... 出力䟋 2 -1 0 1 2 3 4 5 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 1 2 0 1 -1 -1 -1 -1 0 1 2 3 -1 -1 0 1 0 0 1 2 -1 -1 -1 -1 0 1 2 3 入出力䟋 1 ではJOI 垂は 3 × 4 個の小区画に区切られおいる今の JOI 垂の雲の状況は以䞋の通りである図の䞊が北を衚す この埌1 分ごずに雲は以䞋のように移動する 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,271
有料道路通行料 20XX幎に喜倚方垂熱塩加玍町から南䌚接町たでの6区間、総距離58kmの䌚接䞭倮道路が完成し開通する予定です。 開通埌、半幎間は利甚促進のため17時30分〜19時30分たでに出発ICか到着ICを通過し、なおか぀走行距離が40km以䞋の車に察する通行料金は半額になりたす。ただし料金は50円単䜍ずし、端数は切り䞊げたす。䞋蚘の衚は料金ず距離の䞀芧衚です。 䟋えば喜倚方から䌚接若束たでは料金が450円、距離が12kmずなりたす。半額時間垯であれば250円になりたす。 出発IC、出発IC通過時刻、到着IC、到着IC通過時刻を入力ずし、料金を蚈算しお出力するプログラムを䜜成しおください。ただし、入力される時刻は24 時間衚蚘の倀ずしたす。なお、17時30分および19時30分ちょうどに通過した堎合も半額時間垯に含めたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 d h d m d a h a m a 行目に出発ICの番号 d (1 ≀ d ≀ 7)、行目に出発ICの通過時刻の時間 h d (0 ≀ h d ≀ 23) ず分 m d (0 ≀ m d ≀ 59) が䞎えられたす。 3行目に到着ICの番号 a (1 ≀ a ≀ 7)、行目に到着ICの通過時刻の時間 h a (0 ≀ h a ≀ 23) ず分 m a (0 ≀ m a ≀ 59) が䞎えられたす。 Output デヌタセットごずに通行料金敎数を行に出力したす。 Sample Input 2 17 25 4 17 45 4 17 25 7 19 35 0 Output for the Sample Input 250 1300
35,272
宿題 (Homework) 問題 冬䌑みの宿題に毎回苊しめられおきた JOI 君が今回は宿題を蚈画的に実行するこずにした宿題は囜語ず算数のドリルであり囜語のドリルは A ペヌゞ算数のドリルは B ペヌゞある JOI 君は1 日に囜語のドリルを最倧 C ペヌゞず算数のドリルを最倧 D ペヌゞ進めるこずができるが宿題をするずその日は遊ぶこずができない 冬䌑みは L 日ありJOI 君は冬䌑み䞭に宿題を終わらせなければならないJOI 君が冬䌑み䞭に最倧で䜕日遊べるかを求めるプログラムを䜜成せよ 入力 入力は 5 行からなり1 行に 1 ぀ず぀正の敎数が曞かれおいる 1 行目には敎数 L (2 ≩ L ≩ 40) が曞かれおおり冬䌑みの日数を衚す 2 行目には敎数 A (1 ≩ A ≩ 1000) が曞かれおおり囜語のドリルのペヌゞ数を衚す 3 行目には敎数 B (1 ≩ B ≩ 1000) が曞かれおおり算数のドリルのペヌゞ数を衚す 4 行目には敎数 C (1 ≩ C ≩ 100) が曞かれおおりJOI 君が 1 日に進めるこずができる囜語のドリルの最倧ペヌゞ数を衚す 5 行目には敎数 D (1 ≩ D ≩ 100) が曞かれおおりJOI 君が 1 日に進めるこずができる算数のドリルの最倧ペヌゞ数を衚す ただし䞎えられる入力デヌタにおいおはJOI 君が冬䌑み䞭に宿題を必ず終わらせるこずができ 少なくずも 1 日は遊べるこずが保蚌されおいる 出力 JOI 君が冬䌑み䞭に遊べる日数の最倧倀を 1 行で出力せよ 入出力䟋 入力䟋 1 20 25 30 6 8 出力䟋 1 15 入出力䟋 1 では冬䌑みは 20 日間あり囜語のドリルが 25 ペヌゞ算数のドリルが 30 ペヌゞあるJOI 君は 1 日に囜語のドリルを最倧 6 ペヌゞ算数のドリルを最倧 8 ペヌゞ進めるこずができる䟋えば JOI 君が冬䌑み初日から囜語のドリルを 6 ペヌゞ算数のドリルを 8 ペヌゞず぀進めたずするず囜語のドリルを 5 日目に算数のドリルを 4 日目に終わらせるこずができ15 日間遊ぶこずができるこれが JOI 君が冬䌑み䞭に遊べる日数の最倧倀なので15 を出力する 入力䟋 2 15 32 48 4 6 出力䟋 2 7 入出力䟋 2 では 䟋えば JOI 君が初日から囜語のドリルを 4 ペヌゞ算数のドリルを 6 ペヌゞず぀進めたずするず8 日目に䞡方のドリルを終わらせるこずができ7 日間遊ぶこずができるこれが JOI 君が冬䌑み䞭に遊べる日数の最倧倀なので7 を出力する 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,273
Score : 100 points Problem Statement AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a , the color of the one he bought yesterday is b , and the color of the one he bought today is c . Here, the color of each paint can is represented by an integer between 1 and 100 , inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints 1≩a,b,c≩100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Sample Input 1 3 1 4 Sample Output 1 3 Three different colors: 1 , 3 , and 4 . Sample Input 2 3 3 33 Sample Output 2 2 Two different colors: 3 and 33 .
35,274
Score : 700 points Problem Statement Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2 . At the end of the movie, the viewers go out of the cinema in a certain order: the i -th viewer leaving her seat is the one denoted by the number P_i . The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y ; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints 2 \le N \le 500 The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \{1, 2, \dots, N^2\} . Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Sample Input 1 3 1 3 7 9 5 4 8 6 2 Sample Output 1 1 Before the end of the movie, the viewers are arranged in the cinema as follows: 1 2 3 4 5 6 7 8 9 The first four viewers leaving the cinema ( 1 , 3 , 7 , 9 ) can leave the cinema without going through any seat, so they will not be hated by anybody. Then, viewer 5 must go through one of the seats where viewers 2 , 4 , 6 , 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers. Finally the remaining viewers can leave the cinema (in the order 4 , 8 , 6 , 2 ) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all). Sample Input 2 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Sample Output 2 3 Sample Input 3 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Sample Output 3 11
35,275
I: 偶奇゜ヌト 問題文 $0$ から $N-1$ たでの敎数を䞊び替えた長さ $N$ の順列 $P$ が䞎えられたす。 以䞋の操䜜を最倧 $30$ 回たで行っお、 $P$ を昇順に䞊べ替えおください。 操䜜 $1$ 回の操䜜では、次の 1 から 4 を順に行う。 0 ず 1 からなる長さ $N$ の文字列 $S$ ず、敎数 $t$ ( $t = 0$ たたは $t = 1$ ) を宣蚀する。 空の数列 $A, B$ を甚意し、敎数 $i$ を $1$ から $N$ たで動かしながら以䞋を行う。 $S_i$ が 0 のずき、䜕もしない。 $S_i$ が 1 のずき、 $P_i$ が偶数ならば $A$ の末尟に $P_i$ を远加する。 $P_i$ が奇数ならば $B$ の末尟に $P_i$ を远加する。 数列 $C$ を以䞋のように定矩する。 $t = 0$ のずき、 $C$ は $A$ ず $B$ をこの順に連結したものずする。 $t = 1$ のずき、 $C$ は $B$ ず $A$ をこの順に連結したものずする。 敎数 $i$ を $1$ から $N$ たで動かしながら以䞋を行う。 $S_i$ が 0 のずき、䜕もしない。 $S_i$ が 1 のずき、 $P_i$ を $C$ の先頭の芁玠に眮き換え、 $C$ の先頭の芁玠を消す。 䟋えば、$N = 7, P = {0, 4, 2, 3, 6, 5, 1}$ ずしたす。 $S$ を "1101101" ずし、$t = 1$ ずした操䜜を $1$ 回行うず、以䞋の図のように $P = {3, 1, 2, 0, 4, 5, 6}$ ずなりたす。 制玄 $1 \leq N \leq 15000$ $P$ は $0$ から $N-1$ たでの敎数を䞊び替えた順列 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $P_1$ $P_2$ $\ldots$ $P_N$ 出力 $30$ 回以内の操䜜で $P$ を昇順に䞊べ替えるような操䜜列の $1$ ぀を以䞋の圢匏で出力せよ。このような操䜜列は耇数存圚するかもしれないが、どれを出力しおも正答ずなる。 $1$ 行目には、操䜜を行う回数 $K$ を出力せよ。ただし、 $0 \le K \le 30$ でなければならない。 $i + 1$ 行目 $(1 \le i \le K)$ には、 $i$ 回目の操䜜で宣蚀する敎数 $t_i (t_i = 0,1)$ および長さ $N$ の文字列 $S_i$ をこの順に出力せよ。 $K$ $t_1$ $S_1$ $t_2$ $S_2$ $\vdots$ $t_K$ $S_K$ 入力䟋1 7 0 4 2 3 6 5 1 出力䟋1 1 1 0100101 操䜜を行うず、$A = { 4, 6 }, B = { 1 }$ ずなり、 $t = 1$ より $C = { 1, 4, 6 }$ ずなりたす。 $P$ の芁玠を $C$ によっお眮き換えるず、 $P = { 0, 1, 2, 3, 4, 5, 6 }$ ずなるので、この操䜜で $P$ を昇順に䞊べ替えるこずができたす。 入力䟋2 4 1 0 3 2 出力䟋2 2 0 1100 0 0011 䟋えば以䞋のような出力も正答ずなりたす。 2 0 1111 1 0110 入力䟋3 1 0 出力䟋3 0 操䜜を行わなくおも構いたせん。
35,276
Score : 300 points Problem Statement There are N integers, A_1, A_2, ..., A_N , written on the blackboard. You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written. Find the maximum possible greatest common divisor of the N integers on the blackboard after your move. Constraints All values in input are integers. 2 \leq N \leq 10^5 1 \leq A_i \leq 10^9 Output Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum possible greatest common divisor of the N integers on the blackboard after your move. Sample Input 1 3 7 6 8 Sample Output 1 2 If we replace 7 with 4 , the greatest common divisor of the three integers on the blackboard will be 2 , which is the maximum possible value. Sample Input 2 3 12 15 18 Sample Output 2 6 Sample Input 3 2 1000000000 1000000000 Sample Output 3 1000000000 We can replace an integer with itself.
35,277
Score : 1200 points Problem Statement In Republic of AtCoder, Snuke Chameleons (Family: Chamaeleonidae, Genus: Bartaberia) are very popular pets. Ringo keeps N Snuke Chameleons in a cage. A Snuke Chameleon that has not eaten anything is blue. It changes its color according to the following rules: A Snuke Chameleon that is blue will change its color to red when the number of red balls it has eaten becomes strictly larger than the number of blue balls it has eaten. A Snuke Chameleon that is red will change its color to blue when the number of blue balls it has eaten becomes strictly larger than the number of red balls it has eaten. Initially, every Snuke Chameleon had not eaten anything. Ringo fed them by repeating the following process K times: Grab either a red ball or a blue ball. Throw that ball into the cage. Then, one of the chameleons eats it. After Ringo threw in K balls, all the chameleons were red. We are interested in the possible ways Ringo could have thrown in K balls. How many such ways are there? Find the count modulo 998244353 . Here, two ways to throw in balls are considered different when there exists i such that the color of the ball that are thrown in the i -th throw is different. Constraints 1 \leq N,K \leq 5 \times 10^5 N and K are integers. Input Input is given from Standard Input in the following format: N K Output Print the possible ways Ringo could have thrown in K balls, modulo 998244353 . Sample Input 1 2 4 Sample Output 1 7 We will use R to represent a red ball, and B to represent a blue ball. There are seven ways to throw in balls that satisfy the condition: BRRR , RBRB , RBRR , RRBB , RRBR , RRRB and RRRR . Sample Input 2 3 7 Sample Output 2 57 Sample Input 3 8 3 Sample Output 3 0 Sample Input 4 8 10 Sample Output 4 46 Sample Input 5 123456 234567 Sample Output 5 857617983
35,278
Score : 2718 points Problem Statement There is a very long bench. The bench is divided into M sections, where M is a very large integer. Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action: We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other). After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of X and - : the i -th character of the string is X if the i -th section from the left in the interval is occupied, and - otherwise. Note that the photo is directed. For example, -X--X and X--X- are different photos. What is the probability that the photo matches a given string s ? This probability depends on M . You need to compute the limit of this probability when M goes infinity. Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm): p + \frac{q}{e} + \frac{r}{e^2} Your task is to compute these three rational numbers, and print them modulo 10^9 + 7 , as described in Notes section. Notes When you print a rational number, first write it as a fraction \frac{y}{x} , where x, y are integers and x is not divisible by 10^9 + 7 (under the constraints of the problem, such representation is always possible). Then, you need to print the only integer z between 0 and 10^9 + 6 , inclusive, that satisfies xz \equiv y \pmod{10^9 + 7} . Constraints 1 \leq N \leq 1000 |s| = N s consists of X and - . Input Input is given from Standard Input in the following format: N s Output Print three rational numbers p, q, r , separated by spaces. Sample Input 1 1 X Sample Output 1 500000004 0 500000003 The probability that a randomly chosen section is occupied converge to \frac{1}{2} - \frac{1}{2e^2} . Sample Input 2 3 --- Sample Output 2 0 0 0 After the actions, no three consecutive unoccupied sections can be left. Sample Input 3 5 X--X- Sample Output 3 0 0 1 The limit is \frac{1}{e^2} . Sample Input 4 5 X-X-X Sample Output 4 500000004 0 833333337 The limit is \frac{1}{2} - \frac{13}{6e^2} . Sample Input 5 20 -X--X--X-X--X--X-X-X Sample Output 5 0 0 183703705 The limit is \frac{7}{675e^2} . Sample Input 6 100 X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X- Sample Output 6 0 0 435664291
35,279
Balance Scale You, an experimental chemist, have a balance scale and a kit of weights for measuring weights of powder chemicals. For work efficiency, a single use of the balance scale should be enough for measurement of each amount. You can use any number of weights at a time, placing them either on the balance plate opposite to the chemical or on the same plate with the chemical. For example, if you have two weights of 2 and 9 units, you can measure out not only 2 and 9 units of the chemical, but also 11 units by placing both on the plate opposite to the chemical (Fig. C-1 left), and 7 units by placing one of them on the plate with the chemical (Fig. C-1 right). These are the only amounts that can be measured out efficiently. Fig. C-1 Measuring 11 and 7 units of chemical You have at hand a list of amounts of chemicals to measure today. The weight kit already at hand, however, may not be enough to efficiently measure all the amounts in the measurement list. If not, you can purchase one single new weight to supplement the kit, but, as heavier weights are more expensive, you'd like to do with the lightest possible. Note that, although weights of arbitrary positive masses are in the market, none with negative masses can be found. Input The input consists of at most 100 datasets, each in the following format. n m a 1 a 2 ... a n w 1 w 2 ... w m The first line of a dataset has n and m , the number of amounts in the measurement list and the number of weights in the weight kit at hand, respectively. They are integers separated by a space satisfying 1 ≀ n ≀ 100 and 1 ≀ m ≀ 10. The next line has the n amounts in the measurement list, a 1 through a n , separated by spaces. Each of a i is an integer satisfying 1 ≀ a i ≀ 10 9 , and a i ≠ a j holds for i ≠ j . The third and final line of a dataset has the list of the masses of the m weights at hand, w 1 through w m , separated by spaces. Each of w j is an integer, satisfying 1 ≀ w j ≀ 10 8 . Two or more weights may have the same mass. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing an integer specified as follows. If all the amounts in the measurement list can be measured out without any additional weights, 0 . If adding one more weight will make all the amounts in the measurement list measurable, the mass of the lightest among such weights. The weight added may be heavier than 10 8 units. If adding one more weight is never enough to measure out all the amounts in the measurement list, -1 . Sample Input 4 2 9 2 7 11 2 9 6 2 7 3 6 12 16 9 2 9 5 2 7 3 6 12 17 2 9 7 5 15 21 33 48 51 75 111 36 54 57 93 113 0 0 Output for the Sample Input 0 5 -1 5
35,280
Score : 200 points Problem Statement You are given an H × W grid. The squares in the grid are described by H strings, S_1,...,S_H . The j -th character in the string S_i corresponds to the square at the i -th row from the top and j -th column from the left (1 \leq i \leq H,1 \leq j \leq W) . . stands for an empty square, and # stands for a square containing a bomb. Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square. (Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.) He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square. Print the strings after the process. Constraints 1 \leq H,W \leq 50 S_i is a string of length W consisting of # and . . Input Input is given from Standard Input in the following format: H W S_1 : S_H Output Print the H strings after the process. The i -th line should contain a string T_i of length W , where the j -th character in T_i corresponds to the square at the i -th row from the top and j -th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W) . Sample Input 1 3 5 ..... .#.#. ..... Sample Output 1 11211 1#2#1 11211 For example, let us observe the empty square at the first row from the top and first column from the left. There is one bomb square adjacent to this empty square: the square at the second row and second column. Thus, the . corresponding to this empty square is replaced with 1 . Sample Input 2 3 5 ##### ##### ##### Sample Output 2 ##### ##### ##### It is possible that there is no empty square. Sample Input 3 6 6 #####. #.#.## ####.# .#..#. #.##.. #.#... Sample Output 3 #####3 #8#7## ####5# 4#65#2 #5##21 #4#310
35,281
問題名 Zero division checker 森䞋さんは困っおいたした... 姉原さんにプログラムを曞いおもらったのですが、そのプログラムがクラッシュするのです。 姉原さんに曞いおもらったのは、逆ポヌランド蚘法の匏を読んで、その蚈算結果を出力するプログラムで、クラッシュログによるず、 0 で割り算をしおしたったのが原因のようです。これは、森䞋さんが間違った匏を入力しおしたったからかもしれたせんし、もしかしたら、もしかしたら、姉原さんの曞いたプログラムにバグがあるのかもしれたせん。 姉原さんの曞いたプログラムを読んでみようず思いたしたが、姉原さんのプログラムはアセンブリずいうよくわからないこずばで曞かれおいるようで、芋おいるだけで頭がガンガンしおきたす。 そこで、森䞋さんはあなたに、匏が間違っおいないかどうか調べるプログラムを曞いおもらうこずにしたした。匏が間違っおいる、ずは、その匏に埓っお蚈算するず、 0 での割り算をしおしたう可胜性のあるこずをいいたす。 なお、姉原さんの曞いたコヌドはずおもふるいコンピュヌタヌで動くもので、加枛乗陀は敎数で行い、結果は 8 bit の笊号なし敎数ずしお保存されたす。 䟋えば、 255+1 は 0 になっおしたうし、 3/2 は 1 になりたす。 あ、姉原さんにはないしょですよ。 (参考)逆ポヌランド蚘法で衚された匏を蚈算する、擬䌌コヌド s = 空のスタック n = 匏の芁玠数 for i in 1..n: if 匏のi番目の芁玠が敎数: sにその敎数をプッシュする if 匏のi番目の芁玠が倉数: sにその倉数の倀をプッシュする if 匏のi番目の芁玠が挔算子: sから倀をポップし、bずする sから倀をポップし、aずする if 挔算子が'+': r = (a + b) % 256 ずする if 挔算子が'-': r = (a - b + 256) % 256 ずする if 挔算子が'*': r = (a * b) % 256 ずする if 挔算子が'/': r = (a / b) % 256 ずする sにrをプッシュする sから倀をポップし、匏の蚈算結果ずする Input m name 1 lb 1 ub 1 ... name m lb m ub m n e 1 e 2 ... e n 0 ≀ m ≀ 100 0 ≀ lb i ≀ ub i ≀ 255 1 ≀ name i (1 ≀ i ≀ m) の長さ ≀ 20 1 ≀ n ≀ 100 m は倉数の数、 name i , lb i , ub i はそれぞれ倉数iの名前、䞋限、䞊限を衚したす。 n は匏に含たれる芁玠の数を衚し、 e i は匏の i 番目の芁玠を衚したす。 各倉数は匏の䞭に高々1回しか珟れたせん。 Output 匏が間違っおいるずきには error 、間違っおいないずきには correct ず䞀行で出力しおください。 Sample Input 1 1 a 1 10 3 10 a / Output for the Sample Input 1 correct Sample Input 2 2 a 1 10 b 1 10 5 1 a b - / Output for the Sample Input 2 error Sample Input 3 1 a 0 255 7 1 a 2 * 1 + / Output for the Sample Input 3 correct
35,282
Score : 200 points Problem Statement You have an integer variable x . Initially, x=0 . Some person gave you a string S of length N , and using the string you performed the following operation N times. In the i -th operation, you incremented the value of x by 1 if S_i= I , and decremented the value of x by 1 if S_i= D . Find the maximum value taken by x during the operations (including before the first operation, and after the last operation). Constraints 1≀N≀100 |S|=N No characters except I and D occur in S . Input The input is given from Standard Input in the following format: N S Output Print the maximum value taken by x during the operations. Sample Input 1 5 IIDID Sample Output 1 2 After each operation, the value of x becomes 1 , 2 , 1 , 2 and 1 , respectively. Thus, the output should be 2 , the maximum value. Sample Input 2 7 DDIDDII Sample Output 2 0 The initial value x=0 is the maximum value taken by x , thus the output should be 0 .
35,283
Building a Space Station You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space station is made up with a number of units, called cells. All cells are sphere-shaped, but their sizes are not necessarily uniform. Each cell is fixed at its predetermined position shortly after the station is successfully put into its orbit. It is quite strange that two cells may be touching each other, or even may be overlapping. In an extreme case, a cell may be totally enclosing another one. I do not know how such arrangements are possible. All the cells must be connected, since crew members should be able to walk from any cell to any other cell. They can walk from a cell A to another cell B, if, (1) A and B are touching each other or overlapping, (2) A and B are connected by a `corridor', or (3) there is a cell C such that walking from A to C, and also from B to C are both possible. Note that the condition (3) should be interpreted transitively. You are expected to design a configuration, namely, which pairs of cells are to be connected with corridors. There is some freedom in the corridor configuration. For example, if there are three cells A, B and C, not touching nor overlapping each other, at least three plans are possible in order to connect all three cells. The first is to build corridors A-B and A-C, the second B-C and B-A, the third C-A and C-B. The cost of building a corridor is proportional to its length. Therefore, you should choose a plan with the shortest total length of the corridors. You can ignore the width of a corridor. A corridor is built between points on two cells' surfaces. It can be made arbitrarily long, but of course the shortest one is chosen. Even if two corridors A-B and C-D intersect in space, they are not considered to form a connection path between (for example) A and C. In other words, you may consider that two corridors never intersect. Input The input consists of multiple data sets. Each data set is given in the following format. n x 1 y 1 z 1 r 1 x 2 y 2 z 2 r 2 ... x n y n z n r n The first line of a data set contains an integer n , which is the number of cells. n is positive, and does not exceed 100. The following n lines are descriptions of cells. Four values in a line are x- , y- and z- coordinates of the center, and radius (called r in the rest of the problem) of the sphere, in this order. Each value is given by a decimal fraction, with 3 digits after the decimal point. Values are separated by a space character. Each of x , y , z and r is positive and is less than 100.0. The end of the input is indicated by a line containing a zero. Output For each data set, the shortest total length of the corridors should be printed, each in a separate line. The printed values should have 3 digits after the decimal point. They may not have an error greater than 0.001. Note that if no corridors are necessary, that is, if all the cells are connected without corridors, the shortest total length of the corridors is 0.000. Sample Input 3 10.000 10.000 50.000 10.000 40.000 10.000 50.000 10.000 40.000 40.000 50.000 10.000 2 30.000 30.000 30.000 20.000 40.000 40.000 40.000 20.000 5 5.729 15.143 3.996 25.837 6.013 14.372 4.818 10.671 80.115 63.292 84.477 15.120 64.095 80.924 70.029 14.881 39.472 85.116 71.369 5.553 0 Output for the Sample Input 20.000 0.000 73.834
35,284
Score : 400 points Problem Statement Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r -th row from the top and the c -th column from the left. Each square is painted black or white. The grid is said to be good if and only if the following condition is satisfied: From (1, 1) , we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square. Note that (1, 1) and (H, W) must be white if the grid is good. Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations. Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W) . For each pair r, c ( r_0 \leq r \leq r_1, c_0 \leq c \leq c_1 ), invert the color of (r, c) - that is, from white to black and vice versa. Constraints 2 \leq H, W \leq 100 Input Input is given from Standard Input in the following format: H W s_{11} s_{12} \cdots s_{1W} s_{21} s_{22} \cdots s_{2W} \vdots s_{H1} s_{H2} \cdots s_{HW} Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white. Output Print the minimum number of operations needed. Sample Input 1 3 3 .## .#. ##. Sample Output 1 1 Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2) , and we are done. Sample Input 2 2 2 #. .# Sample Output 2 2 Sample Input 3 4 4 ..## #... ###. ###. Sample Output 3 0 No operation may be needed. Sample Input 4 5 5 .#.#. #.#.# .#.#. #.#.# .#.#. Sample Output 4 4
35,285
Loading Aizu Ocean Transport Company (AOTC) accepted a new shipping order. The pieces of the cargo included in this order all have the same square footprint (2m x 2m). The cargo room has a rectangular storage space of 4m width with various longitudinal extents. Each pieces of cargo must be placed in alignment with partition boundaries which form a 1m x 1m square grid. A straddling arrangement with a neighboring partition (for example, protrusion by 50 cm) is not allowed. Neither an angled layout nor stacked placement are allowed. Note also that there may be some partitions on which any cargo loading is prohibited. AOTC wishes to use the currently available ship for this consignment and needs to know how many pieces of cargo it can accommodate. Make a program to report the maximum cargo capacity of the cargo space given the following information: the depth (m) of the cargo space and the loading-inhibited partitions. Input The input is given in the following format. H N x_1 y_1 x_2 y_2 : x_N y_N The first line provides the longitudinal depth H (2 ≀ H ≀ 10 4 ) of the cargo space in meters and the number of load-inhibited partitions N (0 ≀ N ≀ 4 × 10 4 ). Each of the subsequent N lines defines the position of the i -th load-inhibited partition x_i (0 ≀ x_i ≀ 3) and y_i (0 ≀ y_i ≀ H -1) in integers, where x = 0 and y = 0 indicate the bottom-left corner partition. A load-inhibited partition appears only once in the list. Output Output the maximum number of cargo pieces loaded into the cargo space. Sample Input 1 5 3 0 1 1 2 3 3 Sample Output 1 2 Input example 1 corresponds to the cargo layout shown in the left-most figure. Sample Input 2 6 4 0 2 1 3 3 4 0 5 Sample Output 2 4
35,286
Minimum-Cost Arborescence Find the sum of the weights of edges of the Minimum-Cost Arborescence with the root r for a given weighted directed graph G = ( V , E ). Input |V| |E| r s 0 t 0 w 0 s 1 t 1 w 1 : s |E|-1 t |E|-1 w |E|-1 , where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V| -1 respectively. r is the root of the Minimum-Cost Arborescence. s i and t i represent source and target verticess of i -th directed edge. w i represents the weight of the i -th directed edge. Output Print the sum of the weights the Minimum-Cost Arborescence. Constraints 1 ≀ |V| ≀ 100 0 ≀ |E| ≀ 1,000 0 ≀ w i ≀ 10,000 G has arborescence(s) with the root r Sample Input 1 4 6 0 0 1 3 0 2 2 2 0 1 2 3 1 3 0 1 3 1 5 Sample Output 1 6 Sample Input 2 6 10 0 0 2 7 0 1 1 0 3 5 1 4 9 2 1 6 1 3 2 3 4 3 4 2 2 2 5 8 3 5 3 Sample Output 2 11
35,287
E - すごろく 友達のいないき぀ねのボブは今日は䞀人ですごろく遊びをしお過ごすこずにした 各面に敎数 a_1,a_2,a_3,a_4,a_5,a_6 が曞かれた6面ダむスず駒ず盎線䞊にM個のマスがあり 巊から順に 1 から M たでの番号が割り圓おられたすごろく盀を甚いるこずにした すごろく盀の各マスには指瀺が数字で曞かれおおり i 番目のマスには数字 N_i が曞かれおいる これはその倀が正なら右負なら巊にその絶察倀だけ駒を移動せよずいう意味である すなわち i+N_i 番目のマスに駒を移動せよずいう意味である ボブは以䞋のようにしおすごろく遊びをするたず駒をスタヌト地点に眮く 次にサむコロを振りサむコロが出た目の数を芋お駒を「珟圚のマスから右に移動する」か 「巊に移動する」か「珟圚のマスに留たる」かを遞択するこずができる マスから移動する堎合はサむコロの出目の距離だけ駒を移動しお移動した先のマスの指瀺に埓う 指瀺に埓っお移動した先のマスの指瀺には埓わない 以埌ボブは䞊蚘のようにサむコロを振っお駒を移動するこずを繰り返す 移動した結果すごろく盀の倖に駒が出おしたったらボブの負けであり誀答( Wrong Answer )ず刀定される この問題の目的はこのすごろくをゎヌルするこずであるサむコロを振る回数は3000回以䞋ずする 入出力圢匏 入力は以䞋の圢匏で䞎えられる M a_1 a_2 a_3 a_4 a_5 a_6 s g N_1 ... N_M M はすごろく盀のマスの数である a_1 ... a_6 はサむコロの各面に曞かれおいる敎数の倀である s ず g はそれぞれすごろく盀のスタヌトずゎヌルの番号である N_i は i 番目のマスに曞かれおいる指瀺である これらの入力の埌 さいころを振った結果を衚す倀 dice が改行ずずもに匕き続いお入力から䞎えられる これはサむコロを振っお出た目が a dice であるこずを衚す これに察しおあなたのプログラムは駒を進めるかどうかを決めその遞択を出力しなければならない駒を右に移動するならば 1 を巊に移動するならば -1 を珟圚のマスに留たるならば 0 を出力せよ出力の埌には改行を出力せよ あなたのプログラムが進むか戻るか留たるかを出力するず次のサむコロを振った結果を入力から受け取るこずができるこれを繰り返す 䟋えばC/C++では scanf("%d", &dice); ずしおサむコロの面の番号を受け取りこれに察しお巊に移動するなら printf("-1\n"); fflush(stdout); ずする次に scanf("%d", &dice); ずするず次のサむコロの面の番号を受け取るこずが出来るすごろくにゎヌルしたら盎ちにプログラムを終了せよ ゎヌルに蟿り着くたでにサむコロを振る回数は3000回以䞋でなければならない サむコロを振った回数が途䞭で3000回を超えた堎合誀答ず刀定される 制玄 2 ≀ M ≀ 300 1 ≀ s ≀ M, 1 ≀ g ≀ M s \neq g 1 ≀ a_i ≀ M-1 1 ≀ dice ≀ 6 N_s = N_g = 0 マスの呜什に埓っお駒を進めお枠倖に出る事はない dice は \{1,2,
,6\} から擬䌌乱数で䞀様ランダムに遞ばれる いくらサむコロを振ったずしおもゎヌルできない盀面は䞎えられない 入力の倀は党お敎数である 入出力䟋1 すごろくの説明 プログラムの出力 プログラムぞの入力 サむコロの目 駒の移動埌のマスの番号 10 1 6 2 5 3 4 1 10 0 -1 3 -1 3 -2 -6 -5 -7 0 1回目のサむコロ 1 1 1回目のプログラムの出力 0 1 2回目のサむコロ 3 2 2回目のプログラムの出力 1 6 3回目のサむコロ 5 3 3回目のプログラムの出力 0 6 4回目のサむコロ 1 1 4回目のプログラムの出力 -1 8 5回目のサむコロ 3 2 5回目のプログラムの出力 1 10
35,288
Eulerian Flight Tour You have an airline route map of a certain region. All the airports in the region and all the non-stop routes between them are on the map. Here, a non-stop route is a flight route that provides non-stop flights in both ways. Named after the great mathematician Leonhard Euler, an Eulerian tour is an itinerary visiting all the airports in the region taking a single flight of every non-stop route available in the region. To be precise, it is a list of airports, satisfying all of the following. The list begins and ends with the same airport. There are non-stop routes between pairs of airports adjacent in the list. All the airports in the region appear at least once in the list. Note that it is allowed to have some airports appearing multiple times. For all the airport pairs with non-stop routes in between, there should be one and only one adjacent appearance of two airports of the pair in the list in either order. It may not always be possible to find an Eulerian tour only with the non-stop routes listed in the map. Adding more routes, however, may enable Eulerian tours. Your task is to find a set of additional routes that enables Eulerian tours. Input The input consists of a single test case. $n$ $m$ $a_1$ $b_1$ ... $a_m$ $b_m$ $n$ ($3 \leq n \leq 100$) is the number of airports. The airports are numbered from 1 to $n$. $m$ ($0 \leq m \leq \frac{n(n-1)}{2}$) is the number of pairs of airports that have non-stop routes. Among the $m$ lines following it, integers $a_i$ and $b_i$ on the $i$-th line of them ($1 \leq i \leq m$) are airport numbers between which a non-stop route is operated. You can assume $1 \leq a_i < b_i \leq n$, and for any $i \ne j$, either $a_i \ne a_j$ or $b_i \ne b_j$ holds. Output Output a set of additional non-stop routes that enables Eulerian tours. If two or more different sets will do, any one of them is acceptable. The output should be in the following format. $k$ $c_1$ $d_1$ ... $c_k$ $d_k$ $k$ is the number of non-stop routes to add, possibly zero. Each of the following $k$ lines should have a pair of integers, separated by a space. Integers $c_i$ and $d_i$ in the $i$-th line ($c_i < d_i$) are airport numbers specifying that a non-stop route is to be added between them. These pairs, ($c_i, d_i$) for $1 \leq i \leq k$, should be distinct and should not appear in the input. If adding new non-stop routes can never enable Eulerian tours, output -1 in a line. Sample Input 1 4 2 1 2 3 4 Sample Output 1 2 1 4 2 3 Sample Input 2 6 9 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Sample Output 2 -1 Sample Input 3 6 7 1 2 1 3 1 4 2 3 4 5 4 6 5 6 Sample Output 3 3 1 5 2 4 2 5 Sample Input 4 4 3 2 3 2 4 3 4 Sample Output 4 -1 Sample Input 5 5 5 1 3 1 4 2 4 2 5 3 5 Sample Output 5 0
35,289
Problem H: いけるかな あるずころに日本各地をたわりながら商売をする瀟長がいた 圌はある日䞍思議な切笊を手に入れた その切笊を䜿うずなんず目的地たでの距離によらず電車の運賃が無料になるずいう ただし 珟圚の駅から隣接する駅ぞ移動するのを1ステップず数えたずきに 移動するステップ数がちょうど切笊に曞かれた数ず等しくならないず 远加料金を取られおしたう ある区間をただちに折り返すような移動は犁止されおいるが 既に蚪れた駅や区間を耇数回通るこず自䜓は蚱される たずえば駅1・駅2・駅1ず移動するこずはできないが 駅1・駅2・駅3・駅1・駅2のような移動は問題ない たた最終的に目的地に到着するならば出発地や目的地も䜕床でも通っおよい 瀟長はさっそくこの切笊を次の目的地に行くために䜿っおみようず考えた しかし路線図は入り組んでいるため簡単には経路が定たらない あなたの仕事は瀟長に代わっお目的地に無料で到達できるかどうかを 刀定するプログラムを曞くこずである 駅は 1 から N たでの番号が振られおおり 出発地の駅は 1目的地の駅は N ず決たっおいる 路線図は2぀の駅を結ぶ区間の列によっお䞎えられる 区間はどちらの方向にも通行するこずができる 同じ駅同士を結ぶような区間は存圚しないこずず ある駅の察を結ぶ区間はたかだか1぀であるこずが保蚌されおいる Input 入力は耇数のデヌタセットからなる それぞれのデヌタセットは次のような圢匏で䞎えられる N M Z s 1 d 1 s 2 d 2 ... s M d M N は駅の総数 M は区間の総数であり Z は切笊に曞かれたステップ数である s i ず d i (1 ≀ i ≀ M) は駅の番号を衚す敎数であり それぞれ駅 s i ず駅 d i の間に区間が存圚するこずを衚珟しおいる N , M , Z は正の敎数であり次の条件を満たす 2 ≀ N ≀ 50, 1 ≀ M ≀ 50, 0 < Z < 2 31  最埌のデヌタセットの埌ろに “ 0 0 0 ” ず曞かれた1行がある デヌタセットの数は30を越えない Output それぞれのデヌタセットに察し チケットに曞かれおいるステップ数ちょうどで 目的地に到達できるならば “ yes ” 到達できないならば “ no ” のみを含む1行の文字列を出力せよ Sample Input 2 1 1 1 2 2 1 2 1 2 3 1 2 1 2 8 8 5778 1 2 2 3 2 4 3 5 5 6 6 7 7 8 4 8 8 8 5777 1 2 2 3 2 4 3 5 5 6 6 7 7 8 4 8 0 0 0 Output for the Sample Input yes no no yes no
35,290
Score : 400 points Problem Statement Given a string t , we will call it unbalanced if and only if the length of t is at least 2 , and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s . Constraints 2 ≩ |s| ≩ 10^5 s consists of lowercase letters. Partial Score 200 points will be awarded for passing the test set satisfying 2 ≩ N ≩ 100 . Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s , print -1 -1 . If there exists an unbalanced substring of s , let one such substring be s_a s_{a+1} ... s_{b} (1 ≩ a < b ≩ |s|) , and print a b . If there exists more than one such substring, any of them will be accepted. Sample Input 1 needed Sample Output 1 2 5 The string s_2 s_3 s_4 s_5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted. Sample Input 2 atcoder Sample Output 2 -1 -1 The string atcoder contains no unbalanced substring.
35,291
爆匟の連鎖 瞊 8、暪 8 のマスからなる図1 のような平面がありたす。その平面䞊に、いく぀かの爆匟が眮かれおいたす。図2 にその䟋を瀺したす● = 爆匟。 □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ ● □ □ ● □ □ □ □ □ □ ● □ □ ● □ □ □ ● □ □ ● □ □ ● □ □ □ ● □ □ ● □ □ □ □ □ □ □ □ □ □ ● □ □ □ ● □ ● □ □ □ ● □ □ ● □ ● □ □ ● □ 図1 図2 爆匟が爆発するず、その爆匟の䞊䞋巊右 3 マスに爆颚の圱響が及び、それらのマスに眮かれおいる爆匟も連鎖的に爆発したす。たずえば、図 3 に瀺す爆匟が爆発するず図 4 の■のマスに爆颚の圱響が及びたす。 □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ ● □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ ■ □ □ □ □ □ □ □ ■ □ □ □ □ □ □ □ ■ □ □ □ □ ■ ■ ■ ● ■ ■ ■ □ □ □ □ ■ □ □ □ □ □ □ □ ■ □ □ □ □ 図3 図4 爆匟が眮かれおいる状態ず最初に爆発する爆匟の䜍眮を読み蟌んで、最終的な平面の状態を出力するプログラムを䜜成しおください。 Input 入力は以䞋の圢匏で䞎えられたす。 n 空行 デヌタセット 1 空行 デヌタセット 2 .. .. デヌタセット n 行目にデヌタセットの数 n ( n ≀ 20) が䞎えられたす。続いお n 個のデヌタセットが䞎えられたす。各デヌタセットの盎前に぀の空行が䞎えられたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 g 1,1 g 2,1 ... g 8,1 g 1,2 g 2,2 ... g 8,2 : g 1,8 g 2,8 ... g 8,8 X Y 最初の行に、平面を衚す぀の文字列が䞎えられたす。各文字列は文字からなる数列で、爆匟が眮かれおいるマスを 1、眮かれおいないマスを 0 で衚したす。 続く行で、最初に爆発する爆匟の X 座暙ず Y 座暙が䞎えられたす。巊䞊、巊䞋、右䞊、右䞋の座暙が、それぞれ、(1, 1)、(1, 8)、(8, 1)、(8, 8) です。䟋えば、図 4 に瀺す爆匟が最初に爆発するずき、䞎えられる座暙は (4, 6) です。 Output 各デヌタセットに぀いお以䞋のように出力しお䞋さい。 爆発しないで残った爆匟のあるマスを 1、爆匟のないマスを 0 で衚珟するこずずしたす。平面の䞀行を数字 8 個からなる䞀行ずし 8 行の文字列で最終的な平面の状態を出力しおください。各デヌタセットの先頭は、サンプル出力のように Data x : から出力しなければなりたせん。ここで、 x はデヌタセットの番号です。 Sample Input 2 00010010 00000100 10001001 00100010 01000000 00001000 10100010 01010010 2 5 00010010 00000100 10001001 00100010 01000000 00001000 10100010 01010010 2 5 Output for the Sample Input Data 1: 00000000 00000100 10001001 00100000 00000000 00001000 10100000 00000000 Data 2: 00000000 00000100 10001001 00100000 00000000 00001000 10100000 00000000
35,292
䞉角圢の個数の和 座暙平面䞊の原点$O$を巊䞋、座暙$(W,H)$にある点を右䞊ずする長方圢の領域が䞎えられおいたす。この領域に含たれる、$x$座暙ず$y$座暙がずもに敎数である点を個以䞊含む点の集たりを考えたす。このような点の集たりのすべおの組み合わせの個数を$N$ずし、点の集たりのそれぞれを$D_1, D_2, ..., D_N$で衚したずき、$D_1, D_2, ..., D_N$のそれぞれに含たれる点を頂点ずする䞉角圢を考えたす。 たずえば、図のように$W=H=1$のずき、点$O(0,0)$、$A(1,0)$、$B(1,1)$、$C(0,1)$のうち、個以䞊含む点の集たりは$\{O,A,B\}$、$\{O,A,C\}$、$\{O,B,C\}$、$\{A,B,C\}$、$\{O,A,B,C\}$の぀です。この䟋の堎合、点の集たりのそれぞれに含たれる䞉角圢は次のようになりたす。 $\{O,A,B\}$に含たれる䞉角圢は$OAB$のみ。 $\{O,A,C\}$に含たれる䞉角圢は$OAC$のみ。 $\{O,B,C\}$に含たれる䞉角圢は$OBC$のみ。 $\{A,B,C\}$に含たれる䞉角圢は$ABC$のみ。 $\{O,A,B,C\}$に含たれる䞉角圢は$OAB$、$OAC$、$OBC$、$ABC$の個。 この䟋からわかるように、同じ䞉角圢が぀以䞊の点の集たりに含たれる堎合がありたす。たずえば、䞉角圢$OAB$は点の集たり$\{O,A,B\}$にも$\{O,A,B,C\}$にも含たれたす。 $W$ず$H$が䞎えられる。点の集たり$D_1, D_2, ..., D_N$それぞれに含たれる点を頂点ずする䞉角圢の個数を$t_1, t_2, ..., t_N$ずするずき、䞉角圢の個数の和$t_1+t_2+...+t_N$を蚈算するプログラムを䜜成せよ。 入力 入力は以䞋の圢匏で䞎えられる。 $W$ $H$ 行に$W$ず$H$ ($1 \leq W,H \leq 1,000$)が䞎えられる。 出力 それぞれの点の集たりに含たれる䞉角圢の個数の和を1,000,000,007で割った䜙りを行に出力する。 入出力䟋 入力䟋 1 1 出力䟋 8 入力䟋 1 2 出力䟋 144 入力䟋 100 100 出力䟋 879128399
35,293
Problem L: Select Sets Problem 敎数の集合が N 個ある。 これらの集合にはそれぞれ1から N たでの番号が割り振られおいる。 i 番目の集合の芁玠数は K i であり、その集合の芁玠の䞭で j 番目に小さい敎数は a i,j である。 倪郎君はこの䞭から任意の数だけ集合を遞ぶこずになった。 ただ適圓に遞ぶだけでは぀たらないず思った倪郎君は、少なくずも3぀の集合を遞ぶこずにした。たた、"遞んだ集合の和集合の芁玠数"ず"遞んだ集合の積集合の芁玠数"の積ができるだけ倧きくなるように遞ぶこずにした。 倪郎君が最適に集合を遞んだずき、"遞んだ集合の和集合の芁玠数"ず"遞んだ集合の積集合の芁玠数"の積の最倧倀を求めよ。 Input 入力は以䞋の圢匏で䞎えられる。 N K 1 a 1,1 a 1,2 ... a 1,K 1 K 2 a 2,1 a 2,2 ... a 2,K 2 ... K N a N,1 a N,2 ... a N,K N Constraints 入力は以䞋の条件を満たす。 入力は党お敎数である。 3 ≀ N ≀ 20,000 1 ≀ a i,j ≀ 22 a i,j < a i,j+1 ( 1 ≀ i ≀ N ) ( 1 ≀ j < K i ) Output 倪郎君が最適に遞んだずきの"遞んだ集合の和集合の芁玠数"ず"遞んだ集合の積集合の芁玠数"の積の最倧倀を出力せよ。 Sample Input 1 4 1 5 2 3 5 3 3 5 7 4 3 5 7 9 Sample Output 1 8 Sample Input 2 5 4 1 4 5 6 4 1 7 8 9 3 1 2 3 3 1 2 3 3 1 2 3 Sample Output 2 9 Sample Input 3 3 2 3 4 2 2 5 2 1 6 Sample Output 3 0
35,294
出口調査 あるデパヌトで買い物金額の出口調査を行いたした。買い物金額のデヌタを入力ずし、1 人あたりの平均買い物金額を蚈算し、出力するプログラムを䜜成しおください。調査人数は、10 䞇人以䞋ずし、䞀人あたりの買い物金額は 100 䞇円を超えないものずしたす。 Input 入力は以䞋の圢匏で䞎えられたす。 n v 1 v 2 : v n 1 行目に調査人数 n 、続く n 行に i 人目の買い物金額を衚す敎数 v i が䞎えられたす。 Output 平均買い物金額敎数小数点以䞋は切り捚おを行に出力しおください。 Sample Input 6 12300 5600 33800 0 26495 52000 Output for the Sample Input 21699
35,295
Score : 100 points Problem Statement You are given an array a_0, a_1, ..., a_{N-1} of length N . Process Q queries of the following types. 0 p x : a_p \gets a_p + x 1 l r : Print \sum_{i = l}^{r - 1}{a_i} . Constraints 1 \leq N, Q \leq 500,000 0 \leq a_i, x \leq 10^9 0 \leq p < N 0 \leq l_i < r_i \leq N All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Sample Input 1 5 5 1 2 3 4 5 1 0 5 1 2 4 0 3 10 1 0 5 1 0 3 Sample Output 1 15 7 25 6
35,296
鉛筆 (Pencils) 問題文 JOI 君は鉛筆を N 本買うために近くの文房具店に行くこずにした 文房具店では鉛筆が䞀定の本数ず぀のセットで売られおいるセット X は A 本で B 円セット Y は C 本で D 円である JOI 君はセット X かセット Y の䞀方を遞び遞んだセットをいく぀か賌入する䞡方のセットを賌入するこずはできない N 本以䞊の鉛筆を埗るために必芁な金額の最小倀を求めよ 制玄 1 \leq N \leq 1000 1 \leq A \leq 1000 1 \leq B \leq 1000 1 \leq C \leq 1000 1 \leq D \leq 1000 入力・出力 入力 入力は以䞋の圢匏で暙準入力から䞎えられる N A B C D 出力 JOI 君が N 本以䞊の鉛筆を手に入れるのに必芁な金額の最小倀を出力せよ 入出力䟋 入力䟋 1 10 3 100 5 180 出力䟋 1 360 JOI 君は10本の鉛筆を入手したいセット X は3本で100円セット Y は5本で180円であるこの時セット X を遞んだ堎合はセットを4぀賌入する必芁があり400円必芁であるセット Y を遞んだ堎合はセットを2぀賌入する必芁があり360円必芁であるしたがっお必芁な金額の最小倀は400円ず360円の小さい方で360円である 入力䟋 2 6 2 200 3 300 出力䟋 2 600 このずきセット X を遞んだ堎合もセット Y を遞んだ堎合も必芁な金額は600円である必芁な金額の最小倀は600円である 情報オリンピック日本委員䌚䜜 『第 17 回日本情報オリンピック JOI 2017/2018 予遞競技課題』
35,297
小孊生のむクタ君は、ある日おじいさんから数匏が曞かれた玙をもらった。 どうやらおじいさんは数匏の答えの金額だけお小遣いをくれるらしい。 むクタ君はただ足し算、匕き算、掛け算しか習っおなかったので、数匏にも足し算ず匕き算ず掛け算しか䜿われおいない。 通垞の蚈算では足し算ず匕き算より掛け算を先に蚈算しなければならないのだが、むクタ君は挔算子の優先順䜍に぀いおの理解があやふやだったので、ずりあえず数匏の蚈算結果が最倧になるような郜合の良い優先順䜍を考えるこずにした。 +−× の3぀の二項挔算子ず括匧を含む数匏が䞎えられる。 3぀の挔算子の優先順䜍を任意に倉曎しお、数匏を最倧化したずきの蚈算結果を答えよ。 ただし、以䞋の点に泚意せよ。 挔算子は必ず巊結合である。 (同じ優先順䜍の挔算子は必ず数匏の巊偎から蚈算する。) 異なる挔算子が同じ優先順䜍であっおもよい。 䞀぀の数匏を蚈算しおいる途䞭で優先順䜍を倉曎しおはならない。 Input 入力は以䞋の圢匏で䞎えられる。 0〜9の数字ず挔算子'+','-','*'、括匧'(',')'で構成された数匏 正確に蚘述するず入力は以䞋のBNFで瀺される圢匏になっおいる <expr> ::= ( <expr> ) | <number> | <expr> <op> <expr> <op> ::= + | - | * <number>は非負敎数を衚す。 Constraints 入力は以䞋の制玄を満たす。 数匏は200文字以䞋である。 どのような優先順䜍を蚭定しおも、蚈算の結果やその途䞭で64bit敎数型でoverflowするようなこずはない。 Output 数匏から埗られる最倧倀を1行で出力せよ。 Sample Input 1 3-2*3 Output for the Sample Input 1 3 *の優先順䜍を-より䜎くするこずでこのようになる。 Sample Input 2 (5-3*4)*(0-2+1) Output for the Sample Input 2 21 優先順䜍は+>*>- Sample Input 3 1-2+3-4+5-6*0 Output for the Sample Input 3 3 優先順䜍が䞀般的な堎合 Sample Input 4 (1989967-3*1-211+4487) Output for the Sample Input 4 8511076028
35,298
Score : 800 points Problem Statement There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns of squares. We will denote the square at the i -th row from the top and j -th column from the left by (i,\ j) . Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, S , there is a frog trying to get to another leaf T . The state of square (i,\ j) is given to you by a character a_{ij} , as follows: . : A square without a leaf. o : A square with a leaf floating on the water. S : A square with the leaf S . T : A square with the leaf T . The frog will repeatedly perform the following action to get to the leaf T : "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located." Snuke is trying to remove some of the leaves, other than S and T , so that the frog cannot get to the leaf T . Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove. Constraints 2 ≀ H, W ≀ 100 a_{ij} is . , o , S or T . There is exactly one S among a_{ij} . There is exactly one T among a_{ij} . Input Input is given from Standard Input in the following format: H W a_{11} ... a_{1W} : a_{H1} ... a_{HW} Output If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print -1 instead. Sample Input 1 3 3 S.o .o. o.T Sample Output 1 2 Remove the upper-right and lower-left leaves. Sample Input 2 3 4 S... .oo. ...T Sample Output 2 0 Sample Input 3 4 3 .S. .o. .o. .T. Sample Output 3 -1 Sample Input 4 10 10 .o...o..o. ....o..... ....oo.oo. ..oooo..o. ....oo.... ..o..o.... o..o....So o....T.... ....o..... ........oo Sample Output 4 5
35,299