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 ...
随机推荐
- centos7基于luks对磁盘进行加密
centos7基于luks对磁盘进行加密 1,504 views A+ 所属分类:linux 收 藏 LUKS(Linux Unified Key Setup)为Linux硬盘加密提供了一种标准,它 ...
- 052.Python前端Django框架路由层和视图层
一.路由层(URLconf) 1.1 路由层简单配置 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Dj ...
- 下面附上top和sar的使用方法,方便参考! "top"工具
下面附上top和sar的使用方法,方便参考! "top"工具 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示 ...
- Redis(3)- 数据结构
一.Redis数据结构 Redis数据结构:Redis在数据类型上常用的有5种数据类型,而底层实现拥有种.可以使用命令OBJECT ENCODING K1查询底层数据结构. # 查询key的底层数据类 ...
- 阿里云AIoT云端一体:迎接云原生+低代码时代的到来
距上次2019年参加上海阿里云开发者大会已经近2年的时间了,也许因为疫情的原因,这一两年线下大型活动基本很少了,这次在北京国家会议中心举办的阿里云开发者大会,无论是参会人员的规模,还有演讲嘉宾的级别和 ...
- unity用json和服务器数据交互
第一种类型:服务器json数据是个对象 /// <summary> /// 获取用户信息初始化信息 /// </summary> void InitUserMessage() ...
- MyBatis-框架使用和分析
一.基础知识 MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作.MyBatis 可 ...
- salesforce零基础学习(一百零三)项目中的零碎知识点小总结(五)
本篇参考:Salesforce Admin篇(四) Security 之Two-Factor Authentication & Single Sign On https://developer ...
- Python+Selenium学习笔记7 - os模块
os模块是关于文件/目录方面的 导入语法 import os 相关方法 path.abspath() 用来获取当前路径下的文件 os.path.abspath('checkbox.html') ...
- A100计算能力
A100计算能力 A100 GPU支持新的计算功能8.0.表1比较了NVIDIA GPU架构的不同计算功能的参数. 表1.计算能力:GP100 vs. GV100 vs. GA100. MIG架构 尽 ...