题意:求
为毛一直都没有人告诉我上面的幂级数展开少了个 (−1)i 。。。。调了好久啊TAT
奇怪的Code:(懒得删注释了)
#include
//using namespace std;
#define rep(i,a,b) for(int i = a , _ = b ; i <= _ ; i ++)
#define per(i,a,b) for(int i = a , _ = b ; i >= _ ; i --)
#define cr(x) memset(x , 0 , sizeof x)
#define gprintf(...) //fprintf(stderr , __VA_ARGS__)
typedef long long ll;
const int maxp = 100007;
const int maxk = 121;
ll mod , n , s[maxk][maxk] , inva[maxp];
ll powa[maxp][maxk] , S[maxk] , powp[maxk];
inline ll add(ll a , ll b , ll mod) { a = (a + b) % mod ; if (a < 0) a += mod ; if (a >= mod) a -= mod ; return a ; }
inline ll dec(ll a , ll b , ll mod) { a = (a - b) % mod ; if (a < 0) a += mod ; if (a >= mod) a -= mod ; return a ; }
inline ll qmul(ll a , ll b , ll p) {
return (a * b - (ll)(a / (double)p * b + 1e-3) * p + p) % p;
}
#define neg(x) (((x) & 1) ? -1 : 1)
int p , k;
void input() {
scanf("%d%d%lld" , &p , &k , &n);
mod = 1;
rep (i , 1 , k) mod = mod * p;
}
void init_stirling(int k , ll mod) {
// gprintf("initalization of stirling mod %lld\n" , mod);
s[0][0] = 1;
s[1][0] = 0 , s[1][1] = 1;
rep (i , 2 , k) {
s[i][0] = 0;
rep (j , 1 , k) {
s[i][j] = add(qmul(i - 1 , s[i - 1][j] , mod) , s[i - 1][j - 1] , mod);
// gprintf("%lld%c" , s[i][j] , j == k ? '\n' : ' ');
}
}
}
void init_inv(int k , ll mod) {
// gprintf("calculating inverse of mod %lld\n" , mod);
inva[1] = 1;
powa[1][1] = 1;
rep (i , 2 , p - 1) {
inva[i] = dec(0 , qmul(inva[mod % i] , mod / i , mod) , mod);
// gprintf("%lld\n" , qmul(inva[i] , i , mod));
powa[i][1] = inva[i];
}
powp[0] = 1;
rep (i , 1 , k - 1) powp[i] = powp[i - 1] * p;
rep (a , 1 , p - 1) {
ll t = inva[a];
rep (i , 2 , k)
powa[a][i] = qmul(powa[a][i - 1] , t , mod);
}
}
namespace ASS {
#include
static ll tmp[maxk];
static ll _pow[100 * 10][25];
void _S(ll n , ll mod) {
rep (i , 1 , n) {
_pow[i][1] = i;
rep (j , 2 , k)
_pow[i][j] = qmul(_pow[i][j - 1] , i , mod);
}
rep (i , 0 , k) {
tmp[i] = 0;
rep (j , 1 , n)
tmp[i] = add(tmp[i] , _pow[j][i] , mod);
if (!i) tmp[i] = (n + 1) % mod;
gprintf("%lld%c" , tmp[i] , i == k ? '\n' : ' ');
assert(tmp[i] == S[i]);
}
}
}
void get_S(ll n , ll mod , int k) {
// gprintf("calculating sum %lld of power %d\n" , n , k);
S[0] = (n + 1) % mod;
// if (!n) S[0] = 1;
if (n < k) {
static ll pown[maxk][maxk];
rep (i , 1 , n) {
pown[i][1] = i;
rep (j , 2 , k)
pown[i][j] = qmul(pown[i][j - 1] , i , mod);
}
rep (i , 1 , k) {
S[i] = 0;
rep (j , 1 , n)
S[i] = add(S[i] , pown[j][i] , mod);
}
} else {
int cur_k = -1;
rep (K , 1 , k) {
S[K] = 1;
rep (j , 0 , K) {
if ((n + 1 - j) % (K + 1) == 0)
S[K] = qmul((n + 1 - j) / (K + 1) , S[K] , mod);
else
S[K] = qmul(n + 1 - j , S[K] , mod);
}
int cur_i = cur_k;
rep (i , 0 , K - 1) {
S[K] = dec(S[K] , cur_i * qmul(s[K][i] , S[i] , mod) , mod);
cur_i = - cur_i;
}
cur_k = - cur_k;
}
}
#ifdef DEBUG
rep (i , 0 , k) {
gprintf("%lld%c" , S[i] , i == k ? '\n' : ' ');
}
ASS::_S(n , mod);
#endif
}
ll G(ll n , ll mod , int k) {
init_inv(k , mod);
init_stirling(k , mod);
// gprintf("calculating G(%lld , %lld)\n" , n , mod);
ll ret = 0;
if (n % p == 0) {
get_S(n / p - 1 , mod , k);
rep (a , 1 , p - 1)
rep (i , 0 , k - 1) {
// gprintf("p^%d : %lld , 1/%d^(%d+1) %lld , S[%d] : %lld\n" , i , powp[i] , a , i , powa[a][i + 1] , i , S[i]);
ret = add(ret , neg(i) * qmul(qmul(powp[i] , powa[a][i + 1] , mod) , S[i] , mod) , mod);
}
} else {
if (n < p) {
rep (i , 1 , n)
ret = add(ret , inva[i] , mod);
gprintf("result of G(%lld , %lld) = %lld\n" , n , mod , ret);
return ret;
}
ll r = n % p;
get_S(n / p , mod , k);
rep (a , 1 , r)
rep (i , 0 , k - 1) {
// gprintf("p^%d : %lld , 1/%d^(%d+1) %lld , S[%d] : %lld\n" , i , powp[i] , a , i , powa[a][i + 1] , i , S[i]);
ret = add(ret , neg(i) * qmul(qmul(powp[i] , powa[a][i + 1] , mod) , S[i] , mod) , mod);
}
get_S(n / p - 1 , mod , k);
rep (a , r + 1 , p - 1)
rep (i , 0 , k - 1)
ret = add(ret , neg(i) * qmul(qmul(powp[i] , powa[a][i + 1] , mod) , S[i] , mod) , mod);
}
gprintf("result of G(%lld , %lld) = %lld\n" , n , mod , ret);
return ret;
}
ll F(ll n , ll mod , int k) {
if (!n) return 0;
return (G(n , mod , k) + F(n / p , mod * p , k + 1) / p) % mod;
}
void solve() {
// printf("%lld\n" , G(102728 , 823543));
ll ans = F(n , mod , k);
printf("%lld\n" , ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("math.in" , "r" , stdin);
#endif
input();
solve();
return 0;
}