A. Beer Barrels (combinatorial mathematics)

Insert picture description hereInsert picture description hereInsert picture description here
This question is a rather pitted question;
I feel that the hole is in ABC:
In fact, this problem can be done quickly, but it's timed out. . . . .
So after thinking about it, I can find this problem; the
question is whether there are k vacancies, and then each vacancy can only be filled with A and B; then find the number of times C appears in all permutations;
then can I start from k Choose any i from the position and put C (here the premise is AC or BC, so it's equivalent to putting A or B);
then is the selected item multiplied by i, isn't it the number? Then add up;
so the final formula is:
Insert picture description here
See the code for specific details:
AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD  1000000007
#define MAXN  200005
ll F[MAXN], Finv[MAXN], inv[MAXN];//F是阶乘,Finv是逆元的阶乘 
void init() {
    
    
    inv[1] = 1;
    for (ll i = 2; i < MAXN; i++) {
    
    
        inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
    }
    F[0] = Finv[0] = 1;
    for (ll i = 1; i < MAXN; i++) {
    
    
        F[i] = F[i - 1] * 1ll * i % MOD;
        Finv[i] = Finv[i - 1] * 1ll * inv[i] % MOD;
    }
}
ll CC(ll n,ll m) 
{
    
    
    if (n < 0 || m < 0 || m > n) return 0;
    return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
}
int main(){
    
    
  ll A,B,K,C;
  init();
  scanf("%lld %lld %lld %lld",&A,&B,&K,&C);
  if(C!=A&&C!=B||K==0){
    
    //这里是因为不想等或者K==0的话就不可能有C出现,所以直接输出0
  	puts("0");return 0;
  }
  ll sum=0;
    for(ll i=1;i<=K;i++){
    
    
    	  sum+=(i*CC(K,i))%MOD;//求和
	}
   if(A==C&&B==C){
    
    //这个点比较坑,因为题目上说每个数字等于A或者等于B,所以如果A==B了,那么只能出现一次,比如A==B==1,K=2那么只有一个排列:1,1;就是这个意思;
  		printf("%lld\n",K);
  }else{
    
    
  	  printf("%lld\n",sum%MOD);
  }

	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44555205/article/details/104450875