0w1

JOI 2007 春合宿 Fermat ( Fastpow )

fermat: フェルマー方程式 (Fermat) - 2007年 日本情報オリンピック春合宿OJ | AtCoder
http://www.ioi-jp.org/camp/2007/2007-sp-tasks/2007-sp-day2_21.pdf
Although it is a known fact that no integer solution exists for x^n + y^n = z^n, for n ≥ 3, being a modular equation will make it no relation at all ( not sure though ). Anyways, I simply enumerated each ( x^n, y^n ), and it just worked. Maybe the computers got better in the recent 9 years. Really wondering if this is supposed to be correct.

int P, N;

int int_pow( int v, int n ){
    int res = 1;
    while( n ){
        if( n & 1 ) ( res *= v ) %= P;
        ( v *= v ) %= P;
        n >>= 1;
    }
    return res;
}

void solve(){
    int ans = 0;
    cin >> P >> N;

    vector< int > cnt( P );
    for( int i = 0; i < P; ++i )
        ++cnt[ int_pow( i, N ) ];

    for( int i = 0; i < P; ++i )
        for( int j = 0; j < i; ++j )
            ans += 2 * cnt[ i ] * cnt[ j ] * cnt[ ( i + j ) % P ];

    for( int i = 0; i < P; ++i )
        ans += cnt[ i ] * cnt[ i ] * cnt[ ( i + i ) % P ];

    cout << ans << "\n";
}