Gym 100283F Bakkar In The Army
数学公式: 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的更多相关文章
- 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 ...
- 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 ...
- F. Bakkar In The Army 二分
http://codeforces.com/gym/100283/problem/F 思路是二分第几行,二分出来的行是总和 >= n的,那么第k - 1行一定要选,那么再在第k行中二分那一列. ...
- 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 ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- poj3069 Saruman's Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
随机推荐
- ARM64平台编译stream、netperf出错解决办法 解决办法:指定编译平台为alpha [root@localhost netperf-2.6.0]# ./configure –build=alpha
ARM64平台编译stream.netperf出错解决办法 http://ilinuxkernel.com/?p=1738 stream编译出错信息: [root@localhost stream]# ...
- CCproxy 代理上网
相信有些同学在工作过程中遇到过公司内网环境无法上网的情况,下面给大家介绍一下CCproxy代理上网的配置 场景:linux虚拟机通过CCproxy代理访问外网 环境:只能访问内网的linux环境,可以 ...
- mysql的日志文件及其作用
MySQL中有七种日志文件,分别是: 重做日志(redo log) 回滚日志(undo log) 二进制日志(binlog) 中继日志(relay log) 错误日志(errorlog) 慢查询日志( ...
- https 真的安全吗,可以抓包吗,如何防止抓包吗
Android_interview github 地址 大家好,我是程序员徐公,加上实习,有五年中大厂经验.自荐一下,可以关注我的微信公众号程序员徐公 公众号程序员徐公回复黑马,获取 Android ...
- 在js中将map对象转换成json 和 js对cookie的操作
在js中将map对象转换成json //msp转objectlet obj= Object.create(null); for (let[k,v] of map) { obj[k] = v; }//o ...
- 『动善时』JMeter基础 — 38、JMeter中实现跨线程组关联
目录 1.JMeter中实现跨线程组关联说明 (1)JMeter中实现跨线程组关联步骤 (2)测试计划内包含的元件 2.用户登陆请求的相关操作 (1)进行登陆操作获取Cookie信息 (2)把Cook ...
- CVPR2020 论文解读:少点目标检测
CVPR2020 论文解读:具有注意RPN和多关系检测器的少点目标检测 Few-Shot Object Detection with Attention-RPN and Multi-Relation ...
- Python_Selenium 之以login_page为例实现对basepage封装好的方法调用和对common中公共方法的调用
目的:简化代码,提供框架该有的东西每一个函数 -提供了一个功能 - 公共的功能有了basepage,在PageObjects当中直接调用元素操作. 以下以login_page 为例,实现从配置文件中读 ...
- Spring Boot 实战:如何自定义 Servlet Filter
1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等. ...
- alert日志报错:ERROR: failed to establish dependency between database RACDB and diskgroup resource ora.DATA.dg
一.打开数据库alert日志,发现有报错 ERROR: failed to establish dependency between database RACDB and diskgroup reso ...