F. Bakkar In The Army 二分
http://codeforces.com/gym/100283/problem/F
思路是二分第几行,二分出来的行是总和 >= n的,那么第k - 1行一定要选,那么再在第k行中二分那一列、
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
LL f1(LL n) {
return n * (n + ) / * ( * n + ) / ;
}
LL f2(int which, int pos) {
if (pos <= which) {
return 1LL * pos * ( + pos) / ;
} else {
LL t = * which - - pos;
return 1LL * which * ( + which) - which - f2(which, t);
}
}
void work() {
LL n;
scanf("%I64d", &n);
int be = , en = 2e6 + ;
while (be <= en) {
int mid = (be + en) >> ;
LL res = f1(mid);
if (res >= n) {
en = mid - ;
} else be = mid + ;
}
// cout << be << endl;
// cout << f1(be) << endl;
LL ans = ((LL)be - ) * (be - );
// cout << n << endl;
// cout << f1(be - 1) << endl;
n -= f1(be - );
// cout << n << endl;
// cout << f2(4, 8) << endl;
int L = , R = * be - ;
while (L <= R) {
int mid = (L + R) >> ;
LL res = f2(be, mid);
if (res >= n) {
R = mid - ;
} else L = mid + ;
}
ans += L;
static int f = ;
printf("Case %d: %I64d\n", ++f, ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
// freopen("army.in", "r", stdin);
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
F. Bakkar In The Army 二分的更多相关文章
- 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 ...
- 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 Bakkar In The Army
数学公式: n^2的前n项和n(n+1)(2*n+1)/6,用二分进行查找: 算出层数后继续二分查找位于这一层的哪一位,也可以推出相应公式 #include <iostream> #inc ...
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
- Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案
Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...
- POJ 1759 Garland(二分+数学递归+坑精度)
POJ 1759 Garland 这个题wa了27次,忘了用一个数来储存f[n-1],每次由于二分都会改变f[n-1]的值,得到的有的值不精确,直接输出f[n-1]肯定有问题. 这个题用c++交可以 ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 1969 Pie [二分]
1.题意:一项分圆饼的任务,一堆圆饼共有N个,半径不同,厚度一样,要分给F+1个人.要求每个人分的一样多,圆饼允许切但是不允许拼接,也就是每个人拿到的最多是一个完整饼,或者一个被切掉一部分的饼,要求你 ...
- 二维动态规划&&二分查找的动态规划&&最长递增子序列&&最长连续递增子序列
题目描述与背景介绍 背景题目: [674. 最长连续递增序列]https://leetcode-cn.com/problems/longest-continuous-increasing-subseq ...
随机推荐
- DB2删除表分区
近日,由于部门数据库读库空间过小,提出删除掉两个月之前日志表的分区(数据库分区是按时间月分区),记述如下: 上网搜索资料发现删除表分区大概分这么几步: 1.查询需要删除掉的分区: select t.D ...
- 三剑客之awk数组实战
shell数组for循环 第一种: #!/bin/sh array=(1 2 3) for i in ${array[*]} do echo $i done 第二种: #!/bin/sh ...
- javascript之创建对象的方式
1.object构造函数创建 var obj=new Object(); obj.name='xue'; 2.对象字面量创建 var obj={ name:'xue' } 3.构造函数创建 funct ...
- Python框架下django 的并发和多线程
django 的并发能力真的是令人担忧,django本身框架下只有一个线程在处理请求,任何一个请求阻塞,就会影响另一个情感求的响应,尤其是涉及到IO操作时,基于框架下开发的视图的响应并没有对应的开启多 ...
- C语言之fileno()函数--获取已经打开的文件的文件描述符(小技巧)
open函数相关的: /* open 是系统调用 返回的是文件句柄*/ #include <sys/stat.h> #include <fcntl.h> int open(c ...
- SQL Server 2008将数据导出为脚本 [SQL Server]
之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...
- FFmpeg在Linux下搭建 ***
今天介绍下FFmpeg在Linux下安装编译过程,总体过程比较顺利,就是在ffmpeg等的时间稍长点,仅当记录. 关于FFmpeg FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采 ...
- bzoj2718
二分图匹配 首先有个定理:最长反链=最小链覆盖 最小链覆盖可以重复经过点 所以我们不能直接建图 那么我们用floyd判断是否相连 然后建图就行了 #include<bits/stdc++.h&g ...
- Python interview_python
https://github.com/taizilongxu/interview_python 1 Python的函数参数传递 strings, tuples, 和numbers是不可更改的对象,而l ...
- UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 2: ordinal not in range(128)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 2: ordinal not in ran ...