[规律] lightoj 1213

题意

for( i1 = 0; i1 < n; i1++ ) {
            for( i2 = 0; i2 < n; i2++ ) {
                for( i3 = 0; i3 < n; i3++ ) {
                    ...
                    for( iK = 0; iK < n; iK++ ) {
                        res = ( res + A[i1] + A[i2] + ... + A[iK] ) % MOD;
                    }
                    ...
                }
            }

代码

//我们很容易就知道最内成的加法式子执行了n^K次,每次加了K个数,
//所以一共加了K*n^K个数,一共有n个数,每个数加的次数一定是相同的,
//所以每个数都加了K*n^(K-1)次,所以结果就是Sum*K*n^(K-1)%mod;
//快速幂求一下即可;
// http://www.cnblogs.com/qq2424260747/p/4942627.html

#include 
#include 
#include 

typedef long long ll;

ll Pow ( int n, int k, int md ) {
    ll ans = 1;
    n %= md;
    while ( k ) {
        if ( k % 2 == 1 )
            ans = ( ans * n ) % md;
        n = ( n * n ) % md;
        k /= 2;
    }
    return ans;
}

int main () {
    int T;
    scanf ( "%d", &T );
    for ( int ks = 1; ks <= T; ++ks ) {
        int n, k, mod;
        scanf ( "%d%d%d", &n, &k, &mod );
        ll sum = 0;
        int ai;
        for ( int i = 0; i < n; ++i ) {
            scanf ( "%d", &ai );
            sum += ai;
        }

        ll ans = Pow ( n, k - 1, mod );
        ans = ( ans * k ) % mod;
        sum = ( sum * ans ) % mod;

        printf ( "Case %d: %lld\n", ks, sum );
    }
    return 0;
}

你可能感兴趣的:(ACM,14-数论基础)