首页 > 其他分享 >ABC300E Dice Product 3

ABC300E Dice Product 3

时间:2023-05-03 14:33:46浏览次数:44  
标签:std Product ABC300E frac Dice 2x 5x copy cur

题意

初始一个整数为 \(1\),你有一个可以等概率地掷出 \(1\) 至 \(6\) 这六个数值的骰子,再给定一个整数 \(N\),当初始给出的整数严格小于 \(N\) 时,重复以下操作:

  • 掷一次骰子,将初始给出的整数乘上你掷出来的数字。

求出最终得到 \(N\) 的概率模上 \(998244353\) 的值。

思路

先判无解。由于骰子只能掷出 \(1\) 至 \(6\) 的值,而 \([1, 6]\) 中共有三个质数 \(2\),\(3\) 与 \(5\),因此如果一个整数能由以上操作得到,则其一定能被分解为 \(2^{a}3^{b}5^{c}\) 的形式,否则无论如何也不能得到。

我们令 \(P(x)\) 为当前数为 \(x\) 时,最终得到 \(N\) 的概率。不难得到当当前得到的数字已经比 \(N\) 还大时,得到 \(N\) 的概率为 \(0\),而当 \(x = N\),即我们已得到 \(N\) 时,概率为 \(1\)。而当当前数字比 \(N\) 小时,根据题意,需要再掷一次骰子并将当前得到的数乘上投出来的数。于是我们可以写出如下的方程:

\[P(x) = \begin{cases} 0 & x \gt N \\ 1 & x = N \\ \frac{P(x) + P(2x) + P(3x) + P(4x) + P(5x) + P(6x)}{6} & x \lt N \end{cases} \]

第三种情况的式子有点怪,两边都有 \(P(x)\),因此我们考虑移项:

\[\begin{align*} P(x) &= \frac{P(x)}{6} + \frac{P(2x) + P(3x) + P(4x) + P(5x) + P(6x)}{6} \\ \frac{5P(x)}{6} &= \frac{P(2x) + P(3x) + P(4x) + P(5x) + P(6x)}{6} \\ P(x) &= \frac{P(2x) + P(3x) + P(4x) + P(5x) + P(6x)}{5} \end{align*} \]

然后就做完了,由于这个式子显然没有后效性,因此我们可以记忆化,但是由于要模上 \(998244353\),因此不能使用普通的数组,而是要用 map 状物来保存计算值。

代码

使用了 AtCoder 官方黑科技 atcoder_lib,用于简化逆元计算。这个代码写得还是有点复杂,其实还有很多地方可以简化。

#include <atcoder/modint.hpp>
#include <bits/stdc++.h>

using i64 = long long;
using f80 = long double;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  i64 n;
  std::cin >> n;

  i64 copy = n;
  std::vector<int> factor(6);
  for (auto i : {5, 3, 2}) {
    while (copy % i == 0) {
      copy /= i;
      factor[i]++;
    }
    if (copy == 1) {
      break;
    }
  }

  if (copy != 1) {
    std::cout << 0 << "\n";
    return 0;
  }

  std::map<i64, atcoder::modint998244353> mp;

  std::function<atcoder::modint998244353(i64)> dfs = [&](i64 cur) -> atcoder::modint998244353 {
    if (cur == n) {
      return 1;
    } else if (cur > n) {
      return 0;
    } else if (mp.count(cur)) {
      return mp[cur];
    }

    atcoder::modint998244353 res = 0;
    for (int i = 2; i <= 6; i++) {
      res += dfs(i * cur) / 6;
    }

    return mp[cur] = res * atcoder::modint998244353(6) / atcoder::modint998244353(5);
  };

  std::cout << dfs(1).val() << "\n";

  return 0;
}

标签:std,Product,ABC300E,frac,Dice,2x,5x,copy,cur
From: https://www.cnblogs.com/forgot-dream/p/17369028.html

相关文章

  • As a restaurant owner, write a professional email to the supplier to get these p
    Asarestaurantowner,writeaprofessionalemailtothesuppliertogettheseproductseveryweek:Wine(x10)Eggs(x24)Bread(x12)DearSupplier,Ihopethismessagefindsyouwell.Mynameis[YourName],andIamwritingonbehalfofmyrestaurant......
  • 向量点积dot,叉积cross product
    点积概括地说,向量的内积(点乘/数量积)。对两个向量执行点乘运算,就是对这两个向量对应位一一相乘之后求和的操作,点乘的结果是一个标量(数量而不是向量)点积(点乘)的几何意义包括:表征或计算两个向量之间的夹角b向量在a向量方向上的投影叉积两个向量的外积,又叫叉乘、叉积向量积,其运......
  • Selling Products
    AsalespersonmustsellnitemsinabagwithrandomIDs.Thesalespersoncanremoveasmanyasmitemsfromthebag.DeterminetheminimumnumberofdifferentIDsthefinalbagcancontainafterperforming,atmost,mdeletions.Examplen=6ids=[1,1......
  • 简单实现可以多选的ProductListDialog<T>
    只是一个范例,是为了代码快速迭代而写的使用了listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);效果图importjava.util.ArrayList;importjava.util.List;importandroid.app.Dialog;importandroid.content.Context;importandroid.os.Bundle......
  • [LeetCode] 1339. Maximum Product of Splitted Binary Tree 分裂二叉树的最大乘积
    Giventhe root ofabinarytree,splitthebinarytreeintotwosubtreesbyremovingoneedgesuchthattheproductofthesumsofthesubtreesismaximized.Return themaximumproductofthesumsofthetwosubtrees.Sincetheanswermaybetoolarge,re......
  • magento 在产品页添加评论 Add Review Form in Magento Product View Page
    Magento产看产品评论需要点击到另外一个页面中,这种设计对于用户体验和SEO都相当不利。一方面用户无法在产品页面查看该产品的一些用户评价,另外,搜索引擎也会收录很多与产品无关的页面。那么如何让产品评论直接显示在产品页面呢?我们需要修改一下模板文件,很简单即可实现。 首先,在lay......
  • Navigate your way to production bliss with Caretta
    https://www.cncf.io/blog/2023/01/23/navigate-your-way-to-production-bliss-with-caretta/Guestpostoriginallypublishedon groundcover’sblog byUdiRotGet......
  • Magento : Make 'Continue Shopping' button redirect to the product index page
    Magento:Make'ContinueShopping'buttonredirecttothelast-added-to-cartproduct'scategory Editcart.phtmlandreplacefollowingcode<?php......
  • Version 1.5.0_07 of the JVM is not suitable for this product. Version: 1.6 or gr
    在今天启动Eclipse的时候遇到一个Version1.5.0_07oftheJVMisnotsuitableforthisproduct.Version:1.6orgreaterisrequired.的错误,我尝试着到eclipse安装路......
  • 【CF1009F Dominant Indices】(长链剖分)
    原题链接题意给定一棵以\(1\)为根,\(n\)个节点的树。设\(d(u,x)\)为\(u\)子树中到\(u\)距离为\(x\)的节点数。对于每个点,求一个最小的\(k\),使得\(d(u,k)\)......