首页 > 代码库 > UVA - 10886 - Standard Deviation(化简 + 暴力)
UVA - 10886 - Standard Deviation(化简 + 暴力)
题意:求如下函数产生的值的前 n 项的标准差(1 <= n <= 10000000,0 <= seed < 2^64).
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; }
写出方差的式子,然后展开分子所有平方项,合并,化简之后,方差即为
∑(xi^2) / n + (∑(xi) / n) ^ 2
标准差开根号即可。
复杂度为O(n),3s之内可以承受。
代码如下:
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> #define fin freopen("in.txt", "r", stdin) #define fout freopen("out.txt", "w", stdout) #define pr(x) cout << #x << " : " << x << " " #define prln(x) cout << #x << " : " << x << endl #define Min(a, b) a < b ? a : b #define Max(a, b) a < b ? b : a typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const ll MOD = 1e9 + 7; using namespace std; #define NDEBUG #include<cassert> const int MAXN = 100 + 10; const int MAXT = 10000 + 10; llu seed; int n, T; long double gen(){ static const long double Z = (long double)1.0 / (1ll << 32); seed >>= 16; seed &= (1llu << 32) - 1; seed *= seed; return seed * Z; } int main(){ int kase = 0; scanf("%d", &T); while(T--){ scanf("%d%llu", &n, &seed); double sum1 = 0.0, sum2 = 0.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; }
UVA - 10886 - Standard Deviation(化简 + 暴力)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。