lightoj1427

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

1427 - Substring Frequency (II)

InputA string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given a string T and n queries each with a string Pi, where the strings contain lower case English alphabets only. You have to find the number of times Pi occurs as a substring of T.

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 500). The next line contains the string T (1 ≤ |T| ≤ 106). Each of the next n lines contains a string Pi (1 ≤ |Pi| ≤ 500).

Output

For each case, print the case number in a single line first. Then for each string Pi, report the number of times it occurs as a substring of T in a single line.

Sample Input

Output for Sample Input

25ababacbabcababa

ac

a

abc

3

lightoj

oj

light

lit

Case 1:2314

1

Case 2:

1

1

0

Notes

  1. Dataset is huge, use faster I/O methods.
  2. If S is a string then |S| denotes the length of S.

 

题目类型:AC自动机

算法分析:将子串插入到Trie树中,然后使用cnt数组统计在文本串T中,相应子串出现的个数即可,注意子串可能出现重复的情况!!!由于n比较小,所以可以使用O(n^2)的枚举将标号后面重复的相应子串的个数求解得到

 

lightoj1369

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

1369 - Answering Queries

long long f( int A[], int n ) { // n = size of AThe problem you need to solve here is pretty simple. You are give a function f(A, n), where A is an array of integers and n is the number of elements in the array. f(A, n) is defined as follows:

long long sum = 0;

for( int i = 0; i < n; i++ )

for( int j = i + 1; j < n; j++ )

sum += A[i] - A[j];

return sum;

}

Given the array A and an integer n, and some queries of the form:

1)      0 x v (0 ≤ x < n, 0 ≤ v ≤ 106), meaning that you have to change the value of A[x] to v.

2)      1, meaning that you have to find f as described above.

Input

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

Each case starts with a line containing two integers: n and q (1 ≤ n, q ≤ 105). The next line contains n space separated integers between 0 and 106 denoting the array A as described above.

Each of the next q lines contains one query as described above.

Output

For each case, print the case number in a single line first. Then for each query-type "1" print one single line containing the value of f(A, n).

Sample Input

Output for Sample Input

13 51 2 310 0 310 2 1

1

Case 1:-404

Note

Dataset is huge, use faster I/O methods.

 

题目类型:简单数学

算法分析:首先将sum求出来,然后更新时只更新a[x]的值所引起的变化,查询时直接输出结果,注意两个int型变量相乘的中间结果也是int的,可能会溢出!!!

 

lightoj1337

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

1337 - The Crystal Maze

To be more exact, the maze can be modeled as an M x N 2D grid where M denotes the number of rows and N denotes the number of columns. There are three types of cells in the grid:You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As the name suggests, the maze is full of crystals. Your task is to collect as many crystals as possible.

  1. '#' denotes a wall, you may not pass through it.
  2. 'C' denotes a crystal. You may move through the cell.
  3. '.' denotes an empty cell. You may move through the cell.

Now you are given the map of the maze, you want to find where to land such that you can collect maximum number of crystals. So, you are spotting some position x, y and you want to find the maximum number of crystals you may get if you land to cell (x, y). And you can only move vertically or horizontally, but you cannot pass through walls, or you cannot get outside the maze.

Input

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

Each case starts with a line containing three integers Mand Q (2 ≤ M, N ≤ 500, 1 ≤ Q ≤ 1000). Each of the next M lines contains N characters denoting the maze. You can assume that the maze follows the above restrictions.

Each of the next Q lines contains two integers xi and yi (1 ≤ xi ≤ M, 1 ≤ yi ≤ N) denoting the cell where you want to land. You can assume that cell (xi, yi) is empty i.e. the cell contains '.'.

Output

For each case, print the case number in a single line. Then print Q lines, where each line should contain the maximum number of crystals you may collect if you land on cell (xi, yi).

Sample Input

Output for Sample Input

14 5 2..#...C#C.##..#..C#C

1 1

4 1

Case 1:12

Note

Dataset is huge, use faster I/O methods.

 

题目类型:记忆化搜索

算法分析:本题其实求每一个连通块中所有水晶的数量,如果对于每个查询都调用一次DFS或者是BFS会TLE,此时应该使用记忆化搜索的思想使能通过调用一次搜索算法计算出的所有节点(在同一个连通块中)的水晶数量记录下来,然后对于已经计算过的查询操作(查询的坐标在已经计算过的连通块中),则直接输出相应的记录即可

 

lightoj1325

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

1325 - Distributing Chocolates

InputNow as the result can be large, I asked for the result modulo 100 000 007 (a prime). But after that I found that this problem is easier than I thought. So, I changed the problem a little bit. I will give you the number of my cousins K and the result modulo 100 000 007. Your task is to find the number of chocolates I have bought. If there are several solutions, you have to find the minimum one.I have bought n chocolates for my young cousins. Every chocolate is different. So, in the contest I added the problem that how many ways I can distribute the chocolates to my K cousins. I can give more chocolates to some cousins, and may give no chocolate to some. For example, I have three cousins and I bought 2 chocolates a and b. Then I can distribute them in the following 9 ways:

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

