0w1

Entries from 2017-01-01 to 1 year

Yuki 301 サイコロで確率問題 (1)

Problem Description: You have a dice with 6 faces, mapping to integer in 1..6. You roll the dice, and sum up them along the process. However, you will reset the sum to 0, when the sum has exceeded N. Find the expected number of rolls you h…

HR Permutation Happiness

Problem Description: Happiness is a value defined upon a permutation, where it is given by the number of values with at least one value higher than itself that is adjacent. Given Q queries, output number of size n permutation with at least…

CFR 815 C. Karen and Supermarket

Problem Description: There are N items, the i'th item costs C[i], and we have a coupon that can reduce its price by D[i]. However, for every coupon i to be effective, coupon X[i] must be used, i.e., i'th item bought. You have B units of mo…

Yuki 550 夏休みの思い出(1)

Problem Description: Solve f(x) = x * x * x + A * x * x + B * x + C, given A, B, and C. Output the solutions in increasing order.Constraints: -1e18 -1e9 It is guaranteed that there are exactly 3 distinct solutionsSolution: pekempey showed …

CFR 825 F. String Compression

Problem Description: Given string S, find the minimum length of its running length encoding.Constraints: 1 ≤ len(S) ≤ 8000Solution: dp[i]: answer for S[0...i] dp[j] = min(dp[i] + cost(i, j) for i in 0...j) kmp[i]: maximum length of common …

Yuki 546 オンリー・ワン

Problem Description: Given N numbers as C[0...N], L, and H, find the number of values in range [L, H] that is multiple of exactly one value in C.Constraints: 1 ≤ N ≤ 10 1 ≤ C[i] ≤ 1e9 1 ≤ L ≤ H ≤ 1e9Solution: Solve for (L' = 1, H' = H) and…

CFR 843 D. Dynamic Shortest Path

Problem Description: You are given a directed weighted graph with N nodes and M edges. Q queries follow: OP = 1: Output the shortest path from node 1 to node V. OP = 2: Read C values, the C[i]'th edge should have its weight incremented by …

CFR 428 D. Winter is here

Problem Description: You are given an array A[] of N values. For each subset that has gcd greater than 1, its contribution is gcd * size_of_subset. Output the sum of contributions for every such subset, modulo 1e9 + 7.Constraints: 1 ≤ N ≤ …

Yuki 599 回文かい

Problem Description: You are given a string S. You are requested to split it into some substrings, and suppose you decompose it into k substring as , then must hold for each . You are interested in knowing the number of ways to decompose t…

Yuki 612 Move on grid

Problem Description: Initially you are at coordinate (0, 0, 0). At each second, you have equal probability to move yourself with vector (-a, 0, 0), (a, 0, 0), (0, -b, 0), (0, b, 0), (0, 0, -c), (0, 0, c). Given t, a, b, c, d, e, and let p …

Yuki 615 集合に分けよう

Problem Description: There is a set of N numbers. You would like to split the set into K non-empty subsets, so that the sum of contribution of each subset is minimal. Contribution of a subset can be computed as the absolute difference betw…

Yuki 616 へんなソート

Problem Description: You are given an array A with length N. You have a weird "sorting" algorithm, the output is non-deterministic, but we know that it must be some arrangement of the input array, and contains no more than K inversions. Fi…

Yuki 623 fudan no modulus to tigau

Problem Description: Given Q queries of X's, compute f(X), modulo 998244353.Constraints: 2≤n≤50 1≤q≤50 0≤ai,bi≤i - 1 0≤xi≤998244352Solution: Compute each query independently, the only variant is n (relevantly small), so we can apply DP.Cod…

Yuki 626 Randomized 01 Knapsack

Problem Description: 0/1 Knapsack problem. Test cases are generated at complete randomness.Constraints: 2 ≤ N ≤ 5000 v[i], w[i] is uniformly distributed in range [1, 1e12] 1 ≤ W ≤ 1e12 * NSolution: Branch and Bound. Referenced kimiyuki's b…

SOJ 86. 一維陣列區間加值區間乘值區間查詢總和

Problem Description: Initially, you are given an array of length N. Q operations follow: OP 1: Add C to every element in X..Y OP 2: Multiply by W on every element in X..Y OP 3: Output the sum of elements in X..Y, modulo 1e9 + 7Constraints:…

ARC 039 C - 幼稚園児高橋君

Problem Description: You are on an infinitely large grid. Initially you are at coordinate (0, 0). You would move towards of the four directions (URDL) each unit of time, and stop at the first integer coordinate that had not yet been visite…

