CF923E Perpetual Subtraction

生成函数好题!

搬一手铃悬的题解(侵删)

现在只需要考虑怎么求出g和逆变换即可,其实也就是对函数F(x)求F(x+1)和F(x-1)。

直接二项式定理展开发现是个卷积的形式,大力NTT即可。

#include<bits/stdc++.h>
#define N 440000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
    char ch=0;
    int x=0,flag=1;
    while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*flag;
}
const int d=3,mo=998244353;
int ksm(int x,int k)
{
    int ans=1;
    while(k)
    {
        if(k&1)ans=1ll*ans*x%mo;
        k>>=1;x=1ll*x*x%mo;
    }
    return ans;
}
int rev[N];
void ntt(int *f,int n,int flag)
{
    for(int i=0;i<n;i++)
    {
        rev[i]=(rev[i>>1]>>1)+(i&1)*(n>>1);
        if(i<rev[i])swap(f[i],f[rev[i]]);
    }
    for(int k=2,kk=1;k<=n;k<<=1,kk<<=1)
    {
        int wn=ksm(d,(mo-1)/k);
        if(flag==-1)wn=ksm(wn,mo-2);
        for(int i=0;i<n;i+=k)
        for(int j=0,w=1;j<kk;j++,w=1ll*w*wn%mo)
        {
            int t=1ll*w*f[i+j+kk]%mo;
            f[i+j+kk]=(f[i+j]-t)%mo;
            f[i+j]=(f[i+j]+t)%mo;
        }
    }
    if(flag==-1)
    {
        int k=ksm(n,mo-2);
        for(int i=0;i<n;i++)f[i]=1ll*f[i]*k%mo;
    }
}
int a[N],b[N];
void mul(int len)
{
    ntt(a,len,+1);ntt(b,len,+1);
    for(int i=0;i<len;i++)a[i]=1ll*a[i]*b[i]%mo;
    ntt(a,len,-1);
}
int n,m,len,f[N],g[N],fac[N],vac[N];
int main()
{
    n=read();ll t;cin>>t;m=(t%(mo-1));len=1;
    while(len<2*(n+1))len<<=1;
    for(int i=0;i<=n;i++)f[i]=read();
    fac[0]=vac[0]=1;
    for(int i=1;i<=len;i++)fac[i]=1ll*fac[i-1]*i%mo;
    vac[len]=ksm(fac[len],mo-2);
    for(int i=len-1;i>=1;i--)vac[i]=1ll*vac[i+1]*(i+1)%mo;
    //get g(x)=f(x+1)
    for(int i=0;i<=n;i++)a[i]=1ll*f[i]*fac[i]%mo,b[i]=vac[i];
    for(int i=n+1;i<len;i++)a[i]=b[i]=0;
    reverse(a,a+n+1);mul(len);
    for(int i=0;i<=n;i++)g[i]=1ll*vac[i]*a[n-i]%mo;
    //solve get g*(x)
    for(int i=0;i<=n;i++)g[i]=1ll*ksm(ksm(i+1,m),mo-2)*g[i]%mo;
    //get f*(x)=g(x-1)
    for(int i=0;i<=n;i++)a[i]=1ll*g[i]*fac[i]%mo,b[i]=1ll*ksm(-1,i)*vac[i]%mo;
    for(int i=n+1;i<len;i++)a[i]=b[i]=0;
    reverse(a,a+n+1);mul(len);
    for(int i=0;i<=n;i++)f[i]=1ll*vac[i]*a[n-i]%mo;
    //print f(x)
    for(int i=0;i<=n;i++)printf("%d ",(f[i]%mo+mo)%mo);
    return 0;
}

原文地址:https://www.cnblogs.com/Creed-qwq/p/10787950.html

时间: 2024-10-08 13:22:29

CF923E Perpetual Subtraction的相关文章

Codeforces 947E Perpetual Subtraction (线性代数、矩阵对角化、DP)

手动博客搬家: 本文发表于20181212 09:37:21, 原地址https://blog.csdn.net/suncongbo/article/details/84962727 呜啊怎么又是数学了啊...数学比例\(\frac{16}{33}=0.4848\) orz yhx-12243神仙 题目链接: https://codeforces.com/contest/947/problem/E 题意: 有一个\([0,n]\)的随机数\(x\)初始为\(i\)的概率为\(p_i\). \(m

【CF947E】Perpetual Subtraction

大致内容都在rqy的blog 链接 这里解释一些步骤 “搞一个生成函数”从第 3 步到第 4 步 把分母的 $x-1$ 提到 $\sum$ 外,分子的 $f_j$ 不动. 然后剩下两项很烦,考虑简化. 我们把分子的两项分开,即看作 $\frac{x^{j+1}}{j+1} - \frac{1}{j+1}$ $\frac{x^{j+1}}{j+1}$ 的导数是 $x^j$,即函数 $f(j)=\frac{x^{j+1}}{j+1}$ 的导函数是 $f(j)=x^j$. 根据牛莱公式可知,导函数在

[cf 947E] Perpetual Subtraction

题意 现在有个正整数\(x\),你要进行\(m\)轮操作,每次将\(x\)随机变为\([0, x]\)中的一个整数. 问\(m\)轮之后,这个数为\(i(0 \leq i \leq x)\)的概率. 题解 考虑一个normaldp:设\(f_{i, j}\)表示第\(i\)轮后,这个数为\(j\)的概率,则: \[ f_{i, j} = \sum_{k \geq j} f_{i - 1, k} * \frac{1}{k + 1} \] 则可以写出转移矩阵: \[ T = \begin{bmatr

1050. String Subtraction (20)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1050. String Subtraction (20) 时间限制10 ms 内存限制65536 kB 代码长度限制16000 B Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2 for any giv

Dictionary subtraction

Finding the words from the book that are not in the word list from words.txt is a problem you might recognize as set subtraction; that is, we want to find all the words from one set (the words in the book) that are not in another set (the words in th

1050. String Subtraction

1050. String Subtraction (20) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calc

[OpenCV] Background subtraction

不错的草稿.但进一步处理是必然的,也是难点所在. http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html#gsc.tab=0 //opencv #include "opencv2/imgcodecs.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/videoio.hpp" #include <

codechef Subtraction Game 1题解

Subtraction Game 1 Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows. If all the numbers are equal, the game ends. Otherwise Select two numbers which are unequal Subtract the smaller number

[leetcode-592-Fraction Addition and Subtraction]

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2, you need to change