ABC300E Dice Product 3
题意
初始一个整数为
- 掷一次骰子,将初始给出的整数乘上你掷出来的数字。
求出最终得到
思路
先判无解。由于骰子只能掷出
我们令
第三种情况的式子有点怪,两边都有
然后就做完了,由于这个式子显然没有后效性,因此我们可以记忆化,但是由于要模上 map
状物来保存计算值。
代码
使用了 AtCoder 官方黑科技 atcoder_library
,用于简化逆元计算。这个代码写得还是有点复杂,其实还有很多地方可以简化。
#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; }
本文作者:ForgotDream
本文链接:https://www.cnblogs.com/forgot-dream/p/17369028.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步