처음해보는 TCO.. 처음엔 좀 많이 긴장했는데.. 한문제만 풀자는 생각으로 침착하게 임했다.. 약간 아쉬움이 남긴 하지만.. 그럭저럭 무난했다.. 아침 11시에 열리는 매치라 좀 부담스러웠는데.. 밤을 샌 덕분에(?) 비교적 좋은 컨디션을 유지한것같다.. 이로써 TCO08 Qual Round는 얼떨결에가뿐하게 통과했다.. rating도 조금 상승하고..
이번 퀄 라운드1은 우승자가 한국인이다!!! 우아.. 나는 언제 우승해보나.. div2에서라도 한번 해보고싶다. ㅠ_ㅠ
이번 매치는 떡밥이 좀 많았는데도 불구하고 챌린지를 또 실패하고 말았다.. ㅠ_ㅠ;; 완벽한 먹이감을 하나 발견하긴했지만.. 다른놈이 먼저하고말았다.. 젠장!!! 다들 코딩만 빠른게아니라 챌도 빠르다.. ㅠ_ㅠ;;
흠.. 근데 이거 한문제씩만 풀어가지고는 한계가 있는데.. 빨리 실력을 쌓아서 두문제 이상풀던가.. 아니면 챌을 많이하던가.. 해야할텐데..;; 음.. 오늘도 600을 끈질기게 잡고 풀었으면 어땠을까 하는 생각이 든다.. 900 코딩하다 안드로메다 가고.. 막판 5분을 남기고 600점짜리 DP 솔루션이 번뜩 떠올랐는데.. 아쉬비..
방 7등 전체 253등 (대략 1100여명 참여)
250점 문제는 2번째로 빨리풀었군.. 그냥 내 나름대로 간단하게 생각해서 풀었는데 red및 yellow들도 다 잘 못풀고있어서 마지막 순간까지 내 솔루션을 의심할수밖에 없었다.. 음.. 근데 우리방에 딱 한명있는 저 red는 점수가 왜저렇지.. 600을 포기하고 900을 열었군.. -_-;;
[250] MonstersAndBunnies
Problem Statement
You just entered a strange town. The town is currently inhabited by M monsters and B bunnies. The inhabitants interact in the following way: Whenever two bunnies meet, nothing happens. Whenever a monster meets a bunny, the monster eats the bunny. Whenever two monsters meet, they start fighting and they both die in the fight. Whenever you meet a bunny, you can decide whether you kill it or not. A bunny will never kill you. You can not kill a monster. However, if you meet a monster, it will kill you without hesitation. All the town's inhabitants roam the town at random. More precisely, we will make the following assumptions. Meetings will occur at different moments in time. Each time exactly two beings will meet. Those two beings are always chosen uniformly at random from the set of all living beings in town, including you. You win if at some moment you can not be killed anymore. You are not allowed to leave the town until you either win or get killed. Write a method that will calculate the expected probability that you will win. Definition
Class: MonstersAndBunnies Method: survivalProbability Parameters: int, int Returns: double Method signature: double survivalProbability(int M, int B) (be sure your method is public)
Notes - The returned value must be accurate to within a relative or absolute value of 1E-9. - When deciding whether to kill a bunny you just met, your only goal is to maximize your winning probability. Constraints - M will be between 0 and 1,000, inclusive. - B will be between 0 and 1,000, inclusive. Examples 0)
0 0 Returns: 1.0 This town is empty. As soon as you enter it, you win. 1)
0 47 Returns: 1.0 Peaceful bunny pastures, you will not get killed here. 2)
1 0 Returns: 0.0 Sooner or later the monster will find you and kill you. 3)
1 47 Returns: 0.0 The bunnies won't help you. Sooner or later the monster will find you and kill you. 4)
2 0 Returns: 0.3333333333333333 In 1/3 of all cases the two monsters meet first, kill each other and you win. In the other 2/3 cases, one of the two monsters meets you first and kills you.
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
마을에 M마리의 monster와 B마리의 bunny와 내가 산다.. bunny끼리 만날때는 아무일이없고.. monster끼리 만날때는 둘이 싸우다 죽는다.. monster는 bunny나 사람을 만나면 죽인다.. 모든 생물체는 한순간에 한쌍만 마주친다. 내가 죽지않으면서 죽을 위험이 없어질 확률을 구하는 문제..
아.. 첫문제부터 나의 취약분야중 하나인 확률이 나와서 당황..!!! 그래도 나름대로 해법을 잘 찾아낸것 같다..
1) 우선 이 문제에서 bunny는 결과에 영향을 주지 않는다.. 그냥 무시!! 2) monster개수가 홀수이면 결국 하나는 살아남기때문에 답은 0 3) (monster끼리 만날 경우의수) / (monster + 나 중에서 두 생물체가 만날 경우의수) 가 monster끼리 싸우다 죽는 확률이 된다..
3)의 사건이 발생하면 monster가 2마리씩 죽게되고.. 이 과정을 0마리가 될때까지 반복하면 된다. 풀고나서도 그닥 확신이 않섰던 문제..
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <string> 6 using namespace std; 7 8 int nC2(int n) 9 { 10 int res; 11 res = n * (n-1); 12 res /= 2; 13 return res; 14 } 15 16 class MonstersAndBunnies { 17 public: 18 19 double survivalProbability(int M, int B) 20 { 21 int a, b; 22 double res; 23 if (M & 1) 24 return 0.0; 25 if (M == 0) 26 return 1.0; 27 res = 1.0; 28 while (M) { 29 a = nC2(M); 30 b = nC2(M+1); 31 res *= (double)a / (double)b; 32 M -= 2; 33 } 34 printf("res = %lf\n", res); 35 return res; 36 } 37 38 };
[600] PrimeSums
Problem Statement
The vector <int> bag describes a bag of non-negative integers. A bag is the same thing as a set, only it may contain repeated elements. The order of elements in a bag does not matter. Given two bags A and B, we say that A is a sub-bag of B if A can be obtained by erasing zero or more elements from B. The weight of a bag is the sum of its elements. Examples: The bags (1,2,1,3,1) and (3,1,1,1,2) are the same, but different from the bag (1,2,3,3). Bags (1,2) and (3,1,1) are sub-bags of the bag (1,2,1,3,1), but bag (1,2,2) is not. The weight of the bag (1,2,1,3,1) is 1+2+1+3+1=8. Write a method that will compute how many sub-bags of bag have a prime weight. Definition
Class: PrimeSums Method: getCount Parameters: vector <int> Returns: long long Method signature: long long getCount(vector <int> bag) (be sure your method is public)
Notes - A prime number is a positive integer with exactly two positive integer divisors. - Zero (0) and one (1) are not prime numbers. Constraints - bag will contain between 1 and 50 elements, inclusive. - Each element in bag will be between 0 and 10,000, inclusive. Examples 0)
{1,1,2,7} Returns: 5 This bag has 12 different sub-bags: (1,1,2,7), (1,2,7), (2,7), (1,1,7), (1,7), (7), (1,1,2), (1,2), (2), (1,1), (1), and (). Out of these 12, 5 have prime weights: (1,1,2,7) has weight 11, (7) has weight 7, (1,2) has weight 3, and both (2) and (1,1) have weight 2. 1)
{1,1,1,1,1,1,1,1,1,1} Returns: 4 This bag has eleven different sub-bags. Out of them four have prime weights (2, 3, 5, and 7). 2)
{4,6,8,10,12,14} Returns: 0 The empty sub-bag has weight zero, and any other sub-bag has an even weight greater than 2. 3)
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
주어진 bag (multiset이라고 생각하면 된다)의 sub-bag 중 원소의 합이 prime이 되는 sub-bag의 개수를 구하는 문제..
이 문제는 동전교환문제의 변형정도로 생각해서 DP로 풀면 될듯하다.. 문제는 중복제거와 0에 대한 특별처리 정도.. 이문제를 끝까지 잡았으면 풀었으려나..? 못풀었으려나..?
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <string> 6 using namespace std; 7 8 int is_prime[500001]; 9 long long dp[500001]; 10 void sieve() 11 { 12 int i, j; 13 memset(is_prime, -1 ,sizeof(is_prime)); 14 is_prime[1] = is_prime[0] = 0; 15 for (i = 2; i <= 500000; i++) { 16 if (is_prime[i]) { 17 for(j = 2; i*j <= 500000; j++) 18 is_prime[i*j] = 0; 19 } 20 } 21 } 22 23 class PrimeSums { 24 public: 25 26 long long getCount(vector <int> bag) 27 { 28 int n; 29 int i, j, k; 30 int cnt[10001]; 31 long long sum, res; 32 33 sieve(); 34 memset(cnt, 0, sizeof(cnt)); 35 memset(dp, 0, sizeof(dp)); 36 n = bag.size(); 37 sum = 0; 38 39 for (i = 0; i < n; i++) { 40 cnt[bag[i]]++; 41 sum += bag[i]; 42 } 43 dp[0] = 1 + cnt[0]; 44 for (i = 1; i <= 10000; i++) { 45 if (cnt[i] == 0) 46 continue; 47 for (j = sum; j-i >= 0; j--) { 48 for (k = 1; k <= cnt[i]; k++) { 49 if (j - (i*k) >= 0) 50 dp[j] += dp[j-i*k]; 51 } 52 } 53 } 54 res = 0; 55 for (i = 2; i <= sum; i++) { 56 if (is_prime[i] && dp[i] != 0) { 57 res += dp[i]; 58 } 59 } 60 printf("res = %lld\n", res); 61 return res; 62 63 } 64 65 };
[900] MagicFingerprint
Problem Statement
Given a positive integer N with at least two digits, we can define magic(N) as follows: Write N as a string of decimal digits. Now, for each two consecutive digits, compute their difference (a non-negative number less than ten), and write it down. In this way you'll obtain a new number, possibly with leading zeroes. Drop unnecessary leading zeroes if there are any. For example, magic(5913)=482, magic(1198)=081=81, and magic(666)=00=0. For any number N the sequence: N, magic(N), magic(magic(N)), ... will sooner or later terminate with a single-digit number. This final value is called the magic fingerprint of N. For example, for N=5913 we get the sequence: 5913, 482, 46, 2. Therefore the magic fingerprint of 5913 is 2. There are not too many numbers with the magic fingerprint seven (7). These numbers are considered lucky. Write a method that will compute the count of lucky numbers between A and B, inclusive. Definition
Class: MagicFingerprint Method: countLuckyNumbers Parameters: int, int Returns: int Method signature: int countLuckyNumbers(int A, int B) (be sure your method is public)
Constraints - B will be between 1 and 1,000,000,000, inclusive. - A will be between 1 and B, inclusive. Examples 0)
1 9 Returns: 1 The only single-digit lucky number is, of course, the lucky number seven. 1)
1 100 Returns: 6 There are five two-digit lucky numbers: 18, 29, 70, 81, and 92. 2)
1198 1198 Returns: 1 The number 1198 is lucky. The corresponding sequence is: 1198, 81, 7. 3)
1223 1299 Returns: 0 This range contains no lucky numbers. 4)
999999930 1000000000 Returns: 2 The two lucky numbers in this range are 999,999,980 and 999,999,992. 5)
100000 1000000000 Returns: 159720 The largest output is obviously not much larger. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
magic이란 연산은 임의의 수를 이웃하는 digit끼리의 차로 이루어진 새로운 수로 변환한다.. magic 연산을 계속 반복하면 결국 한자리 수가 되는데 그 수가 7이 되는 수를 lucky number라고 한다. A와 B사이의 수 중 lucky number의 개수 구하기..
이 문제를 푼사람 중에 정말 어이없게도 1부터 10억까지 미리 다돌려서 lucky number를 다 찍어놓은다음에.. 그 수를 수동으로 다 입력시켜놓은 사람도 있었다.. 아..!! 나는 왜 그런방법조차 생각을 못했지..????