Project Euler #28: Number spiral diagonals

  • + 0 comments

    For C++, we need to use n % mod and inverse modulo to do the division.

    C++

    int mod = 1000000007, imod3 = 333333336;
    int main() {
        long long t, n;
        cin >> t;
        while (t--) {
            cin >> n;
            long long nm = n % mod, n2m = n / 2 % mod;
            long long res = nm * (nm + 1) % mod * (2 * nm + 1) % mod * imod3 - 1;
            cout << (res - 2 * n2m * n2m % mod) % mod << endl;
        }
        return 0;
    }