1008 - Fibsieve`s Fantabulous Birthday
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.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.
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.
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 |
382025 | Case 1: 2 3Case 2: 5 4Case 3: 1 5 |
题目类型:简单递推
算法分析:首先得到对角线上数的递推公式,然后求得最接近输入数S的对角线上的数ans,然后判断S是否在[ans – (temp - 1), ans + (temp + 1)]中,其中temp表示对角线元素的坐标(temp,temp),然后再分类讨论即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include <iostream> #include <fstream> #include <sstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int INF = 0x7FFFFFFF; const int maxn = 366 + 66; long long F (long long val) { return val * val - val + 1; } int main() { // freopen ("aaa.txt", "r", stdin); int t, flag = 1; cin >> t; while (t--) { printf ("Case %d: ", flag++); long long val, ans; scanf ("%lld", &val); long long temp = (long long) sqrt ((double) val); while (1) { ans = F (temp); if (val >= ans - (temp - 1) && val <= ans + (temp - 1)) break; if (val < ans - (temp - 1)) temp--; else temp++; } if (temp % 2) { if (val >= ans - (temp - 1) && val <= ans) printf ("%lld %lld\n", temp, temp - (ans - val)); else printf ("%lld %lld\n", temp - (val - ans), temp); } else { if (val >= ans - (temp - 1) && val <= ans) printf ("%lld %lld\n", temp - (ans - val), temp); else printf ("%lld %lld\n", temp, temp - (val - ans)); } } return 0; } |