lightoj1112

maksyuki 发表于 oj 分类,标签:
0

1112 - Curious Robin Hood

Now each time he can he can do one of the three tasks.Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.

1)                  Give all the money of the ith sack to the poor, leaving the sack empty.

2)                  Add new amount (given in input) in the ith sack.

3)                  Find the total amount of money from ith sack to jth sack.

Since he is not a programmer, he seeks your help.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in the ith sack (0 ≤ i < n).

Each of the next q lines contains a task in one of the following form:

1 i        Give all the money of the ith (0 ≤ i < n) sack to the poor.

2 i v     Add money v (1 ≤ v ≤ 1000) to the ith (0 ≤ i < n) sack.

3 i j      Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).

Output

For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.

Sample Input

Output for Sample Input

15 63 2 1 4 51 4

2 3 4

3 0 3

1 2

3 0 4

1 1

Case 1:5141

13

2

Notes

Dataset is huge, use faster I/O methods.

 

题目类型:线段树

算法分析:线段树简单的单点更新和区间求和问题,直接建树,更新和查询即可

 

lightoj1088

maksyuki 发表于 oj 分类,标签:
0

1088 - Points in Segments

For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then there are 2 points that lie in the segment.Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points in ascending order. All the integers are distinct and each of them range in [0, 108].

Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.

Output

For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.

Sample Input

Output for Sample Input

15 31 4 6 8 100 5

6 10

7 100000

Case 1:232

Note

Dataset is huge, use faster I/O methods.

 

题目类型:二分法

算法分析:读入数据,然后二分求得所求区间的上下界在输入序列中的下标pos_low和pos_high,最后直接pos_high – pos_low即可

 

lightoj1083

maksyuki 发表于 oj 分类,标签:
0

1083 - Histogram

Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3 measured in units where the width of the rectangles is 1.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case contains a line with an integer N (1 ≤ N ≤ 30000) denoting the number of rectangles. The next line contains N space separated positive integers (≤ 30000) denoting the heights.

Output

For each case, print the case number and the largest rectangle that can be made.

Sample Input

Output for Sample Input

272 1 4 5 1 3 354 4 3 2 4 Case 1: 8Case 2: 10

Note

Dataset is huge; use faster I/O methods.

 

题目类型:单调队列

算法分析:一道经典的使用单调队列思想解的题。坐标从小到大分析,如果存在一个高度不超过先前的高度,则应该将先前高度的矩形块的面积出栈并计算,更新最大值。否则就一直入栈。最后将还在栈中的矩形块的面积计算出来并更新最大值

 

lightoj1080

maksyuki 发表于 oj 分类,标签:
0

1080 - Binary Simulation

'I i j'    which means invert the bit from i to j (inclusive)Given a binary number, we are about to do some operations on the number. Two types of operations can be here.

'Q i'    answer whether the ith bit is 0 or 1

The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a binary integer having length n (1 ≤ n ≤ 105). The next line will contain an integer q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form 'I i j' where i, j are integers and 1 ≤ i ≤ j ≤ n. Or the query will be in the form 'Q i' where i is an integer and 1 ≤ i ≤ n.

Output

For each case, print the case number in a single line. Then for each query 'Q i' you have to print 1 or 0 depending on the ith bit.

Sample Input

Output for Sample Input

200110011006I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

1011110111

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

Case 1:011

0

Case 2:

0

0

0

1

Note

Dataset is huge, use faster i/o methods.

 

题目类型:线段树

算法分析:需要使用lazy标记来进行区间修改,lazy布尔数组当为false时表示当前区间没有翻转,而true表示当前区间元素需要翻转。由于本题要进行单点查询,所以可以在查询到叶子节点后再判断lazy,且不需要PushUp

 

lightoj1078

maksyuki 发表于 oj 分类,标签:
0

1078 - Integer Divisibility

For example you have to find a multiple of 3 which contains only 1's. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3's then, the result is 6, because 333333 is divisible by 7.If an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.

Input

Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case will contain two integers n (0 < n ≤ 106 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).

Output

For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.

Sample Input

Output for Sample Input

33 17 39901 1 Case 1: 3Case 2: 6Case 3: 12

 

题目类型:简单数学

算法分析:直接迭代计算temp = (10 * temp + k) % n直到temp == 0为止,输出迭代的次数即可,因为666 = 66 * 10 + 6,则有666 % n = (66 % n * 10 + 6 % n) % n

 

