ZOJ - 4032 Magic Points 【几何构造】

本文介绍了一个基于魔法点的连线算法,旨在通过特定规则连接这些点以形成尽可能多的交点,即梦之点。该算法适用于竞赛编程场景,特别是在解决平面几何问题时,通过巧妙的构造确保了任意两线段的交点不重复。

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

Magic Points

Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

Given an integer , we say a point  on a 2D plane is a magic point, if and only if both  and  are integers, and exactly one of the following conditions is satisfied:

  •  and ;

  •  and ;

  •  and ;

  •  and .

It's easy to discover that there are  magic points in total. These magic points are numbered from  to  in counter-clockwise order starting from .

DreamGrid can create  magic lines from these magic points. Each magic line passes through exactly two magic points but cannot be parallel to the line  or  (that is to say, the coordinate axes).

The intersections of the magic lines are called dream points, and for some reason, DreamGrid would like to make as many dream points as possible. Can you tell him how to create these magic lines?

Input

There are multiple test cases. The first line of input contains an integer  (about 100), indicating the number of test cases. For each test case, there is only one integer  ().

Output

For each case output  integers  in one line separated by one space, indicating that in your answer, point  and point  is connected by a line for all .

If there are multiple answers, you can print any of them.

Sample Input
3
2
3
4
Sample Output
0 2 1 3
1 4 2 5 3 6
0 6 1 9 3 8 4 10
Hint

The sample test cases are shown as follow:


Author: ZHOU, Yuchen
Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

思路:

很容易想到(i,n+i)相连,这样任意两边的交点一定不会重合。构造最后一个点时可以让(n,3*n-4) 或(n,3*n-2)相连,这样与任意边的交点也不会重合。论标准画图的重要性。。。n=2时特判一下。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n==2)
        {
            printf("0 2 1 3\n");
            continue;
         }
        for(int i=0;i<n-1;i++)
        {
            printf("%d %d ",i,i+n);
        }
        printf("%d %d\n",n-1,3*n-4);
    }
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值