UVa 10161 Ant on a Chessboard

一道数学水题,找找规律。

首先要判断给的数在第几层,比如说在第n层。然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系。

还要注意n的奇偶。


 Problem A.Ant on a Chessboard 

Background

One day, an ant called Alice came to an M*M chessboard. She wanted to go around all the grids. So she began to walk along the chessboard according to this way: (you can assume that her speed is one grid per second)

At the first second, Alice was standing at (1,1). Firstly she went up for a grid, then a grid to the right, a grid downward. After that, she went a grid to the right, then two grids upward, and then two grids to the left…in a word, the path was like a snake.

For example, her first 25 seconds went like this:

( the numbers in the grids stands for the time when she went into the grids)


25


24


23


22


21


10


11


12


13


20


9


8


7


14


19


2


3


6


15


18


1


4


5


16


17

5

4

3

2

1

1      2     3      4      5

At the 8th second , she was at (2,3), and at 20th second, she was at (5,4).

Your task is to decide where she was at a given time.

(you can assume that M is large enough)

Input

Input file will contain several lines, and each line contains a number N(1<=N<=2*10^9), which stands for the time. The file will be ended with a line that contains a number 0.

Output

For each input situation you should print a line with two numbers (x, y), the column and the row number, there must be only a space between them.

Sample Input

8

20

25

0

Sample Output

2 3

5 4

1 5

AC代码:

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7
 8 int main(void)
 9 {
10     #ifdef LOCAL
11         freopen("10161in.txt", "r", stdin);
12     #endif
13
14     int N;
15     while(scanf("%d", &N) == 1 && N)
16     {
17         int n = (int)ceil(sqrt(N));
18         int x, y;
19         if(n & 1 == 1)
20         {
21             if(N < n * n - n + 1)
22             {
23                 x = n;
24                 y = N - (n - 1) * (n - 1);
25             }
26             else
27             {
28                 y = n;
29                 x = n * n - N + 1;
30             }
31         }
32         else
33         {
34             if(N < n * n - n + 1)
35             {
36                 y = n;
37                 x = N - (n - 1) * (n - 1);
38             }
39             else
40             {
41                 x = n;
42                 y = n * n - N + 1;
43             }
44         }
45
46         cout << x << " " << y << endl;
47     }
48     return 0;
49 }

代码君

UVa 10161 Ant on a Chessboard

时间: 2024-10-10 10:01:42

UVa 10161 Ant on a Chessboard的相关文章

Uva10161 Ant on a Chessboard

Uva10161 Ant on a Chessboard 10161 Ant on a Chessboard One day, an ant called Alice came to an M*M chessboard. She wanted to go around all the grids. So she began to walk along the chessboard according to this way: (you can assume that her speed is o

Ant on a Chessboard UVA 10161

说说:首先,最终蚂蚁的行走路线可以看成一直在沿着正方形的边沿走.而最大的正方形的边长是平方之后小于所给时间N的最大整数.这个最大的正方形的边长可以通过二分法获得.而且要注意的是根据边长的奇偶性,剩余路线的出发点可能在右下角,也可能在左上角.然后将剩余的要走的路再分成在到达顶点(即转折点)之前和之后,通过简单的数学计算最后就可以判断出蚂蚁最后的位置啦! 题目:  Ant on a Chessboard Background One day, an ant called Alice came to

[UVA 12633] Super Rooks on Chessboard FFT+计数

如果只有行和列的覆盖,那么可以直接做,但现在有左上到右下的覆盖. 考虑对行和列的覆盖情况做一个卷积,然后就有了x+y的非覆盖格子数. 然后用骑士的左上到右下的覆盖特判掉那些x+y的格子就可以了. 注意题意,Row是从上到下来的,被坑得好惨. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctim

UVA - 12633 Super Rooks on Chessboard FFT

题目链接:点这里 题意: 给你一个矩阵R*C,n个点,与给的的n个点同一列同一行,同一条主对角线上的点都被染黑 问你最后有多少个点没有被染黑 题解: 把每一列每一行没有被染黑的x,y找出来,其任意组合起来是没这种情况下的答案(同一条主对角线上的点都被染黑) 对于 x - y,我们可以拿来判断两个点是不是相同的一条主对角线上 那么对x.-y进行任意组合,FFT加速 总的答案就是,没有被染过行数*没有被染过的列数 - (找不到相同的x- y) 具体看代码吧 #include<bits/stdc++.

UVA 12633 Super Rooks on Chessboard (生成函数+FFT)

题面传送门 题目大意:给你一张网格,上面有很多骑士,每个骑士能横着竖着斜着攻击一条直线上的格子,求没被攻击的格子的数量总和 好神奇的卷积 假设骑士不能斜着攻击 那么答案就是没被攻击的 行数*列数 接下来考虑斜着攻击对答案的贡献 以左下角为坐标原点建立坐标系,发现一条对角线的点的$(x+y)$坐标是相同的 考虑卷积,设计两个生成函数$a,b$ 如果第i行没骑士,则$a_{i}=1$,反之为$0$ 如果第i列没骑士,则$b_{i}=1$,反之为$0$ 我们对两个式子进行卷积,可以求出每一条对角线上还

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大