Codeforces Round 949 (Div. 2 ABCD) 视频讲解

2024-06-02 00:44

本文主要是介绍Codeforces Round 949 (Div. 2 ABCD) 视频讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A. Turtle and Piggy Are Playing a Game

Problem Statement

Turtle and Piggy are playing a number game.

First, Turtle will choose an integer x x x, such that l ≤ x ≤ r l \le x \le r lxr, where l , r l, r l,r are given. It’s also guaranteed that 2 l ≤ r 2l \le r 2lr.

Then, Piggy will keep doing the following operation until x x x becomes 1 1 1:

  • Choose an integer p p p such that p ≥ 2 p \ge 2 p2 and p ∣ x p \mid x px (i.e. x x x is a multiple of p p p).
  • Set x x x to x p \frac{x}{p} px, and the score will increase by 1 1 1.

The score is initially 0 0 0. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains two integers l , r l, r l,r ( 1 ≤ l ≤ r ≤ 1 0 9 , 2 l ≤ r 1 \le l \le r \le 10^9, 2l \le r 1lr109,2lr) — The range where Turtle can choose the integer from.

Output

For each test case, output a single integer — the maximum score.

Example

Example

input
5
2 4
3 6
2 15
6 22
114514 1919810
output
2
2
3
4
20

Note

In the first test case, Turtle can choose an integer x x x, such that 2 ≤ x ≤ 4 2 \le x \le 4 2x4. He can choose x = 4 x = 4 x=4. Then Piggy can choose p = 2 p = 2 p=2 for 2 2 2 times. After that, x x x will become 1 1 1, and the score will be 2 2 2, which is maximized.

In the second test case, Turtle can choose an integer 3 ≤ x ≤ 6 3 \le x \le 6 3x6. He can choose x = 6 x = 6 x=6. Then Piggy can choose p = 2 p = 2 p=2, then choose p = 3 p = 3 p=3. After that, x x x will become 1 1 1, and the score will be 2 2 2, which is maximum.

In the third test case, Turtle can choose x = 12 x = 12 x=12.

