AtCoder题解 —— AtCoder Grand Contest 050 —— A - AtCoder Jumper —— 二叉树

本文是AtCoder Grand Contest 050 A题的题解。题目要求为有N页的网站设计网页链接方案,使任意两页跳转次数不超10次。题解分析考点为二叉树,根据完全二叉树性质建立链接,还给出样例数据分析,时间复杂度O(N),空间复杂度O(1)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目相关

题目链接

AtCoder Grand Contest 050 A 题,https://atcoder.jp/contests/agc050/tasks/agc050_a

Problem Statement

Have you noticed this part of AtCoder website?
在这里插入图片描述Here the numbers are carefully chosen so that we can jump from any page to any page in small number of steps, while each page doesn’t contain too many links. In this task you are asked to do a similar thing with only two links on each page!
Snuke made a website with N pages numbered 1 through N. For each i (1≤i≤N), choose two integers a i a_i ai and b i b_i bi ( 1 ≤ a i , b i ≤ N 1≤a_i,b_i≤N 1ai,biN), and add two links on Page i: a link to Page a i a_i ai and a link to Page b i b_i bi. The website must satisfy the following constraint:

  • You must be able to jump from any page to any other page by clicking at most 10 links.

Under the constraints of the problem, we can prove that this is always possible.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer in the following format:

a1 b1
.
.
an bn

In case there are multiple possible answers, print any.

Sample 1

Sample Input 1

1

Sample Output 1

1 1

Explaination

Snuke made an excellent website with only one page. It even contains two links to itself!

Sample 2

Sample Input 2

3

Sample Output 1

2 3
1 3
1 2

Explaination

Here we can jump from any page to any other page by a direct link.

Constraints

  • 1 ≤ N ≤ 1000 1 \leq N \leq 1000 1N1000

题解报告

题目翻译

给一个 N,表示某网站有 N 页。设计一个网页链接方案,要求任意两页之间调整次数不能超过 10 次。该网页的设计方法是这样:第 i 页放置两个链接 a i , b i a_i, b_i ai,bi,第一个链接可以到达 a i a_i ai 页,第二个链接可以到达 b i b_i bi 页。

题目分析

一个简单的数据结构,考点二叉树。
根据题目,我们知道每页有放置两个链接,要求之间不能超过 10 10 10 步,因此这样的设计,从任意 x x x 页出发,我们在 10 10 10 步以内,可以到达的最大数据为 2 10 = 1204 2^{10}=1204 210=1204。这样,从 x x x 出发,在 10 步以内,我们可以到达 1024 x , 1024 x + 1 , ⋯   , 1024 x + 1023 1024x, 1024x+1, \cdots, 1024x+1023 1024x,1024x+1,,1024x+1023。这不就是一个二叉树。
因此,假设当前网页页码为 x x x,根据完全二叉树的性质,我们知道, x x x 的两个儿子分别为 2 ∗ x 2*x 2x 2 ∗ x + 1 2*x+1 2x+1。我们只需要将 ( 2 ∗ x ) m o d    N + 1 (2*x) \mod N + 1 (2x)modN+1 ( 2 ∗ x + 1 ) m o d    N + 1 (2*x+1) \mod N + 1 (2x+1)modN+1 之间建立链接。
由于本题的数据范围 N ≤ 1000 N \leq 1000 N1000,自然我们可以保证任意两个网页在 10 步之内到达。
注意:
1、取模运算,是为了防止数据超出 N N N
2、取模运算后加上一,因为我们取模的最小值是 0 0 0,所以需要加上 1 1 1

样例数据分析

样例数据 1

根据输入, N = 1 N=1 N=1
在第 1 1 1 页建立两个链接,第一个链接指向第 1 1 1 页,第二个链接指向第 1 1 1 页。
问题就解决了。

样例数据 2

根据输入,N=3。
在第 1 1 1 页建立两个链接,第一个链接指向第 3 3 3 页,第二个链接指向第 1 1 1 页。
在第 2 2 2 页建立两个链接,第一个链接指向第 2 2 2 页,第二个链接指向第 3 3 3 页。
在第 3 3 3 页建立两个链接,第一个链接指向第 1 1 1 页,第二个链接指向第 2 2 2 页。
问题解决。

数据规模分析

N 最大值为 1000,非常小。用 int 没问题。

AC 参考代码

//https://atcoder.jp/contests/agc050/tasks/agc050_a
//A - AtCoder Jumper
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n;
    cin>>n;
    //
    for (int i=1; i<=n; i++) {
        cout<<(2*i)%n+1<<" "<<(2*i+1)%n+1<<"\n";
    }

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

在这里插入图片描述

时间复杂度

O(N)。

空间复杂度

O(1)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值