0

How we can write code to find solution of following: (N+1)*X =LCM( 1,2,3,4,5,6,......,N,N+1) use mod when it gets greater then (10^9 +7) . USE MOD=(10^9 +7). I had written following code fragement but its not working:

ll dp[100005];
ll gcd (ll a,ll b)
 {
        if (b == 0)
                return a;
        else
                return gcd (b, a % b);
}
void extend_euclid(int a,int b,int &x,int &y)
{
    if(a==0)
    {
                x=0;y=1;
                return;
        }
        int x1,y1;
        extend_euclid(b%a,a,x1,y1);
        x=y1-(b/a)*x1;
        y=x1;
}
void init()
{
    dp[1]=1;
    for(ll i=2;i<100002;i++)
    {
        ll x,y;
        x=dp[i-1]*i;
        x=x%mod;
        y=gcd(i,dp[i-1]);
        y=y%mod;
                int z1,z2;
        extend_euclid(y,mod,z1,z2);
                y=(z1+mod)%mod;
                dp[i]=(y*x)%mod;
    }
}
Sumurai8
  • 20,333
  • 11
  • 66
  • 100

1 Answers1

0

I'm presuming this is your problem (correct me if I'm wrong):

Find X for when N*X = LCM(1 -> N) (mod 1E9 +7)

First, get LCM(1 -> N), then divide by N.

Instead of working out the LCM of each number up to N, you can look at each prime ≤ N and take the highest exponent that is ≤ N, multiplying them all together. So if N=20, you'd take: 16(2^4), 9(3^2), 5, 7, 11, 13, 19. Multiplying them together you get 232792560, which is equal to LCM(1 -> 20).

With this, N will ALWAYS divide into LCM(1->N), (I'll leave you to work out why) and finding X is easy.

Now, you'll notice at N=23, you have to (mod 1E9 +7) and your LCM(1->23) isn't divisible by 23. Can you clarify what the question requires? Do you have to find a X*N (mod 1E9 +7) which would equal the LCM(1->23)??

user2514440
  • 138
  • 2
  • 5