In the fourth test case, Turtle can choose x = 16 x = 16 x=16.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve() {int l, r;cin >> l >> r;int x = 0;while ((1ll << x) <= r) x ++;x --;cout << x << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

B. Turtle and an Infinite Sequence

Problem Statement

There is a sequence a 0 , a 1 , a 2 , … a_0, a_1, a_2, \ldots a0,a1,a2, of infinite length. Initially a i = i a_i = i ai=i for every non-negative integer i i i.

After every second, each element of the sequence will simultaneously change. a i a_i ai will change to a i − 1 ∣ a i ∣ a i + 1 a_{i - 1} \mid a_i \mid a_{i + 1} ai1aiai+1 for every positive integer i i i. a 0 a_0 a0 will change to a 0 ∣ a 1 a_0 \mid a_1 a0a1. Here, ∣ | denotes bitwise OR.

Turtle is asked to find the value of a n a_n an after m m m seconds. In particular, if m = 0 m = 0 m=0, then he needs to find the initial value of a n a_n an. He is tired of calculating so many values, so please help him!

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains two integers n , m n, m n,m ( 0 ≤ n , m ≤ 1 0 9 0 \le n, m \le 10^9 0n,m109).

Output

For each test case, output a single integer — the value of a n a_n an after m m m seconds.

Example

Example

input
9
0 0
0 1
0 2
1 0
5 2
10 1
20 3
1145 14
19198 10
output
0
1
3
1
7
11
23
1279
19455

Note

After 1 1 1 second, [ a 0 , a 1 , a 2 , a 3 , a 4 , a 5 ] [a_0, a_1, a_2, a_3, a_4, a_5] [a0,a1,a2,a3,a4,a5] will become [ 1 , 3 , 3 , 7 , 7 , 7 ] [1, 3, 3, 7, 7, 7] [1,3,3,7,7,7].

After 2 2 2 seconds, [ a 0 , a 1 , a 2 , a 3 , a 4 , a 5 ] [a_0, a_1, a_2, a_3, a_4, a_5] [a0,a1,a2,a3,a4,a5] will become [ 3 , 3 , 7 , 7 , 7 , 7 ] [3, 3, 7, 7, 7, 7] [3,3,7,7,7,7].

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve() {int n, m;cin >> n >> m;int l = max(0ll, n - m), r = n + m;if (l == r) cout << l << endl;for (int i = 30; i >= 0; i --)if ((r >> i & 1) && !(l >> i & 1)) {cout << (l | ((1ll << i + 1) - 1)) << endl;return;}
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

C. Turtle and an Incomplete Sequence

Problem Statement

Turtle was playing with a sequence a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an consisting of positive integers. Unfortunately, some of the integers went missing while playing.

Now the sequence becomes incomplete. There may exist an arbitrary number of indices i i i such that a i a_i ai becomes − 1 -1 1. Let the new sequence be a ′ a' a.

Turtle is sad. But Turtle remembers that for every integer i i i from 1 1 1 to n − 1 n - 1 n1, either a i = ⌊ a i + 1 2 ⌋ a_i = \left\lfloor\frac{a_{i + 1}}{2}\right\rfloor ai=2ai+1 or a i + 1 = ⌊ a i 2 ⌋ a_{i + 1} = \left\lfloor\frac{a_i}{2}\right\rfloor ai+1=2ai holds for the original sequence a a a.

Turtle wants you to help him complete the sequence. But sometimes Turtle makes mistakes, so you need to tell him if you can’t complete the sequence.

Formally, you need to find another sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn consisting of positive integers such that:

  • For every integer i i i from 1 1 1 to n n n, if a i ′ ≠ − 1 a'_i \ne -1 ai=1, then b i = a i ′ b_i = a'_i bi=ai.
  • For every integer i i i from 1 1 1 to n − 1 n - 1 n1, either b i = ⌊ b i + 1 2 ⌋ b_i = \left\lfloor\frac{b_{i + 1}}{2}\right\rfloor bi=2bi+1 or b i + 1 = ⌊ b i 2 ⌋ b_{i + 1} = \left\lfloor\frac{b_i}{2}\right\rfloor bi+1=2bi holds.
  • For every integer i i i from 1 1 1 to n n n, 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1bi109.

If there is no sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn that satisfies all of the conditions above, you need to report − 1 -1 1.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 5 1 \le t \le 10^5 1t105). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105) — the length of the sequence.

The second line of each test case contains n n n integers a 1 ′ , a 2 ′ , … , a n ′ a'_1, a'_2, \ldots, a'_n a1,a2,,an ( a i ′ = − 1 a'_i = -1 ai=1 or 1 ≤ a i ′ ≤ 1 0 8 1 \le a'_i \le 10^8 1ai108) — the elements of the sequence a ′ a' a.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, if there is no sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn that satisfies all of the conditions, output a single integer − 1 -1 1.

Otherwise, output n n n integers b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn — the elements of the sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn you find. The sequence should satisfy that 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1bi109 for every integer i i i from 1 1 1 to n n n. If there are multiple answers, print any of them.

Example

input
9
8
-1 -1 -1 2 -1 -1 1 -1
4
-1 -1 -1 -1
6
3 -1 -1 -1 9 -1
4
-1 5 -1 6
4
2 -1 -1 3
4
1 2 3 4
2
4 2
5
-1 3 -1 3 6
13
-1 -1 3 -1 -1 -1 -1 7 -1 -1 3 -1 -1
output
4 9 4 2 4 2 1 2
7 3 6 13
3 1 2 4 9 18
-1
-1
-1
4 2
6 3 1 3 6
3 1 3 1 3 7 3 7 3 1 3 1 3

Note