lightoj1074

maksyuki 发表于 oj 分类,标签:
0

1074 - Extended Traffic

InputDhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to nrespectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.

Output

For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a '?'.

Sample Input

Output for Sample Input

256 7 8 9 106

1 2

2 3

3 4

1 5

5 4

4 5

2

4

5

 

2

10 10

1

1 2

1

2

Case 1:34Case 2:?

 

题目类型:单源最短路径(SPFA)

算法分析:SPFA算法的直接应用,注意判断输出’?’的条件是不可达、最短路径长度小于3或者是不存在负权值回路,注意一定要判断是否存在负权值回路,否则程序会TLE!!!!!!

 

lightoj1070

maksyuki 发表于 oj 分类,标签:
0

1070 - Algebraic Problem

InputGiven the value of a+b and ab you will have to find the value of an+bna and b not necessarily have to be real numbers.

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and q denotes the value of ab. Each number in the input file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.

Output

For each test case, print the case number and (an+bn) modulo 264.

Sample Input

Output for Sample Input

210 16 27 12 3 Case 1: 68Case 2: 91

 

题目类型:递推+矩阵快速幂取模

算法分析:首先推出递推公式:Sn = p*Sn-1 - q * Sn-2,然后使用矩阵快速幂取模求解。注意要使用unsigned long long 来定义变量,输入、输出!!!!!!

 

lightoj1067

maksyuki 发表于 oj 分类,标签:
0

1067 - Combinations

For example, say there are 4 items; you want to take 2 of them. So, you can do it 6 ways.Given n different objects, you want to take k of them. How many ways to can do it?

Input

Input starts with an integer T (≤ 2000), denoting the number of test cases.

Each test case contains two integers n (1 ≤ n ≤ 106), k (0 ≤ k ≤ n).

Output

For each case, output the case number and the desired value. Since the result can be very large, you have to print the result modulo 1000003.

Sample Input

Output for Sample Input

34 25 06 4 Case 1: 6Case 2: 1Case 3: 15

 

题目类型:组合数取模(Lucas)

算法分析:由于模值p不算太大且为质数,则可以使用Lucas定理求解

 

lightoj1062

maksyuki 发表于 oj 分类,标签:
0

1062 - Crossed Ladders

InputA narrow street is lined with tall buildings. An x foot long ladder is rested at the base of the building on the right side of the street and leans on the building on the left side. A y foot long ladder is rested at the base of the building on the left side of the street and leans on the building on the right side. The point where the two ladders cross is exactly c feet from the ground. How wide is the street?

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each test case contains three positive floating point numbers giving the values of xy, and c.

Output

For each case, output the case number and the width of the street in feet. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

430 40 1012.619429 8.163332 310 10 3

10 10 1

Case 1: 26.0328775442Case 2: 6.99999923Case 3: 8Case 4: 9.797958971

 

 

题目类型:二分

算法分析:直接手算得出求c的公式,然后二分枚举胡同的宽度即可,注意二分枚举的开始区间为[0, min (a,b)]!!!!!!

 

lightoj1056

maksyuki 发表于 oj 分类,标签:
0

1056 - Olympics

One of the most important tasks is to build the stadium. You are appointed as a programmer to help things out in certain matters - more specifically in designing and building the athletics tracks. After some study, you find out that athletics tracks have a general shape of a rectangle with two sliced circles on two ends. Now the turf that is placed inside this rectangle is prepared elsewhere and comes in different shapes - different length to width ratios. You know one thing for certain - your track should have a perimeter of 400 meters. That's the standard length for athletics tracks. You are supplied with the design parameter - length to width ratio. You are also told that the sliced circles will be such that they are part of the same circle. You have to find the length and width of the rectangle.The next Olympic is approaching very shortly. It's a hard job for the organizers. There are so many things to do - preparing the venues, building the Olympic village for accommodating athletes and officials, improving the transportation of the entire city as the venues are located all over the city and also there will be great number of tourists/spectators during the Olympics.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with the ratio of the length and width of the rectangle in the format: "a : b". Here, a and b will be integers and both will be between 1 and 1000 (inclusive).

Output

For each case, print the case number, the length and the width. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

23 : 25 : 4 Case 1: 117.1858168 78.12387792Case 2: 107.29095604 85.8327648

 

 

