数学公式: n^2的前n项和n(n+1)(2*n+1)/6,用二分进行查找;

算出层数后继续二分查找位于这一层的哪一位,也可以推出相应公式

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll; ll f(ll n) //a(n)=n^2的前n项和公式
{
return n*(n+1)*(2*n+1)/6;
} ll f2(ll i,ll n) //这个公式要推一下
{
if(i<=n) return i*(i+1)/2;
return n*n-(2*n-i)*(2*n-i-1)/2;
} int main()
{
freopen("army.in","r",stdin);
int t;
scanf("%d",&t);
int ti=1;
while(t--)
{
ll n;
scanf("%I64d",&n);
int lef=1,rig=1500000,floor;
while(1) //二分算哪一层
{
floor=(lef+rig)/2;
if(n>f(floor-1) && n<=f(floor))
break;
if(n<=f(floor)) rig=floor-1;
else lef=floor+1;
}
ll tmp=f(floor-1);
lef=1;
rig=2*floor-1;
int index;
while(1) //二分算这一层的哪一个
{
index=(lef+rig)/2;
if(n>tmp+f2(index-1,floor) && n<=tmp+f2(index,floor))
break;
if(n<=tmp+f2(index,floor)) rig=index-1;
else lef=index+1;
}
printf("Case %d: %I64d\n",ti++,(ll)(floor-1)*(floor-1)+index);
}
return 0;
}

Gym 100283F Bakkar In The Army的更多相关文章

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

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

  3. F. Bakkar In The Army 二分

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

  4. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  5. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  6. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  7. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  8. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  9. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

随机推荐

  1. 045.Python线程队列

    线程队列 1 基本语法和用法 put 往线程队列里防止,超过队列长度,直接阻塞 get 从队列中取值,如果获取不到,直接阻塞 put_nowait: 如果放入的值超过队列长度,直接报错(linux) ...

  2. VS 中的 lib 和 dll 的区别和使用

    在 vs/c# 项目开发中,经常会遇到 lib 和 dll 文件,而且创建工程项目以及工程项目打包时也是必须要面对的,所以有必要掌握 lib 和 dll 的区别和使用. 静态库:在链接步骤中,连接器将 ...

  3. centos下安装visual studio code-(来自官网)

    (https://code.visualstudio.com/docs/setup/linux) Running VS Code on Linux Installation 安装完成后可执行:$cod ...

  4. 【Java】Jackson解析xml的坑

    为了获取xml数据,在spring mvc中针对 @ResponseBody配置了jackson. 刚用的时候内心是狂喜的,终于不用自己解析了----  but----------还是有坑的-- 坑一 ...

  5. java 文件上传下载

    翻新十年前的老项目,文件上传改为调用接口方式,记录一下子~~~ java后台代码: //取配置文件中的上传目录 @Value("${uploadPath}") String pat ...

  6. JS轮播图(网易云轮播图)

    JS 轮播图 写在前面 最聪明的人是最不愿浪费时间的人.--但丁 实现功能 图片自动切换 鼠标移入停止自动播放,显示按钮 点击按钮,实现前后翻 鼠标移入小圆圈,可以跳转到对应图片 点击左右两侧图片部分 ...

  7. Task类学习教程—组合任务ContinueWith

    Task类学习教程-组合任务.ContinueWith 一.简介 通过任务,可以指定在任务完成之后,应开始运行之后另一个特定任务.ContinueWith是Task根据其自身状况,决定后续应该作何操作 ...

  8. 使用JS获取两个时间差(JS写一个倒计时功能)

    <body onload="myFunction()"> <p id="demo"></p> <script> ...

  9. 打造住院新体验,GVS智慧病房有何独到之处?

    3月26-28日,由广东省医院协会主办的"2021第二届广东省医院建设大会暨医院建筑与装备展览会"在广州琶洲国际采购中心盛大举办,来自全国各地的医院代表及企事业单位代表4000余人 ...

  10. Vue项目开发环境搭建

    初步学习vue.js相关知识,下面是我搭建环境中一些经验总结,希望可以帮到有需要的同学.首选先安装好以下的工具和环境. 一.软件安装 1.WebStorm官网下载地址:https://www.jetb ...