【洛谷】P2000 拯救世界
题解
小迪的blog : https://www.cnblogs.com/RabbitHu/p/9178645.html
请大家点推荐并在sigongzi的评论下面点支持谢谢!
掌握了小迪生成函数的有趣姿势之后,我们考虑一下这个问题
由于出题人语死早,我们认为是十种石头的生成函数直接乘起来
\(\frac{1}{1 - x^6} \cdot \frac{1 - x^{10}}{1 - x} \cdot \frac{1 - x^{5}}{1 - x} \cdot \frac{1}{1 - x^4} \cdot \frac{1 - x^8}{1 - x} \cdot \frac{1}{1 - x^2} \cdot \frac{1 - x^2}{1 - x} \cdot \frac{1}{1 - x^8} \cdot \frac{1}{1 - x^10} \cdot \frac{1 - x^4}{1 - x} = \frac{1}{(1 - x)^5}\)
这个正好是上面那个blog里和组合数有关的生成函数,所以我们要求的值就是
\(\binom{n + 5 - 1}{5 - 1} = \binom{n + 4}{4} = \frac{(n + 1)(n + 2)(n + 3)(n + 4)}{24}\)
显然需要高精度了,还得是FFT
可以用NTT代替(因为值不会太大)
“我司嘉祺就算是TLE死,死外面,从这里跳下去,也绝对不会开O2的!”
“氧气真好吸。”
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define enter putchar('\n')
#define space putchar(' ')
#define MAXN 1000005
//#define ivorysi
#define pb push_back
using namespace std;
typedef long long int64;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 - '0' + c;
c = getchar();
}
res = res * f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
const int MOD = 998244353,MaxL = (1 << 20);
int W[(1 << 20) + 5],N;
char a[1000005];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
struct Bignum {
vector<int> v;
friend Bignum operator + (const Bignum &a,const int &x) {
Bignum c;c.v.clear();
int g = (a.v[0] + x) / 10;
c.v.pb((a.v[0] + x) % 10);
for(int i = 1 ; i < a.v.size() ; ++i) {
int t = a.v[i] + g;
c.v.pb(t % 10);g = t / 10;
}
if(g) c.v.pb(g);
return c;
}
friend void NTT(Bignum &f,int LEN,int on) {
f.v.resize(LEN);
for(int i = 1 , j = LEN / 2 ; i < LEN - 1 ; ++i) {
if(i < j) swap(f.v[i],f.v[j]);
int k = LEN / 2;
while(j >= k) {
j -= k;
k >>= 1;
}
j += k;
}
for(int h = 2 ; h <= LEN ; h <<= 1) {
int wn = W[(MaxL + on * MaxL / h) % MaxL];
for(int k = 0 ; k < LEN ; k += h) {
int w = 1;
for(int j = k ; j < k + h / 2 ; ++j) {
int u = f.v[j],t = mul(w,f.v[j + h / 2]);
f.v[j] = inc(u,t);
f.v[j + h / 2] = inc(u,MOD - t);
w = mul(w,wn);
}
}
}
if(on == -1) {
int InvL = fpow(LEN,MOD - 2);
for(int i = 0 ; i < LEN ; ++i) f.v[i] = mul(f.v[i],InvL);
}
}
friend Bignum operator * (Bignum a,Bignum b) {
int s = a.v.size() + b.v.size() - 2,t = 1;
while(t <= s) t <<= 1;
NTT(a,t,1);NTT(b,t,1);
Bignum c;c.v.clear();
for(int i = 0 ; i < t ; ++i) c.v.pb(mul(a.v[i],b.v[i]));
NTT(c,t,-1);
int64 x = 0;
for(int i = 0 ; i < t ; ++i) {
x += c.v[i];
c.v[i] = x % 10;
x /= 10;
}
while(x) {
c.v.pb(x % 10);
x /= 10;
}
for(int i = c.v.size() - 1 ; i > 0 ; --i) {
if(c.v[i] == 0) c.v.pop_back();
else break;
}
return c;
}
friend Bignum operator / (const Bignum &a,int k) {
Bignum c;c.v.clear();c.v.resize(a.v.size());
int x = 0;
for(int i = a.v.size() - 1 ; i >= 0 ; --i) {
x = x * 10 + a.v[i];
c.v[i] = x / k;
x %= k;
}
for(int i = a.v.size() - 1 ; i > 0 ; --i) {
if(c.v[i] == 0) c.v.pop_back();
else break;
}
return c;
}
void print() {
for(int i = v.size() - 1 ; i >= 0 ; --i) {
putchar('0' + v[i]);
}
}
}A,B;
void Solve() {
W[0] = 1;W[1] = fpow(3,(MOD - 1) / (1 << 20));
for(int i = 2 ; i < (1 << 20) ; ++i) W[i] = mul(W[i - 1],W[1]);
scanf("%s",a + 1);
N = strlen(a + 1);
A.v.clear();
for(int i = N ; i >= 1 ; --i) A.v.pb(a[i] - '0');
B = ((A + 1) * (A + 2)) * ((A + 3) * (A + 4));
B = B / 24;
B.print();enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
【洛谷】P2000 拯救世界的更多相关文章
- 洛谷P2000 拯救世界(生成函数)
题面 题目链接 Sol 生成函数入门题 至多为\(k\)就是\(\frac{1-x^{k+1}}{1-x}\) \(k\)的倍数就是\(\frac{1}{1-x^k}\) 化简完了就只剩下一个\(\f ...
- [洛谷P2000 拯救世界]
生成函数版题. 考虑对于这些条件写出\(OGF\) \(1 + x^6 + x^{12} + x^{18}..... = \frac{1}{1 - x^6}\) \(1 + x + x ^ 2 + x ...
- 洛谷 P1506 拯救oibh总部
P1506 拯救oibh总部 题目背景 oibh总部突然被水淹没了!现在需要你的救援…… 题目描述 oibh被突来的洪水淹没了>.<还好oibh总部有在某些重要的地方起一些围墙,用*号表示 ...
- luogu P2000 拯救世界
嘟嘟嘟 题目有点坑,要你求的多少大阵指的是召唤kkk的大阵数 * lzn的大阵数,不是相加. 看到这个限制条件,显然要用生成函数推一推. 比如第一个条件"金神石的块数必须是6的倍数" ...
- 洛谷P4823 拯救小矮人 [TJOI2013] 贪心+dp
正解:贪心+dp 解题报告: 传送门! 我以前好像碰到过这题的说,,,有可能是做过类似的题qwq? 首先考虑这种显然是dp?就f[i][j]:决策到了地i个人,跑了j个的最大高度,不断更新j的上限就得 ...
- 洛谷 P1506 拯救oibh总部【DFS/Flood Fill】
题目背景 oibh总部突然被水淹没了!现在需要你的救援…… 题目描述 oibh被突来的洪水淹没了>.<还好oibh总部有在某些重要的地方起一些围墙,用号表示,而一个封闭的号区域洪水是进不去 ...
- luogu P2000 拯救世界 生成函数_麦克劳林展开_python
模板题. 将所有的多项式按等比数列求和公式将生成函数压缩,相乘后麦克劳林展开即可. Code: n=int(input()) print((n+1)*(n+2)*(n+3)*(n+4)//24)
- 【做题笔记】洛谷P1506 拯救oibh总部
跑一遍染色法,最后判断哪些位置没被染色即可 一些技巧: 为了判断方便,把字符转换成 int 型的数字. 注意边界问题 详细解释见代码 #include <iostream> #includ ...
- [题解] Luogu P2000 拯救世界
生成函数板子题...... 要写高精,还要NTT优化......异常dl 这个并不难想啊...... 一次召唤会涉及到\(10\)个因素,全部写出来,然后乘起来就得到了答案的生成函数,输出\(n\)次 ...
随机推荐
- webapi框架搭建-安全机制(二)-身份验证
webapi框架搭建系列博客 身份验证(authentication)的责任是识别出http请求者的身份,除此之外尽量不要管其它的事.webapi的authentication我用authentica ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...
- BFS搜索:POJ No 3669 Meteor Shower
#include <iostream> #include <cstring> #include <queue> #include <cstdio> #i ...
- [Luogu 3952] NOIP2017 时间复杂度
[Luogu 3952] NOIP2017 时间复杂度 一年的时间说长不长,说短,也不短. 一年之内无数次觉得难得可怕的题目,原来也就模拟这么回事儿. #include <cstdio> ...
- Mac下crontab定时python任务
1.新建crontab_file vim输入代码*/ * * * * /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Us ...
- CSS3 颜色模式
HSL .test{background-color:hsl(<length>, <percentage>, <percentage>);} 1.length 表示 ...
- 监控redis数据库应用状态:python,tornado实现
公司里最近redis服务器压力越来越大,其大概情况,只能从操作系统层面看,并不详尽,故同事在网上找了一个叫做 redis-live的开源项目,我配合部署了一下,还真有点意思,并解决了其中部分小debu ...
- 网摘关于BarCodeControl控件
简介 BarCodeControl是一个用户制作条形码的控件. MicrosoftBarcodeControl9.0是可以在MicrosoftOfficeAccess窗体和报表中显示条码符号的Acti ...
- 【leetcode 简单】 第一百一十题 分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有一个尺寸 ...
- Dynamic Rankings(动态第k大+树套树)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题目: 思路: 树套树板子题. 代码实现如下: #inclu ...