All Euler problems
Project Euler

Roots on the Rise

Given the equation: 1/x = (k/x)^2 (k + x^2) - kx Let a_k, b_k, c_k represent its three solutions (real or complex). For k = 5, the solutions are approximately {5.727244, -0.363622+2.057397i, -0.363...

Source sync Apr 19, 2026
Problem #0479
Level Level 12
Solved By 1,534
Languages C++, Python
Answer 191541795
Length 449 words
modular_arithmeticalgebrabrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let \(a_k\), \(b_k\), and \(c_k\) represent the three solutions (real or complex numbers) to the equation \(\frac 1 x = (\frac k x)^2(k+x^2)-k x\).

For instance, for \(k=5\), we see that \(\{a_5, b_5, c_5 \}\) is approximately

\(\{5.727244, -0.363622+2.057397i, -0.363622-2.057397i\}\).

Let \(\displaystyle S(n) = \sum _{p=1}^n\sum _{k=1}^n(a_k+b_k)^p(b_k+c_k)^p(c_k+a_k)^p\).

Interestingly, \(S(n)\) is always an integer. For example, \(S(4) = 51160\).

Find \(S(10^6)\) modulo \(1\,000\,000\,007\).

Problem 479: Roots on the Rise

Mathematical Analysis

Simplifying the Equation

Starting from 1/x = (k^2/x^2)(k + x^2) - kx, multiply both sides by x^2:

x = k^2(k + x^2) - kx^3

Rearranging: kx^3 - k^2*x^2 + x - k^3 = 0

This is a cubic in x with roots a_k, b_k, c_k.

Vieta’s Formulas

For the cubic kx^3 - k^2*x^2 + x - k^3 = 0:

  • a + b + c = k (sum of roots)
  • ab + bc + ca = 1/k (sum of products of pairs)
  • abc = k^2 (product of roots)

Computing the Product of Pairwise Sums

(a+b)(b+c)(c+a) = (s-c)(s-a)(s-b) where s = a+b+c = k

So (a+b)(b+c)(c+a) = (k-a)(k-b)(k-c)

Now expand: (k-a)(k-b)(k-c) = k^3 - k^2(a+b+c) + k(ab+bc+ca) - abc = k^3 - k^2k + k(1/k) - k^2 = k^3 - k^3 + 1 - k^2 = 1 - k^2

The Sum S(n)

Define P_k = (a_k+b_k)(b_k+c_k)(c_k+a_k) = 1 - k^2

Then: S(n) = sum_{k=1}^{n} sum_{p=1}^{n} P_k^p = sum_{k=1}^{n} sum_{p=1}^{n} (1 - k^2)^p

For each k, the inner sum is a geometric series: sum_{p=1}^{n} (1-k^2)^p = (1-k^2) * ((1-k^2)^n - 1) / ((1-k^2) - 1) = (1-k^2) * ((1-k^2)^n - 1) / (-k^2) = ((1-k^2)^{n+1} - (1-k^2)) / (-k^2)

Modular Arithmetic

All computations are done modulo 10^9 + 7. We need modular inverse of k^2.

Editorial

S(n) = sum_{k=1}^{n} sum_{p=1}^{n} (1-k^2)^p mod 10^9+7 Key insight: (a_k+b_k)(b_k+c_k)(c_k+a_k) = 1 - k^2 from Vieta’s formulas. For each k >= 2, the inner sum is a geometric series. For k = 1, 1-k^2 = 0 so contribution is 0. We iterate over each k from 1 to n. We then compute q = (1 - k^2) mod p where p = 10^9+7. Finally, compute geometric sum: q*(q^n - 1)/(q - 1) = (q^{n+1} - q)/(q - 1).

Pseudocode

For each k from 1 to n:
Compute q = (1 - k^2) mod p where p = 10^9+7
Compute geometric sum: q*(q^n - 1)/(q - 1) = (q^{n+1} - q)/(q - 1)
Use modular inverse for division
Sum all contributions

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • For each k: O(log n) for modular exponentiation
  • Total: O(n log n)

Answer

191541795\boxed{191541795}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_479/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MOD = 1000000007LL;

ll power(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    if (base < 0) base += mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

ll modinv(ll a, ll mod) {
    return power(a, mod - 2, mod);
}

int main() {
    ll n = 1000000; // 10^6

    ll ans = 0;

    // For k=1: 1 - k^2 = 0, so contribution is 0
    // For k >= 2:
    // sum_{p=1}^{n} (1-k^2)^p = ((1-k^2)^{n+1} - (1-k^2)) / ((1-k^2) - 1)
    //                          = ((1-k^2)^{n+1} - (1-k^2)) / (-k^2)

    for (ll k = 2; k <= n; k++) {
        ll k2 = k % MOD * (k % MOD) % MOD;
        ll q = (1 - k2 % MOD + MOD) % MOD; // q = 1 - k^2 mod MOD

        // Numerator: q^{n+1} - q
        ll qn1 = power(q, n + 1, MOD);
        ll num = (qn1 - q + MOD) % MOD;

        // Denominator: q - 1 = -k^2
        // So we divide by (q - 1) = -k^2
        // num / (q - 1) = num / (-k^2) = -num * inv(k^2)
        ll inv_k2 = modinv(k2, MOD);
        ll term = (MOD - num) % MOD * inv_k2 % MOD;

        ans = (ans + term) % MOD;
    }

    printf("%lld\n", ans);

    return 0;
}