Rokua P4233: Shot pill Notes (counting principle + tolerant principle + polynomial seeking inverse/sub-treatment NTT)

Source: Internet
Author: User
Tags pow printf

Topic Portal: https://www.luogu.org/problemnew/show/P4233

Topic Analysis: This problem is I play the game when I saw, but at that time did not want to come out, even some points are not bothered to write Qaq. Later I analyzed why I always can not think of the NTT's topic, found that it is their own count, tolerance, DP learning too bad, the province before the game or to find a time to fill a supplement.

Others have written a very detailed explanation, I will not be wordy: the official portal

I add: This question and BZOJ3456 city planning that the problem is very similar, but this problem is enumerated in the topology map of the smallest of the strong connected components, and finally introduced a similar formula:

G[N]=∑K=1NF[K]∗G[N−K] G [n] =∑k = 1 n F [k]∗g [n−k] g[n]=\sum_{k=1}^{n}f[k]*g[n-k]

Known g, requires F. Then there is a new posture of polynomial inversion: Notice that this is probably a f*g=g form, and that f is a meta-function. But the Nether of K here is 1. In order for the Nether of K to become 0, we force the f[0]=0 to be made, then a (x) =g (x) (x>0), a (0) =0 A (x) = G (x) (x > 0), a (0) = 0 A (x) =g (x) (x>0), a (0) = 0, So there is A=f*g (G[0] Originally 1, but because F[0] is forced to 0, so a[0] must be 0). Then use polynomial inversion, time complexity O (Nlog (n)) O (n log (n)) O (N\log (n)).

CODE:

#include <iostream> #include <string> #include <cstring> #include <cmath> #include <cstdio

> #include <cstdlib> #include <stdio.h> #include <algorithm> using namespace std;
const int maxn=1000000;
Const long Long m=998244353;
Const long Long g=3;

typedef long Long LL;
LL A[MAXN];

LL B[MAXN];
int REV[MAXN];

int N,LG;
LL F[MAXN];

LL G[MAXN];
LL FAC[MAXN];
LL NFAC[MAXN];

int n;
    ll Pow (ll X,ll y) {if (!y) return 1LL;
    LL Temp=pow (x,y>>1);
    temp=temp*temp%m;
    if (y&1) temp=temp*x%m;
return temp;

    } void DFT (LL *a,int f) {for (int i=0; i<n; i++) if (I<rev[i]) swap (a[i],a[rev[i]);
        for (int len=2; len<=n; len<<=1) {int mid= (len>>1);
        LL E=pow (g, (M-1)/len);

        if (f==-1) E=pow (e,m-2);
            for (ll *p=a; p!=a+n; p+=len) {ll wn=1;
           for (int i=0; i<mid; i++) {LL temp=wn*p[mid+i]%m;     p[mid+i]= (p[i]-temp+m)%M;
                p[i]= (p[i]+temp)%M;
            wn=wn*e%m;
        }}}} void NTT (bool rev) {for (int i=0; i<n; i++) {rev[i]=0;
    for (int j=0; j<lg; J + +) if (i& (1<<j)) rev[i]|= (1<< (lg-j-1));
    } DFT (a,1);
    DFT (b,1);
    if (rev.) for (int i=0; i<n; i++) a[i]=a[i]*a[i]%m;
    for (int i=0; i<n; i++) a[i]=a[i]*b[i]%m;

    DFT (a,-1);
    LL Inv=pow (n,m-2);
for (int i=0; i<n; i++) a[i]=a[i]*inv%m;
    } void Poly_rev (int m) {if (m==1) F[0]=pow (g[0],m-2);

        else {Poly_rev (m>>1);
        n=2*m,lg=0;
        while ((1&LT;&LT;LG)!=n) lg++;
        for (int i=0; i<m; i++) a[i]=f[i],b[i]=g[i];
        for (int i=m; i<n; i++) a[i]=b[i]=0;

        NTT (TRUE);
    for (int i=0; i<m; i++) f[i]=2ll*f[i]%m,f[i]= (f[i]-a[i]+m)%M;
    }} int main () {freopen ("4233.in", "R", stdin);

    Freopen ("4233.out", "w", stdout); scanf ("%D ", &n);
    N=1;
    while (n<=n+2) n<<=1;
    Fac[0]=1;
    for (LL i=1; i<n; i++) fac[i]=fac[i-1]*i%m;
    for (int i=0; i<n; i++) Nfac[i]=pow (fac[i],m-2);
    for (LL i=0; i<n; i++) G[i]=pow (2LL, i* (I-1LL)/2ll) *nfac[i]%m;

    Poly_rev (N);
    n=1,lg=0;
    while (n<2*n+2) n<<=1,lg++;
    for (int i=0; i<=n; i++) a[i]=f[i],b[i]=g[i];
    for (int i=n+1; i<n; i++) a[i]=b[i]=0;
    b[0]=0;
    NTT (FALSE);
        for (LL i=1; i<=n; i++) {a[i]=a[i]*fac[i]%m; if (!
        A[i]) printf (" -1\n");
            else {LL Ans=fac[i-1]*pow (2LL, Max (i* (I-1LL)/2ll-i,0ll))%M;
            Ans=ans*pow (a[i],m-2)%M;
        printf ("%i64d\n", ans);
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.