LCM of n numbers modulo 1000000007

后端 未结 2 1449
花落未央
花落未央 2020-12-21 15:55

I have to find LCM of n numbers MODULO 10^9+7.My approach is find LCM of two numbers and then MOD them.Then take the LCM of next element and the answer obtained from the pre

2条回答
  •  醉酒成梦
    2020-12-21 16:52

    Just for future reference, here's a C++ implementation that uses no prime factorization.

    One possible solution is to keep an array of factors for the answer. Each factor will be each number from 1..N, divided by GCD(number, [all previous numbers]). For this to work, you have to code a special GCD that computes the result between a single number and an array of factors. This C++ code shows how this would work:

    #include 
    #include 
    #define lli long long int
    using namespace std;
    
    vector T;
    
    lli gcd(lli a, lli b) {
        if(b == 0) 
            return a;
        return gcd(b, a%b);
    }
    
    lli gcd_vector(vector& a, lli b) {
        lli ma = 1;
        for(int i=0; i

提交回复
热议问题