【YBTOJ】【Luogu P1771】方程的解
阿新 • • 發佈:2020-12-11
題目連結:
題目大意:
給定 \(k,n\),求出 \(k\) 個數之和等於 \(n^n\bmod 1000\) 的方案數是多少。
正文:
根據插板法容易得到答案就是 \(C_{\left(n^n\bmod 1000\right)-1}^{k-1}\)。這麼一來我們只要用高精度求組合數就能求出來了。
程式碼:
struct node { int a[1010]; int &operator [](int x){return a[x];} node() { memset (a, 0, sizeof a); } inline void print() { for (int i = a[0]; i >= 1; i--) printf ("%d", a[i]); } }c; node operator +(node A, node B) { int s = 0, g = 0; node C; int len = max(A[0], B[0]); for (int i = 1; i <= len + 10; i++) { s = A[i] + B[i] + g, g = s / 10; C[i]= (s % 10); if (!s && !g && i > len) break; C[0] = i; } return C; } node operator * (node A, int a) { int s = 0, g = 0; node C; int len = A[0]; for (int i = 1; i <= len + 10; i++) { s = A[i] * a + g, g = s / 10; C[i] = s % 10; if (!s && !g && i > len) break; C[0] = i; } return C; } node operator / (node A, int a) { int s = 0; node C; C[0] = A[0]; for (int i = A[0]; i; i--) { s = s * 10 + A[i]; C[i] = s / a, s %= a; } while (!C[C[0]] && C[0] > 1) --C[0]; return C; } int qpow(int a, int b) { a %= 1000; ll ans = 1; for(; b; b >>= 1, a = a * a % 1000) if(b & 1) ans = ans * a % 1000; return ans; } int n, m; int main() { scanf ("%d%d", &m, &n); n = qpow(n, n); n--, m--; c[0] = c[1] = 1; for (int t = 1; t <= m; t++) { c = c * (n - t + 1); c = c / t; } c.print(); return 0; }