UVa 10889 - The Lost Gift

题目:给你R个红球和B个黑球,从这些球中取出相同颜色的概率是50%;

然后丢了一些黑球,剩下的黑球不少于原来的70%;

现在给你红球和剩下的黑球个数,求可能丢了几个黑球。

分析:数学题。

首先,根据组合数学列出等式2*[C(n,2)+C(m,2)] = C(m+n,2):

解得 m = 0.5*(2*n+1±sqrt(8*n+1));

然后,进行判断:

1.m不是整数,则红球非法;

2.m是整数,丢的球不在0-30%之间,黑球非法;

3.都合法的情况下,输出丢失的黑球个数即可。

方法2:通项公式法(前n项和),通过观察数据可知红球和黑球个数满足如下条件:

(a【k】,a【k-1】)或者(a【k】,a【k+1】)

a【k】= sum(1,k)= k*(k+1)/ 2

可知 C(a【k】+a【k-1】,2)= 2 *(C(a【k】,2)+C(a【k-1】,2))

给据通项公式,找到前面和后面的项即为解。(可打表计算,略)

说明:有可能有2个解,判断时直接用<=0.3不要加精度控制。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;

int ans[5];

int main()
{
	int R,B;
	while ( cin >> R >> B && R+B ) {
		int r = (int)sqrt(8*R+1.0);
		if ( r*r == 8*R+1 && r%2 ) {
			int b1 = (2*R+1-r)/2;
			int b2 = (2*R+1+r)/2;
			int count = 0;
			if ( b1 >= B && (b1-B+0.0)/b1 <= 0.3 )
				ans[count ++] = b1-B;
			if ( b2 >= B && (b2-B+0.0)/b2 <= 0.3 )
			if ( !count || ans[0] != b2-B )
				ans[count ++] = b2-B;
			if ( count ) {
				for ( int i = 0 ; i < count ; ++ i ) {
					if ( i ) printf(" ");
					printf("%d",ans[i]);
				}printf("\n");
			}else cout << "No. of black balls invalid" << endl;
		}else cout << "No. of red balls invalid" << endl;
	}
	return 0;
}

UVa 10889 - The Lost Gift

时间: 2024-10-14 00:32:21

UVa 10889 - The Lost Gift的相关文章

UVA 10471 Gift Exchanging

题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率.求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少 dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用13进制数表示)时的概率, 那么转移就是dp[N][sta]=sum(dp[N-1][sta-1]*G[n][k]) (表示第N个人选择第k个盒子) 那么答案应该是max(P(第1个人选择i号盒子)/总状态概率)(i<=5) #include <map> #include <set&g

UVa 10120 - Gift?!

题目大意 美丽的村庄里有一条河,N个石头被放置在一条直线上,从左岸到右岸编号依次为1,2,...N.两个相邻的石头之间恰好是一米,左岸到第一个石头的距离也是一米,第N个石头到右岸同样是一米.礼物被放置在第M个石头上,Frank从左岸开始跳跃,对于第i步,必须跳2*i-1米,每次可以向左方向跳,也可以向右方向跳,跳到河岸跳跃就结束了,问Frank能否拿到礼物. 分析 搜索题,N>=49直接输答案,N<49时搜索,原因点击这里 http://www.algorithmist.com/index.p

UVA - 10120 Gift?! 暴力+规律

题目大意:有n块石头,礼物在第m块石头上,相邻石头的距离为1,规定小青蛙第一步跳到第一块石头上,接下来的跳跃要符合该规则,假设这是第n次跳跃,那么小青蛙跳跃的距离为(2 * n - 1),且每次跳跃都必须跳到石头上 解题思路:石头数量如果超过49的话,小青蛙就可以跳到任意一块石头上,其他的情况只需暴力dfs就可以解决了 #include<cstdio> #include<cstring> int pos, len; bool dfs(int cur, int time) { if(

UVA - 11987 - Almost Union-Find (又是并查集~)

UVA - 11987 Almost Union-Find Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem A Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to implement som

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

[UVA] 11991 - Easy Problem from Rujia Liu? [STL应用]

11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ con

uva Easy Problem from Rujia Liu?

Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ contests like Rujia Liu's Presents 1 and 2), he occasionally sets

uva 12356 - Army Buddies

 Description ??? Problem J Jin Ge Jin Qu [h]ao (If you smiled when you see the title, this problem is for you ^_^) For those who don't know KTV, see: http://en.wikipedia.org/wiki/Karaoke_box There is one very popular song called Jin Ge Jin Qu(?Š2?-