利用P是素数所以有:
(1). A/B%P=((A%(B*P))/B)%p;
(2). A/B%P=A*(B`)%P 其中B`是B对于P的逆元
方法一: #include<stdio.h> int main() { int i, t, x, v = 1, n, m, ans; long long P, a[51000], sum; scanf("%d", &t); while (t-- && scanf("%d", &n)) { a[0] = 1; sum = 0; P = 1000003; P *= n; for (i = 1; i <= 40000; i++) { a[i] = 2 * a[i - 1]; if (a[i] >= P) a[i] -= P; } for (i = 0; i < n; i++) { scanf("%d", &x); sum += a[x]; if (sum >= P) sum -= P; } ans = sum / n; printf("Case %d:%d\n", v++, ans); } } 方法二: #include<stdio.h> #include<math.h> #define nmax 1000003 #define nnum 40001 int num[nnum], x, y; int extend_gcd(int a, int b) { if (b == 0) { x = 1, y = 0; return a; } int d = extend_gcd(b, a % b); int tx = x; x = y; y = tx - a / b * y; return d; } void init() { int i, te; for (i = 0, te = 1; i < nnum; i++) { num[i] = te; te = te * 2 % nmax; } } int main() { #ifndef ONLINE_JUDGE freopen("t.txt", "r", stdin); #endif int t, n, i, j, k; long long res; init(); while (scanf("%d", &t) != EOF) { for (i = 1; i <= t; i++) { scanf("%d", &n); for (j = 0, res = 0; j < n; j++) { scanf("%d", &k); res += num[k]; if (res >= nmax) { res -= nmax; } } extend_gcd(n, nmax); x = (x % nmax + nmax) % nmax; res = res * x % nmax; printf("Case %d:%I64d\n", i, res); } } return 0; }