lightoj1255

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

1255 - Substring Frequency

InputA string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given two non-empty strings A and B, both contain lower case English alphabets. You have to find the number of times B occurs as a substring of A.

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

Each case starts with two lines. First line contains A and second line contains B. You can assume than 1 ≤ length(A), length(B) ≤ 106.

Output

For each case, print the case number and the number of times B occurs as a substring of A.

Sample Input

Output for Sample Input

4axbyczdabcabcabcabcabc

abc

aabacbaabbaaz

aab

aaaaaa

aa

Case 1: 0Case 2: 4Case 3: 2Case 4: 5

Note

Dataset is huge, use faster I/O methods.

 

题目类型:KMP

算法分析:这是一道使用KMP算法求解模式串在目标串中数量的题目,思路是如果模式串匹配成功,则将模式串指针j回溯至Next[j],看看能不能再一次匹配

 

lightoj1215

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

1215 - Finding LCM

You will be given a, b and L. You have to find c such that LCM (a, b, c) = L. If there are several solutions, print the one where c is as small as possible. If there is no solution, report so.LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, c) = L if and only if L is the least integer which is divisible by a, b and c.

Input

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

Each case starts with a line containing three integers a b L (1 ≤ a, b ≤ 106, 1 ≤ L ≤ 1012).

Output

For each case, print the case number and the minimum possible value of c. If no solution is found, print 'impossible'.

Sample Input

Output for Sample Input

33 5 30209475 6992 770868002 6 10 Case 1: 2Case 2: 1Case 3: impossible

 

题目类型:素因子分解

算法分析:这是一道使用素因子分解求解LCM相关问题的典型例题。首先将a、b和L进行素因子分解,然后从小到大枚举判断L的每个素因子的指数值k,若a和b对应的指数值的最大值小于k,则c的对应的指数值就是k。若a和b对应的指数值的最大值等于k,则不需要进行任何操作。若大于k,则不可能出现这种情况,直接输出”impossible”即可。枚举完L的所有素因子之后,注意要查看此时a和b是否还有没在前面判断中出现的素因子。若还有,则也输出”impossible”。反之,则输出c的值

 

lightoj1213

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

1213 - Fantasy of a Summation

#include <stdio.h>If you think codes, eat codes then sometimes you may get stressed. In your dreams you may see huge codes, as I have seen once. Here is the code I saw in my dream.

int cases, caseno;
int n, K, MOD;
int A[1001];

int main() {
scanf("%d", &cases);
while( cases-- ) {
scanf("%d %d %d", &n, &K, &MOD);

int i, i1, i2, i3, ... , iK;

for( i = 0; i < n; i++ ) scanf("%d", &A[i]);

int res = 0;
for( i1 = 0; i1 < n; i1++ ) {
for( i2 = 0; i2 < n; i2++ ) {
for( i3 = 0; i3 < n; i3++ ) {
...
for( iK = 0; iK < n; iK++ ) {
res = ( res + A[i1] + A[i2] + ... + A[iK] ) % MOD;
}
...
}
}
}
printf("Case %d: %d\n", ++caseno, res);
}
return 0;
}

Actually the code was about: 'You are given three integers nKMOD and n integers: A0, A1, A2 ... An-1, you have to write K nested loops and calculate the summation of all Ai where i is the value of any nested loop variable.'

Input

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

Each case starts with three integers: n (1 ≤ n ≤ 1000), K (1 ≤ K < 231), MOD (1 ≤ MOD ≤ 35000). The next line contains n non-negative integers denoting A0, A1, A2 ... An-1. Each of these integers will be fit into a 32 bit signed integer.

Output

For each case, print the case number and result of the code.

Sample Input

Output for Sample Input

23 1 350001 2 32 3 35000

1 2

Case 1: 6Case 2: 36

 

题目类型:简单数学 

算法分析:可以直接计算出每一位中可能出现的数的和,然后再乘以位数k就可以了,公式为sum * (n ^ (k - 1)) * k % mod,sum表示一位中所有数的总和,n ^ (k - 1)表示在一位中出现一种数字的次数,由于数据比较大,所以需要使用整数快速幂取模来计算

 

lightoj1212

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

1212 - Double Ended Queue

pushLeft(): inserts an item to the left end of the queue with the exception that the queue is not full.A queue is a data structure based on the principle of 'First In First Out' (FIFO). There are two ends; one end can be used only to insert an item and the other end to remove an item. A Double Ended Queue is a queue where you can insert an item in both sides as well as you can delete an item from either side. There are mainly four operations available to a double ended queue. They are:

  1. pushRight(): inserts an item to the right end of the queue with the exception that the queue is not full.
  2. popLeft(): removes an item from the left end of the queue with the exception that the queue is not empty.
  3. popRight(): removes an item from the right end of the queue with the exception that the queue is not empty.

Now you are given a queue and a list of commands, you have to report the behavior of the queue.

Input

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

Each case starts with a line containing two integers n, m (1 ≤ n ≤ 10, 1 ≤ m ≤ 100), where n denotes the size of the queue and m denotes the number of commands. Each of the next mlines contains a command which is one of:

pushLeft x        pushes x (-100 ≤ x ≤ 100) in the left end of the queue

