博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gym 100283F Bakkar In The Army
阅读量:7055 次
发布时间:2019-06-28

本文共 1240 字,大约阅读时间需要 4 分钟。

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

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

#include 
#include
#include
#include
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;}

 

转载于:https://www.cnblogs.com/pach/p/6725048.html

你可能感兴趣的文章
挖机全车无动作是什么故障原因引起的?
查看>>
监狱电视系统设计原则及应用场景
查看>>
JDK 源码阅读 :ByteBuffer
查看>>
python面试题
查看>>
vscode 使用小结
查看>>
我的友情链接
查看>>
Isilon整合Hadoop
查看>>
我的友情链接
查看>>
.NET反编译的九大金刚
查看>>
开源项目:Android-Universal-Image-Loader总结
查看>>
tapestry 5 development series - 1
查看>>
shell编程输入输出,控制结构,函数
查看>>
CentOS6.5 ping: unknown host 解决方
查看>>
C语言拷贝文件夹(包含文件属性信息)
查看>>
keepalive配置
查看>>
使用Ansible部署LAMP环境
查看>>
mac下 连接windows远程桌面
查看>>
如何在LoadRunner中使用winsocket协议
查看>>
设置mysql密码策略,忘记管理员密码重置密码
查看>>
部署DNS服务器之主要区域
查看>>