(AtCoder Beginner Contest 333)--- F - Bomb Game 2 --- 题解

2024-02-14 23:04

本文主要是介绍(AtCoder Beginner Contest 333)--- F - Bomb Game 2 --- 题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

F - Bomb Game 2:

        题目大意:

 

思路解析:

         这道题一读题面就知道他是个概率dp,假如有 i 个人我们要输出这i个人每个人最后能够留下的概率。

        设状态dp[i][j] 表示当前有i个人,目标人物位于这i个人中的第j个位置。如果我们通过dp[i][j]往前推,让dp[1][1]为目标状态,这样一次dp就只能得到一个人的答案概率。

        但是如果我们让dp[1][1]为初始状态,dp[i][j]表示当前有i个人,目标人物位于这i个人中的第j个位置。然后转移过程改为有1/2的概率在当前队列的第一个位置加入一个人,有1/2的概率将当前队列的最后一个人移动到当前队列的队首。

        dp[i][j] = (dp[i-1][j-1] + dp[i][j-1) * 1/2 %mod;   这里*1/2需要用到逆元。 j>=2;

        dp[i][1] += dp[i-1][i-j+1] * pt[i][j]。j>=2 这里需要乘以 pt[i][j]的概率。

        例如 dp[3][1] += dp[2][2] * pt[3][2].

        dp[2][2] 代表当前有两个人目标人物在第二个, 添加一个人后变为当前有三个人,目标人物在第三个,我们需要通过移动来讲目标人物移动到第一个, pt[i][j] 就代表这次移动的概率。

代码实现:

        


import java.io.*;
import java.math.BigInteger;
import java.util.*;public class Main {static int mod = 998244353;public static void main(String[] args) throws IOException {int n = input.nextInt();long inv2 = pow(2);long[] p = new long[n+1];p[0] = 1;for (int i = 1; i <= n; i++) {p[i] = p[i - 1] * inv2 % mod;}long[][] pt = new long[n +1][n+1];for (int i = 2; i <= n; i++) {long mu = pow(((1 - p[i]) + mod) % mod);for (int j = 2; j <= i; j++) pt[i][j] = p[j] * mu % mod;}long[][] dp = new long[n + 1][n + 1];dp[1][1] = 1;for (int i = 2; i <= n; i++) {for (int j = 2; j <= i; j++) {dp[i][1] = (dp[i][1] + dp[i - 1][i - j + 1] * pt[i][j] % mod) % mod;}for (int j = 2; j <= i; j++) {dp[i][j] = (dp[i][j - 1] + dp[i-1][j - 1]) * inv2% mod;}}for (int i = 1; i <= n; i++) {out.print(dp[n][i] + " ");}out.flush();out.close();br.close();}public static long pow(long a){long b = mod - 2;long res = 1;while (b > 0){if ((b & 1) == 1) res = (res * a) % mod;a = (a * a) % mod;b >>= 1;}return res;}static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));static Input input = new Input(System.in);static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static class Input {public BufferedReader reader;public StringTokenizer tokenizer;public Input(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}public String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) {try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();}public String nextLine() {String str = null;try {str = reader.readLine();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}return str;}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public Double nextDouble() {return Double.parseDouble(next());}public BigInteger nextBigInteger() {return new BigInteger(next());}}}

        

这篇关于(AtCoder Beginner Contest 333)--- F - Bomb Game 2 --- 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/709819

相关文章

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

fzu 2275 Game KMP

Problem 2275 Game Time Limit: 1000 mSec    Memory Limit : 262144 KB  Problem Description Alice and Bob is playing a game. Each of them has a number. Alice’s number is A, and Bob’s number i

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>