Sexy primes are pairs of primes of the form (, ), so-named since "sex" is the Latin word for "six". (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)
Now given an integer, you are supposed to tell if it is a sexy prime.
Each input file contains one test case. Each case gives a positive integer ().
For each case, print in a line Yes
if is a sexy prime, then print in the next line the other sexy prime paired with (if the answer is not unique, output the smaller number). Or if is not a sexy prime, print No
instead, then print in the next line the smallest sexy prime which is larger than .
47
Yes
41
21
No
23
#include <cstdio> bool isprime(int x) { if (x <= 1) return false; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } int main() { int n; scanf("%d", &n); if (isprime(n) && isprime(n - 6)) printf("Yes\n%d", n - 6); else if (isprime(n) && isprime(n + 6)) printf("Yes\n%d", n + 6); else { while (true) { n++; if (isprime(n) && (isprime(n - 6) || isprime(n + 6))) break; } printf("No\n%d", n); } return 0; }
测试点 | 结果 | 得分 | 耗时 | 内存 |
---|---|---|---|---|
0 | 答案正确 | 6 | 3 ms | 352 KB |
1 | 答案正确 | 6 | 4 ms | 392 KB |
2 | 答案正确 | 2 | 4 ms | 384 KB |
3 | 答案正确 | 2 | 3 ms | 424 KB |
4 | 答案正确 | 3 | 4 ms | 384 KB |
5 | 答案正确 | 1 | 2 ms | 356 KB |
a.cpp: In function ‘int main()’: a.cpp:17:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses] if (ans = issexy(n)) { ^ a.cpp:16:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID's of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.
Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer (). Then lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X
. It is guaranteed that all the ID's are distinct.
The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer (). Then lines follow, each contains an ID number of a guest. It is guaranteed that all the ID's are distinct.
First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus -- notice that the 7th - 14th digits of the ID gives one's birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
3
150702193604190912
#include <bits/stdc++.h> using namespace std; int main() { int n, m, cnt = 0; string oldestAlumni = "xxxxxx99999999", oldestGuest = "xxxxxx99999999", id; scanf("%d", &n); map<string, bool> isalumni; for (int i = 0; i < n; i++) { cin >> id; isalumni[id] = true; } scanf("%d", &m); for (int i = 0; i < m; i++) { cin >> id; if (id.substr(6, 8) < oldestGuest.substr(6)) oldestGuest = id; if (isalumni[id]) { if (id.substr(6, 8) < oldestAlumni.substr(6)) oldestAlumni = id; cnt++; } } printf("%d\n%s", cnt, cnt > 0 ? oldestAlumni.c_str() : oldestGuest.c_str()); return 0; }
测试点 | 结果 | 得分 | 耗时 | 内存 |
---|---|---|---|---|
0 | 答案正确 | 13 | 4 ms | 420 KB |
1 | 答案正确 | 2 | 2 ms | 384 KB |
2 | 答案正确 | 1 | 3 ms | 384 KB |
3 | 答案正确 | 4 | 245 ms | 22272 KB |
4 | 答案正确 | 5 | 223 ms | 22144 KB |
a.cpp: In function ‘int main()’: a.cpp:5:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^ a.cpp:12:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &m); ^
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.
A person must be detected as a suspect if he/she makes more than short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. makes a short phone call to means that the total duration of the calls from to is no more than 5 minutes.
Each input file contains one test case. For each case, the first line gives 3 positive integers (, the threshold(阈值) of the amount of short phone calls), (, the number of different phone numbers), and (, the number of phone call records). Then lines of one day's records are given, each in the format:
caller receiver duration
where caller
and receiver
are numbered from 1 to , and duration
is no more than 1440 minutes in a day.
Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. 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.
If no one is detected, output None
instead.
5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1
3 5
6
Note: In sample 1, although 1
had 9 records, but there were 7 distinct receivers, among which 5
and 15
both had conversations lasted more than 5 minutes in total. Hence 1
had made 5 short phone calls and didn't exceed the threshold 5, and therefore is not a suspect.
5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1
None
#include <bits/stdc++.h> using namespace std; int father[1010]; int findFather(int x) { return x == father[x] ? x : father[x] = findFather(father[x]); } void uni(int a, int b) { int faA = findFather(a); int faB = findFather(b); if (faA < faB) father[faB] = faA; if (faA > faB) father[faA] = faB; } int main() { for (int i = 0; i < 1010; i++) father[i] = i; int k, n, m, a, b, time; scanf("%d%d%d", &k, &n, &m); vector<map<int, int> > record(n + 1); for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &time); record[a][b] += time; } vector<int> suspect; for (int i = 1; i <= n; i++) { if (record[i].size() <= k) continue; int callto = 0, callback = 0; for (auto it : record[i]) { if (it.second <= 5) { callto++; if (record[it.first].find(i) != record[it.first].end()) callback++; } } if (callto > k && callback * 1.0 / callto <= 0.2) suspect.push_back(i); } if (suspect.size() == 0) { printf("None\n"); return 0; } for (int i = 0; i < suspect.size() - 1; i++) { for (int j = i + 1; j < suspect.size(); j++) { a = suspect[i]; b = suspect[j]; if (record[a][b] > 0 && record[b][a] > 0) uni(a, b); } } map<int, vector<int> > mp; for (auto it : suspect) mp[findFather(it)].push_back(it); for (auto it : mp) { vector<int> gang = move(it.second); sort(gang.begin(), gang.end()); for (int i = 0; i < gang.size(); i++) { if (i != 0) printf(" "); printf("%d", gang[i]); } printf("\n"); } return 0; }
测试点 | 结果 | 得分 | 耗时 | 内存 |
---|---|---|---|---|
0 | 答案正确 | 13 | 4 ms | 424 KB |
1 | 答案正确 | 1 | 4 ms | 548 KB |
2 | 答案正确 | 6 | 4 ms | 344 KB |
3 | 答案正确 | 1 | 4 ms | 384 KB |
4 | 答案正确 | 4 | 79 ms | 4608 KB |
a.cpp: In function ‘int main()’: a.cpp:27:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if (record[i].size() <= k) continue; ~~~~~~~~~~~~~~~~~^~~~ a.cpp:43:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (int i = 0; i < suspect.size() - 1; i++) { ~~^~~~~~~~~~~~~~~~~~~~ a.cpp:44:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (int j = i + 1; j < suspect.size(); j++) { ~~^~~~~~~~~~~~~~~~ a.cpp:59:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (int i = 0; i < gang.size(); i++) { ~~^~~~~~~~~~~~~ a.cpp:19:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d%d", &k, &n, &m); ^ a.cpp:22:39: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d%d", &a, &b, &time); ^
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.
Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:
Note:
Each input file contains one test case. For each case, the first line gives a positive integer (), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than and are separated by a space.
Then another positive integer () is given, followed by lines of statements. It is guaranteed that both A
and B
in the statements are in the tree.
For each statement, print in a line Yes
if it is correct, or No
if not.
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Yes
No
Yes
No
Yes
Yes
Yes
#include <bits/stdc++.h> using namespace std; vector<int> post, in; int tree[1010], pos[1010], level[1010], len = -1; int create(int postl, int postr, int inl, int inr, int i, int depth) { if (postl > postr) return 0; len = max(len, i); level[post[postr]] = depth; tree[i] = post[postr]; int k = inl; while (k <= inr && in[k] != post[postr]) k++; int numLeft = k - inl; tree[i * 2] = create(postl, postl + numLeft - 1, inl, k - 1, i * 2, depth + 1); tree[i * 2 + 1] = create(postl + numLeft, postr - 1, k + 1, inr, i * 2 + 1, depth + 1); return post[postr]; } int main() { int n, m; scanf("%d", &n); post.resize(n); in.resize(n); for (int i = 0; i < n; i++) scanf("%d", &post[i]); for (int i = 0; i < n; i++) scanf("%d", &in[i]); create(0, n - 1, 0, n - 1, 1, 1); bool isfull = true; for (int i = 1; i <= len; i++) { if (tree[i] == 0) continue; pos[tree[i]] = i; if ((tree[i * 2] != 0 || tree[i * 2 + 1] != 0) && (tree[i * 2] * tree[i * 2 + 1] == 0)) { isfull = false; } } scanf("%d", &m); getchar(); while (m--) { string s; bool ans; getline(cin, s); if (s[0] == 'I') ans = isfull; else { int a = stoi(s.substr(0, s.find_first_of(' '))); if (s[s.length() - 1] == 't') ans = pos[a] == 1; else if (isdigit(s[s.length() - 1])) { int b = stoi(s.substr(s.find_last_of(' ') + 1)); if (s.find("pa") != string::npos) ans = pos[a] == pos[b] / 2; else if (s.find("le") != string::npos) ans = pos[a] == pos[b] * 2; else ans = pos[a] == pos[b] * 2 + 1; } else { s.erase(0, s.find_first_of(' ')); while (!isdigit(s[0])) s.erase(s.begin()); int b = stoi(s.substr(0, s.find_first_of(' '))); if (s[s.length() - 1] == 's') ans = pos[a] / 2 == pos[b] / 2; else ans = level[a] == level[b]; } } printf("%s\n", ans ? "Yes" : "No"); } return 0; }
测试点 | 结果 | 得分 | 耗时 | 内存 |
---|---|---|---|---|
0 | 答案正确 | 15 | 3 ms | 296 KB |
1 | 答案正确 | 5 | 4 ms | 384 KB |
2 | 答案正确 | 5 | 4 ms | 424 KB |
3 | 答案正确 | 2 | 5 ms | 424 KB |
4 | 答案正确 | 3 | 4 ms | 384 KB |
a.cpp: In function ‘int main()’: a.cpp:25:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^ a.cpp:26:54: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] for (int i = 0; i < n; i++) scanf("%d", &post[i]); ^ a.cpp:27:52: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] for (int i = 0; i < n; i++) scanf("%d", &in[i]); ^ a.cpp:28:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &m); ^ a.cpp:58:15: warning: ‘ans’ may be used uninitialized in this function [-Wmaybe-uninitialized] printf("%s\n", ans ? "Yes" : "No"); ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~