lightoj 1213 - Fantasy of a Summation 【数学计数】

题目链接:lightoj 1213 - Fantasy of a Summation

题意:求解

#include 

int cases, caseno;
int n, K, MOD;
int A[1001];

int main() {
    scanf("%d", &cases);
    while( cases-- ) {
        scanf("%d %d %d", &n, &K, &MOD);

        int i, i1, i2, i3, ... , iK;

        for( i = 0; i < n; i++ ) scanf("%d", &A[i]);

        int res = 0;
        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;
                    }
                    ...
                }
            }
        }
        printf("Case %d: %d\n", ++caseno, res);
    }
    return 0;
}

公式:
ans=knk1(ni=1a[i])

AC 代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e7 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
//const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
int MOD;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL a[1010];
LL Pow(LL a, int n) {
    LL ans = 1;
    while(n) {
        if(n & 1)
            ans = ans * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return ans;
}
int main()
{
    int t, kcase = 1; scanf("%d", &t);
    while(t--)
    {
        int n, k; scanf("%d%d%d", &n, &k, &MOD);
        LL sum = 0;
        for(int i = 0; i < n; i++) {
            scanf("%lld", &a[i]);
            add(sum, a[i]);
        }
        printf("Case %d: ", kcase++);
        if(k == 1) {
            printf("%lld\n", sum);
        }
        else {
            LL ans = 1LL * k * Pow(n, k-1) % MOD * sum % MOD;
            printf("%lld\n", ans);
        }
    }
    return 0;
}

你可能感兴趣的:(数学)