LightOJ 1008 Fibsieve`s Fantabulous Birthday

来源:互联网 发布:纯四个数字域名价格 编辑:程序博客网 时间:2024/05/17 09:23

1008 - Fibsieve`s Fantabulous Birthday

   PDF (English)StatisticsForum
Time Limit: 0.5 second(s)Memory Limit: 32 MB

Fibsieve had a fantabulous (yes, it's an actual word) birthday party this year. He had so many gifts that he was actually thinking of not having a party next year.

Among these gifts there was an N x N glass chessboard that had a light in each of its cells. When the board was turned on a distinct cell would light up every second, and then go dark.

The cells would light up in the sequence shown in the diagram. Each cell is marked with the second in which it would light up.

(The numbers in the grids stand for the time when the corresponding cell lights up)

In the first second the light at cell (1, 1) would be on. And in the 5th second the cell (3, 1) would be on. Now, Fibsieve is trying to predict which cell will light up at a certain time (given in seconds). Assume that N is large enough.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case will contain an integer S (1 ≤ S ≤ 1015) which stands for the time.

Output

For each case you have to print the case number and two numbers (x, y), the column and the row number.

Sample Input

Output for Sample Input

3

8

20

25

Case 1: 2 3

Case 2: 5 4

Case 3: 1 5

 

大意:根据图中数字规律,输入一个n输出它的坐标。

观察图中左边一列和下边一行的数,左边一列若行号(从下向上)为奇数,则行号的平方等于对应的数,并从它向右向下递减,下边一行若列号是偶数,则列号的平方等于对应数,并从它向上向左递减,这就是规律,对于一个给定的n首先判断它小于谁平方,找出它对应的走向,若小于i的平方,且i为偶数,如果i * i- n  <  i 则x  =  i ;    y  = i*i -n + 1; 否则 y = i ; x = n - (i*i - i*2 + 2) + 1 ; 如果i为奇数,则x,y相反。

#include<cstdio>#include<cmath>#define ll long longint main(){ll t,k=0;scanf("%lld",&t);while(t--){ll n,i;ll x,y,a;scanf("%lld",&n);for(i = sqrt(n) ; i <= n ; i++)//i的初值为sqrt(n),避免执行前面无用的数 {if(i*i >= n){if(i*i - n < i)//这里我假设i为偶数 {x = i;y = i*i - n + 1;}else{y = i;x = n - (i*i - i*2 + 2) + 1;}break;}}if(i % 2 != 0)//如果i为奇数x,y交换 {a = x;x = y;y = a;}printf("Case %lld: %lld %lld\n",++k,x,y);} } 



0 0
原创粉丝点击