Gym - 100283F F. Bakkar In The Army —— 二分

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

 



代码如下:

 1 #include <iostream>//Gym - 100283F 二分
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define ms(a, b)  memset((a), (b), sizeof(a))
16 #define LOCAL
17 #define eps 0.0000001
18 #define LNF (1<<60)
19 typedef long long LL;
20 const int inf = 0x3f3f3f3f;
21 const int maxn = 100000+10;
22 const int mod = 1e9+7;
23 
24 LL f1(LL n)//找一整段的
25 {
26     LL l = 0, r = 2e6, mid;//若r=n会超时, 所以要准确估计r的最大值
27     while(l<=r)
28     {
29         mid = (l+r)>>1;
30         if(1LL*mid*(mid+1)*(2*mid+1)/6<=n)
31             l = mid+1;
32         else
33             r = mid-1;
34     }
35     return r;
36 }
37 
38 LL f2(LL n, int x)//找剩下的
39 {
40     LL l = 0, r = 2*x+1, mid ,s;
41     while(l<=r)
42     {
43         mid = (l+r)>>1;
44         if(mid<=x)
45             s = 1LL*mid*(mid+1)/2;
46         else
47             s = 1LL*x*x -  1LL*(2*x-1-mid)*(2*x-mid)/2; // 和 = 完整-缺少
48 
49         if( s>=n)
50             r = mid-1;
51         else
52             l = mid+1;
53     }
54     return l;
55 }
56 
57 void solve()
58 {
59     LL n;
60     scanf("%lld",&n);
61     LL x = f1(n);
62     n -= 1LL*x*(x+1)*(2*x+1)/6;
63     LL y = f2(n,x+1);
64     printf("%lld\n",1LL*x*x+y);
65 }
66 
67 int main()
68 {
69 #ifdef LOCAL
70     freopen("army.in", "r", stdin);
71 //      freopen("output.txt", "w", stdout);
72 #endif // LOCAL
73     int T ,tt = 0;
74     scanf("%d",&T);
75     while(T--) {
76         printf("Case %d: ",++tt);
77         solve();
78     }
79     return 0;
80 }
View Code

 


 

posted on 2017-04-19 20:52  h_z_cong  阅读(352)  评论(0编辑  收藏  举报

导航