• Codeforces Round #341 (Div. 2)--C. Wet Shark and Flowers


     

      题目连接:  http://codeforces.com/contest/621/problem/C

      题意:给你n个区间和一个素数p; 让你从每个区间里选一个数; 从这n个数中若有相邻的两个数乘积能整除p,则要给2000。求给的钱的数学期望。

      解题: 首先,因为p为素数,则在相邻的两个数乘积要能整除p就需要其中一个数必须为p的整倍数。 然后,, 在一个区间 [l,r] 里能整除p的个数为 (r/p)-(l-1)/p, 那么在这个区间里取一个数能整除p的概率就为 ((r/p)-(l-1)/p)/(r-l+1)。  这样,每个区间的概率就求出来了,但我们是要知道区间的乘积能整除p的概率; 刚刚求出来的是区间i能整除p的概率即p(i),,而我需要的是每组相邻区间能整除p的概率即p[ iU(i+1) ] (U是指或运算...),然后根据概率论公式p[ iU(i+1) ] =p(i)+p(i+1)-p(i)*p(i+1); 这样把n个区间扫一遍,得出每一个p[ iU(i+1) ]然后再乘上2000,,所得的和就是期望了。

      

      附上代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 #include <utility>
    10 #include <map>  //GL&HF
    11 #include <set>
    12 typedef long long ll;
    13 const int inf=0x3f3f3f3f;
    14 const double PI=acos(-1.0);
    15 const double EPS=1e-8;
    16 using namespace std;
    17 typedef pair<int,int> P;
    18 
    19 int n;
    20 typedef struct node
    21 {
    22     ll l,r;
    23     double num;
    24 }node;
    25 node a[100010];
    26 ll p;
    27 int main()
    28 {
    29     //freopen("input.txt","r",stdin);
    30     scanf("%d%I64d",&n,&p);
    31     for(int i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].l,&a[i].r);
    32 
    33     for(int i=1;i<=n;i++)
    34         a[i].num=(double)( a[i].r/p-(a[i].l-1)/p )/ (double)( a[i].r-a[i].l+1 );
    35 
    36     double sum=0;
    37     for(int i=1;i<n;i++)
    38         sum+=( a[i].num+a[i+1].num-a[i].num*a[i+1].num );
    39 
    40     sum+=( a[1].num+a[n].num-a[1].num*a[n].num );
    41 
    42     printf("%.8f
    ",sum*2000.0);
    43     return 0;
    44 }
  • 相关阅读:
    Linux下处理文件中的^M
    python selenium-webdriver 生成测试报告 (十四)
    Apache 配置Https 转发Tomcat Http
    自动化测试神器 之 python unittest 断言
    创建高性能索引笔记
    【转】正向代理vs反向代理
    vue 常用问题
    eslint 代码规范2
    WebStrom2018注册码
    Vue-selller 饿了吗
  • 原文地址:https://www.cnblogs.com/geek1116/p/5866045.html
Copyright © 2020-2023  润新知