硬币水题II
Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name: Main
Font Size:
Type: None Graph Theory 2-SAT Articulation/Bridge/Biconnected Component Cycles/Topological Sorting/Strongly Connected Component Shortest Path Bellman Ford Dijkstra/Floyd Warshall Euler Trail/Circuit Heavy-Light Decomposition Minimum Spanning Tree Stable Marriage Problem Trees Directed Minimum Spanning Tree Flow/Matching Graph Matching Bipartite Matching Hopcroft–Karp Bipartite Matching Weighted Bipartite Matching/Hungarian Algorithm Flow Max Flow/Min Cut Min Cost Max Flow DFS-like Backtracking with Pruning/Branch and Bound Basic Recursion IDA* Search Parsing/Grammar Breadth First Search/Depth First Search Advanced Search Techniques Binary Search/Bisection Ternary Search Geometry Basic Geometry Computational Geometry Convex Hull Pick's Theorem Game Theory Green Hackenbush/Colon Principle/Fusion Principle Nim Sprague-Grundy Number Matrix Gaussian Elimination Matrix Exponentiation Data Structures Basic Data Structures Binary Indexed Tree Binary Search Tree Hashing Orthogonal Range Search Range Minimum Query/Lowest Common Ancestor Segment Tree/Interval Tree Trie Tree Sorting Disjoint Set String Aho Corasick Knuth-Morris-Pratt Suffix Array/Suffix Tree Math Basic Math Big Integer Arithmetic Number Theory Chinese Remainder Theorem Extended Euclid Inclusion/Exclusion Modular Arithmetic Combinatorics Group Theory/Burnside's lemma Counting Probability/Expected Value Others Tricky Hardest Unusual Brute Force Implementation Constructive Algorithms Two Pointer Bitmask Beginner Discrete Logarithm/Shank's Baby-step Giant-step Algorithm Greedy Divide and Conquer Dynamic Programming
小胖有一个正反面不对称的硬币。如果抛一次这个硬币,它的正面朝上的概率为p,反面朝上的概率为1-p。现在,小胖想用这个硬币来产生等概率的决策(50%对50%)。当然,只抛一次是不行的。小胖的策略是这样的:每一次决策,需要抛硬币两次,如果都是正面朝上或者都是反面朝上,那么就重新再做一次决策;如果是一正一反,那么如果第一次是正面朝上,就说抛了正面,如果第一次是反面朝上,那么就视为抛了反面。这样,就能得到一个公平的决策了。
现在问题是,给定一个p,小胖平均要抛多少次,才能得到一个决策呢(即不用再抛了)?
Input
第一行包含一个整数N(N<=100),表示测试数据的个数。
接下来包括N行,每行一个测试数据,包括一个3位的浮点数p(0<p<1)。
Output
对每一个测试数据,输出一行,包括一个浮点数,表示小胖抛硬币的平均次数。
结果保留两位小数。
Sample Input
3
0.500
0.800
0.300
0.500
0.800
0.300
Sample Output
4.00
6.25
4.76
6.25
4.76
Source
Author
zhanyu
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; double sum,p,q,tk; bool OK=false; void fun(double x,int n) { if(OK) return ; double tem=x*tk*n; if(tem<1e-9) return ; sum+=tem; fun(x*p*p+x*q*q,n+1); } int main() { int T; cin>>T; while(T--) { cin>>p; q=1-p; int cnt=1; tk=2*(2*p*q); sum=0;OK=false; fun(1,1); printf("%.2lf ",sum); } return 0; } |