LightOJ-1008-Fibsieve`s Fantabulous Birthday(推公式)

链接:

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

题意:

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.

思路:

考虑每一个横竖区间,最大值为(n^2)的数列,直接先计算到属于区间,再细算。

代码:

#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
", ++cnt, x, y);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11817269.html