|
| 1 | +// BEGIN CUT HERE |
| 2 | + |
| 3 | +// END CUT HERE |
| 4 | +#include <algorithm> |
| 5 | +#include <iostream> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | +#include <queue> |
| 10 | +#include <set> |
| 11 | +#include <map> |
| 12 | +#include <cstdio> |
| 13 | +#include <cstdlib> |
| 14 | +#include <cctype> |
| 15 | +#include <cmath> |
| 16 | +#include <string> |
| 17 | +#include <cstring> |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +// BEGIN CUT HERE |
| 21 | +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) |
| 22 | + |
| 23 | +template<typename T> void print( T a ) { |
| 24 | + cerr << a; |
| 25 | +} |
| 26 | +static void print( long long a ) { |
| 27 | + cerr << a << "L"; |
| 28 | +} |
| 29 | +static void print( string a ) { |
| 30 | + cerr << '"' << a << '"'; |
| 31 | +} |
| 32 | +template<typename T> void print( vector<T> a ) { |
| 33 | + cerr << "{"; |
| 34 | + for ( int i = 0 ; i != a.size() ; i++ ) { |
| 35 | + if ( i != 0 ) cerr << ", "; |
| 36 | + print( a[i] ); |
| 37 | + } |
| 38 | + cerr << "}" << endl; |
| 39 | +} |
| 40 | +template<typename T> void eq( int n, T have, T need ) { |
| 41 | + if ( have == need ) { |
| 42 | + cerr << "Case " << n << " passed." << endl; |
| 43 | + } else { |
| 44 | + cerr << "Case " << n << " failed: expected "; |
| 45 | + print( need ); |
| 46 | + cerr << " received "; |
| 47 | + print( have ); |
| 48 | + cerr << "." << endl; |
| 49 | + } |
| 50 | +} |
| 51 | +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { |
| 52 | + if( have.size() != need.size() ) { |
| 53 | + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements."; |
| 54 | + print( have ); |
| 55 | + print( need ); |
| 56 | + return; |
| 57 | + } |
| 58 | + for( int i= 0; i < have.size(); i++ ) { |
| 59 | + if( have[i] != need[i] ) { |
| 60 | + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "."; |
| 61 | + print( have ); |
| 62 | + print( need ); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + cerr << "Case " << n << " passed." << endl; |
| 67 | +} |
| 68 | +static void eq( int n, string have, string need ) { |
| 69 | + if ( have == need ) { |
| 70 | + cerr << "Case " << n << " passed." << endl; |
| 71 | + } else { |
| 72 | + cerr << "Case " << n << " failed: expected "; |
| 73 | + print( need ); |
| 74 | + cerr << " received "; |
| 75 | + print( have ); |
| 76 | + cerr << "." << endl; |
| 77 | + } |
| 78 | +} |
| 79 | +// END CUT HERE |
| 80 | + |
| 81 | +#define REP(i,n) for(int i=0;i<(n);++i) |
| 82 | +#define FOR(i,a,b) for(int i=(a);i<=(b);++i) |
| 83 | +#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) |
| 84 | +#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it) |
| 85 | +#define CLR(x) memset((x),0,sizeof((x))) |
| 86 | +typedef long long LL; |
| 87 | +typedef vector<int> VI; |
| 88 | +typedef vector<string> VS; |
| 89 | + |
| 90 | +// BEGIN CUT HERE |
| 91 | +vector<string> split( const string& s, const string& delim =" " ) { |
| 92 | + vector<string> res; |
| 93 | + string t; |
| 94 | + for ( int i = 0 ; i != s.size() ; i++ ) { |
| 95 | + if ( delim.find( s[i] ) != string::npos ) { |
| 96 | + if ( !t.empty() ) { |
| 97 | + res.push_back( t ); |
| 98 | + t = ""; |
| 99 | + } |
| 100 | + } else { |
| 101 | + t += s[i]; |
| 102 | + } |
| 103 | + } |
| 104 | + if ( !t.empty() ) { |
| 105 | + res.push_back(t); |
| 106 | + } |
| 107 | + return res; |
| 108 | +} |
| 109 | + |
| 110 | +vector<int> splitInt( const string& s, const string& delim =" " ) { |
| 111 | + vector<string> tok = split( s, delim ); |
| 112 | + vector<int> res; |
| 113 | + for ( int i = 0 ; i != tok.size(); i++ ) |
| 114 | + res.push_back( atoi( tok[i].c_str() ) ); |
| 115 | + return res; |
| 116 | +} |
| 117 | +// END CUT HERE |
| 118 | + |
| 119 | +// BEGIN CUT HERE |
| 120 | +int s2i(string s) { |
| 121 | + stringstream ss; |
| 122 | + ss << s; |
| 123 | + int res; |
| 124 | + ss >> res; |
| 125 | + return res; |
| 126 | +} |
| 127 | + |
| 128 | +string i2s(int n) { |
| 129 | + stringstream ss; |
| 130 | + ss << n; |
| 131 | + string res; |
| 132 | + ss >> res; |
| 133 | + return res; |
| 134 | +} |
| 135 | +// END CUT HERE |
| 136 | + |
| 137 | +int mm[2500]; |
| 138 | +double vv[2500]; |
| 139 | + |
| 140 | +class TheSwapsDivOne { |
| 141 | +public: |
| 142 | + double find(vector <string> sequence, int k) { |
| 143 | + string str = ""; |
| 144 | + REP(i,sequence.size()) str += sequence[i]; |
| 145 | + int n = str.length(); |
| 146 | + REP(i,n) mm[i] = str[i] - '0'; |
| 147 | + |
| 148 | + double pn = (n - 2) * 1.0 / n, pc = 1 - pn; |
| 149 | + double p1 = 1.0, p2 = 1 - p1; |
| 150 | + FOR(i,1,k) { |
| 151 | + p1 = p1 * pn + p2 * pc / (n - 1); |
| 152 | + p2 = 1 - p1; |
| 153 | + } |
| 154 | + |
| 155 | + double sum = 0; |
| 156 | + REP(i,n) { |
| 157 | + vv[i] = (i + 1) * (n - i); |
| 158 | + sum += vv[i]; |
| 159 | + } |
| 160 | + |
| 161 | + double total = 0.0; |
| 162 | + REP(i,n) total += mm[i] * ((sum - vv[i]) * p2 / (n - 1) + vv[i] * p1); |
| 163 | + return total / (n * (n - 1) / 2 + n); |
| 164 | + } |
| 165 | +}; |
| 166 | +// BEGIN CUT HERE |
| 167 | +int main() { |
| 168 | + { |
| 169 | + string sequenceARRAY[] = {"4", "77"}; |
| 170 | + vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) ); |
| 171 | + TheSwapsDivOne theObject; |
| 172 | + eq(0, theObject.find(sequence, 1),10.0); |
| 173 | + } |
| 174 | + { |
| 175 | + string sequenceARRAY[] = {"4", "77"}; |
| 176 | + vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) ); |
| 177 | + TheSwapsDivOne theObject; |
| 178 | + eq(1, theObject.find(sequence, 47),10.0); |
| 179 | + } |
| 180 | + { |
| 181 | + string sequenceARRAY[] = {"1", "1", "1", "1", "1", "1", "1"}; |
| 182 | + vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) ); |
| 183 | + TheSwapsDivOne theObject; |
| 184 | + eq(2, theObject.find(sequence, 1000000),3.0); |
| 185 | + } |
| 186 | + { |
| 187 | + string sequenceARRAY[] = {"572685085149095989026478064633266980348504469", "19720257361", "9", "69"}; |
| 188 | + vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) ); |
| 189 | + TheSwapsDivOne theObject; |
| 190 | + eq(3, theObject.find(sequence, 7),98.3238536775161); |
| 191 | + } |
| 192 | + return 0; |
| 193 | +} |
| 194 | +// END CUT HERE |
0 commit comments