sicily 1302. Magic Square

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 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);
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值