Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (超几何分布)

题目链接:Codeforces Round #273 (Div. 2) B. Dreamoon and WiFi

题意:“+”表示前进1个单位,“-”表示后退1个单位,问以0为起点经过S1,S2两个命令后达到的位置相同的概率。

思路:统计“+”和“-”的数量。如果S2中的“+”或者“-”比S1中的多,概率是0。其他条件下,形成的是超几何分布。

AC代码:

#include <stdio.h>
#include <string.h>
int fac(int n,int m)
{
	int i,s=1;
	for(i=m;i>m-n;i--)
		s*=i;
	return s;
}
int C(int n,int m)
{
	int a=fac(n,m);
	int b=fac(n,n);
	return a/b;
}
double ipow(double n,int p)
{
	int i;
	double s=1.0;
	for(i=0;i<p;i++)
		s*=n;
	return s;
}
int main()
{
	char s1[20],s2[20];
	int len,i;
	while(scanf("%s",s1)!=EOF)
	{
		len=strlen(s1);
		int addsum,subsum;
		addsum=subsum=0;
		for(i=0;i<len;i++)
		{
			if(s1[i]=='+') addsum++;
			else subsum++;
		}
		scanf("%s",s2);
		int m=0,pos=0,sum;
		for(i=0;i<len;i++)
		{
			if(s2[i]=='?') m++;
			else if(s2[i]=='+') addsum--;
			else subsum--;
		}
		if(addsum<0 || subsum<0)
			printf("%.9lf\n",0);
		else
		{
			if(addsum+subsum!=m)
				printf("%.9lf\n",0);
			else
			{
				double ans;
				ans=ipow(0.5,m)*C(addsum,m);
				printf("%.9lf\n",ans);
			}
		}
	}
	return 0;
}
/*
++++++++++
+++??++?++

--+++---+-
??????????

*/
时间: 2024-12-25 02:06:39

Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (超几何分布)的相关文章

Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer number in range[1, a

Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式

C. Dreamoon and Sums Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer numb

Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 1  - (b-1).那么我们可以从这一点入口来进行解题.. mod (x, b) = 1 时, x  =  b + 1, 2b + 1, 3b + 1..... a * b + 1. mod (x , b) = 2 时, x =  2b + 2, 4b + 2, 6b + 2, ..... 2a * b

Codeforces Round #272 (Div. 2) D.Dreamoon and Sets 找规律

D. Dreamoon and Sets Dreamoon likes to play with sets, integers and .  is defined as the largest positive integer that divides both a and b. Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for

Codeforces Round #272 (Div. 2) D. Dreamoon and Sets (思维 数学 规律)

题目链接 题意: 1-m中,四个数凑成一组,满足任意2个数的gcd=k,求一个最小的m使得凑成n组解.并输出 分析: 直接粘一下两个很有意思的分析.. 分析1: 那我们就弄成每组数字都互质,然后全体乘以k不就行了么…… 然后看了看样例…… 这个该怎么说……我是觉得额这道题的output暴露了数据规律怎么破……我算是看出规律再证明的方式A的这道题 当时我看到22那个样例的时候……在想他干嘛要把22放这里……然后发现 2/4/6/10 14/16/18/22也是行的哇…… 化成乘以k之前的数据……

Codeforces Round #272 (Div. 2) Dreamoon and WiFi 暴力

B. Dreamoon and WiFi Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them. Each command is one of the following two types: Go 1 unit towards the

Codeforces Round #272 (Div. 2)

A. Dreamoon and Stairs 题意:给出n层楼梯,m,一次能够上1层或者2层楼梯,问在所有的上楼需要的步数中是否存在m的倍数 找出范围,即为最大步数为n(一次上一级),最小步数为n/2+n%2 在这个范围里找即可 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<algorithm> 6 using n

Codeforces Round #272 (Div. 2) B

题目: B. Dreamoon and WiFi time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dream

Codeforces Round #272 (Div. 2) ABCDE

A. Dreamoon and Stairs 题解: 首先写出尽可能2多的步数,然后判断能否%m,不能就加上最小的数使其能%m就行了 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define LL long long #define CLR(x) memset(x,0,sizeof x)