In the first test case, [ 4 , 2 , 1 , 2 , 1 , 2 , 1 , 3 ] [4, 2, 1, 2, 1, 2, 1, 3] [4,2,1,2,1,2,1,3] can also be the answer, while [ 4 , 2 , 5 , 10 , 5 , 2 , 1 , 3 ] [4, 2, 5, 10, 5, 2, 1, 3] [4,2,5,10,5,2,1,3] and [ 4 , 2 , 1 , 2 , 1 , 2 , 1 , 4 ] [4, 2, 1, 2, 1, 2, 1, 4] [4,2,1,2,1,2,1,4] cannot.

In the second test case, [ 1 , 2 , 5 , 2 ] [1, 2, 5, 2] [1,2,5,2] can also be the answer.

From the fourth to the sixth test cases, it can be shown that there is no answer, so you should output − 1 -1 1.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve() {int n;cin >> n;std::vector<int> a(n + 1), b(n + 1, -1);bool cs = 1;for (int i = 1; i <= n; i ++) cin >> a[i], cs &= (a[i] == -1);if (cs) {for (int i = 1, j = 1; i <= n; i ++, j ^= 1)if (j) cout << 1 << " ";else cout << 2 << " ";cout << endl;return;}int lst = -1, pos, cnt = 0;for (int i = 1; i <= n; i ++)if (a[i] != -1) {if (lst != -1) {std::vector<int> avl;int tmp = lst;while (tmp) avl.emplace_back(tmp), tmp /= 2;bool flg = 0;for (int j = 0; j < avl.size(); j ++) {for (int x = 0; avl[j] * (1ll << x) <= a[i]; x ++) {tmp = a[i] - avl[j] * (1ll << x);if (tmp >= (1ll << x) || x + j > i - pos || (i - pos - x - j) % 2 != 0) continue;for (int k = pos, v = lst; k <= pos + j; k ++, v /= 2)b[k] = v;for (int k = pos + j + 1; k <= i - x; k += 2)b[k] = avl[j] * 2, b[k + 1] = avl[j];for (int k = i - x + 1, v = x - 1; k <= i; k ++, v --)if (tmp >> v & 1) b[k] = b[k - 1] * 2 + 1;else b[k] = b[k - 1] * 2;flg = 1;break;}if (flg) break;}}lst = a[i], pos = i, cnt ++;}if (cnt == 1) {for (int i = 1; i <= n; i ++)if (a[i] != -1)b[i] = a[i];}for (int i = 1; i <= n; i ++)if (a[i] != -1) {for (int j = i - 1, k = 1; j >= 1; j --, k ^= 1)if (k) b[j] = a[i] * 2;else b[j] = a[i];break;}for (int i = n; i >= 1; i --)if (a[i] != -1) {for (int j = i + 1, k = 1; j <= n; j ++, k ^= 1)if (k) b[j] = a[i] * 2;else b[j] = a[i];break;}for (int i = 1; i < n; i ++)if (b[i] <= 0 || b[i] / 2 != b[i + 1] && b[i + 1] / 2 != b[i]) {cout << -1 << endl;return;}for (int i = 1; i <= n; i ++)cout << b[i] << " ";cout << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

D. Turtle and Multiplication

Problem Statement

Turtle just learned how to multiply two integers in his math class, and he was very excited.

Then Piggy gave him an integer n n n, and asked him to construct a sequence a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an consisting of integers which satisfied the following conditions:

  • For all 1 ≤ i ≤ n 1 \le i \le n 1in, 1 ≤ a i ≤ 3 ⋅ 1 0 5 1 \le a_i \le 3 \cdot 10^5 1ai3105.
  • For all 1 ≤ i < j ≤ n − 1 1 \le i < j \le n - 1 1i<jn1, a i ⋅ a i + 1 ≠ a j ⋅ a j + 1 a_i \cdot a_{i + 1} \ne a_j \cdot a_{j + 1} aiai+1=ajaj+1.

Of all such sequences, Piggy asked Turtle to find the one with the minimum number of distinct elements.

Turtle definitely could not solve the problem, so please help him!

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2n106) — the length of the sequence a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case, output n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an — the elements of the sequence a a a.

If there are multiple answers, print any of them.

Example

input
3
2
3
4
output
114514 114514
1 2 2
3 3 4 4

Note

