数学公式: 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. $(cd "$(dirname "$0")",pwd) 解析

    xx.sh 文件内容如下: #!/bin/bash BIN_FOLDER=$(cd "$(dirname "$0")";pwd) echo $BIN_FOLDE ...

  2. 解决 Ubuntu 无法使用 root 用户进行 ssh 远程登陆

    解决 Ubuntu 无法使用 root 用户进行 ssh 远程登陆 操作系统 Ubuntu 20.04.2 LTS 一.修改sshd配置文件 //打开 /etc/ssh/sshd_config 配置文 ...

  3. 使用find命令查找大文件

    使用find命令查找大文件 find命令是Linux系统管理员工具库中最强大的工具之一.它允许您根据不同的标准(包括文件大小)搜索文件和目录. 例如,如果在当前工作目录中要搜索大小超过100MB的文件 ...

  4. VMware安装RedHat7、CentOS7后无网卡解决办法

    由于Vmware虚拟网卡和linux兼容问题导致驱动无法正常安装,默认的网卡类型不兼容找到我们的Vmware虚拟机文件夹,将VMware 虚拟机配置 (.vmx),追加一条设置,网卡类型etherne ...

  5. Cron 定时任务命令-配置文件详解

    定时任务 定时任务的作用 1.系统级别的定时任务 ​ 临时文件清理 ​ 系统信息采集 ​ 日志文件切割 2.用户级别的定时任务 ​ 定时向互联网同步时间 ​ 定时备份系统配置文件 ​ 定时备份数据库的 ...

  6. Linux进阶之磁盘管理及LVM逻辑卷

    本节内容 磁盘管理 LVM 一.磁盘管理 1.硬盘接口 种类及其应用: IDE接口硬盘多用于家用产品,部分应用于服务器 SATA SCSI接口硬盘主要应用于服务器 SAS只在高端服务器上,价格昂贵 2 ...

  7. linux中getopt的用法-(转自pengyingh)

    getopt被用来解析命令行选项参数.就不用自己写东东处理argv了. #include <unistd.h>       extern char *optarg;   //选项的参数指针 ...

  8. C语言编程 菜鸟练习100题(31-40)

    [练习31]判断质数 0. 题目: 判断质数 1. 分析: 质数(prime number),指大于 1的.且除 1 和本身以外没有其他因数的自然数. 2. 程序: #include <stdi ...

  9. 案例分享:Qt modbus485调试工具(读写Byte、Int、DInt、Real、DReal)(当前v1.3.0)

    前言   西门子PLC.台达PLC.法兰克机床等等多年以前玩得比较多,有tcp/ip通讯也有modbus通讯,modbus又分为网络,485,232等.  医疗项目,焊接机器人项目,工控机床项目,数控 ...

  10. Java枚举类与注解详解——一篇文章读懂枚举类与注解详

    目录 一.枚举类 ① 自定义枚举类 ② enum关键字定义枚举类 ③ enum 枚举类的方法 ④ enum 枚举类实现接口 二.注解 ① 生成文档相关注解 ②注解在编译时进行格式检查 ③注解跟踪代码的 ...