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

相关文章

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详