Calculating 1^X + 2^X + ... + N^X mod 1000000007

derivative calculator
multiplicative inverse of 1
derivative of 1/x
integral of 1/x^2
multiplicative inverse calculator
multiplicative inverse of 0
what is the multiplicative inverse of 1/4
multiplicative inverse of 8

Is there any algorithm to calculate (1^x + 2^x + 3^x + ... + n^x) mod 1000000007? Note: a^b is the b-th power of a.

The constraints are 1 <= n <= 10^16, 1 <= x <= 1000. So the value of N is very large.

I can only solve for O(m log m) if m = 1000000007. It is very slow because the time limit is 2 secs.

Do you have any efficient algorithm?

There was a comment that it might be duplicate of this question, but it is definitely different.

You can sum up the series

1**X + 2**X + ... + N**X

with the help of Faulhaber's formula and you'll get a polynomial with an X + 1 power to compute for arbitrary N.

If you don't want to compute Bernoulli Numbers, you can find the the polynomial by solving X + 2 linear equations (for N = 1, N = 2, N = 3, ..., N = X + 2) which is a slower method but easier to implement.

Let's have an example for X = 2. In this case we have an X + 1 = 3 order polynomial:

    A*N**3 + B*N**2 + C*N + D

The linear equations are

      A +    B +   C + D = 1              =  1 
    A*8 +  B*4 + C*2 + D = 1 + 4          =  5
   A*27 +  B*9 + C*3 + D = 1 + 4 + 9      = 14
   A*64 + B*16 + C*4 + D = 1 + 4 + 9 + 16 = 30 

Having solved the equations we'll get

  A = 1/3
  B = 1/2
  C = 1/6
  D =   0 

The final formula is

  1**2 + 2**2 + ... + N**2 == N**3 / 3 + N**2 / 2 + N / 6

