算概率(dp,数论)
链接:https://ac.nowcoder.com/acm/contest/3003/C
来源:牛客网
题目描述
不过,牛牛知道第 i 道题的正确率是 pi。
牛牛 想知道这 n 题里恰好有 0,1,…,n 题正确的概率分别是多少,对 109+7取模。
对 109+7取模的含义是:对于一个 b≠0的不可约分数 a/b,存在 q 使得 b×q mod (109+7)=a,q 即为 a/b 对 109+7取模的结果。
输入描述:
第一行,一个正整数 n 。
第二行,n 个整数 p1,p2,…,pn,在模 109+7意义下给出。
保证 1≤n≤2000。
输出描述:
输出一行 n+1个用空格隔开的整数表示答案(对 109+7取模)。
输入
输出
说明
有 1 道题,做对的概率是 1/2 ( 1/2在模 109+7意义下为 500000004 )。
先说一点:
mod为109+7,为质数
inv(b,mod) 表示b对mod的逆元,由费马小定理知b对mod的逆元即为bmod-2。
求(a/b)%mod即为求(a*inv(b,mod))%mod,即等于该题的p。
long long fpow(long long a, long long b)
{
if (a == ) return ;
long long ans = ;
for (; b; b >>= , a = (a % mod * a% mod) % mod)
if (b & ) ans = (ans % mod * a % mod) % mod;
return ans;
}
LL inv(LL a,LL mo)
{
return fpow(a,mo-)%mo;
}
//求(a/b)%mod: printf("%lld\n",a*inv(b,mod)%mod);
另外注意到(a/b)%mod+(1- a/b)%mod = mod+1,由此可通过某事件概率求得其对立概率。
该题总体来说还是用dp来做,只不过用概率%mod(即题中p)来代替原本的概率即可。
dp[ i ][ j ] 表示前 i 道题做对 j 道的概率。
转移时考虑第 j 道题是否做对,转移方程为:dp[ i ][ j ]=dp[ i-1 ][ j ]×(1−pi)+dp[ i-1 ][ j-1 ]×pi 。
时间复杂度 O(n2)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const LL mod=1e9+;
const int maxn=1e4+;
using namespace std; LL P[];//表示第i道题做对的概率
LL dp[][];//dp[i][j]表示前i道题做对j道的概率 int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&P[i]);
dp[][]=;
for (int i = ; i <= n; i++)
{
dp[i][] = dp[i - ][] * (mod + - P[i]) % mod;
for (int j = ; j <= i; ++j)
dp[i][j] = (dp[i - ][j] * (mod + - P[i]) + dp[i - ][j - ] * P[i]) % mod;
}
for(int i=;i<=n;i++)
printf(i==n?"%lld\n":"%lld ",dp[n][i]); return ;
}
-
算概率(dp,数论)的更多相关文章
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- HDU 4599 概率DP
先推出F(n)的公式: 设dp[i]为已经投出连续i个相同的点数平均还要都多少次才能到达目标状态. 则有递推式dp[i] = 1/6*(1+dp[i+1]) + 5/6*(1+dp[1]).考虑当前这 ...
- poj 2096 Collecting Bugs (概率dp 天数期望)
题目链接 题意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcom ...
- HDU 4599 Dice (概率DP+数学+快速幂)
题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...
- poj 2151 Check the difficulty of problems(概率dp)
poj double 就得交c++,我交G++错了一次 题目:http://poj.org/problem?id=2151 题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 问 ...
- POJ2151-Check the difficulty of problems(概率DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4512 ...
- 概率dp专辑
求概率 uva11021 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- HDU5985 Lucky Coins 概率dp
题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...
- 概率DP求解例题
1,逆推状态:山东省赛2013年I题 Problem I: The number of steps Description Mary stands in a strange maze, the maz ...
- 洛谷P3158 放棋子 [CQOI2011] dp+数论
正解:dp+数论 解题报告: 传送门! 考虑对每种颜色的棋子单独考虑鸭,那显然有,当某一行或某一列已经被占据的时候,那一行/一列就不能再放别的颜色的棋子了,相当于直接把那一行/一列直接消了 显然就能考 ...
随机推荐
- Git删除无效远程分支
当远程分支删除的时候,本地同步下来的远程分支却不会删除,久而久之,本地积累了不少无效的远程分支.找了一下,可以用如下命令清除它们: git remote update -p
- gem5-gpu 运行 PARSEC2.1
PARSEC是针对共享内存多核处理器(CPU)的一套基准测试程序,详细介绍见wiki:http://wiki.cs.princeton.edu/index.php/PARSEC,主要参考:http:/ ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-download
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- C. Basketball Exercise dp
C. Basketball Exercise time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 剑指offer自学系列(一)
题目描述:输入n个整数,找出其中最小的k个数,例如,输入{4,5,1,6,2,7,3,8}这8个数字,最小的4个数字是1,2,3,4 题目分析:首先我能想到的是先对数组排序,从小到大,然后直接输出想要 ...
- Python3 格式化输出
Python3 格式化输出 今天用字符串功能的时候,我突然忘记了格式化输出的方式X﹏X.所以赶紧恶补一下. 1.打印字符串 print("My name is %s" %(&quo ...
- 【pwnable.kr】lotto
pwnable.好像最近的几道题都不需要看汇编. ssh lotto@pwnable.kr -p2222 (pw:guest) 直接down下来源码 #include <stdio.h> ...
- 5 —— node —— 响应一段中文给客户端
const http = require('http'); const server = http.createServer(); server.on('request',function(req,r ...
- cf 785#
23333再次水惨了..(心酸啊) A题呵呵呵呵呵 #include<cstdio> #include<iostream> using namespace std; int m ...
- 一次C语言编程遇到的问题总结
今天用C语言做了一个简单的用户登录注册存取款等功能的系统,发现有很多功能并不会实现,大概是使用Java太多了导致许多C的知识都忘记了,现在把碰到的问题总结如下: 1.字符串复制问题 java等一些编程 ...