题目类型:简单二分

算法分析:二分体育场的长,然后对于每一个长通过几何关系计算出体育场跑道的总周长。如果周长大于400,则向小的方向二分,反之,则向大的方向二分

 

lightoj1054

maksyuki 发表于 oj 分类,标签: , ,
0

1054 - Efficient Pseudo Code

pseudo codeSometimes it's quite useful to write pseudo codes for problems. Actually you can write the necessary steps to solve a particular problem. In this problem you are given a pseudo code to solve a problem and you have to implement the pseudo code efficiently. Simple! Isn't it? 🙂

{

take two integers n and m

let p = n ^ m (n to the power m)

let sum = summation of all the divisors of p

let result = sum MODULO 1000,000,007

}

Now given n and m you have to find the desired result from the pseudo code. For example if n = 12 and m = 2. Then if we follow the pseudo code, we get

pseudo code

{

take two integers n and m

so, n = 12 and m = 2

let p = n ^ m (n to the power m)

so, p = 144

let sum = summation of all the divisors of p

so, sum = 403, since the divisors of p are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 36, 48, 72, 144

let result = sum MODULO 1000,000,007

so, result = 403

}

Input

Input starts with an integer T (≤ 5000), denoting the number of test cases.

Each test case will contain two integers, n (1 ≤ n) and m (0 ≤ m). Each of n and m will be fit into a 32 bit signed integer.

Output

For each case of input you have to print the case number and the result according to the pseudo code.

Sample Input

Output for Sample Input

312 212 136 2 Case 1: 403Case 2: 28Case 3: 3751

 

题目类型:素因子分解+逆元+整数快速幂取模

算法分析:首先使用Euler筛将素数预打表,然后从小到大判断每个素数在a中出现的指数值,然后带入约数和公式边模边求解,这里使用了一个求逆元非常好的公式:a / b mod (p) = a mod (mb) / m,由于指数比较大,所以要使用整数快速幂来加速运算,注意在判断条件primelist[i] * primelist[i] <= a时primelist[i]*primelist[i]相乘的结果应该强制转换成long long,否则会RE

 

lightoj1045

maksyuki 发表于 oj 分类,标签:
0

1045 - Digits of Factorial

f(0) = 1 f(n) = f(n - 1) * n, if(n > 0)Factorial of an integer is defined by the following function

So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170.

In this problem, you have to find the number of digit(s) of the factorial of an integer in a certain base.

Input

Input starts with an integer T (≤ 50000), denoting the number of test cases.

Each case begins with two integers n (0 ≤ n ≤ 106) and base (2 ≤ base ≤ 1000). Both of these integers will be given in decimal.

Output

For each case of input you have to print the case number and the digit(s) of factorial n in the given base.

Sample Input

Output for Sample Input

55 108 1022 3

1000000 2

0 100

Case 1: 3Case 2: 5Case 3: 45Case 4: 18488885

Case 5: 1

 

题目类型:简单数学

算法分析:结果是log base n!,直接打表计算出ln i的值(前缀和),然后对于每一个查询直接计算结果,注意使用cout输出格式可能会出现问题!输出结果要加上EPS!!!!!!

 

lightoj1043

maksyuki 发表于 oj 分类,标签:
0

1043 - Triangle Partitioning

You are given ABAC and BCDE is parallel to BC. You are also given the area ratio between ADE and BDEC. You have to find the value of AD.See the picture below.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case begins with four real numbers denoting AB, AC, BC and the ratio of ADE and BDEC (ADE / BDEC). You can safely assume that the given triangle is a valid triangle with positive area.

Output

For each case of input you have to print the case number and AD. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

4100 100 100 210 12 14 17 8 9 10

8.134 9.098 7.123 5.10

Case 1: 81.6496580Case 2: 7.07106781Case 3: 6.6742381247Case 4: 7.437454786

 

 

题目类型:简单二分

算法分析:可以直接的手算出计算的公式计算AD长

 

lightoj1042

maksyuki 发表于 oj 分类,标签:
0

1042 - Secret Origins

But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - "Dokhina Batash". She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr's existence know very little about her. For example, no one has any clue as to which time period she is originally from.

The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.

You are now given a similar task. Find the first number after N which has the same Onoroy value as N.

Input

Input starts with an integer T (≤ 65), denoting the number of test cases.

Each case begins with an integer N (1 ≤ N ≤ 109).