pushRight x      pushes x (-100 ≤ x ≤ 100) in the right end of the queue

popLeft             pops an item from the left end of the queue

popRight           pops an item from the right end of the queue

 

Output

For each case, print the case number in a line. Then for each operation, show its corresponding output as shown in the sample. Be careful about spelling.

Sample Input

Output for Sample Input

13 8pushLeft 1pushLeft 2pushRight -1pushRight 1

popLeft

popRight

popLeft

popRight

Case 1:Pushed in left: 1Pushed in left: 2Pushed in right: -1The queue is fullPopped from left: 2

Popped from right: -1

Popped from left: 1

The queue is empty

 

 

题目类型:双端队列

算法分析:直接使用STL中的deque模拟即可,只不过还要动态维护一个队列中元素个数cnt

 

lightoj1202

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

1202 - Bishops

Now you are given the position of the two bishops. You have to find the minimum chess moves to take one to another. With a chess move, a bishop can be moved to a long distance (along the diagonal lines) with just one move.There is an Infinite chessboard. Two bishops are there. (Bishop means the chess piece that moves diagonally).

Input

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

Each case contains four integers r1 c1 r2 c2 denoting the positions of the bishops. Each of the integers will be positive and not greater than 109. You can also assume that the positions will be distinct.

Output

For each case, print the case number and the minimum moves required to take one bishop to the other. Print 'impossible' if it's not possible.

Sample Input

Output for Sample Input

31 1 10 101 1 10 111 1 5 3 Case 1: 1Case 2: impossibleCase 3: 2

 

题目类型:简单数学

算法分析:如果两个点的坐标都不相等且之间的直线的斜率为1或者是-1,则输出1,反之继续判断。判断的思路是做两条过两点,斜率分别为1和-1的直线,判断两直线的交点是否在整数点上

 

 

lightoj1198

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

1198 - Karate Competition

Your secret agents have determined the skill of every member of the opposing team, and of course you know the skill of every member of your own team. You can use this information to decide which opposing player will play against each of your players in order to maximize your score. Assume that the player with the higher skill in a game will always win, and if the players have the same skill then they will draw.Your karate club challenged another karate club in your town. Each club enters N players into the match, and each player plays one game against a player from the other team. Each game that is won is worth 2 points, and each game that is drawn is worth 1 point. Your goal is to score as many points as possible.

You will be given the skills of your players and of the opposing players, you have to find the maximum number of points that your team can score.

Input

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

Each case starts with a line containing an integer N (1 ≤ N ≤ 50). The next line contains N space separated integers denoting the skills of the players of your team. The next line also contains N space separated integers denoting the skills of the players of the opposite team. Each of the skills lies in the range [1, 1000].

Output

For each case, print the case number and the maximum number of points your team can score.

Sample Input

Output for Sample Input

424 76 2

2

6 2

4 7

3

5 10 1

5 10 1

4

10 7 1 4

15 3 8 7

Case 1: 4Case 2: 2Case 3: 4Case 4: 5

 

题目类型:贪心

算法分析:贪心策略是如果我最强比对方最强的能力高,则两者比;如果我最强的比对方最强的弱,则拿我队最弱的和对方最强的比;如果我最强的和对方最强的能力相同,则比较我队最弱的和对方最弱的,如果我队最弱的比对方最弱的强,则两者比;否则拿我最弱和对方最强的比

 

lightoj1197

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

1197 - Help Hanzo

Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.

He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!

Input

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

Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 231, b - a ≤ 100000).

Output

For each case, print the case number and the number of safe territories.

Sample Input

Output for Sample Input

32 363 733 11 Case 1: 11Case 2: 20Case 3: 4

Note

A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...

 

题目类型:素数

算法分析:由于问题的规模比较大,不可能将所有的范围内的素数都打表出来,只能将1~sqrt (2 ^ 31 - 1)内的素数先打表出来,然后再对于每个给定的区间(区间长度小于1000000)进行求解

 

lightoj1183

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

1183 - Computing Fast Average

1 i j v - change the value of the elements from ith index to jth index to v.Given an array of integers (0 indexed), you have to perform two types of queries in the array.

  1. 2 i j - find the average value of the integers from ith index to jth index.

You can assume that initially all the values in the array are 0.

Input

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

Each case contains two integers: n (1 ≤ n ≤ 105), q (1 ≤ q ≤ 50000), where n denotes the size of the array. Each of the next q lines will contain a query of the form:

1 i j v (0 ≤ i ≤ j < n, 0 ≤ v ≤ 10000)

2 i j (0 ≤ i ≤ j < n)

Output

For each case, print the case number first. Then for each query of the form '2 i j' print the average value of the integers from i to j. If the result is an integer, print it. Otherwise print the result in 'x/y' form, where x denotes the numerator and y denotes the denominator of the result and x and y are relatively prime.

Sample Input

Output for Sample Input

110 61 0 6 62 0 1

1 1 1 2

2 0 5

1 0 3 7

2 0 1

Case 1:616/37

Note

Dataset is huge. Use faster i/o methods.

 

题目类型:线段树+Lazy-Tag

算法分析:直接使用Lazy标记的线段树求解即可,注意多组数据之间要初始化lazy数组和sum数组!!!

 

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

 

 

题目类型:简单栈模拟

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