题目相关
题目链接
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
1≤ai,bi≤N), 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 1≤N≤1000
题解报告
题目翻译
给一个 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
2∗x 和
2
∗
x
+
1
2*x+1
2∗x+1。我们只需要将
(
2
∗
x
)
m
o
d
N
+
1
(2*x) \mod N + 1
(2∗x)modN+1 和
(
2
∗
x
+
1
)
m
o
d
N
+
1
(2*x+1) \mod N + 1
(2∗x+1)modN+1 之间建立链接。
由于本题的数据范围
N
≤
1000
N \leq 1000
N≤1000,自然我们可以保证任意两个网页在 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)。