洛谷 P1582 倒水 题解

（下面说的所有东西可能都不太好想，请认真思考。）

1、找到数字中倒数第一个1.
2、把接下来从后向前连续的所有1变成0.
3、再把接下来不是1的第一个0变成1.

 123456789101112131415161718192021222324252627282930313233343536373839404142 #include #include #include #include #include #include #include using namespace std; inline int & setBit(int & v, int sv, int p) {     if (sv) {         v |= 1 << p;     }     else {         v &= ~(1 << p);     }     return v; } inline int getBit(int v, int p) {     return (v >> p) & 1; } bool checkBits(int value, int num) {     int cnt = 0;     for (int i = 0; i < 32; i++) {         cnt += getBit(value, i);     }     return cnt <= num; } int main() {     int n, k;     cin >> n >> k;     int no = n;     while (!checkBits(n, k)) {         int ptr = 0;         while (!getBit(n, ptr))ptr++;         while (getBit(n, ptr)) {             setBit(n, 0, ptr);             ptr++;         }         setBit(n, 1, ptr);     }     cout << n-no; }

循环移位(Cycle)

Description

Cycle shifting refers to following operation on the sting. Moving first letter to the end and keeping rest part of the string. For example, apply cycle shifting on ABCD will generate BCDA. Given any two strings, to judge if arbitrary times of cycle shifting on one string can generate the other one.

Input

There m lines in the input, while each one consists of two strings separated by space. Each string only contains uppercase letter ‘A’~’Z’.

Output

For each line in input, output YES in case one string can be transformed into the other by cycle shifting, otherwise output NO.

Example

Input

 1234 AACD CDAA ABCDEFG EFGABCD ABCD ACBD ABCDEFEG ABCDEE

Output

 1234 YES YES NO NO

Restrictions

0 <= m <= 5000

1 <= |S1|, |S2| <= 10^5

Time: 2 sec

Memory: 256 MB

限制

0 ≤ m ≤ 5000

1 ≤ |S1|, |S2| ≤ 10^5

1000ms

65536kB

5
wwwww
wwwww
wwwww
wwwww
wwwww

15

1681

Openjudge Calling Extraterrestrial Intelligence Again 题解

Calling Extraterrestrial Intelligence Again

1000ms

65536kB

A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November 16, 1974. The message consisted of 1679 bits and was meant to be translated to a rectangular picture with 23 x 73 pixels. Since both 23 and 73 are prime numbers, 23 x 73 is the unique possible size of the translated rectangular picture each edge of which is longer than 1 pixel. Of course, there was no guarantee that the receivers would try to translate the message to a rectangular picture. Even if they would, they might put the pixels into the rectangle incorrectly. The senders of the Arecibo message were optimistic.

We are planning a similar project. Your task in the project is to find the most suitable width and height of the translated rectangular picture. The term “most suitable” is defined as follows. An integer m greater than 4 is given. A positive fraction a/b less than or equal to 1 is also given. The area of the picture should not be greater than m. Both of the width and the height of the translated picture should be prime numbers. The ratio of the width to the height should not be less than a/b nor greater than 1. You should maximize the area of the picture under these constraints.

In other words, you will receive an integer m and a fraction a/b. It holds that m > 4 and 0 < a/b <= 1. You should find the pair of prime numbers p, q such that pq <= m and a/b <= p/q <= 1, and furthermore, the product pq takes the maximum value among such pairs of two prime numbers. You should report p and q as the “most suitable” width and height of the translated picture.

The input is a sequence of at most 2000 triplets of positive integers, delimited by a space character in between. Each line contains a single triplet. The sequence is followed by a triplet of zeros, 0 0 0, which indicates the end of the input and should not be treated as data to be processed.

The integers of each input triplet are the integer m, the numerator a, and the denominator b described above, in this order. You may assume 4 < m <= 100000 and 1 <= a <= b <= 1000.

The output is a sequence of pairs of positive integers. The i-th output pair corresponds to the i-th input triplet. The integers of each output pair are the width p and the height q described above, in this order.

Each output line contains a single pair. A space character is put between the integers as a delimiter. No other characters should appear in the output.

5 1 2
99999 999 999
1680 5 16
1970 1 1
2002 4 11
0 0 0

2 2
313 313
23 73
43 43
37 53

Japan 2002 Kanazawa

Openjudge Sudoku 题解

2982:Sudoku

2000ms

65536kB

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3×3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3×3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Southeastern Europe 2005

重名剔除(Deduplicate)

Description

Mr. Epicure is compiling an encyclopedia of food. He had collected a long list of candidates nominated by several belly-gods. As candidates in list are nominated by several people, duplication of name is inevitable. Mr. Epicure pay you a visit for help. He request you to remove all duplication, which is thought an easy task for you. So please hold this opportunity to be famous to all belly-gods.

Input

1 integer in fist line to denote the length of nomination list. In following n lines, each nomination is given in each line.

Output

All the duplicated nomination (only output once if duplication appears more multiple times), which is sorted in the order that duplication appears firstly.

Example

Input

 1234567891011 10 brioche camembert cappelletti savarin cheddar cappelletti tortellni croissant brioche mapotoufu

Output

 12 cappelletti brioche

Restrictions

1 < n < 6 * 10^5

All nominations are only in lowercase. No other character is included. Length of each item is not greater than 40.

Time: 2 sec

Memory: 256 MB

Hash

描述

Epicure先生正在编撰一本美食百科全书。为此，他已从众多的同好者那里搜集到了一份冗长的美食提名清单。既然源自多人之手，其中自然不乏重复的提名，故必须予以筛除。Epicure先生因此登门求助，并认定此事对你而言不过是“一碟小菜”，相信你不会错过在美食界扬名立万的这一良机

1 < n < 6 * 10^5

任务调度(Schedule)

Description

Given initial priority setting of every task, your job is to predict the running order of a batch of tasks.

Input

First line contains two integers, says n and m. n stands for the number of tasks in initial state. m stands for the length of predicted sequence. Every line is ended by a line break symbol. In each one of the following n lines, an integer and a string are included. This string is shorter than 8, which only contains lowercase letters and numbers. The integer is priority number and the string is the task name. The integer and string is separated by space.

Output

At most m lines, each one contains a string. Output the name of tasks according to the order that tasks are executed. If the number of executed tasks is less than m, then output all the executed tasks.

Example

Input

 1234 3 3 1 hello 2 world 10 test

Output

 123 hello hello world

Restrictions

0 <= n <= 4,000,000

0 <= m <= 2,000,000

0 < Priority number < 2^32

Time: 2 sec

Memory: 512 MB

Priority queue

限制

0 ≤ n ≤ 4,000,000

0 ≤ m ≤ 2,000,000

0 < 每个任务的初始优先级 < 2^32

Openjudge 百练 最大子矩阵 题解

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

9 2
-4 1
-1 8

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

15

描述

1,1,5; 1,5,1; 5,1,1;

 1 7 3

 1 4

NOIP2001第二题

洛谷 [SCOI2005] 繁忙的都市 题解

题目描述

1．改造的那些道路能够把所有的交叉路口直接或间接的连通起来。

2．在满足要求1的情况下，改造的道路尽量少。

3．在满足要求1、2的情况下，改造的那些道路中分值最大的道路分值尽量小。

4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8

3 6