The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool “Prime Day”. That is, not only that the corresponding number of the date 20190523 is a prime, but all its sub-strings ended at the last digit 3 are prime numbers.
Now your job is to tell if a given date is a Prime Day.
Input Specification:
Each input file contains one test case. For each case, a date between January 1st, 0001 and December 31st, 9999 is given, in the format yyyymmdd.
Output Specification:
For each given date, output in the decreasing order of the length of the substrings, each occupies a line. In each line, print the string first, followed by a space, then Yes if it is a prime number, or No if not. If this date is a Prime Day, print in the last line All Prime!.
Sample Input 1:
20190523
Sample Output 1:
20190523 Yes
0190523 Yes
190523 Yes
90523 Yes
0523 Yes
523 Yes
23 Yes
3 Yes
All Prime!
Sample Input 2:
20191231
Sample Output 2:
20191231 Yes
0191231 Yes
191231 Yes
91231 No
1231 Yes
231 No
31 Yes
1 No
作者:陈越
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
分析:题目给出一个日期(8位数字),判断该日期是不是“质数日”,从图片和文字描述中可以看出该判断哪几位。
非常简单的签到题,刷过算法笔记的话,读题+写出正确代码应该不会超过十分钟。
#include
#include
#include
#include
#include
#include
using namespace std;
bool isprime(int n) { //判断是否质数
if (n < 2) return false; //小于2直接特判 false
for (int i = 2; i*i <= n; ++i) if (n%i == 0) return false;
return true;
}
int main() {
string s;
cin >> s;
bool flag = true;
for (int i = 0; i < 8; ++i) {
string cur = s.substr(i, 8 - i);
cout << cur << " "; //如果是转化成整数int类型再输出,注意补上前导0,如果用字符串输出则没有这个问题
if (isprime(stoi(cur))) printf("Yes\n");
else {
printf("No\n");
flag = false;
}
}
if (flag) printf("All Prime!\n");
return 0;
}
A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are previously given, and must not be duplicated to any of the existed numbers. The game will run for several rounds. The one who gives a duplicate number or even a wrong number will be kicked out.
Your job is to write a judger program to judge the players’ numbers and to determine the final winners.
Input Specification:
Each input file contains one test case. For each case, the first line gives two distinct positive integers to begin with. Both numbers are in [1,1e5].(10的五次方)
In the second line, two numbers are given: N (2≤N≤10), the number of players, and M (2≤M≤1e3), the number of rounds.
Then N lines follow, each contains M positive integers. The i-th line corresponds to the i-th player (i=1,⋯,N). The game is to start from the 1st player giving his/her 1st number, followed by everybody else giving their 1st numbers in the 1st round; then everyone give their 2nd numbers in the 2nd round, and so on so forth.
Output Specification:
If the i-th player is kicked out in the k-th round, print in a line Round #k: i is out… The rest of the numbers given by the one who is out of the game will be ignored. If more than one player is out in the same round, print them in increasing order of their indices. When the game is over, print in the last line Winner(s): W1 W2 … Wn, where W1 … Wn are the indices of the winners in increasing order. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line. If there is no winner, print No winner. instead.
Sample Input 1:
101 42
4 5
59 34 67 9 7
17 9 8 50 7
25 92 43 26 37
76 51 1 41 40
Sample Output 1:
Round #4: 1 is out.
Round #5: 3 is out.
Winner(s): 2 4
Sample Input 2:
42 101
4 5
59 34 67 9 7
17 9 18 50 49
25 92 58 1 39
102 32 2 6 41
Sample Output 2:
Round #1: 4 is out.
Round #3: 2 is out.
Round #4: 1 is out.
Round #5: 3 is out.
No winner.
作者:陈越
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
这道题我第一次读的时候也没懂,我知道difference有差值的意思,但是误以为是紧挨着的前两个数的差值再取绝对值,所以一直没明白究竟是怎么淘汰。大概10分钟没想明白就跳过去做后面题了,全部做完之后才回来继续死磕这道题。猛然发现并不是紧挨着的前两个数的差值,而是前面所有数,任意两个的差值即可
分析:这道题是典型的模拟题,小模拟,题目怎么说,就按部就班一步一步地处理。关键是difference的意思(考完了群里很多人说不知道有差值意思),还有就是要意识到是前面任意两个数做差。这两点理解到,就算是读懂题了。第三点就是如何判断一个人给出的数是否有效(1,是否可以前面数值做差得到,2,是否第一次出现),每次来一个数,都把之前的数两两做差算一算来比较肯定超时,这里只需要把有有效的差值存储起来,每来一个数就去检查一次即可,若有效,则再加入可能出现的新的差值。这里有同学就一下子想到set,map,unordered_set,unordered_map。set,map内部数据结构是红黑树,unordered内部用的是hash。这里要注意到,开始给的两个数范围是小于10的5次方,这里仔细想想不难发现,以后所有有效的数,也都只能小于10的5次方(这里很简单的想一想即可,要详细证明可用数学归纳法),那么直接可以开一个数组 bool visit[100010],用visit[i]的true 和 false来表示i这个数是否有效,比unordered_set更快,不会超时。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1e5 + 5;
bool sub[maxn] = { 0 }, num[maxn] = { 0 }; //sub[i] 表示是否i这个数可否通过前面的数做差得到 num[i]表示i这个数是否出现过
int number[15][1005]; //存储每一轮每个人报的数
int main() {
vector<int> A(2); //用A这个向量来表示所有有效的报数
scanf("%d%d", &A[0], &A[1]); //以下3行为初始化num 和 sub数组
num[A[0]] = num[A[1]] = true;
sub[abs(A[0] - A[1])] = true; //注意要加绝对值
int round, cnt;
scanf("%d%d", &cnt, &round);
vector<bool> winner(cnt + 1, true); //顾名思义 winner[i]=true 即i号玩家是winner,初始化时无人被淘汰,都是winner
for (int i = 1; i <= cnt; ++i) {
for (int j = 1; j <= round; ++j) {
scanf("%d", &number[i][j]);
}
}
for (int j = 1; j <= round; ++j) {
vector<int> loser; //每一轮都可能有人被淘汰,则记入loser
for (int i = 1; i <= cnt; ++i) {
if (winner[i] == false) continue; //如果i号玩家已经被淘汰,则跳过
if (sub[number[i][j]] == true&&num[number[i][j]] == false) { //可通过前面的数做差得到,且是第一次出现,则有效
num[number[i][j]] = true;
for (const auto& val : A) sub[abs(val - number[i][j])] = true; //更新有效的差值
A.push_back(number[i][j]);
}
else {
winner[i] = false;
loser.push_back(i);
}
}
for (int i = 0; i < loser.size(); ++i) printf("Round #%d: %d is out.\n", j, loser[i]);
}
bool flag = true;
for (int i = 1; i <= cnt && flag; ++i) if (winner[i] == true) flag = false;
if (flag == true) {
printf("No winner.");
}
else {
printf("Winner(s):");
for (int i = 1; i <= cnt; ++i) if (winner[i] == true) printf(" %d", i);
}
return 0;
}
A safari park(野生动物园)has K species of animals, and is divided into N regions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this is an NP complete problem, you are not expected to solve it. Instead, they have designed several distribution plans. Your job is to write a program to help them tell if a plan is feasible.
Input Specification: Then R lines follow, each gives the indices of a pair of neighboring regions, separated by a space. Finally there is a positive M (≤20) followed by M lines of distribution plans. Each plan gives N indices of species in a line (the i-th index is the animal in the i-th rigion), separated by spaces. It is guaranteed that any pair of neighboring regions must be different, and there is no duplicated neighboring relations. Output Specification: Sample Input: Sample Output: 作者:陈越 做这道题之前,可以先做做题库里1154 Vertex Coloring (25分) 两道题没有本质区别 When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory. Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run. For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }. Your job is to implement this replacement selection algorithm. Input Specification: Output Specification: Sample Input: Sample Output: 作者:陈越 去年十二月第四题考了一道笛卡尔树,这次是一个外部排序算法,两道题考察内容不一样,但考察的能力是一样的。这种题不是需要去掌握偏难怪的数据结构,而是题目给出详细定义和例子,让我们去模拟,推导这个过程,这种能力就只有靠多做题去锻炼了。我想这也就是姥姥说的没有模板题了吧。我的建议是在草稿纸上把给出的样例每一步结合定义都推演明白,然后找到合适的方法,数据结构来写题。
Each input file contains one test case. For each case, the first line gives 3 integers: N (0
For each plan, print in a line Yes if no animals in the two neighboring regions are the same, or No otherwise. However, if the number of species given in a plan is not K, you must print Error: Too many species. or Error: Too few species. according to the case.6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
5
1 2 3 3 1 2
1 2 3 4 5 6
4 5 6 6 4 5
2 3 4 2 3 4
2 2 2 2 2 2
Yes
Error: Too many species.
Yes
No
Error: Too few species.
单位:浙江大学
代码长度限制:16 KB
时间限制:300 ms
内存限制:64 MB
其实这道题说了很多动物园,物种,什么的东西,关键是要相应的东西抽象成图的边和节点。
K种动物,放到N个regions,相邻的regions不能是同种动物
对应成图的语言,输入N,R,K,即N个节点,R条边,每条边连接的两个节点的值(动物类型)不同。#include
7-4 Replacement Selection (30分)
Each input file contains several test cases. The first line gives two positive integers N (≤1e5) and M (
For each test case, print in each line a run (in ascending order) generated by the replacement selection algorithm. All the numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.13 3
81 94 11 96 12 99 17 35 28 58 41 75 15
11 81 94 96 99
12 17 28 35 41 58 75
15
单位:浙江大学
代码长度限制:16 KB
时间限制:300 ms
内存限制:64 MB
这道题背景是外部排序,给出一个无序的初始序列,通过一个算法,得到多个有序的序列。根据题目给出的定义,可以把算法分为三个阶段。1,输入m个数到内存,此时只有输入没有输出;2,输出一个数,输入一个数;3,没有输入了,把内存中剩下的数全输出。.
阶段1可以暂时不看。先注意到阶段二,题目说if the next record is not smaller than the record we have just output, then it can be included in the run.意思就是如果进来的数大于等于刚出去的数,那么这两个数属于同一条run。反之,如果进来的数小于刚出去的数,那么一是另一条run。那么再思考,内存中同一时间最多几条run呢?最多两条,当前正在输出的一条,其他所有无法输出到当前run的数值组成另一条。此时再思考,用什么样的数据结构来表示这个run呢?注意到每次我们都选的是最小输出,若用sort每次进行排序则时间复杂度很高,很可能超时。很自然想到用堆/优先队列这个方法,入队和出队都是log级别复杂度。(有同学用set,其实也是为了方便每次取最小,只是内部数据结构不一样)。#include