首页 > 代码库 > 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(化简 + 暴力)