【YBTOJ】【Luogu P1771】方程的解
题目链接:
题目大意:
给定 \(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;
}