shoi2015bzoj4591 super-Energy particle cannon/modified

Source: Internet
Author: User
Tags pow

According to Lucas ' theorem, we have
Ans (n,k) =ans (⌊np⌋,⌊kp⌋−1) ∗∑i=0p−1 (N%PI) + (⌊n/p⌋⌊k/p⌋) ∑i=0k%p (n%pi) ans (n,k) =ans (\lfloor\frac n p \rfloor,\lfloor \ Frac k p\rfloor-1) *\sum_{i=0}^{p-1}\binom{n\%p}i+\binom{\lfloor N/p\rfloor}{\lfloor k/p\rfloor}\sum_{i=0}^{k\%p}\ Binom{n\%p}i
One of the ans-ans-term recursive computations, combined number with Lucas violence calculation, and type O (p2) o (p^2) pretreatment.
Complexity O (P2+TLOGNLOGP) o (P^2+t\log n\log p).

#include <cstdio> #include <algorithm> using namespace std;
#define LL Long Long const int p=2333;
    int pow (int base,int k) {int ret=1;
    for (; k;k>>=1,base=base*base%p) if (k&1) ret=ret*base%p;
return ret;
} int fac[p+10],inv[p+10],sum[p+10][p+10];
    int C (LL n,ll k) {if (k>n) return 0;
    if (n<p&&k<p) return fac[n]*inv[k]%p*inv[n-k]%p;
return C (n%p,k%p) *c (n/p,k/p)%p;
    } int Solve (LL n,ll k) {int ret=0,tem=0;
    if (n<p&&k<p) return sum[n][k];
Return (Sum[n%p][p-1]*solve (n/p,k/p-1)%p+sum[n%p][k%p]*c (n/p,k/p)%p)%p;
    } int main () {LL n,k;
    for (int i=1;i<p;i++) fac[i]=fac[i-1]*i%p;
    Inv[p-1]=pow (fac[p-1],p-2);
    for (int i=p-2;i;i--) inv[i]=inv[i+1]* (i+1)%p;
        for (int i=0;i<p;i++) {sum[i][0]=1;
    for (int j=1;j<p;j++) sum[i][j]= (Sum[i][j-1]+c (i,j))%p;
    } int T;
    scanf ("%d", &t); while (t--) {scanf ("%lld%lld",&AMP;N,&AMP;K);
    printf ("%d\n", Solve (n,k)); }
Related Article

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: 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.