ARC 039 D - 旅行会社高橋君

Problem Description: You have a graph with N vertices, M edges. You are then given Q queries, each denoted by three indexes of nodes, A, B, and C. For every such query, you need to output whether it is possible to visit node A, B, and C, i…

POJ 1286 Necklace of Beads

Problem Description: Three colors of beads are available. Count the number of different circular necklace that could be formed. Two necklaces are identical iff after some rotation or flip, each position has the same color of bead.Constrain…

POJ 2345 Central heating

Problem Description: There are N switches and N technicians. Each technician has a list of valves, and if he were hired, he would flip the switch of each of them in his list. It is known that for any technician i, it is impossible to repla…

GCJ Japan 2011 Final B. Multiplication of Bacteria

Problem Description: There are T standalone test cases in this problem. In the beginning, there are A units of bacteria. For any moment t with x units of bacteria, we know that moment t + 1 would have x**x units of bacteria. Given A, B, C,…

POJ 2720 Last Digits

Problem Description: f(x) = 1, if x = 0 B ** f(x - 1), otherwise Given B, I, and N, find the last N digits of f(I).Constraints: Solution: We want to find . For any , we can observe that in , we can always find some such that . We will recu…

POJ 2115 C Looooops

Problem Description: You have a for loop that looks like this: for (value_type i = A; i != B; i += C) ; , where value_type is K-bits unsigned integer data type. You want to know whether the loop will reach an end, and if it will, the numbe…

POJ 1150 The Last Non-zero Digit

Problem Description: Given N, M, find the last non-zero digit in N! / (N - M)!.Constraints: 0 ≤ M ≤ N ≤ 2e7Solution: We can find a and k such that in O(lg x) time, given that e is 2 or 5 (because they are prime, we can apply Wilson's theor…

Yuki 589 Counting Even

Problem Description: Given a non-negative integer N, find the number of i, such that C(N, i) % 2 == 0 and 0 ≤ i ≤ N.Constraints: 0 ≤ N ≤ 1e18Solution: According Lucas's theorem, since we are considering modulo 2, we can observe that i can …

Yuki 125 悪の花弁

Problem Description: There are K kinds of colors, each with at least one instance of object. There are C[i] number of instances of color i. You are to arrange them into a ring, and you want to know how many different kinds of rings could b…

Yuki 42 貯金箱の溜息

Problem Description: There are 6 kinds of coins, with units 1, 5, 10, 50, 100, 500, respectively. You have infinite number of each of them. You are to deal with T test cases, and in each case you will be given a value M, where you should o…

Geometry Default Code

#include <iostream> #include <cmath> #include <algorithm> #include <complex> #include <vector> #include <iomanip> using namespace std; namespace kika { // using cod = complex<double>; typedef complex<double> cod; const double EPS = 1e-9; const double PI = acos(-1); int dcmp(double x) { if (abs(x) < …</double></double></iomanip></vector></complex></algorithm></cmath></iostream>

CFR 444 C. DZY Loves Colors ( Segment Tree, Dummy Constraints )

Problem - 444C - Codeforces題意: 給長度 N 的數列 A[]。初始時 A[ i ] = i, for all 1 ≤ i ≤ N。 M 筆詢問,兩種形式: 1 l r x:將 [ l, r ] 都塗色為 x 2 l r:輸出 [ l, r ] 的貢獻總和 初始時所有元素的貢獻為 0。 當一個元素由 u 變為 v 時,增加貢…

CFR 522 D. Closest Equals ( Segment Tree )

Problem - 522D - Codeforces題意: 給你長度 N 的數列 A[]。 M 筆詢問,問 [ L, R ] 中,最近的相同元素距離是多少。若不存在,輸出 -1。制約: 1 ≤ N, M ≤ 5e5 abs( A[ i ] ) ≤ 1e9解法: 可以很快想到,給每一個位置 i 紀錄前一個 A[ i ] 的位置 pre[ i ]…

CFR 556 D. Restructuring Company ( Union Find )

Problem - 566D - Codeforces題意: 給你 N 個人,初始時他們自己一個組。 Q 筆詢問,三種形式: 1 x y:merge( x, y ) 2 x y:merge( x, x + 1, ..., y ) 3 x y:print "YES" if group( x ) == group( y ) else "NO"制約: 1 ≤ N ≤ 2e5 1 ≤ Q ≤ 5e5解法: …