1 1 2 3
1/2 6/5HintCase 1: S=‘01‘ or S=‘10‘, so the expected times = 1/2 = 1/2 Case 2: S=‘00011‘ or S=‘00101‘ or S=‘00110‘ or S=‘01001‘ or S=‘01010‘ or S=‘01100‘ or S=‘10001‘ or S=‘10010‘ or S=‘10100‘ or S=‘11000‘, so the expected times = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5
用next_permutation将所有的全排列都打印出来,然后找结果,打表(反正结果也不大 12*12);
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> const int M = 1e2+5; const int INF = 0x3f3f3f3f; using namespace std; char str[15]; int c[12][12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 3, 12, 30, 60, 105, 168, 252, 360, 495, 660, 858, 1092, 4, 20, 60, 140, 280, 504, 840, 1320, 1980, 2860, 4004, 5460, 5, 30, 105, 280, 630, 1260, 2310, 3960, 6435, 10010, 15015, 21840, 6, 42, 168, 504, 1260, 2772, 5544, 10296, 18018, 30030, 48048, 74256, 7, 56, 252, 840, 2310, 5544, 12012, 24024, 45045, 80080, 136136, 222768, 8, 72, 360, 1320, 3960, 10296, 24024, 51480, 102960, 194480, 350064, 604656, 9, 90, 495, 1980, 6435, 18018, 45045, 102960, 218790, 437580, 831402, 1511640, 10, 110, 660, 2860, 10010, 30030, 80080, 194480, 437580, 923780, 1847560, 3527160, 11, 132, 858, 4004, 15015, 48048, 136136, 350064, 831402, 1847560, 3879876, 7759752, 12, 156, 1092, 5460, 21840, 74256, 222768, 604656, 1511640, 3527160, 7759752, 16224936}; int d[12][12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 5, 15, 35, 70, 126, 210, 330, 495, 715, 1001, 1365, 1820, 6, 21, 56, 126, 252, 462, 792, 1287, 2002, 3003, 4368, 6188, 7, 28, 84, 210, 462, 924, 1716, 3003, 5005, 8008, 12376, 18564, 8, 36, 120, 330, 792, 1716, 3432, 6435, 11440, 19448, 31824, 50388, 9, 45, 165, 495, 1287, 3003, 6435, 12870, 24310, 43758, 75582, 125970, 10, 55, 220, 715, 2002, 5005, 11440, 24310, 48620, 92378, 167960, 293930, 11, 66, 286, 1001, 3003, 8008, 19448, 43758, 92378, 184756, 352716, 646646, 12, 78, 364, 1365, 4368, 12376, 31824, 75582, 167960, 352716, 705432, 1352078, 13, 91, 455, 1820, 6188, 18564, 50388, 125970, 293930, 646646, 1352078, 2704156}; /*int f(char s[], int len){ int ans = 0; for(int i = 1; i < len; ++ i) if(s[i] == '1'&&s[i-1] == '0') ++ans; return ans; } bool cmp(char a, char b){ return a < b; }*/ int gcd(int a, int b){ if(a < b) swap(a, b); if(b == 1) return 1; int t = a%b; while(t){ a = b; b = t; t = a%b; } return b; } int main(){ int n, m; while(cin >> n >>m){ int temp = gcd(d[n-1][m-1], c[n-1][m-1]); printf("%d/%d\n", c[n-1][m-1]/temp, d[n-1][m-1]/temp); } /*freopen("D:\\hah.txt", "w", stdout); //打表; for(int i = 1; i <= 12; ++ i){ for(int j = 1; j <= 12; ++ j){ int t = 0, s = i; while(s --){ str[t++] = '0'; } s = j; while(s --){ str[t++] = '1'; } str[t] = '\0'; c[i][j] = f(str, i+j); d[i][j] = 1; while(next_permutation(&str[0], &str[i+j], cmp)){ //temp1++; ++d[i][j]; c[i][j] += f(str, i+j); } //d[i][j] = temp1; //cout << i << " "<< j <<" " <<c[i][j] << "\n"; } } int t = 0; for(int i = 1; i <= 12; ++ i){ for(int j = 1; j <= 12; ++ j){ cout << c[i][j] <<", "; t++; if(t %12 == 0) cout << "\n"; } } cout << endl <<endl <<endl; t = 0; for(int i = 1; i <= 12; ++ i){ for(int j = 1; j <= 12; ++ j){ cout << d[i][j] <<", "; t++; if(t %12 == 0) cout << "\n"; } } fclose(stdout);*/ return 0; }
Hdoj 5194 DZY Loves Balls 【打表】+【STL】
原文地址:http://blog.csdn.net/shengweisong/article/details/44758695