Skip to content

Commit

Permalink
Create combination.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
potato167 committed Aug 6, 2023
1 parent c12f163 commit 9be2fa2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions math/combination.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include<vector>
#include<assert.h>

namespace po167{
struct combination{
int upper;
int MOD;
std::vector<long long> fact;
std::vector<long long> rev;
std::vector<long long> fact_rev;
combination(int max,long long mod):upper(max),MOD(mod),fact(max+1),rev(max+1),fact_rev(max+1){
for(long long i=0;i<=max;i++){
if(i<2){
fact[i]=1;
fact_rev[i]=1;
rev[i]=1;
continue;
}
fact[i]=(fact[i-1]*i)%mod;
rev[i]=mod-((mod/i)*rev[mod%i])%mod;
fact_rev[i]=(fact_rev[i-1]*rev[i])%mod;
}
}
long long Comb(int x,int y){
assert(upper>=x);
if (x<y||y<0||x<0) return 0;
return (((fact_rev[y]*fact_rev[x-y])%MOD)*fact[x])%MOD;
}
long long P(int x,int y){
assert(upper>=x);
if (x<y||y<0||x<0) return 0;
return (fact_rev[x-y]*fact[x])%MOD;
}
};
}
using po167::combination;

0 comments on commit 9be2fa2

Please sign in to comment.