[T2] 2019.8.15 Cixi simulation game combination number (Binom) (Lucas Theorem prefix and high dimensional +)

Lucas Theorem

Speaking of the title \ (p \) is a prime number.

At this time, the required number of combinations to the results modulo a prime number, it can be used Lucas theorem:

\[C_x^y=C_{x\ div\ p}^{y\ div\ p}\cdot C_{x\ mod\ p}^{y\ mod\ p}\]

In other words, we can \ (x \) and \ (y \) is converted into two \ (p \) binary number, then every request separately and then multiply the number of combinations.

So the question is, when a combination of a number of value-mode \ (p \) is \ (0 \) ?

Since it is a prime, so for a number of combinations of \ (C_a ^ B \) , if and only if \ (a <b \) when its value is only \ (0 \) .

That is, for two numbers \ (the X-, the y-\) , as long as the \ (y \) in \ (p \) has a lower value than the binary \ (x \) that a big, \ (^ C_x = 0 Y \) .

And high-dimensional prefix

Consider first the inclusion-exclusion.

We use the total number (program \ (the n-^ 2 \) ) minus the number of combinations is not \ (0 \) the number of groups, is the answer.

And \ (C_x ^ y \) is not \ (0 \) , if and only if \ (Y \) in \ (P \) the binary value at each bit are less \ (X \) This is a .

It is not thought of high-dimensional partial order. . .

Then, this question becomes high and the bare-dimensional prefix.

If you do not know the prefix and high-dimensional, you can go check out my blog post: On the high-dimensional and prefixes .

Code

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 1000000
#define LL long long
#define Qinv(x) Qpow(x,X-2)
using namespace std;
int n,X,a[N+5],s[N+5];
class FastIO
{
    private:
        #define FS 100000
        #define tc() (A==B&&(B=(A=FI)+fread(FI,1,FS,stdin),A==B)?EOF:*A++)
        #define tn (x<<3)+(x<<1)
        #define D isdigit(c=tc())
        char c,*A,*B,FI[FS];
    public:
        I FastIO() {A=B=FI;}
        Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
}F;
int main()
{
    freopen("binom.in","r",stdin),freopen("binom.out","w",stdout);
    RI i;LL p,ans=0;for(F.read(n),F.read(X),i=1;i<=n;++i) F.read(a[i]),++s[a[i]];//读入
    for(p=1;p<=N;p*=X) for(i=0;i<=N;++i) (i/p)%X&&(s[i]+=s[i-p]);//高维前缀和
    for(i=1;i<=n;++i) ans+=n-s[a[i]];return printf("%lld",ans),0;//容斥求答案
}

Guess you like

Origin www.cnblogs.com/chenxiaoran666/p/Contest20190815T2.html