分析题目后,得到要求的是最接近n的一个数,并且这个数字能写成1+2+3+....+x = ans这种形式。

要求的是最大的值。

这题就直接二分去做吧。二分出一个f(mid)<=n的最大值。

最后的end就是所求的f(end)

为什么呢?,我来分析下我这个二分是怎么实现的

  1. while (begin<=end)
  2. {
  3. LL mid = (begin + end) / ;
  4. if (f(mid) == n)
  5. {
  6. printf ("Case #%d: %lld\n",++ff,n);
  7. return ;
  8. }
  9. if (f(mid) < n)
  10. {
  11. begin = mid+; //去找一个可能比n大的
  12. }
  13. else end = mid-; //如果比n大了的话。就回来找一个小的。
  14. //cout<<f(mid)<<endl;
  15. }

当f(mid)<n的时候 begin = mid+1,如果这个时候[mid+1,end]的所有数字的f()值都大于n呢?那么,最后一步就肯定是begin=end,然后end-1.去到的是第一个小于n的f()值。

其他也是一样分析啦。

f(end),第一个小于n的f值。

f(begin),第一个大于n的f值。考虑最后一步begin==end的时候,就能看清楚了。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define inf (0x3f3f3f3f)
  8. typedef long long int LL;
  9.  
  10. #include <iostream>
  11. #include <sstream>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #include <queue>
  16. #include <string>
  17. LL f(LL x)
  18. {
  19. if (x&)
  20. {
  21. return (x+)/*x;
  22. }
  23. else return x/*(x+);
  24. }
  25. int ff;
  26. void work ()
  27. {
  28. LL n;
  29. cin>>n;
  30. LL begin=,end=2e9;
  31. while (begin<=end)
  32. {
  33. LL mid = (begin + end) / ;
  34. if (f(mid) == n)
  35. {
  36. printf ("Case #%d: %lld\n",++ff,n);
  37. return ;
  38. }
  39. if (f(mid) < n)
  40. {
  41. begin = mid+; //去找一个可能比n大的
  42. }
  43. else end = mid-; //如果比n大了的话。就回来找一个小的。
  44. //cout<<f(mid)<<endl;
  45. }
  46. printf ("Case #%d: %lld\n",++ff,f(end));
  47. return ;
  48. }
  49.  
  50. int main()
  51. {
  52. #ifdef local
  53. freopen("data.txt","r",stdin);
  54. #endif
  55. int t;
  56. cin>>t;
  57. while(t--) work();
  58. return ;
  59. }

UVALive 7500 Boxes and Balls 2015EC final 签到题 二分的更多相关文章

  1. UVaLive 7500 Boxes and Balls (数学)

    题意:给定 n 个球,每次从每篮子里拿出来一个放在一个新篮子里,并移除相同的,按球的个数进行排序,问你用最多几个球能完成循环. 析:数学问题,很容易发现前n项和就是最多的球数,所以我们只要找最大的n项 ...

  2. uvalive 7500 Boxes and Balls

    https://vjudge.net/problem/UVALive-7500 题意: 找到规律之后发现给出一个数n,要求找到1 + 2i + ... + x <= n,找出1到x的和. 思路: ...

  3. 【CF884D】Boxes And Balls 哈夫曼树

    [CF884D]Boxes And Balls 题意:有n个箱子和若干个球,球的颜色也是1-n,有ai个球颜色为i,一开始所有的球都在1号箱子里,你每次可以进行如下操作: 选择1个箱子,将里面所有的球 ...

  4. Educational Codeforces Round 31- D. Boxes And Balls

    D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...

  5. CF388C&&2018EC Final D题——博弈&&水题

    一下两个题目都是按堆取石子,轮流取,每个人都贪心的取即可,感觉都不像博弈. CF388C 有n排石子,每排有若干堆.Ciel可以选择一排,拿走这一排的第一堆石子.Jiro可以选择一排,拿走这一排的最后 ...

  6. A 洛谷 P3601 签到题 [欧拉函数 质因子分解]

    题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...

  7. fjwc2019 D3T1 签到题 (贪心)

    #184. 「2019冬令营提高组」签到题 每次询问接近O(1).......考虑贪心 怎么贪心呢? 对于相邻的两个数,我们要保证异或x后单调不降 我们找到两个数二进制上最高的相异位 当左边的数相异位 ...

  8. CTF-练习平台-WEB之 签到题

    一.签到题 根据提示直接加群在群公告里就能找到~

  9. 洛谷P3601签到题(欧拉函数)

    题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...

随机推荐

  1. Python 中list, dictionary 与 file相互操作

    Python的list,dictionary可以写入file, 也可以从file中读取. 关于list: 1)写入文件         self.existedBlog.write("you ...

  2. get and set

    (function () { function Student(name, age, gender) { this._name = name; this._age = age; this._gende ...

  3. oracle--基础查询(1)

    --查询所有列语句 select * from emp; ---查询指定列表的查询语句 select empno,ename,sal from emp; --带有简单算术运算符的简单查询 select ...

  4. numpy.zeros(shape, dtype=float, order='C')

    numpy.zeros Return a new array of given shape and type, filled with zeros. Parameters: shape : int o ...

  5. 相关符号标点的英文(IOS学习)

    尖括号: angle bracket 方括号: square bracket 花括号: curly brace 圆括号: parentheses 逗号: comma 冒号: colon 逗号: sem ...

  6. 创建sharepoint网站

    1.首先打开管理中心 输入用户名和密码(默认是本机的管理员名称及密码) 在“应用程序管理”选择“管理WEB应用程序” 新建应用程序 选择一个没有占用的端口,选择允许匿名访问 数据库名称一般为WSS_C ...

  7. Learning Python 008 正则表达式-003 search()方法

    Python 正则表达式 - search()方法 findall()方法在找到第一个匹配之后,还会继续找下去,findall吗,就是找到所有的匹配的意思.如果你只是想找到第一个匹配的信息后,就不在继 ...

  8. 22、IDP-ASE

    IDPASE https://github.com/bdeonovic/IDPASE.jl Prepare necessary input files (1)FASTQ file of your hy ...

  9. Win7环境下Sublime Text 3下安装NodeJS插件

    1.首先下载安装Node.JS,配置好环境变量(安装好Node.JS默认是配置好了环境变量的). 2.Sublime Text 3下安装NodeJS插件. 参考的两篇文章:http://www.cnb ...

  10. hdu1057

    #include <iostream> #include <string> #include <cstring> using namespace std; int ...