In the third test case, a = [ 3 , 4 , 2 , 6 ] a = [3, 4, 2, 6] a=[3,4,2,6] violates the second condition since a 1 ⋅ a 2 = a 3 ⋅ a 4 a_1 \cdot a_2 = a_3 \cdot a_4 a1a2=a3a4. a = [ 2 , 3 , 4 , 4 ] a = [2, 3, 4, 4] a=[2,3,4,4] satisfy the conditions but its number of distinct elements isn’t minimum.

Solution

具体见文后视频。

Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 4e6 + 10;int n;
int h[N], e[N], ne[N], idx, vis[N];
std::vector<int> path, prm;
vector<int> Prime(int n) {std::vector<int> st(n + 1, 0), prm;for (int i = 2; i <= n; i ++) {if (!st[i]) prm.emplace_back(i);for (int j = 0; prm[j] * i <= n; j ++) {st[prm[j] * i] = true;if (i % prm[j] == 0) break;}}return prm;
}
void dfs(int u) {for (int &i = h[u]; ~i;) if (!vis[i]) {int j = e[i];vis[i] = vis[i ^ 1] = 1, i = ne[i], dfs(j);} elsei = ne[i];path.emplace_back(u);
}
void add(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}void solve() {cin >> n;int l = 1, r = n;while (l < r) {int mid = l + r >> 1;if ((mid & 1) && mid * (mid + 1) / 2 >= n - 1) r = mid;else if ((mid % 2 == 0) && mid * mid / 2 + 1 >= n - 1) r = mid;else l = mid + 1;}path.clear();for (int i = 0; i < r; i ++)for (int j = i; j < r; j ++)if ((r & 1) || j != i + 1 || i % 2 == 0)add(i, j), add(j, i);dfs(0), reverse(path.begin(), path.end());for (int i = 0; i < n; i ++)cout << prm[path[i]] << " ";cout << endl;for (int i = 0; i < r; i ++)h[i] = -1;for (int i = 0; i < idx; i ++) vis[i] = false;idx = 0;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);prm = Prime(100000);memset(h, -1, sizeof h);int dt;cin >> dt;while (dt -- )solve();return 0;
}


视频讲解

Codeforces Round 949 (Div. 2)(A ~ D 题讲解)


最后祝大家早日在这里插入图片描述

这篇关于Codeforces Round 949 (Div. 2 ABCD) 视频讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1022562

相关文章

流媒体平台/视频监控/安防视频汇聚EasyCVR播放暂停后视频画面黑屏是什么原因?

视频智能分析/视频监控/安防监控综合管理系统EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。该平台以其强大的视频处理、汇聚与融合能力,在构建全栈视频监控系统中展现出了独特的优势。视频监控管理系统EasyCVR平台内置了强大的视频解码、转码、压缩等技术,能够处理多种视频流格式,并以多种格式(RTMP、RTSP、HTTP-FLV、WebS

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

《x86汇编语言:从实模式到保护模式》视频来了

《x86汇编语言:从实模式到保护模式》视频来了 很多朋友留言,说我的专栏《x86汇编语言:从实模式到保护模式》写得很详细,还有的朋友希望我能写得更细,最好是覆盖全书的所有章节。 毕竟我不是作者,只有作者的解读才是最权威的。 当初我学习这本书的时候,只能靠自己摸索,网上搜不到什么好资源。 如果你正在学这本书或者汇编语言,那你有福气了。 本书作者李忠老师,以此书为蓝本,录制了全套视频。 试

SAM2POINT:以zero-shot且快速的方式将任何 3D 视频分割为视频

摘要 我们介绍 SAM2POINT,这是一种采用 Segment Anything Model 2 (SAM 2) 进行零样本和快速 3D 分割的初步探索。 SAM2POINT 将任何 3D 数据解释为一系列多向视频,并利用 SAM 2 进行 3D 空间分割,无需进一步训练或 2D-3D 投影。 我们的框架支持各种提示类型,包括 3D 点、框和掩模,并且可以泛化到不同的场景,例如 3D 对象、室