The number of solutions of Bzoj 3129 [Sdoi2013] equation and the modulus of combination number

Source: Internet
Author: User
Tags modulus

Test Instructions:Link Method:The number of solutions of indefinite equation and the modulus of combination number parsing:First look at the limitations of N1 and N2 parts. For the latter part of the limit, we subtract directly from A n1+i ?1 You can convert a positive integer solution. But what about the first half? It's like a monkey. So we can do it. But this problem doesn't seem to be a good place to write. This is not a gift of TMD! How to take the model of large combination number? Please see my "Bzoj gift". In addition, the problem is x1+x2+...+xn=m is not less than equal Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N#define PA pair<int,int>#define MP Make_pair#define FI First#define SE SecondUsing namespace Std;typedef long long ll;ll a[n];ll t,p,tot;ll prime[n],mod[n],num[n],ans[n];void exgcd (ll a,ll B,ll &x, LL &y, LL &AMP;GCD) {if(!B) {x=1,y=0, Gcd=a;return; } EXGCD (B,a%b,y,x, GCD);y=y-a/b*x;} ll Get_inv (LL N,llm) {LLx,y, GCD; EXGCD (N,m,x,y, GCD);//(x/gcd%(m/GCD) +m/gcd)% (M/GCD)//gcd==1    return(x%m+m)%m;} ll Quick_my (LLx, LLy, ll MOD) {ll ret=1; while(y)    {if(y&1) ret= (ret*x)%mod;x=(x*x)%mod;y>>=1; }returnRET;} void Getp () {int x=p; for(intI=2; I*i<=x; i++) {if(x%i==0) {prime[++tot]=i,mod[tot]=1, num[tot]=0; while(x%i==0)            {x/=i,mod[tot]*=i,num[tot]++; }        }    }if(x!=1) {prime[++tot]=x, mod[tot]=x, num[tot]=1; }}ll CRT () {ll ret=0; for(intI=1; i<=tot;i++) {ret= (Ret+p/mod[i]*GET_INV(P/mod[i],mod[i])%p*ans[i])%p; }returnRET;} PA Get_factor (LLx, ll Pri,ll Mod) {ll ans=1;if(x==0||x==1)returnmp0,1); ll cnt_circle=x/mod,cnt_pri=x/pri;if(cnt_circle!=0)    { for(intI=2; i<mod;i++) {if(I%pri!=0) ans= (ans*(ll) i)%mod;    } ans=quick_my (Ans,cnt_circle,mod); } for(intI=cnt_circle*mod+1; i<=x; i++) {if(I%pri!=0) ans= (ans*(ll) i)%mod; } pa tmp=get_factor (Cnt_pri,pri,mod);returnMP (Cnt_pri+tmp.fi,ans*tmp. SE%mod);} ll Get_c (LL N,llm,intK) {if(m==0)return 1; PA tmp=get_factor (N,prime[k],mod[k]), Tmp1=get_factor (m, Prime[k],mod[k]), Tmp2=get_factor (nm, Prime[k],mod[k]);returnQuick_my (Prime[k],tmp.fi-tmp1.fi-tmp2.fi,mod[k])*tmp. SE%modK*GET_INV(Tmp1.se,mod[k])%modK*GET_INV(Tmp2.se,mod[k])%mod[K];} LL Calc (ll N,llm){if(n==m||m==0)return 1;if(n<m)return 0; for(intI=1; i<=tot;i++) Ans[i]=get_c (n,m, i);returnCRT ();} ll Ans;ll N,n1,n2,m; void Dfs (intNow,ll cnt,ll sum) {if(now==n1+1)    {if(CNT%2==0) ans= (Ans+calc (m-sum-1, N-1))%p;ElseAns= ((Ans-calc (m-sum-1, N-1))%p+P)%p;return; } DFS (now+1, cnt,sum); DFS (now+1, cnt+1, Sum+a[now]);}intMain () {scanf ("%lld%lld", &t,&p); GETP (); while(t--) {scanf ("%lld%lld%lld%lld",&n,&n1,&n2,&m); for(intI=1; i<=n1;i++) scanf ("%lld", &a[i]); for(intI=1; i<=n2;i++) {llx; scanf"%lld",&x);m=m-x+1; }if(m<=0) {puts ("0");Continue;} ans=0; Dfs1,0,0);printf("%lld\ n", ANS); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The number of solutions of Bzoj 3129 [Sdoi2013] equation and the modulus of combination number

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.