题目链接:http://codeforces.com/gym/100283/problem/F

F. Bakkar In The Army
time limit per test

2 seconds

memory limit per test

256 megabytes

input

army.in

output

standard output

Bakkar is now a senior college student studying computer science. And as many students; Bakkar fell in love with one of his finest
colleagues Maymona. And as Bakkar has no brothers he is counting on getting an exemption from the military service after graduation.
He got engaged to Maymona in their senior year counting on the exemption and a job he will get after graduation at the same place where he was interning last summer.

Well, man does not always get what he wants; the neither planned nor expected happened. Bakkar’s mother is pregnant and will give birth to Hareedy before Bakkar can
get his exemption.

Hareedy is now born and unfortunately Bakkar will have to postpone his job and marriage plans for a year as he will serve as a military
soldier for one year.

On the first 45 days, soldiers are trained in the military training center. They have to do a variety of exercises daily. One day Bakkar woke up late and didn't appear in the morning lineup at
time. His commander is now angry and is going to punish him.

Bakkar is required to perform push-ups (the push-up position is called 6 esta'ed). His commander tells him to do them in reps (consecutive times) and then rest in between them. The commander wants
him to follow a strict pattern. Given an upper limit, he will perform reps with increasing number of push-ups (1, 2, 3, ...) to warm up, until he reaches the upper limit. After that, he starts decreasing the number of push-ups per rep until he stops completely
(..., 3, 2, 1). After resting, he will repeat the process again but with a higher upper limit. The upper limit starts with 1, and increases each time by a value of 1.

Here are the first 16 reps:

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1 ....

The total number of push-ups he does is the sum of all the reps has has done so far. So for example, the total number of push-ups after completing 4 reps = 1+1+2+1 = 5, and after completing 7 reps = 1+1+2+1+1+2+3 = 11.

Bakkar now has to do at least N push-ups. This is very exhausting
so he needs to know the minimum number of reps to complete using this pattern to reach his punishment reps.

Input

Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test
cases (1  ≤ T  ≤  100,000).
Followed by T test cases, each test case will be a single integer N,
the number of push-ups Bakkar wants to perform (1  ≤  N  ≤  1018).

Output

For each test case print a single line containing "Case n:" (without the quotes) where n is the test case number (starting from 1) followed by a single space, then a single integer representing the minimum number of reps needed as described above.

Examples
input
5
6
9
11
21
35
output
Case 1: 5
Case 2: 7
Case 3: 7
Case 4: 13
Case 5: 19

题解:

关键是找规律,找到n^2的关系式,然后求n^2的前n项和。

学习之处:

1.n^2的前n项和: s = n*(n+1)*(2*n+1)/6

2.求一段不完整的数列和时,可以分段求,也可以用完整的减去缺少的。

3.long long 作乘法时,加上*1LL

4.二分法逐步逼近答案。如:

000000001111111111, 找到第一个1,那么可以这样实现:

int l = , r = n;
while(l<=r)
{
int mid = (l+r)>>;
if(a[mid]==) // 当找到1时,继续尝试下标更小的元素是否也为1
r = mid - ;
else
l = mid + ;
}

代码如下:

 #include <iostream>//Gym - 100283F 二分
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define LOCAL
#define eps 0.0000001
#define LNF (1<<60)
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+; LL f1(LL n)//找一整段的
{
LL l = , r = 2e6, mid;//若r=n会超时, 所以要准确估计r的最大值
while(l<=r)
{
mid = (l+r)>>;
if(1LL*mid*(mid+)*(*mid+)/<=n)
l = mid+;
else
r = mid-;
}
return r;
} LL f2(LL n, int x)//找剩下的
{
LL l = , r = *x+, mid ,s;
while(l<=r)
{
mid = (l+r)>>;
if(mid<=x)
s = 1LL*mid*(mid+)/;
else
s = 1LL*x*x - 1LL*(*x--mid)*(*x-mid)/; // 和 = 完整-缺少 if( s>=n)
r = mid-;
else
l = mid+;
}
return l;
} void solve()
{
LL n;
scanf("%lld",&n);
LL x = f1(n);
n -= 1LL*x*(x+)*(*x+)/;
LL y = f2(n,x+);
printf("%lld\n",1LL*x*x+y);
} int main()
{
#ifdef LOCAL
freopen("army.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int T ,tt = ;
scanf("%d",&T);
while(T--) {
printf("Case %d: ",++tt);
solve();
}
return ;
}

Gym - 100283F F. Bakkar In The Army —— 二分的更多相关文章

  1. F. Bakkar In The Army 二分

    http://codeforces.com/gym/100283/problem/F 思路是二分第几行,二分出来的行是总和 >= n的,那么第k - 1行一定要选,那么再在第k行中二分那一列. ...

  2. Gym - 100283F Bakkar In The Army(二分)

    https://vjudge.net/problem/Gym-100283F 题意: 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 .... 给出这样的序列,然后给出一个n,计算从1 ...

  3. Gym 100283F Bakkar In The Army

    数学公式: n^2的前n项和n(n+1)(2*n+1)/6,用二分进行查找: 算出层数后继续二分查找位于这一层的哪一位,也可以推出相应公式 #include <iostream> #inc ...

  4. Gym 100637F F. The Pool for Lucky Ones

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  5. Gym 101064 D Black Hills golden jewels (二分)

    题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...

  6. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

  7. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  8. Gym 100637F F. The Pool for Lucky Ones 暴力

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  9. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

随机推荐

  1. Android 源码编译记录

    问题1:Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /etc/ ...

  2. OracleCPU使用情况查询

      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 ...

  3. 高通msm8994启动流程简单介绍

    处理器信息 8994包括例如以下子系统: 子系统 处理器 含义 APSS 4*Cortex-A53 应用子系统 APSS 4*Cortex-A57 应用子系统 LPASS QDSP6 v5.5A(He ...

  4. Spring 与 MyBatis 整合

    一.实验介绍 1.1 实验内容 本节课程将整合 Spring 和 MyBatis,并采用 Junit 进行单元测试. 1.2 实验知识点 Spring - MyBatis 整合 Junit 单元测试 ...

  5. 现代数字信号处理——AR模型

    1. AR模型概念观       AR模型是一种线性预测,即已知N个数据,可由模型推出第N点前面或后面的数据(设推出P点),所以其本质类似于插值,其目的都是为了增加有效数据,只是AR模型是由N点递推, ...

  6. Linux 软件大全

    应用 音频 Airtime - Airtime 是一款用于调度和远程站点管理的开放广播软件   Ardour - 在 Linux 上录音,编辑,和混音  Audacious - 开源音频播放器,按你想 ...

  7. AngularJS的Foreach循环示例

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularJSForeach.rar 代码: <!DOCTYPE HTML PUBLIC ...

  8. C++学习总结2

    链接上一篇日志,下面介绍下C++里面的其他内容 补充上一届里面的异常处理代码: try { cout << "try num" << endl; throw ...

  9. Codeforces 569 B. Inventory

    click here~~ **B. Inventory** time limit per test1 second memory limit per test256 megabytes inputst ...

  10. UVA10317- Equating Equations(回溯+剪枝)

    题目链接 题意:给出一个式子,但这个式子不一定是等式,在'+','-','='符号位置不变的情况下,又一次排列数字的位置,使其成为等式.假设能够的话.输出当中一种排列方式. 思路:我们将等号右边的数所 ...