0w1

CFR 599 D. Spongebob and Squares ( Math )

Problem - D - Codeforces
Without losing generality, let n ≤ m, we are to find all such pairs ( n, m ) that satisfy:
{ \displaystyle
X = \sum_{i=1}^{n} ( m - i + 1 ) * ( n - i + 1 )
}
Do a little math and we will conclude that
{ \displaystyle
6 * X = 3 * m * n^3 + 3 * m * n - n^3 + n
}
Since n ≤ m, the upper bound of n is of some constant multiple of X^( 1 / 3 ). Therefore, we will enumerate for each n, and see whether an integer solution exists for m.

typedef long long ll;

ll X;
vector< pair< int, ll > > ans;

void solve(){
    cin >> X;
    for( int n = 1; n < 1400000; ++n ){
        ll n_ = X * 6 + 1LL * n * n * n - n;
        ll _d = 3LL * n * ( n + 1 );
        if( n_ % _d == 0 and 1LL * n <= n_ / _d )
            ans.push_back( { n, n_ / _d } );
    }

    int square_cnt = 0;
    for( int i = 0; i < ans.size(); ++i )
        if( ans[ i ].first == ans[ i ].second )
            ++square_cnt;

    cout << ans.size() * 2 - square_cnt << "\n";
    for( int i = 0; i < ans.size(); ++i )
        cout << ans[ i ].first << " " << ans[ i ].second << "\n";
    for( int i = ans.size() - 1; i >= 0; --i )
        if( ans[ i ].first != ans[ i ].second )
            cout << ans[ i ].second << " " << ans[ i ].first << "\n";
}