LightOJ-1008-Fibsieve`s Fantabulous Birthday (push formula)

link:

https://vjudge.net/problem/LightOJ-1008

Meaning of the questions:

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.

Ideas:

Consider each vertical interval, the maximum value of \ (n ^ 2 \) is the number of columns, first direct calculation belongs to the interval, and then checkout.

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e9+7;

int main()
{
    int t, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        LL s;
        scanf("%lld", &s);
        LL sq = sqrt(s);
        if (sq*sq < s)
            sq++;
        LL sum = sq*sq;
        LL x, y;
        if (sq%2 == 1)
        {
            if (sum-s >= sq)
            {
                x = sq;
                y = s-(sq-1)*(sq-1);
            }
            else
            {
                x = sum-s+1;
                y = sq;
            }
        }
        else
        {
            if (sum-s >= sq)
            {
                x = s-(sq-1)*(sq-1);
                y = sq;
            }
            else
            {
                x = sq;
                y = sum-s+1;
            }
        }
        printf("Case %d: %lld %lld\n", ++cnt, x, y);
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11817269.html