Problem Description
This is a very simple problem. Given three integers N, x, and M, your task is to calculate out the following value:
Input
There are several test cases. For each case, there is a line with three integers N, x, and M, where 1 ≤ N, M ≤ 2*10
9, and 1 ≤ x ≤ 50.
The input ends up with three negative numbers, which should not be processed as a case.
The input ends up with three negative numbers, which should not be processed as a case.
Output
For each test case, print a line with an integer indicating the result.
Sample Input
100 1 10000 3 4 1000 -1 -1 -1
Sample Output
5050 444
代码:
求sum(x^k*k^x) k=1~N
x^(k+1)*(k+1)^x=x^k*x*(k+1)^x 然后用二项式定理展开(k+1)^x即可
例如当x=4时
| 1x 0 0 0 0 0 | |x^k*k^0| |x^(k+1)*(k+1)^0|
| 1x 1x 0 0 0 0 | |x^k*k^1| |x^(k+1)*(k+1)^1|
| 1x 2x 1x 0 0 0 |*|x^k*k^2|=|x^(k+1)*(k+1)^2|
| 1x 3x 3x 1x 0 0 | |x^k*k^3| |x^(k+1)*(k+1)^3|
| 1x 4x 6x 4x 1x 0 | |x^k*k^4| |x^(k+1)*(k+1)^4|
| 1x 4x 6x 4x 1x 1x | | S(k) | | S(k+1) |
*/
<pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
__int64 x, m, n,c[55][55];
struct Matrix
{
__int64 a[55][55];
void init()
{
memset(a, 0, sizeof(a));
}
};
Matrix mul(Matrix &a, Matrix &b)
{
Matrix t;
for (__int64 i = 0;i <=x+1;i++)
{
for (__int64 j = 0;j <= x+1;j++)
{
t.a[i][j] = 0;
for (__int64 k = 0;k <= x+1;k++)
{
t.a[i][j] += (a.a[i][k] * b.a[k][j]);
t.a[i][j] %= m;
}
}
}
return t;
}
Matrix pow(Matrix a, __int64 n)
{
Matrix t;
t.init();
for (__int64 i = 0;i <= x+1;i++)
t.a[i][i] = 1;
while (n)
{
if (n & 1)
t = mul(t, a);
a=mul(a,a);
n >>= 1;
}
return t;
}
Matrix init(Matrix a)
{
Matrix t;
t.init();
memset(c, 0, sizeof(c));
for (int i = 0; i <= x; i++)
{
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; j++)
{
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % m;
}
}
for (__int64 i = 0;i <= x;i++)
for (__int64 j = 0;j <= x;j++)
t.a[i][j] = c[i][j]* x%m;
for (__int64 i = 0;i <= x;i++)
{
t.a[x+1][i] = t.a[x ][i];
}
t.a[x+1][x+1] =1;
return t;
}
int main()
{
while (scanf("%I64d%I64d%I64d",&n,&x,&m)&&n>0&&m>0&&x>0 )
{
Matrix map,map1;
map=init(map);
map1 = pow(map, n -1);
__int64 sum=0, i;
for (i = 0;i <= x+1;i++)
{
sum =sum+ (map1.a[x+1][i])*x;
sum =sum% m;
}
printf("%I64d\n", sum%m);
}
return 0;
}