uvalive 7500 Boxes and Balls

https://vjudge.net/problem/UVALive-7500

题意:

找到规律之后发现给出一个数n,要求找到1 + 2i + ... + x <= n,找出1到x的和。

思路:

一看n就知道要二分,还以为是二分写炸了。。。结果是r的范围太小了,因为n最大是1e18,又有除以2,所以r应该是2e9才对,不是1e9。

代码:

 1 #include <stdio.h>
 2
 3 int main()
 4 {
 5     int t;
 6
 7     scanf("%d",&t);
 8
 9     int cas = 0;
10
11     while(t--)
12     {
13         long long a;
14
15         scanf("%lld",&a);
16
17         long long l = 1,r = 2e9;
18
19         while (l < r - 1)
20         {
21             long long mid = (l + r) >> 1;
22
23             if ((mid) * (mid + 1) / 2 <= a) l = mid;
24             else r = mid - 1;
25         }
26
27         while((l + 1) * (l + 2) / 2 <= a) l++;
28
29         printf("Case #%d: %lld\n",++cas,(l + 1) * l / 2);
30     }
31
32     return 0;
33 }
时间: 2024-10-31 21:23:13

uvalive 7500 Boxes and Balls的相关文章

UVALive 7500 Boxes and Balls 2015EC final 签到题 二分

分析题目后,得到要求的是最接近n的一个数,并且这个数字能写成1+2+3+....+x = ans这种形式. 要求的是最大的值. 这题就直接二分去做吧.二分出一个f(mid)<=n的最大值. 最后的end就是所求的f(end) 为什么呢?,我来分析下我这个二分是怎么实现的 while (begin<=end) { LL mid = (begin + end) / 2; if (f(mid) == n) { printf ("Case #%d: %lld\n",++ff,n);

LA 7500 Boxes and Balls (数学)

题意:给定 n 个球,每次从每篮子里拿出来一个放在一个新篮子里,并移除相同的,按球的个数进行排序,问你用最多几个球能完成循环. 析:数学问题,很容易发现前n项和就是最多的球数,所以我们只要找最大的n项就好了. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&g

【CF884D】Boxes And Balls 哈夫曼树

[CF884D]Boxes And Balls 题意:有n个箱子和若干个球,球的颜色也是1-n,有ai个球颜色为i,一开始所有的球都在1号箱子里,你每次可以进行如下操作: 选择1个箱子,将里面所有的球拿出来,分成k部分(你可以令k=2或3),将每一部分都放到一个空箱子中.花费的代价是这个箱子中球的总数. 现要求你在若干次操作后,所有颜色为i的球都在i号箱子里,求最小代价. n<=200000,ai<=10^9 题解:傻逼题都不会做了.将操作反过来,就变成了将k个箱子合并到一起,这就变成经典的哈

Boxes and Balls UVALive - 7500(练习赛爆零)

原因: 自身: 1.自己并没有考虑过精度所带来的问题. 2.一定要自己读题,独立思考,末被队友带偏(矛盾出真理). 3.加强自身基础,提高自身实力. 队伍: 1.队友缺乏独立思考,需要加强. 题目描述: 给你n个球,求在有限次数变化中,球的变化最后稳定在一种状态. 思路: 打表找过规律后发现,稳定状态下球的个数是1,2,3,6,10,15,21.....是以等差数列的前n项和.s=(n+1)*n/2; 思路一:二分模拟(想到了,但是深度不够),没有尝试. 思路二:借二元一次方程.(x+1)*x-

UVALive 6500 Boxes

Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 6500 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int T,m,n; 6 int i,j,s; 7 int a[105][105]; 8 scanf("%d",

2015 ACM/ICPC EC-Final

A. Boxes and Balls 二分找到最大的不超过$n$的$\frac{x(x+1)}{2}$形式的数即可. #include <bits/stdc++.h> using namespace std ; typedef long long LL ; void solve () { LL n ; scanf ( "%lld" , &n ) ; LL l = 1 , r = 2e9 ; while ( l < r ) { LL m = l + r + 1

UVA - 12293 Box Game (规律)

Description  Box Game  There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves alternatively, Alice mo

UVA 12293 Box Game(博弈入门)

题目链接:Box Game 题面:            12293 Box Game There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves al

Box Game

There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves alternatively, Alice moves first. For each mov