Calculating the coefficients (number of combinations)

 

Title Description

Given a polynomial (ax + by) ^ k, requesting a coefficient ^ x ^ n * y m after entry polynomial expansion.

Input formats:

Total line contains five integers, respectively, a, b, k, n, m, each between two integers separated by a space.

Output formats:

Total output line, comprising an integer, denotes the required coefficient, this coefficient may be large, the output result of modulo 10007.

Sample input:

1 1 3 1 2

Sample output:

3

Explanation

data range

For 30% of the data, there are 0 ≤k ≤10;

For 50% of the data, there are a = 1, b = 1;

To 100% of the data, there are 0 ≤k ≤1,000,0≤n, m ≤k, and n + m = k, 0 ≤a, b ≤1,000,000.

 

Ideas:

ans为C(m+n,n) * a^n * b^m

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 typedef long long ll;
 6 const int MOD=10007;
 7 ll a,b,k,n,m;
 8 ll powMod(ll a,ll b){
 9     a%=MOD;b%=MOD;
10     ll ans=1;
11     for(;b;b>>=1,a=(a*a)%MOD)
12         if(1&b) ans=(ans*a)%MOD;
13     return ans;
14 }
15 ll C(ll n,ll k){
16     ll s1=1,s2=1;
17     if(k>n-k) k=n-k;
18     for(int i=1;i<=k;i++){
19         s1=s1*(n-i+1)%MOD;
20         s2=s2*i%MOD;
21     } 
22     return s1*powMod(s2,MOD-2)%MOD;        //in
23 }
24 int main(){
25     scanf("%lld%lld%lld%lld%lld",&a,&b,&k,&n,&m);
26     printf("%lld",C(m+n,m)*powMod(a,n)*powMod(b,m)%MOD);
27 
28 }

 

 

See the form (X + Y) n- form of the title, immediately think Pascal triangle. The Pascal triangle, can be easily obtained X n- Y m in front of the constant.

At the same time, we also know that a, b and the index is x, y is the same, but also because a large k, difficult to think fast power algorithm As fast power.

. 1 #include <the iostream>
 2 #include <the cmath>
 . 3 #include <cstdio>
 . 4  the using  namespace STD;
 . 5  Long  Long S [ 1010 ] [ 1010 ];
 . 6  int A, B, K, n-, m;
 . 7  Long  Long ANS ;
 . 8  Long  Long fast_pow ( int X, int Y, int MOD) { // fast power 
. 9      Long  Long  Base = X, T = . 1 ; // Note! 10006 * 10006 may explode int 
10     for (;y;base=base*base%mod,y>>=1)
11         if (y&1) t=t*base%mod;
12     return t;
13 }
14 int main() {
15     scanf("%d%d%d%d%d",&a,&b,&k,&n,&m);
16     s[1][1]=1;//计算杨辉三角
17     for (int i = 2;i <= k+1;i++) {
18         for (int j = 1;j <= i;j++) {
19             s[i][j]=(s[i-1][j]+s[i-1][j-1])%10007;
20         }
21     }
22     ans=(s[k+1][k-n+1]*fast_pow(a,n,10007)*fast_pow(b,m,10007))%10007;
23     printf("%lld",ans);
24     return 0;
25 }

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12227799.html