lightoj1174

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

1174 - Commandos

InputA group of commandos were assigned a critical task. They are to destroy an enemy head quarter. The enemy head quarter consists of several buildings and the buildings are connected by roads. The commandos must visit each building and place a bomb at the base of each building. They start their mission at the base of a particular building and from there they disseminate to reach each building. The commandos must use the available roads to travel between buildings. Any of them can visit one building after another, but they must all gather at a common place when their task in done. In this problem, you will be given the description of different enemy headquarters. Your job is to determine the minimum time needed to complete the mission. Each commando takes exactly one unit of time to move between buildings. You may assume that the time required to place a bomb is negligible. Each commando can carry unlimited number of bombs and there is an unlimited supply of commando troops for the mission.

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

The first line of each case starts with a positive integer N (1 ≤ N ≤ 100), where N denotes the number of buildings in the head quarter. The next line contains a positive integer R, where Ris the number of roads connecting two buildings. Each of the next R lines contain two distinct numbers u v (0 ≤ u, v < N), this means there is a road connecting building u to building v. The buildings are numbered from 0 to N-1. The last line of each case contains two integers s d (0 ≤ s, d < N). Where s denotes the building from where the mission starts and d denotes the building where they must meet. You may assume that two buildings will be directly connected by at most one road. The input will be given such that, it will be possible to go from any building to another by using one or more roads.

Output

For each case, print the case number and the minimum time required to complete the mission.

Sample Input

Output for Sample Input

2430 1

2 1

1 3

0 3

2

1

0 1

1 0

Case 1: 4Case 2: 1

 

题目类型:最短路

算法分析:使用SPFA和一个邻接表、一个逆邻接表分别求得最短路径,然后将每个点的两个最短路相加再取最大值,否则会WA!!!!!!

 

lightoj1164

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

1164 - Horrible Queries

0 x y v - you have to add v to all numbers in the range of x to y (inclusive), where x and y are two indexes of the array.World is getting more evil and it's getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr. Horrible, who has a PhD in horribleness (but not in Computer Science). You are given an array of n elements, which are initially all 0. After that you will be given q commands. They are -

  1. 1 x y - output a line containing a single integer which is the sum of all the array elements between x and y (inclusive).

The array is indexed from 0 to n - 1.

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). Each of the next q lines contains a task in one of the following form:

0 x y v (0 ≤ x ≤ y < n, 1 ≤ v ≤ 1000)

1 x y (0 ≤ x ≤ y < n)

Output

For each case, print the case number first. Then for each query '1 x y', print the sum of all the array elements between x and y.

Sample Input

Output for Sample Input

210 50 0 9 101 1 6

0 3 7 2

0 4 5 1

1 5 5

20 3

0 10 12 1

1 11 12

1 19 19

Case 1:6013Case 2:

2

0

Note

Dataset is huge. Use faster i/o methods.

 

题目类型:线段树

算法分析:线段树的成段更新和区间求和问题,直接按照题目的要求求解即可

 

lightoj1140

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

1140 - How Many Zeroes?

InputJimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?

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

Each case contains two unsigned 32-bit integers m and n, (m ≤ n).

Output

For each case, print the case number and the number of zeroes written down by Jimmy.

Sample Input

Output for Sample Input

510 11100 2000 500

1234567890 2345678901

0 4294967295

Case 1: 1Case 2: 22Case 3: 92Case 4: 987654304

Case 5: 3825876150

 

题目类型:组合计数

算法分析:每次从小到大判断N的每一位上的数字,如果当前位上的数字不为0,则该位左边的所有的数字都可以和该位右边的每一位上的数字构成一个合理的结果(每一位上有0~9共10个数)。如果当前位上的数字为0,则该位左边只有小于当前数1的个数可以和右边的right+1个数构成一个合理的结果

 

lightoj1131

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

1131 - Just Two Functions

Find fn % M and gn % M. (% stands for the modulo operation.)Let fn = a1 * fn-1 + b1 * fn-2 + c1 * gn-3 gn = a2 * gn-1 + b2 * gn-2 + c2 * fn-3

Input

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

Each case starts with a blank line. Next line contains three integers a1 b1 c1 (0 ≤ a1, b1, c1 < 25000). Next line contains three integers a2 b2 c2 (0 ≤ a2, b2, c2 < 25000). Next line contains three integers f0 f1 f2 (0 ≤ f0, f1, f2 < 25000). Next line contains three integers g0 g1 g2 (0 ≤ g0, g1, g2 < 25000). The next line contains an integer M (1 ≤ M < 25000).

Next line contains an integer q (1 ≤ q ≤ 100) denoting the number of queries. Next line contains q space separated integers denoting n. Each of these integers is non-negative and less than 231.

Output

For each case, print the case number in a line. Then for each query, you have to print one line containing fn % M and gn % M.

Sample Input

Output for Sample Input

