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

Statements

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

题目比较好懂,但是码起来比较难受,因为这道题包含三个二分,数据处理二分+分类讨论2种二分,思路是有的,不过二分处理起来很难受,首先我们对数据处理二分,我一开始以为数据直接暴力就可以了,没想到也要二分,所以我们要测出它的边界值,也就是当它输出的是10^18时候,应该到达的右边界应该是多少,我测试了150w是可以到达这个值,我们观察那个图形,我们可以发现它类似杨辉三角,不过这里是求和问题

    1        1^2      1

  1 2 1     2^2      3

1 2 3 2 1   3^3     5

我们可以发现每一段的所有数字加起来是n^2,所有从1-n段的数字全部加起来就是sum= n*(n+1)*(2*n+1),而那个1 3 5就是每一段的项数,是个等差数列 求和 n*n 所以我们二分找到我们所输入的  m<=sum的那一段k,因为是<=因此我们二分时候应该注意了,要使得二分出来的数字一定大于m那么我们直接返回l;   要使得二分出来的数字等于m,那么我们就应该有这个条件sum<n

ll binary()
{
    ll l=1,r=2e6,mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=mid*(mid+1)*(2*mid+1)/6;
        if(sum<n)
        {
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return l;
}

二分应该这样做

那么接着是求和处理

我一开始我以为是左边界应该为0的问题,但是后来测试数据发现是第三个二分写错了

第一个我们先比较最后那个值是在二分一的左边还是右边,左右两种情况考虑就可以了,仔细码!!!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
typedef long long ll;
using namespace std;
ll n;
ll bs(ll l,ll r)
{
    ll mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=(mid+1)*mid/2;
       // if(n==sum) return mid;
        if(n>sum) l=mid+1;
        else r=mid-1;
    }
    return l;
}
ll Bs(ll l,ll r,ll i,ll m)
{
    ll mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=(2*i+1-mid)*mid/2;
         //if(n==sum) return mid;
        if(sum<m) l=mid+1;
        else r=mid-1;
    }
    return l;
}
ll binary()
{
    ll l=1,r=2e6,mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=mid*(mid+1)*(2*mid+1)/6;
        if(sum<n)
        {
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return l;
}
int main()
{
    freopen("army.in","r",stdin);
    ll t,Count=1,l;
    scanf("%lld",&t);
    while(t--)
    {
        ll i,k,temp,sum,mid;
        scanf("%lld",&n);
        l=binary();//找到那一行
        printf("Case %d: ",Count++);
        temp=(l-1)*l*(2*(l-1)+1)/6;//前面l-1行的总和
        n-=temp;//直接在第l行做操作
        sum=l*(l+1)/2;//第l行的中间值
       // printf("%lld\n",l);
        if(n<=sum)
        {
            k=bs(1,l);
            //printf("<= n==%lld l==%lld  k==%lld\n",n,l,k);
            printf("%lld\n",(l-1)*(l-1)+k);
        }
        else
        {
            k=Bs(1,l-1,l-1,n-sum);//去掉中间项只有l-1项
           //  printf("> n==%lld l==%lld  k==%lld\n",n,l,k);
            printf("%lld\n",(l-1)*(l-1)+l+k);
        }

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
蛋白质是生物体中普遍存在的一类重要生物大分子,由天然氨基酸通过肽键连接而成。它具有复杂的分子结构和特定的生物功能,是表达生物遗传性状的一类主要物质。 蛋白质的结构可分为四级:一级结构是组成蛋白质多肽链的线性氨基酸序列;二级结构是依靠不同氨基酸之间的C=O和N-H基团间的氢键形成的稳定结构,主要为α螺旋和β折叠;三级结构是通过多个二级结构元素在三维空间的排列所形成的一个蛋白质分子的三维结构;四级结构用于描述由不同多肽链(亚基)间相互作用形成具有功能的蛋白质复合物分子。 蛋白质在生物体内具有多种功能,包括提供能量、维持电解质平衡、信息交流、构成人的身体以及免疫等。例如,蛋白质分解可以为人体提供能量,每克蛋白质能产生4千卡的热能;血液里的蛋白质能帮助维持体内的酸碱平衡和血液的渗透压;蛋白质是组成人体器官组织的重要物质,可以修复受损的器官功能,以及维持细胞的生长和更新;蛋白质也是构成多种生理活性的物质,如免疫球蛋白,具有维持机体正常免疫功能的作用。 蛋白质的合成是指生物按照从脱氧核糖核酸(DNA)转录得到的信使核糖核酸(mRNA)上的遗传信息合成蛋白质的过程。这个过程包括氨基酸的活化、多肽链合成的起始、肽链的延长、肽链的终止和释放以及蛋白质合成后的加工修饰等步骤。 蛋白质降解是指食物中的蛋白质经过蛋白质降解酶的作用降解为多肽和氨基酸然后被人体吸收的过程。这个过程在细胞的生理活动中发挥着极其重要的作用,例如将蛋白质降解后成为小分子的氨基酸,并被循环利用;处理错误折叠的蛋白质以及多余组分,使之降解,以防机体产生错误应答。 总的来说,蛋白质是生物体内不可或缺的一类重要物质,对于维持生物体的正常生理功能具有至关重要的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值