sol:将问题转换为X1+X2+...+Xn=P-(A1+A2+...+An),X1,X2,...,Xn>=0。因此ans=C(n+P-(A1+A2+...+An)-1,n-1).
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #define inf 0x7f7f7f 7 typedef long long ll; 8 typedef unsigned int ui; 9 typedef unsigned long long ull; 10 using namespace std; 11 inline int read(){ 12 int x=0,f=1;char ch=getchar(); 13 for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1; 14 for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0'; 15 return x*f; 16 } 17 inline void print(int x){ 18 if (x>=10) print(x/10); 19 putchar(x%10+'0'); 20 } 21 ll C(int n,int m){ 22 ll res=1; 23 for (int i=1;i<=m;i++) res=res*(n+1-i)/i; 24 return res; 25 } 26 int main(){ 27 int n=read(),tot=0; 28 for (int i=1;i<=n;i++) tot+=read(); 29 int p=read()-tot; 30 //既然每个数字都要有个下限,那就先从总和中减去就好了 31 //接下来套公式求解 32 printf("%lld ",C(n+p-1,n-1)); 33 return 0; 34 }