[noi239]count

Each of ai is expressed as $ ai = ki \ cdot m + ri $, is met $ m \ sum ki + \ sum ri = n $ and $ 0 <ri <m $
enumeration $ S = \ sum ri $ ( S range $ k \ le S \ le k (m-1) $ and congruent with n, k values only), then the equivalent of $ \ sum ki = (nS) / m $, interposer method according to $ o ( K) $ (defined in c) calculated
also computes a $ \ sum ri = S $ the number of programs, provided f (i) represents the i-th satisfies j $ m \ le rj $, the rest of the program an unlimited number, then the answer is $ \ sum_ {i = 0} ^ {k} (-. 1) ^ k \ CDOT F (k) $
F (i) calculation is very simple, from a first selected number k i, i.e. c (k , i); rj is not satisfied then subtracted m-1, i.e., n has a number of programs and the number of $ -S- (m-1) i $, the flapper method also used, but o (1 ) is calculated (i.e., inverse pre-factorial)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define mod 998244353
 4 #define ll long long
 5 #define N 25000005 
 6 int m,k,ans,fac[N],inv[N];
 7 ll n;
 8 int c1(ll n,int m){
 9     int ans=1;
10     for(int i=0;i<m;i++)ans=1LL*(n-i)%mod*ans%mod;
11     return 1LL*ans*inv[m]%mod;
12 }
13 int c2(int n,int m){
14     if (n<m)return 0;
15     return 1LL*fac[n]*inv[m]%mod*inv[n-m]%mod;
16 }
17 int f(int n){
18     int p=-1,ans=0;
19     for(int i=0;i<=k;i++){
20         p*=-1;
21         ans=(ans+1LL*p*c2(k,i)*c2(n-(m-1)*i-1,k-1)%mod+mod)%mod;
22     }
23     return ans;
24 }
25 int main(){
26     scanf("%lld%d%d",&n,&m,&k);
27     fac[0]=inv[0]=inv[1]=1;
28     for(int i=1;i<=k*m;i++)fac[i]=1LL*fac[i-1]*i%mod;
29     for(int i=2;i<=k*m;i++)inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
30     for(int i=1;i<=k*m;i++)inv[i]=1LL*inv[i-1]*inv[i]%mod;
31     for(int i=0;i<k;i++){
32         int j=i*m+n%m;
33         if ((j<k)||(k*(m-1)<j))continue;
34         ans=(ans+1LL*c1((n-j)/m+k-1,k-1)*f(j))%mod;
35     }
36     printf("%d",ans); 
37 }
View Code

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11573890.html