Eve LOJ # 6358 (count combination, inclusion and exclusion)

Topic Link

https://loj.ac/problem/6358

The meaning of problems

Written questions surface like a lump of X, like, I'll repeat it.
There \ (n-\) elements constituting a set, from \ (2 ^ n \) subset are selected such that the number of post sizes \ (4 \) multiples. Not vote counted cross is empty.
Sample explanation: There are selected from the empty set \ (8 \) kinds of programs, is not the empty set is selected from only the program \ (\ {1 \} \ {2 \} \) and \ (\ {1 \} \ {2 \} \ {1,2 \} \) , nothing at all there is a total of \ (11 \) species.

answer

This question really stunned me was immortal. .

First consider a simple inclusion and exclusion: Order \ (F (k) \) represents Imperial \ (K \) th element set must be present in all the selected promoter, the \ (F (k) = { n \ choose k} (2 ^ {2 ^ {NK}} -. 1) \) .
order \ (G (k) \) represents the intersection of exactly (K \) \ number of programs, there are \ (F (k) = \ sum ^ {n} _ {i = k } {k \ choose i} G (k), G (k) = \ sum ^ {n} _ {i = k} (-1) ^ {ik} {k \ choose i F.} (K) \) .
so is required \ (ANS = \ {SUM of N_ ^ K \ equiv 0 (\ MOD. 4)} G (K) \) .

High-energy front -
we consider the structure of a coefficient \ (\ Alpha (i) \) (the official solution to a problem called it "the inclusion-exclusion factor", but I did not find it, and inclusion and exclusion What is the relationship?) So that \ (ans = \ SUM ^ {n-} _ {I = 0} F. (I) \ Alpha (I) \) .
If there is no \ (K \) is \ (4 \) multiple of this condition, all \ (K \) summing, then according to equation repellent capacity can be introduced \ (\ alpha (i) = [i = 0] \) to achieve the purpose.
Now, with this condition, we just consider the fact that we are doing:
for a \ (G (the n-) \) , which is in the \ (F (k) \) is calculated in \ (n \ choose k \) times. times we want total calculation is \ (1 \) times, it is \ [\ forall n, \ sum ^ {n} _ {k = 0} {n \ choose k} \ alpha (k) = 1 \] taken \ (\ alpha (k) = [k = 0] \) to now we are to \ (\ forall n, \ sum ^ {n} _ {k = 0} {n \ choose k} \ alpha (K) = [K \ equiv 0 (\ MOD. 4)] \) . so there is the inversion binomial\(F(n)=\sum^{n}_{k=0} (-1)^{n-k}{n\choose k}[k\equiv 0(\mod 4)]\).

This thing is how fast demand? Number Theory took the street to kill the title of the goods must yield - Unit Root! Order \ (m = 4 \) , \ (\ Omega \) of \ (4 \) views (main) unit root, there are \ [[n \ equiv 0 ( \ mod m)] = \ frac {1} { n} \ sum ^ {m-
1} _ {i = 0} \ omega ^ {in} \] so \ (\ alpha (n) = \ frac {1} {m} \ sum ^ {n} _ {k = 0} (- 1) ^ {nk} {n \ choose k} \ sum ^ {m-1} _ {i = 0} (\ omega ^ i) k = \ sum ^ {m-1} _ {i = 0} (\ omega ^ i
-1) ^ k \) directly can be calculated.

Time complexity \ (O (nm) \) .

Revelation: Even recently made two questions fairy constructed, often constructed of some transition matrix / inclusion and exclusion coefficient things / recursive like to achieve the objective, this line of thinking is worth learning.

Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iostream>
#define llong long long
using namespace std;

inline int read()
{
    int x=0; bool f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
    if(f) return x;
    return -x;
}

const int N = 1e7;
const int P = 998244353;
const llong G = 3ll;
const llong W = 911660635ll;
const llong INV4 = 748683265ll;

int fact[N+3],finv[N+3];
llong f[N+3],a[N+3];
int n;

llong quickpow(llong x,llong y)
{
    llong cur = x,ret = 1ll;
    for(int i=0; y; i++)
    {
        if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;}
        cur = cur*cur%P;
    }
    return ret;
}
llong comb(llong x,llong y) {return x<0||y<0||x<y ? 0ll : (llong)fact[x]*(llong)finv[y]%P*(llong)finv[x-y]%P;}

int main()
{
    fact[0] = 1ll; for(int i=1; i<=N; i++) fact[i] = (llong)fact[i-1]*i%P;
    finv[N] = quickpow(fact[N],P-2); for(int i=N-1; i>=0; i--) finv[i] = (llong)finv[i+1]*(i+1ll)%P;
    scanf("%d",&n);
    f[n] = 2ll; for(int i=n-1; i>=0; i--) f[i] = f[i+1]*f[i+1]%P;
    for(int i=0; i<=n; i++) f[i]--;
    for(int i=0; i<=n; i++) f[i] = f[i]*comb(n,i)%P;
    for(int i=0; i<4; i++)
    {
        llong tmp = 1ll,expn = quickpow(W,i);
        for(int j=0; j<=n; j++)
        {
            a[j] = (a[j]+tmp)%P;
            tmp = tmp*(expn-1ll)%P;
        }
    }
    for(int i=0; i<=n; i++) a[i] = a[i]*INV4%P;
//  for(int i=0; i<=n; i++) printf("%lld ",a[i]); puts("");
    llong ans = 1ll;
    for(int i=0; i<=n; i++) ans = (ans+f[i]*a[i])%P;
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/suncongbo/p/11275081.html