Output

For each case of input you have to print the case number and the desired result.

Sample Input

Output for Sample Input

5231423239178 Case 1: 27Case 2: 14241Case 3: 395Case 4: 11Case 5: 16

 

题目类型:位运算

算法分析:如果直接枚举比n大的数并判断二进制中1的个数是否相同会TLE,此时使用的方法是将n的二进制表示存储到数组中,然后依次求长度为i的二进制位中的下一个排列,如果长度为i的二进制位存在下一个排列,则直接将该数转化为十进制输出即可。如果没有下一个合理的排列,则尝试下一个长度的二进制位的排列。由于尝试的二进制的排列的长度不超过2次,所以时间复杂度几乎为O(n)的,其中n为原十进制数的二进制表示的长度,而由已知可得n不超过32,所以不会TLE

 

lightoj1041

maksyuki 发表于 oj 分类,标签:
0

1041 - Road Construction

You are given the description of roads. Damaged roads are formatted as "city1 city2 cost" and non-damaged roads are formatted as "city1 city2 0". In this notation city1 and city2 are the case-sensitive names of the two cities directly connected by that road. If the road is damaged, cost represents the price of rebuilding that road. Every city in the country will appear at least once in roads. And there can be multiple roads between same pair of cities.There are several cities in the country, and some of them are connected by bidirectional roads. Unfortunately, some of the roads are damaged and cannot be used right now. Your goal is to rebuild enough of the damaged roads that there is a functional path between every pair of cities.

Your task is to find the minimum cost of the roads that must be rebuilt to achieve your goal. If it is impossible to do so, print "Impossible".

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case begins with a blank line and an integer m (1 ≤ m ≤ 50) denoting the number of roads. Then there will be m lines, each containing the description of a road. No names will contain more than 50 characters. The road costs will lie in the range [0, 1000].

Output

For each case of input you have to print the case number and the desired result.

Sample Input

Output for Sample Input

212Dhaka Sylhet 0

Ctg Dhaka 0

Sylhet Chandpur 9

Ctg Barisal 9

Ctg Rajshahi 9

Dhaka Sylhet 9

Ctg Rajshahi 3

Sylhet Chandpur 5

Khulna Rangpur 7

Chandpur Rangpur 7

Dhaka Rajshahi 6

Dhaka Rajshahi 7

 

2

Rajshahi Khulna 4

Kushtia Bhola 1

Case 1: 31Case 2: Impossible

 

题目类型:MST

算法分析:读入字符串然后将字符串转化为数字标识,然后调用kruskal算法直接计算即可

 

lightoj1040

maksyuki 发表于 oj 分类,标签:
0

1040 - Donation

You will be given the lengths (in meters) of cables between each pair of rooms in your house. You wish to keep only enough cable so that every pair of rooms in your house is connected by some chain of cables, of any length. The lengths are given in n lines, each having n integers, where n is the number of rooms in your house. The jth integer of ith line gives the length of the cable between rooms i and j in your house.A local charity is trying to gather donations of Ethernet cable. You realize that you probably have a lot of extra cable in your house, and make the decision that you will donate as much cable as you can spare.

If both the jth integer of ith line and the ith integer of jth line are greater than 0, this means that you have two cables connecting rooms i and j, and you can certainly donate at least one of them. If the ith integer of ith line is greater than 0, this indicates unused cable in room i, which you can donate without affecting your home network in any way. 0 means no cable.

You are not to rearrange any cables in your house; you are only to remove unnecessary ones. Return the maximum total length of cables (in meters) that you can donate. If any pair of rooms is not initially connected by some path, return -1.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case begins with a blank line and an integer n (1 ≤ n ≤ 50) denoting the number of rooms in your house. Then there will be n lines, each having n space separated integers, denoting the lengths as described. Each length will be between 0 and 100.

Output

For each case of input you have to print the case number and desired result.

Sample Input

Output for Sample Input

3227 26

1 52

 

4

0 10 10 0

0 0 1 1

0 0 0 2

0 0 0 0

 

4

0 1 0 0

1 0 0 0

0 0 0 1

0 0 1 0

Case 1: 105Case 2: 12Case 3: -1

 

题目类型:MST

算法分析:使用kruskal算法计算最小生成树的权值和sum,然后直接使用总长度tot减去计算得到的sum,如果图不连通则直接输出-1