C.Cypher Decypher
题意
找 [ i , j ] [i,j] [i,j]区间中有多少个质数
思路
数据范围为
1
≤
i
≤
j
≤
1
0
6
1\le i\le j\le 10^6
1≤i≤j≤106
直接素数筛+前缀和即可
代码
/*
* @Author: Icey_dying
* @Date: 2021-10-02 15:02:33
* @LastEditors: Icey_dying
* @LastEditTime: 2021-10-02 15:09:02
* @FilePath: \Icey_dying\competition\2021\2021.10\2021.10.2\C.cpp
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
int prime[N], b[N];
int cnt = 0, max1 = 1e6;
int init()
{
memset(b, 0, sizeof(b));
b[0] = b[1] = 1;
for (int i = 2; i <= max1; i++) {
if (!b[i])
prime[++cnt] = i;
for (int j = 1; j <= cnt && prime[j] * i <= max1; j++) {
b[prime[j] * i] = 1;
if (i % prime[j] == 0)
break;
}
}
return 0;
}
int a[1000005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
init();
memset(a, 0, sizeof(a));
for (int i = 1; i <= max1; i++)
a[i] = a[i - 1] + !(b[i]);
cin >> t;
int i, j;
while (t--) {
cin >> i >> j;
cout << a[j] - a[i - 1] << endl;
}
return 0;
}
E.Escape Room
题意
地图上有物体,墙,人和出口,现在问你坐标上是否有东西,如果有则输出相应的,如果没有输出如何最快到达出口的方向
思路
从出口BFS即可,注意题目中上左下右有先后顺序,找完之后记得看看有没有更优先的方向
代码
/*
* @Author: Icey_dying
* @Date: 2021-10-04 10:58:31
* @LastEditors: Icey_dying
* @LastEditTime: 2021-10-04 11:11:05
* @FilePath: \Icey_dying\competition\2021\2021.10\2021.10.2\E.cpp
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
char a[N][N], s[N];
int n, m, f[N][N], v[N][N];
int dx[] = { 0, 0, -1, 0, 1 }, dy[] = { 0, 1, 0, -1, 0 };
queue<int> qx, qy;
int x, y, q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
a[i][j] = '#';
for (int i = 1; i <= n; i++) {
cin >> a[i] + 1;
a[i][m + 1] = '#';
}
memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 'E') {
f[i][j] = 0;
qx.push(i);
qy.push(j);
}
}
}
while (qx.size()) {
x = qx.front();
qx.pop();
y = qy.front();
qy.pop();
for (int i = 1; i <= 4; i++) {
if (a[x + dx[i]][y + dy[i]] == 'X' || a[x + dx[i]][y + dy[i]] == '#')
continue;
if (f[x + dx[i]][y + dy[i]] > f[x][y] + 1) {
f[x + dx[i]][y + dy[i]] = f[x][y] + 1;
v[x + dx[i]][y + dy[i]] = i;
qx.push(x + dx[i]);
qy.push(y + dy[i]);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= 4; k++) {
if (f[i][j] + 1 == f[i + dx[k]][j + dy[k]] && v[i + dx[k]][j + dy[k]] > k) {
v[i + dx[k]][j + dy[k]] = k;
}
}
}
}
cin >> q;
while (q--) {
cin >> x >> y;
if (a[x][y] == '#')
cout << "W\n";
else if (a[x][y] == 'X')
cout << "X\n";
else if (a[x][y] == 'E')
cout << "E\n";
else if (f[x][y] == f[0][0])
cout << "?\n";
else {
if (v[x][y] == 1)
cout << "L\n";
else if (v[x][y] == 2)
cout << "D\n";
else if (v[x][y] == 3)
cout << "R\n";
else
cout << "U\n";
}
}
return 0;
}
F.Fixing Subtitles
题意
字幕有延迟,先给出原本的字幕时间和字幕本身和延迟时间,让你输出字幕的正确时间和字幕
思路
这道题只有输入输出麻烦,时间的转换是比较简单的,慢慢调试即可
代码
/*
* @Author: Icey_dying
* @Date: 2021-10-02 15:31:31
* @LastEditors: Icey_dying
* @LastEditTime: 2021-10-02 16:04:50
* @FilePath: \Icey_dying\competition\2021\2021.10\2021.10.2\F.cpp
*/
#include <bits/stdc++.h>
using namespace std;
int n, x, y;
void f(int& h, int& m, int& s, int& ms)
{
ms += y;
s += x + ms / 1000;
ms %= 1000;
m += s / 60;
s %= 60;
h += m / 60;
m %= 60;
}
int h, m, s, ms, now;
string st;
int main()
{
scanf("%d %d.%d", &n, &x, &y);
for (int i = 1; i <= n; i++) {
scanf("%d", &now);
printf("%d\n", now);
scanf("%d:%d:%d,%d", &h, &m, &s, &ms);
f(h, m, s, ms);
printf("%02d:%02d:%02d,%03d", h, m, s, ms);
scanf(" --> %d:%d:%d,%d", &h, &m, &s, &ms);
//cout << ms << endl;
f(h, m, s, ms);
printf(" --> %02d:%02d:%02d,%03d\n", h, m, s, ms);
// getline(cin, st);
getchar();
while (getline(cin, st)) {
if (st.size() == 0) {
cout << "\n";
break;
}
cout << st << "\n";
}
}
return 0;
}
J.Just Send the Email
题意
有一棵树(?),找出每个结点到叶子结点的最小距离(叶子结点本身的距离为1),求和,与总结点数相比
思路
用逆向思维,从叶子结点进行BFS即可
代码
/*
* @Author: Icey_dying
* @Date: 2021-10-04 13:10:13
* @LastEditors: Icey_dying
* @LastEditTime: 2021-10-04 13:28:44
* @FilePath: \Icey_dying\competition\2021\2021.10\2021.10.2\J.cpp
*/
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 1e5 + 5;
long long quickmod(long long a, long long b)
{
long long ret = 1;
while (b) {
if (b & 1)
ret = ret * a % mod;
a = a * a % mod;
b /= 2;
}
return ret;
}
vector<int> v[N];
queue<int> q;
int n;
bool vis[N];
int depth[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(vis, false, sizeof(vis));
cin >> n;
for (int i = 2, x; i <= n; i++) {
cin >> x;
vis[x] = true;
v[x].push_back(i);
v[i].push_back(x);
}
memset(depth, 0, sizeof(depth));
int cnt = n;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
cnt--;
q.push(i);
depth[i] = 1;
}
for (int i = 1; i <= n; i++)
vis[i] = !vis[i];
while (!q.empty()) {
int ans = q.front();
// cout << ans << endl;
q.pop();
for (auto i : v[ans]) {
if (!vis[i]) {
cnt--;
q.push(i);
vis[i] = true;
depth[i] = depth[ans] + 1;
}
}
if (!cnt)
break;
}
// for (int i = 1; i <= n; i++)
// cout << depth[i] << " \n"[i == n];
long long wow = 0;
for (int i = 1; i <= n; i++)
wow += depth[i];
// cout << wow << endl;
long long hh = n;
int ggcd = __gcd(wow, hh);
wow /= ggcd;
hh /= ggcd;
cout << wow % mod * quickmod(hh, mod - 2) % mod << endl;
return 0;
}
K.Kids at the Party
题意
Jaime有5个朋友,现有一些糖果,Jaime想要和朋友一起聚会的前提是他和朋友拥有相同的糖果,且聚会至少有2人,问聚会上可以有几个小朋友(包括Jaime)
思路
直接求糖果可以被几整除即可(e.g. 2,3,4,5,6)
代码
/*
* @Author: Icey_dying
* @Date: 2021-10-02 15:15:01
* @LastEditors: Icey_dying
* @LastEditTime: 2021-10-02 15:24:37
* @FilePath: \Icey_dying\competition\2021\2021.10\2021.10.2\K.cpp
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
char a[N];
int t;
int b[10];
int main()
{
cin >> t;
while (t--) {
cin >> a;
int ans = 0;
memset(b, 0, sizeof(b));
if ((a[strlen(a) - 1] - '0') % 2 == 0) {
b[2] = 1;
ans = 0;
if (strlen(a) > 1)
ans = (a[strlen(a) - 2] - '0') * 10 + a[strlen(a) - 1] - '0';
if (strlen(a) == 1 && (a[strlen(a) - 1] - '0') % 4 == 0)
b[4] = 1;
if (strlen(a) > 1 && ans % 4 == 0)
b[4] = 1;
}
if ((a[strlen(a) - 1] - '0') % 5 == 0)
b[5] = 1;
ans = 0;
for (int i = 0; i < strlen(a); i++) {
ans += a[i] - '0';
}
if (ans % 3 == 0) {
b[3] = 1;
if (b[2])
b[6] = 1;
}
ans = 0;
for (int i = 2; i <= 6; i++)
ans += b[i];
if (ans) {
int cnt = 0;
for (int i = 2; i <= 6; i++) {
if (b[i]) {
cnt++;
cout << i << " \n"[cnt == ans];
}
}
} else
cout << "-1\n";
}
return 0;
}
L.Leonel and the powers of two
题意
问
2
k
2^k
2k的表达式
规则如下:
2
(
k
=
1
)
2(k=1)
2(k=1)
2
∗
2
k
−
1
2*2^{k-1}
2∗2k−1(k为奇数)
(
2
k
2
)
2
(2^{\frac{k}{2}})^2
(22k)2(k为偶数)
思路
直接写一个函数,递归调用即可
代码
/*
* @Author: Icey_dying
* @Date: 2021-10-02 14:56:53
* @LastEditors: Icey_dying
* @LastEditTime: 2021-10-02 15:00:09
* @FilePath: \Icey_dying\competition\2021\2021.10\2021.10.2\L.cpp
*/
#include <bits/stdc++.h>
using namespace std;
string f(long long n)
{
if (n == 1)
return "2";
string s;
if (n % 2)
return "(2*" + f(n - 1) + ")";
else
return "(" + f(n / 2) + ")^2";
}
int t;
long long n;
int main()
{
cin >> t;
while (t--) {
cin >> n;
cout << f(n) << endl;
}
return 0;
}