This commit is contained in:
Zengtudor 2024-10-08 19:02:13 +08:00
parent eb95caa631
commit 49bdd569a7

View File

@ -7,6 +7,7 @@ using ull = unsigned long long;
static ull t, k, n, m, ans; static ull t, k, n, m, ans;
static std::map<ull,ull> fact_map; static std::map<ull,ull> fact_map;
static std::map<std::pair<ull,ull>,ull> C_map;
static ull fact(const ull n)noexcept{ static ull fact(const ull n)noexcept{
if(n==0)return 1; if(n==0)return 1;
@ -14,14 +15,18 @@ static ull fact(const ull n)noexcept{
if(it != fact_map.end())return (*it).second; if(it != fact_map.end())return (*it).second;
ull ret {1}; ull ret {1};
for(ull i{2};i<=n;i++){ for(ull i{2};i<=n;i++){
ret=(ret%k*i%k); ret*=i;
} }
fact_map.insert({n,ret}); fact_map.insert({n,ret});
return ret; return ret;
} }
static ull C(const ull n, const ull m)noexcept{ static ull C(const ull n, const ull m)noexcept{
return fact(n)/(fact(m)*fact(n-m)%k); const auto it = C_map.find({n,m});
if(it!=C_map.end())return (*it).second;
const ull ret = (fact(n))/(fact(m)*fact(n-m));
C_map.insert({{n,m},ret});
return ret;
} }
int main(){ int main(){