21 1 00 0 00 1 1

0 0 0

20000

10

1 2 3 4 5 6 7 8 9 10

 

1 1 1

1 1 1

2 2 2

2 2 2

20000

5

2 4 6 8 10

Case 1:1 01 02 03 0

5 0

8 0

13 0

21 0

34 0

55 0

Case 2:

2 2

10 10

34 34

114 114

386 386

 

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

算法分析:由于两个递推式之间不是独立的,所以需要将两个递推式用一个矩阵乘幂表示!!!之后直接上模板即可

 

lightoj1129

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

1129 - Consistency Checker

123SETI is receiving some weird signals for the past few days. After converting them to our number system they found out that some numbers are repeating. Due to travelling millions of miles signal gets distorted. Now they asked you check the consistency of their data sets. The consistency is that, no number is the prefix another number in that data set. Let's consider a data set:

  1. 5123456
  2. 123456

In this data set, numbers not consistent, because the first number is the prefix of the third one.

Input

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

Each case starts with an integer n (1 ≤ n ≤ 10000) denoting the total numbers in their list. Each of the next n lines contains one unique phone number. A number is a sequence of digits and has length from 1 to 10.

Output

For each case, print the case number and 'YES' if the list is consistent or 'NO' if it's not.

Sample Input

Output for Sample Input

2391197625999

91125426

5

113

12340

123440

12345

98346

Case 1: NOCase 2: YES

 

题目类型:Trie

算法分析:将所有的单词插入到Trie树中,然后对于每个输入中的单词,查询其终止节点是否有后继节点即可

 

lightoj1127

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

1127 - Funny Knapsack

InputGiven n integers and a knapsack of weight W, you have to count the number of combinations for which you can add the items in the knapsack without overflowing the weight.

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

Each case contains two integers n (1 ≤ n ≤ 30) and W (1 ≤ W ≤ 2 * 109) and the next line will contain n integers separated by spaces. The integers will be non negative and less than 109.

Output

For each set of input, print the case number and the number of possible combinations.

Sample Input

Output for Sample Input

31 111 12

3 10

1 2 4

Case 1: 2Case 2: 1Case 3: 8

 

题目类型:折半枚举

算法分析:由于物品的个数比较少,所以将物品大致分为两堆,然后分别枚举组合的情况,最后再查找小于w的组合个数

 

lightoj1116

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

1116 - Ekka Dokka

They want to divide the cake such that N * M = W, where W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the cake such that Ekka gets a share of N square centimeters and Dokka gets a share of M square centimeters whereN is odd and M is even. Both N and M are positive integers.

Input

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

Each case contains an integer W (2 ≤ W < 263). And W will not be a power of 2.

Output

For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print N and M. If there are multiple solutions, then print the result where M is as small as possible.

Sample Input

Output for Sample Input

310512 Case 1: 5 2Case 2: ImpossibleCase 3: 3 4

 

题目类型:位运算

算法分析:直接从小到大枚举所有的合数约数并判断时间复杂度是O(n)的,会TLE。这里使用的方法是首先判断n是否是偶数,如果不是则直接退出判断,输出Impossible;反之则将n左移一位判断移位后的n是否是奇数,如果是则找到,直接退出输出答案即可;反之则继续判断。该方法的时间复杂度也是O(n)的,不过这里的n表示的是输入数的二进制位数,而二进制位数不超过63,所以不会TLE

 

lightoj1113

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

1113 - Discover the Web

BACK: If the backward stack is empty, the command is ignored. Otherwise, push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page.Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. You are asked to implement this. The commands are:

  1. FORWARD: If the forward stack is empty, the command is ignored. Otherwise, push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page.
  2. VISIT <url>: Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
  3. QUIT: Quit the browser.

The browser initially loads the web page at the URL 'http://www.lightoj.com/'

Input

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

Each case contains some commands. The keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 50 characters. The end of case is indicated by the QUIT command and it shouldn't be processed. Each case contains at most 100 lines.

Output

For each case, print the case number first. For each command, print the URL of the current page (in a line) after the command is executed if the command is not ignored. Otherwise, print'Ignored'.

Sample Input

Output for Sample Input

1VISIT http://uva.onlinejudge.org/VISIT http://topcoder.com/BACK

BACK

BACK

FORWARD

VISIT http://acm.sgu.ru/

BACK

BACK

FORWARD

FORWARD

FORWARD

QUIT

Case 1:http://uva.onlinejudge.org/http://topcoder.com/http://uva.onlinejudge.org/

http://www.lightoj.com/

Ignored

http://uva.onlinejudge.org/

http://acm.sgu.ru/

http://uva.onlinejudge.org/

http://www.lightoj.com/

http://uva.onlinejudge.org/

http://acm.sgu.ru/

Ignored

 

 

题目类型:简单栈模拟

算法分析:直接按照题目所说的方式进行栈模拟即可

 

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定理求解