Description
A Young schoolboy would like to calculate the sum
For some fixed natural k and different natural n. He observed that calculating IK for all I (1<=i<=n) and summing up results is a too slow a-do it, because the N Umber of required arithmetical operations increases as n increases. Fortunately, there is another method which takes only a constant number of operations regardless of N. It is possible to show the sum Sk (n) is equal to some polynomial of degree k+1 in the variable n with rational Coeffi Cients, i.e.,
We require that integer M is positive and as small as possible. Under this condition the entire set of such numbers (i.e. M, ak+1, AK, ..., A1, A0) would be a unique for the given K. You has to write a program to find such set of coefficients to help the schoolboy make his calculations quicker.
Input
The input file contains a single integer k (1<=k<=20).
Output
Write integer numbers M, ak+1, AK, ..., A1, A0 to the output file in the given order. Numbers should is separated by one space. Remember that's should write the answer with the smallest positive M possible.
Sample Input
2
Sample Output
6 2 3) 1 0
Source
Northeastern Europe 1997 "Analysis" test instructions is to give a K, find a minimum m so that the middle a[i] are all integers. This is a formula that involves Bernoulli numbers, which is basically impossible to do if you don't know.
1. The relationship between Bernoulli number and natural number power:
2. Bernoulli number recursion:
The Bernoulli number is obtained by recursion, then the 1 formula is used and the middle (n+1) ^ i is changed to n ^ i, followed by n ^ K.
1 /*2 Song Dynasty Zhu Dunru's3 "Xijiang Moon, the world is short like a Dream"4 the world is short like a dream, the human thin like autumn cloud. No need to care about elbow grease heart. Everything turns out to be alive. 5 Fortunately, three cups of wine is good, the situation every flower new. A laugh and a blind date. Tomorrow cloudy and clear undecided. 6 */7#include <cstdio>8#include <cstring>9#include <algorithm>Ten#include <cmath> One#include <queue> A#include <vector> -#include <iostream> -#include <string> the#include <ctime> - #defineLOCAL - Const intMAXN = -+Ten; - Const DoublePi = ACOs (-1.0); + using namespacestd; -typedefLong Longll; +ll GCD (ll A, ll b) {returnb = =0? A:GCD (b, a%b);} A structnum{ atll A, B;//Fractions, B is the denominator -Num (ll x =0, ll y =0) {a = X;b =y;} - voidUpdate () { -ll tmp =gcd (A, b); -A/=tmp; -b/=tmp; in } -Numoperator+ (ConstNum &c) { toll FZ = A * c.b + b * C.A, FM = b *c.b; + if(FZ = =0)returnNum (0,1); -ll tmp =GCD (FZ, FM); the returnNum (FZ/TMP, FM/tmp); * } $ }B[MAXN], A[MAXN];Panax Notoginseng ll C[MAXN][MAXN]; - the voidinit () { + //number of preprocessing combinations A for(inti =0; i < MAXN; i++) c[i][0] = C[i][i] =1; the for(inti =2; i < MAXN; i++) + for(intj =1; J < Maxn; J + +) C[i][j] = c[i-1][J] + c[i-1][j-1]; - //pre-processing Bernoulli number $b[0] = Num (1,1); $ for(inti =1; i < MAXN; i++){ -num TMP = num (0,1), add; - for(intj =0; J < I; J + +){ theAdd =B[j]; -ADD.A *= C[i +1][j];WuyiTMP = tmp +add; the } - if(TMP.A) tmp.b *=-(i +1); Wu tmp.update (); -B[i] =tmp; About } $ } - voidWork () { - intN; -scanf"%d", &n); All M = n +1, flag =0, Lcm; +a[0] = Num (0,1); the for(inti =1; I <= n +1; i++){ - if(B[n +1-I].A = =0) {A[i] = Num (0,1);Continue;} $Num tmp = B[n +1-i]; theTmp.a *= C[n +1][i];//C[n+1][i] = c[n + 1][n + 1-i] the tmp.update (); the if(Flag = =0) LCM = flag =tmp.b; theA[i] =tmp; - } inA[n] = A[n] + Num (n +1,1); the the for(inti =2; I <= n +1; i++){ About if(A[I].A = =0)Continue; theLCM = (LCM * a[i].b)/gcd (Lcm, a[i].b); the } the if(LCM <0) Lcm *=-1; +M *=Lcm; -printf"%lld", M); the for(inti = n +1; I >=0; i--) printf ("%lld", A[I].A * Lcm/a[i].b); Bayi } the the intMain () { - - init (); the Work (); the //printf ("%lld\n", C[5][3]); the return 0; the}
View Code
"POJ1707" "Bernoulli number" Sum of powers