c++ - 对非常大的数字执行 nCr 和逆阶乘 (MODm)

标签 c++ dynamic modular-arithmetic ncr

您好,我在代码 sprint5 问题中实现 nCr MODm 时遇到了问题。 问题链接是…… https://www.hackerrank.com/contests/codesprint5/challenges/matrix-tracing . 我学到的是我可以将模数算术规则应用于阶乘计算和逆阶乘计算以及计算 pow(a,b) MODm。但我不知道我遗漏了什么导致错误答案。 这是我当前的代码。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include<math.h>
using namespace std;
const int md = 1000000007;
const int co = 2000020;
unsigned long long int ft[co];

long long int fact(unsigned long long int n)
{   
   return ft[n];
}

void fct(){
    ft[1]=1;
    for(unsigned long long int i = 2;i<=2000020;i++){
        ft[i]=(i*ft[i-1]) % md;
        }
    }

long long int pow(long long int x, long long int n, long long int mod){
    long long int result=1; 
    while(n>0){
        if(n%2 ==1){
            result = (result*x) % mod;
        }
        n= n>>1;
        x= (x*x)% mod;  
    }
    return result;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    unsigned long long int m , n;
    long long result;
    int T;
    fct();
    cin>>T;
    while(T--){
        cin>>m>>n; 
        unsigned long long int mod = md-2;
         result = (fact(m+n-2) * pow( ( fact(m-1) * fact(n-1) ) , mod, md )) % md ;
        cout<<result<<endl;
    }
    return 0;
}

最佳答案

最后我发现了我的代码中的错误。

错误....

  1. 我应该使用常量变量 mdco 作为 unsigned long long int 而不仅仅是 int
  2. 第二个错误是在 pow() 中计算 pow(a,b) % md 的算法中 函数,在进一步处理之前我应该​​先做 x % md 因为 x 可以传递的概率大于 md

当前的工作代码是......

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include<math.h>
using namespace std;
const unsigned long long int md = 1000000007; 
const unsigned long long int co = 2000020;
unsigned long long int ft[co];

unsigned long long int fact(unsigned long long int n)
{   
    return ft[n];
}

void fct(){
    ft[0]=1;
    for(unsigned long long int i = 1;i<=2000020;i++){
        ft[i]=(i*ft[i-1]) % md;
    }
}

unsigned long long int pow(unsigned long long int x, unsigned long long int n, unsigned long long int mod){
    unsigned long long int result=1; 
    x = x % md;
    while(n>0){
        if(n%2 ==1){
            result = (result*x) % md;
        }
        n= n>>1;
        x= (x*x)% md;   
    }
    return result;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    unsigned long long int m , n;
    unsigned long long int result;
    int T;
    fct();
    cin>>T;
    while(T--){
        cin>>m>>n; 
        unsigned long long int mod = md-2;   
        result = (fact(m+n-2) * pow( ( fact(m-1) * fact(n-1) ) , mod, md )) % md ;
        cout<<result<<endl; 
    }

    return 0;
}

关于c++ - 对非常大的数字执行 nCr 和逆阶乘 (MODm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21279116/

相关文章:

c++ - 为什么要调用 boost::thread::shared_mutex block 的 lock_upgrade()?

dynamic - Kotlin 中动态/双重调度的限制是什么?

python - 将数字的数字增加一位

c++ - 通过指针将结构传递给函数

c++ - 如何消除构造函数选择的强制转换

dynamic - 如何使用 Julia 1.5 在运行时动态加载模块(或脚本)

javascript - 如何使用angularjs通过动态过滤添加来处理高级搜索?

c - 大数的模乘

python - Sympy:在有限域中求解矩阵

java - C++ 中的 Hashmap 等价物