Prime Not-Prime
Difficulty: Hard

Jigar is trying to solve a math problem and prove to Aghaye Mojri that he is good at math, But it seems this problem is too tough to be solved on paper, So he asks you to calculte the SUM of |p-x| for all prime numbers *p* in range [1, n] and all non prime integers *x* in range [1, n]. As the answer can be very large, print the answer mod (10<sup>9</sup>+9).

__Input Format__ <br>
The first line contains an integer __T__, the number of test cases.  
This is followed by __T__ lines each containing 1 integer __n__.

__Constraints__ <br>
1 &le; T ≤ 20 <br>
1 ≤ n ≤ 600000

__Sample Input__ <br>
<pre>
2
2
5
</pre>

__Sample Output__ <br>
<pre>
1
11
</pre>

__Explanation__ <br>

The primes in the range [1, 2] ( both included ) are just [2] and the non primes in the range [1, 2] ( both included ) are just [1]. So, 

we get 

    |2 - 1| = 1

The primes in the range [1, 5] are [2, 3, 5] and the non-primes are [1, 4]. So, 

we get

    |2-1|+|2-4|+ |3-1|+|3-4| + |5-1|+|5-4| = 1+2 + 2+1 + 4+1 = 11


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include<bits/stdc++.h>

    using namespace std;

    vector<int> prime;
    vector<int> minFactor;
    vector<bool> isp;
    vector<int> non_prime;
    vector<int> dp;
    vector<int> sum_prime;
    vector<int> sum_non_prime;

    #define MAX 6e5
    #define MOD 1000000009

    void generatePrime(int n = MAX) {
        isp.resize(n + 1, 1), isp[0] = isp[1] = 0;
        minFactor.resize(n + 1);
        for(int i = 1; i <= n; i++) minFactor[i] = i;
        for(int i = 2; i <= n; i++) {
            if(!isp[i]) continue;
            prime.push_back(i);
            for(int j = i + i; j <= n; j += i) {
                isp[j] = 0;
                if(minFactor[j] == j) minFactor[j] = i;
            }
        }
    }

    void generateNonPrime(int n = MAX)
    {
        int iter = 1;
        for(auto i = prime.begin(); i != prime.end(); i++)
        {
            while(iter < *i)
            {
                non_prime.push_back(iter);
                iter += 1;
            }
            iter = *i + 1;
        }
        for(int i = prime.back()+1; i <= 10; i++)
            non_prime.push_back(i);
    }

    void setSumPrimeNonPrime()
    {
        sum_prime.push_back(prime[0]);
        int temp;
        for(int i = 1; i < (int)prime.size(); i++)
        {
            temp = sum_prime[i-1] + prime[i];
            if(temp >= MOD)
                temp -= MOD;
            sum_prime.push_back(temp);
        }

        sum_non_prime.push_back(non_prime[0]);
        for(int i = 1; i < (int)non_prime.size(); i++)
        {
            temp = sum_non_prime[i-1] + non_prime[i];
            if(temp >= MOD)
                temp -= MOD;
            sum_non_prime.push_back(temp);
        }
    }

    int main()
    {
        int t, x, iter = 1, sum = 0;
        int sum_prime_iter = 1, sum_non_prime_iter = 0;

        cin >> t;
        assert(1 <= t && t <= 20);
        generatePrime();
        generateNonPrime();
        setSumPrimeNonPrime();

        auto prime_iter = prime.begin();
        prime_iter++;

        dp.push_back(0);
        dp.push_back(0);
        dp.push_back(1);
        for(int i = 3; i <= MAX; i++)
        {
            sum = dp[i-1];
            if(i == *prime_iter) // If it's a prime
            {
                int a = (int)(((long long)i * (long long)(sum_non_prime_iter+1)) % MOD);
                int b = sum_non_prime[sum_non_prime_iter];
                int temp = a - b;

                if(temp < 0)
                    temp += MOD;

                sum += temp;

            } else // If it's a non-prime
            {
                int a = (int)(((long long)i * (long long)(sum_prime_iter+1)) % MOD);
                int b = sum_prime[sum_prime_iter];
                int temp = a - b;

                if(temp < 0)
                    temp += MOD;

                sum += temp;

            }

            if(sum >= MOD)
                sum -= MOD;

            dp.push_back(sum);

            if(i+1 != *(++prime_iter))
            {
                prime_iter--;
                sum_non_prime_iter += 1;
            } else
                sum_prime_iter += 1;
        }

        while(t--)
        {
            cin >> x;
            assert(1 <= x && x <= 2e6);
            cout << dp[x] << endl;
        }

        return 0;
    }