HDU 3483

解法:http://www.cppblog.com/Yuan/archive/2010/08/13/123268.html

刚开始用long long,结果RuntimeError了N次,百思不得其解...

#include <iostream> using namespace std; const int maxn = 52; __int64 c[maxn][maxn], a[maxn][maxn], b[maxn][maxn], t[maxn][maxn]; __int64 N, x, M; void init() { int i, j; // 初始化组合数组 memset(c, 0, sizeof(c)); for (i = 0; i <= x; i++) { c[i][0] = c[i][i] = 1; for (j = 1; j < i; j++) { c[i][j] = (c[i-1][j] + c[i-1][j-1]) % M; } } // 初始化矩阵 memset(a, 0, sizeof(a)); for (i = 0; i <= x; i++) { for (j = 0; j <= i; j++) { a[i][j] = (c[i][j] * x) % M; } } memcpy(a[x+1], a[x], sizeof(a[x])); a[x+1][x+1] = 1; // 初始化单位矩阵 memset(b, 0, sizeof(b)); for (i = 0; i <= x+1; i++) { b[i][i] = 1; } } void mul(long long p[maxn][maxn], long long q[maxn][maxn]) { int i, j, k; memset(t, 0, sizeof(t)); for (i = 0; i <= x+1; i++) { for (j = 0; j <= x+1; j++) { for (k = 0; k <= x+1; k++) { t[i][j] = (t[i][j] + p[i][k] * q[k][j]) % M; } } } memcpy(q, t, sizeof(t)); } void cal() { while (N) { if (N & 1) { mul(a, b); } mul(a, a); N >>= 1; } } int main() { while (scanf("%I64d %I64d %I64d", &N, &x, &M), N > 0 && x > 0 && M > 0) { init(); cal(); printf("%I64d/n", b[x+1][0]); } return 0; } 

你可能感兴趣的:(c,2010)