数学公式: 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. 使用find命令查找大文件

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

  2. jolokia配置Java监控

    wget http://search.maven.org/remotecontent?filepath=org/jolokia/jolokia-jvm/1.3.6/jolokia-jvm-1.3.6- ...

  3. 速度竟差9倍!6款32GB USB3.0优盘横评

    速度竟差9倍!6款32GB USB3.0优盘横评 2014-08-22 05:04:00  [  中关村在线 原创  ]   作者: 蒋丽 |  责编:孙玉亮 收藏文章 分页阅读 分享到 评论(90) ...

  4. S5 Linux信息显示与搜索文件命令

    5.1-5 uname.hostname.dmesg.stat.du 5.6 date:显示与设置系统时间 5.7 echo:显示一行文本 5.8-12 watch.which.whereis.loc ...

  5. 浅谈:Redis持久化机制(一)RDB篇

    浅谈:Redis持久化机制(一)RDB篇 ​ 众所周知,redis是一款性能极高,基于内存的键值对NoSql数据库,官方显示,它的读效率可达到11万次每秒,写效率能达到8万次每秒,因为它基于内存以及存 ...

  6. 「 洛谷 」P2151 [SDOI2009]HH去散步

    小兔的话 欢迎大家在评论区留言哦~ HH去散步 题目限制 内存限制:125.00MB 时间限制:1.00s 标准输入 标准输出 题目知识点 动态规划 \(dp\) 矩阵 矩阵乘法 矩阵加速 矩阵快速幂 ...

  7. salesforce零基础学习(一百零三)项目中的零碎知识点小总结(五)

    本篇参考:Salesforce Admin篇(四) Security 之Two-Factor Authentication & Single Sign On https://developer ...

  8. libevent中数据缓冲区buffer分析

    很多时候为了应对数据IO的"慢"或者其他原因都需要使用数据缓冲区.对于数据缓冲,我们不陌生,但是对于如何实现这个缓冲区,相信很多时候大家都没有考虑过.今天就通过分析libevent ...

  9. SOLOv 2:实例分割(动态、更快、更强)

    SOLOv 2:实例分割(动态.更快.更强) SOLOv2:  Dynamic, Faster and Stronger 论文链接: https://arxiv.org/pdf/2003.10152. ...

  10. MIT Graph实践概述

    MIT Graph实践概述 Features功能 •   iCloud Support •   Multi Local & Cloud Graphs •   Thread Safe •   S ...