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\n", ++cnt, x, y);
}
return 0;
}
最新文章
- windows和linux之间“/”, ";\\";的区别
- mybatiGenerator
- ASP.NET 5 (vNext) 理解和概述
- [ruby on rails] 跟我学之(4)路由映射
- 利用VS编译libiconv库
- java 获取当前时间及年月日时分秒
- Kakfa揭秘 Day6 Consumer源码解密
- Struts 2的iterator标签来遍历一个含有双层List的嵌套
- codeforces 401D. Roman and Numbers 数位dp
- 授权给指定用户,使用navicat在其他ip都可以连接linux服务器上的mysql库
- 终端管理软件tmux
- p76泛函 有限维空间真子空间不可能在全空间稠密
- 继续JS之DOM对象二
- SpringMVC连接多数据源配置
- MySQL锁详解!(转载)
- Centos7编译4.7.2内核
- Android MediaPlayer播放音乐并实现进度条
- php备份mysql数据库
- OS X 与传统Unix的一点区别
- pylab.show()没有显示图形图像(python的matplotlib画图包)