您的位置:首页 > 其它

sicily 1302. Magic Square

2015-11-12 16:18 302 查看

1302. Magic Square

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

A magic square is an arrangement of the numbers from 1 to n2 (n-squared) in an n*n matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any
column, or any main diagonal is the same.

This problem is focused on the odd-numbered magic squares. It means that n is odd. You’ll use such a method to construct the odd-numbered magic squares. The basic rule of doing so is that one counts
diagonally upwards to the right. Let’s do it step by step.

Let’s begin in the middle of the top row with the 1. (In this example, n=3)

 

1

 

 

 

 

 

 

 

 

We suppose that the bottom row is the row immediately above the top row. Moving diagonally upwards to the right means moving up one row and over one column to the right. So the 2 goes in the bottom
row one column to the right of the column containing the 1.

 

1

 

 

 

 

 

 

 

2

 

Similarly, moving right one column from the rightmost column puts us in the leftmost column, so the 3 must be placed in the leftmost column, and moving up one row we put the 3 in the row above the
row containing the 2.

 

1

 

 

3

 

 

 

 

 

2

 

What if there is a number already occupying the grid one would like to move into? When this happens, the rule is to abandon, just this once, the plan of moving diagonally upwards to the right and
instead just drop down one grid from the grid one is in presently. So the 4 will be placed directly below the 3.

 

1

 

 

3

 

 

 

4

 

 

2

 

Then:

 

1

 

6

 

3

 

5

 

 

4

 

 

2

 

Since 6 is in the top row, 7 would normally go in the bottom row. Since 6 is on the right edge, 7 would normally go on the left edge. The position which is in the bottom row and the left edge is the
lower left corner. That is where we want to put the 7. Unfortunately, it is already occupied by the number
b3b1
4. So there is a number in the way, and the rule is then to drop down to the square below the 6. So the 7 should be directly below the 6.

 

1

 

6

 

3

 

5

 

7

 

4

 

 

2

 

Then:

8

 

1

 

6

 

3

 

5

 

7

 

4

 

9

 

2

 

You task here is to write a program to determine which number will be put in the lower right corner in the n magic square. Of course, you should use the above rules to construct magic squares.

Input

You will get several n (n is odd natural number, n<1,000,000) from input file. Each one is in a line by itself.

And the input is terminated by a line with a single zero.

Output

For each n, you should print exactly one number in ONE line, representing the number in lower right corner of the n magic square.

Sample Input


30

Sample Output


2


题目分析

求奇数阶魔方右下角的数字

不能直接模拟,因为n是百万级的,开二维数组内存爆了,

通过1318的代码查看

n=1, ans=1

n=3, ans=2

n=5, ans=9

n=7, ans=20

n=9, ans=35

n=11, ans=54

n=13, ans=77

n=15, ans=104

可以发现其通项为

a1 = 1

an = (n-1)*n/2-1

an的类型用long long存储

#include <stdio.h>

int main()
{
long long key;
while (scanf("%lld", &key)) {
if (key == 0)
break;
if (key == 1)
printf("1\n");
else
printf("%lld\n", (key-1)*key/2-1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: