题意:下面是一个随机数发生器。输入seed的初始值,你的任务是求出它得到的前n个随机数标准差,保留小数点后5位(1<=n<=10000000,0<=seed<264)。
分析:方差的式子最好化简一下再计算,否则按部就班,提前算出平均数,代入计算会产生精度误差导致WA。
化简方法:
将上式全部展开,并合并同类项,可得∑(xi2) / n - m2,m是平均数。求标准差,开根号即可。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b) { if(fabs(a - b) < eps) return 0; return a < b ? -1 : 1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1e7 + 10; const int MAXT = 10000 + 10; using namespace std; ULL seed; long double gen(){ static const long double Z = (long double)1.0 / (1LL << 32); seed >>= 16; seed &= (1ULL << 32) - 1; seed *= seed; return seed * Z; } int main(){ int T; scanf("%d", &T); int kase = 0; while(T--){ int n; scanf("%d%llu", &n, &seed); double sum1 = 0; double sum2 = 0; for(int i = 0; i < n; ++i){ double tmp = gen(); sum1 += tmp * tmp; sum2 += tmp; } sum1 /= n; sum2 /= n; printf("Case #%d: %.5lf\n", ++kase, sqrt(sum1 - sum2 * sum2)); } return 0; }