Each case starts with a line containing two integers K (2 ≤ K ≤ 107and result (0 ≤ result < 100000007). You can assume that the input data is valid, that means a solution exists.

Output

For each case, print the case number and the minimum possible number of chocolates I have bought.

Sample Input

Output for Sample Input

23 92 100 Case 1: 2Case 2: 23502611

 

题目类型:离散对数

算法分析:直接使用BSGS算法求解即可

 

lightoj1319

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

1319 - Monkey Tradition

1)       There are N monkeys who play this game and there are N bamboos of equal heights. Let the height be L meters.In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows:

2)       Each monkey stands in front of a bamboo and every monkey is assigned a different bamboo.

3)       When the whistle is blown, the monkeys start climbing the bamboos and they are not allowed to jump to a different bamboo throughout the game.

4)       Since they are monkeys, they usually climb by jumping. And in each jump, the ith monkey can jump exactly pi meters (pi is a prime). After a while when a monkey finds that he cannot jump because one more jump may get him out of the bamboo, he reports the remaining length ri that he is not able to cover.

5)       And before the game, each monkey is assigned a distinct pi.

6)       The monkey, who has the lowest ri, wins.

Now, the organizers have found all the information of the game last year, but unluckily they haven't found the height of the bamboo. To be more exact, they know N, all pi and corresponding ri, but not L. So, you came forward and found the task challenging and so, you want to find L, from the given information.

Input

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 12). Each of the next n lines contains two integers pi (1 < pi < 40, pi is a prime) and ri (0 < ri < pi). All pi will be distinct.

Output

For each case, print the case number and the minimum possible value of L that satisfies the above conditions. If there is no solution, print 'Impossible'.

Sample Input

Output for Sample Input

235 47 6

11 3

4

2 1

3 2

5 3

7 1

Case 1: 69Case 2: 113

 

题目类型:CRT

算法分析:直接使用中国剩余定理求解即可,注意在迭代求解时要注意%M防止溢出ans = (ans % M + M) % M,本题不存在Impossible的情况

 

lightoj1294

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

1294 - Positive Negative Sign

-1 -2 -3 +4 +5 +6 -7 -8 -9 +10 +11 +12Given two integers: n and m and n is divisible by 2m, you have to write down the first n natural numbers in the following form. At first take first m integers and make their sign negative, then take next m integers and make their sign positive, the next m integers should have negative signs and continue this procedure until all the n integers have been assigned a sign. For example, let n be 12 and m be 3. Then we have

If n = 4 and m = 1, then we have

-1 +2 -3 +4

Now your task is to find the summation of the numbers considering their signs.

Input

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

Each case starts with a line containing two integers: n and m (2 ≤ n ≤ 109, 1 ≤ m). And you can assume that n is divisible by 2*m.

Output

For each case, print the case number and the summation.

Sample Input

Output for Sample Input

212 34 1 Case 1: 18Case 2: 2

 

题目类型:数学题

算法分析:直接模拟计算会TLE,这里应该将负数项和正数项分开,发现每一个序列都构成一个等差序列,然后直接使用公式计算出两个序列的和,最后两个序列和再相减即可

 

lightoj1266

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

1266 - Points in Rectangle

InputAs the name says, this problem is about finding the number of points in a rectangle whose sides are parallel to axis. All the points and rectangles consist of 2D Cartesian co-ordinates. A point that lies in the boundary of a rectangle is considered inside.

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

Each case starts with a line containing an integer q (1 ≤ q ≤ 30000) denoting the number of queries. Each query is either one of the following:

1)      0 x y, meaning that you have got a new point whose co-ordinate is (x, y). But the restriction is that, if a point (x, y) is already listed, then this query has no effect.

2)      1 x1 y1 x2 y2 meaning that you are given a rectangle whose lower left co-ordinate is (x1, y1) and upper-right corner is (x2, y2); your task is to find the number of points, given so far, that lie inside this rectangle. You can assume that (x1 < x2, y1 < y2).

You can assume that the values of the co-ordinates lie between 0 and 1000 (inclusive).

Output

For each case, print the case number in a line first. Then for each query type (2), you have to answer the number of points that lie inside that rectangle. Print each of the results in separated lines.

Sample Input

Output for Sample Input

190 1 10 2 6

1 1 1 6 6

1 2 2 5 5

0 5 5

1 0 0 6 5

0 3 3

0 2 6

1 2 1 10 10

Case 1:202

3

Note

Dataset is huge, use faster I/O methods.

 

题目类型:二维树状数组

算法分析:由于题目中点的坐标最大值<=1000,所以可以直接使用二维树状数组存图,再维护一个布尔数组hasval[x][y]表示(x,y)处是否有点,如果有则不进行UpDate操作,反之则UpDate,每次直接查询矩阵中有多少点即可

 

lightoj1259

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

1259 - Goldbach`s Conjecture

Every even integer, greater than 2, can be expressed as the sum of two primes [1].Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:

Now your task is to check whether this conjecture holds for integers up to 107.

Input

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

Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).

Output

For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where

1)      Both a and b are prime

2)      a + b = n

3)      a ≤ b

Sample Input

Output for Sample Input

264 Case 1: 1Case 2: 1

Note

  1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...

 

题目类型:素数筛+枚举

算法分析:使用Euler筛将素数表预先打出,然后对于每个读入的n,枚举小于等于n/2的素数,满足条件的cnt自加即可

 

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数组!!!