51Nod 1228 Bernoulli number
Topic Links:
Click to open link
Test instructions
S (k,n) =1^k+2^k+...+n^k
The power of natural numbers and the modulus of 1e9+7 are obtained.
Limit:
1<= n <= 10^18; 1 <= k <= 2000
Ideas:
Bernoulli number
S (k,n) =s (k,n) =1/(k+1) * (c (k+1,k) *b[k]* (n+1) ^1 + C (k+1,k-1) *b[k-1]* (n+1) ^2 + ... + c (k+1,0) *b[0]* (n+1) ^ (k+1)) (B[i] is the Bernoulli number)
and B[n] are:
B[N]=-1/(n+1) * (c (n+1,0) *b[0] + C (n+1,1) *b[1] + ... + c (n+1,n-1) *b[n-1])
So b[0] ... B[k] can be preprocessed by O (k^2), and then the O (k) can be calculated for each s (k,n).
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
/*51nod 1228 Test instructions S (k,n) =1^k+2^k+...+n^k The power of natural numbers and the modulus of 1e9+7 are obtained. Limit: 1<= n <= 10^18; 1 <= k <= 2000 Ideas: Bernoulli number S (k,n) =s (k,n) =1/(k+1) * (c (k+1,k) *b[k]* (n+1) ^1 + C (k+1,k-1) *b[k-1]* (n+1) ^2 + ... + c (k+1,0) *b[0]* (n+1) ^ (k+1)) (B[i] for last Number of profits) and B[n] are: B[N]=-1/(n+1) * (c (n+1,0) *b[0] + C (n+1,1) *b[1] + ... + c (n+1,n-1) *b[n-1]) So b[0] ... B[k] can be preprocessed by O (k^2), and then the O (k) can be calculated for each s (k,n). */ #include<iostream> #include<cstdio> usingnamespaceStd #defineLL__int64 ConstintMOD =1000000007; ll EXT_GCD (ll A, ll B, LL &x, LL &y) { if(b = =0) { x =1, y =0; returnA } LL ret = EXT_GCD (b, a% B, y, x); Y-= A/b * x; returnRet }
ll Inv (ll A,intMSeeking inverse element { ll d, x, y, t = (LL) m; D = EXT_GCD (A, t, X, y); if(d = =1)return(x% t + t)% T; return-1; }
ConstintN =2005;
LL B[n], c[n][n];
voidInit () { for(inti =0; i < N; ++i) c[i][0] = C[i][i] =1; for(inti =2; i < N; ++i) for(intj =1; J < N; ++J) C[I][J] = (C[i-1][J] + c[i-1][j-1])% MOD; b[0] =1; for(inti =1; i < N; ++i) { LL tmp =0; for(intj =0; J < I; ++J) TMP = (tmp + c[i +1][J] * b[j])% MOD; B[i] = (TMP *-(INV (i +1, MoD)% mod + MoD)% MoD; } }
LL P[n];
voidGao (ll N, ll K) { p[0] =1; for(inti =1; I <= K +1; ++i) P[i] = (P[i-1] * ((n +1) (% MoD))% MoD; LL ans =0; for(inti =0; I <= K; ++i) Ans = (ans + c[k +1][i] * B[i]% MOD * p[k +1-i])% MOD; Ans = (ans * INV (k +1, MoD)% mod + MoD)% MoD; printf"%i64d\n", ans); }
intMain () { Init (); intT LL N, K; scanf"%d", &t); while(t--) { scanf"%i64d%i64d", &n, &k); Gao (n, K); } return0; }
|
51Nod 1228 Bernoulli number