Now, all you have to do is to put an arbitrary large N into the formula. So far the algorithm has O(X**2) complexity (since it doesn't depend on N).

Derivative Calculator • With Steps!, So, I need to calculate 1/X. Is there some workaround so I lose precision but do it faster? share. The mode of a set of data values is the value that appears most often. If X is a discrete random variable, the mode is the value x (i.e, X = x) at which the probability mass function takes its maximum value.

There are a few ways of speeding up modular exponentiation. From here on, I will use ** to denote "exponentiate" and % to denote "modulus".

First a few observations. It is always the case that (a * b) % m is ((a % m) * (b % m)) % m. It is also always the case that a ** n is the same as (a ** floor(n / 2)) * (a ** (n - floor(n/2)). This means that for an exponent <= 1000, we can always complete the exponentiation in at most 20 multiplications (and 21 mods).

We can also skip quite a few calculations, since (a ** b) % m is the same as ((a % m) ** b) % m and if m is significantly lower than n, we simply have multiple repeating sums, with a "tail" of a partial repeat.

Fast 1/X division (reciprocal), Let's just do one, then I'll write out the list of steps for you. Find the inverse of, f( x ) = -( 1 / 3 )x + 1. STEP 1:� Algebra Calculator is a calculator that gives step-by-step help on algebra problems. See More Examples » x+3=5. 1/3 + 1/4. y=x^2+1. Disclaimer: This calculator is

I think Vatine’s answer is probably the way to go, but I already typed this up and it may be useful, for this or for someone else’s similar problem.

I don’t have time this morning for a detailed answer, but consider this. 1^2 + 2^2 + 3^2 + ... + n^2 would take O(n) steps to compute directly. However, it’s equivalent to (n(n+1)(2n+1)/6), which can be computed in O(1) time. A similar equivalence exists for any higher integral power x.

There may be a general solution to such problems; I don’t know of one, and Wolfram Alpha doesn’t seem to know of one either. However, in general the equivalent expression is of degree x+1, and can be worked out by computing some sample values and solving a set of linear equations for the coefficients.

However, this would be difficult for large x, such as 1000 as in your problem, and probably could not be done within 2 seconds.

Perhaps someone who knows more math than I do can turn this into a workable solution?

Edit: Whoops, I see Fabian Pijcke had already posted a useful link about Faulhaber's formula before I posted.

How to Find the Inverse of a Function 1, The Integral Calculator lets you calculate integrals and antiderivatives of functions sign can be left out sometimes, for example we write "5x" instead of "5 *x". After you use the 1/x key, the calculator has put the denominator IN the denominator. Since it is already down there with an invisible 1 standing in the numerator, now you just need to multiply by the numerator, which is 4. So just take the value you have on screen (.0345 …) and multiply by 4.

Integral Calculator • With Steps!, It is impossible. Consider a sequence Xn of random variables, where. P(Xn=n−1 )=P(Xn=n+1)=0.5. Then: Var(Xn)=1for all n. But Var(1Xn) approaches zero as n� The Integral Calculator supports definite and indefinite integrals (antiderivatives) as well as integrating functions with many variables. You can also check your answers! Interactive graphs/plots help visualize and better understand the functions. For more about how to use the Integral Calculator, go to "Help" or take a look at the examples.

Var(X) is known, how to calculate Var(1/X)?, 1/10 = 0.1 = 1 x 10-1, 0.32 = 3.2 x 10-1 (not usually done). 1/100 = 0.01 = 1 x 10-2 Treat this number normally in all subsequent calculations. To check yourself� The calculator will find the inverse of the given function, with steps shown. If the function is one-to-one, there will be a unique inverse.

Math Skills, To write a percentage as an odds ratio, convert the percentage to a decimal x, then calculate as follows: (1/x) - 1 = first number in the odds ratio, while the second� Logarithm calculator finds the log function result in various base numbers 2,10 and exponential e. Calculate the log(x) inverse function of exponentiation.

How to Convert a Percent to an Odds Ratio, To calculate the standard deviation of those numbers: 1. Work out the Mean We already calculated (x1-7)2=4 etc. in the previous step, so just sum them up:. The Derivative Calculator supports computing first, second, …, fifth derivatives as well as differentiating functions with many variables (partial derivatives), implicit differentiation and calculating roots/zeros. You can also check your answers! Interactive graphs/plots help visualize and better understand the functions.

  • @TobySpeight No, it is definitely different. 1^x, 2^x..., n^x is mod exponential, and I want to find FAST ALGORITHM because n <= 10^16 and mod = 10^9+7.
  • n^x mod m is the same as ((n^(x-1) mod m) * (n mod m)) mod m; (a + b) mod m is the same as ((a mod m) + (b mod m)) mod m.
  • @CiaPan Please note that 10^9+7 is a prime.
  • Give a look to's_formula. If your problem comes from a challenge site like HackerRank, I bet the inner product always simplifies to an integer and that there is a way to get rid of the global factor as well.
  • ((Woud like to make it an answer, alas the question got closed, so I can only post this as a comment.)) For small n you can compute the sum directly in reasonable time. For large n I think you can find such pairs of numbers, which add up to 1000000007 (for example 1 and 1000000006, 7 and 1000000000...). Those numbers equal 1 and (–1) mod 1000000007, respectively, so they cancel in summation – and so do their respective powers. You can skip them then, so you'll have to directly calculate no more than 1000000007 terms of the sum...
  • But unfortunately x might be 1000... Solving linear equation can be O(X^3), but I think it is slow. I am checking about Faulhaber's Formula right now.
  • @square1001: if you have to be very fast, I suggest precomputing Bernoulli Numbers (up to 1000); it's not an easy task (like linear equations solving) but in this case you'll get the polynom almost ready to use.
  • I checked about Bernoulli number, and finally understood that B[1], B[2],..., B[x] for O(x^2). So this problem can solve for O(N^2) !
  • @square1001: In O(X**2), to be true, it doesn't depend N (number of items to sum), but of power X they should be raised
  • Implementated. I got Accepted.
  • But your algorithm has to calculate 1^x, 2^x, ..., 1000000006^x. It has over 10^9 terms, so you have to iterate over 10^10 times. So, I think it can't pass because the time limit is 2 secs.
  • @square1001 Just tested a NOP loop on my laptop, takes ~0.1 seconds to iterate 10 ** 10 times and that is likely dominated by "fork" and "exec".
  • This becomes much easier if x is odd, because, for example, 1000000006 is congruent to -1, so 1^x and 1000000006^x cancel out, and so do many other terms. I don’t see a shortcut if x is even, though.
  • And if x is known in advance, you could build a closed formula for the (non-mod) sum of exponentials. But, yes, you can probably collapse this to "calculate 500000003 ** x % m; multiply by floor(n / m); loop for i from 1 to n % m, exponentiate and sum"
  • @Vatine 500000003 is cancelled by 500000004 for odd x. Therefore for odd x and n=m the sum equals zero.