UVA - 10886 Standard Deviation (标准差)(数论)

题意:下面是一个随机数发生器。输入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;
}

  

转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/6382780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值