【bzoj4591】[Shoi2015]超能粒子炮·改 Lucas定理_數論

來源:互聯網
上載者:User
Lucas定理
ans=C(n,0)+C(n,1)+……+C(n,k)
=C(n/2333,0)*C(n%2333,0)+C(n/2333,0)*C(n%2333,1)+……+C(n/2333,0)*C(n%2333,2332)+C(/2333,1)*C(n%2333,0)+……+C(n/2333,k/2333)*(n%2333,k%2333)

=∑C(n/2333,j)*sum[n%2333][2332](0<=j<k/2333)+C(n/2333,k/2333)*sum[n%2333][k%2333]


cal(n,k)=cal(n/2333,k/2333-1)*sum[n%2333][2332]+Lucas(n/2333,k/2333)*sum[n%2333][k%2333]


注意首碼和的處理


#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<iostream>#define mod 2333using namespace std;int T;long long c[mod][mod],sum[mod][mod];long long Lucas(long long x,long long y){if (x<y || y<0) return 0; if (x<mod && y<mod) return c[x][y];return Lucas(x/mod,y/mod)*c[x%mod][y%mod]%mod;}long long cal(long long n,long long k){if (k<0) return 0;return (cal(n/mod,k/mod-1)*sum[n%mod][mod-1]+Lucas(n/mod,k/mod)*sum[n%mod][k%mod])%mod;}int main(){c[0][0]=1;sum[0][0]=1;for (int i=1;i<mod;i++) sum[0][i]=1;for (int i=1;i<mod;i++){c[i][0]=sum[i][0]=1;for (int j=1;j<=i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;for (int j=1;j<mod;j++) sum[i][j]=(sum[i][j-1]+c[i][j])%mod;}scanf("%d",&T);while (T--){long long n,k;scanf("%lld%lld",&n,&k);printf("%lld\n",cal(n,k));}return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.