Description
The sum of the m
th powers of the first n integers
S(n,m) = SUM ( j= 1 to n)( j m)
Can be written as a polynomial of degree m+1 in n:
S(n,m) = SUM (k = 1 to m+1)(F(m,k) *n k)
Fo example:
The coefficients F(m,k) of these formulas form Faulhaber‘s Tr angle:
where rows m start with 0 (at the top) and columns k go from 1 to m+1
Each row of Faulhaber‘s Tr angle can be computed from the previous row by:
a) The element in row i and column j ( j>1) is (i/j )*(the element above left); that is:
F(i,j ) = (i/j )*F(i-1, j-1)
b) The first element in each row F(i,1) is chosen so the sum of the elements in the row is 1
Write a program to find entries in Faulhaber‘s Tr angle as decimal f actions in lowest terms
S(n,m) = SUM ( j= 1 to n)( j m)
Can be written as a polynomial of degree m+1 in n:
S(n,m) = SUM (k = 1 to m+1)(F(m,k) *n k)
Fo example:

The coefficients F(m,k) of these formulas form Faulhaber‘s Tr angle:

where rows m start with 0 (at the top) and columns k go from 1 to m+1
Each row of Faulhaber‘s Tr angle can be computed from the previous row by:
a) The element in row i and column j ( j>1) is (i/j )*(the element above left); that is:
F(i,j ) = (i/j )*F(i-1, j-1)
b) The first element in each row F(i,1) is chosen so the sum of the elements in the row is 1
Write a program to find entries in Faulhaber‘s Tr angle as decimal f actions in lowest terms
Input
The first line of input contains a single integer P, (1 <= P <= 1000), which is the number of data sets that follow. Each data set should be processed identically and independently
Each data set consists of a single line of input consisting of three space separated decimal integers The first integer is the data set number. The second integer is row number m, and the third integer is the index k within the row of the entry for which you are to find F(m, k), the Faulhaber‘s Triangle entry (0 <= m <= 400, 1 <= k <= n+1).
Each data set consists of a single line of input consisting of three space separated decimal integers The first integer is the data set number. The second integer is row number m, and the third integer is the index k within the row of the entry for which you are to find F(m, k), the Faulhaber‘s Triangle entry (0 <= m <= 400, 1 <= k <= n+1).
Output
For each data set there is a single line of output. It contains the data set number, followed by a single space which is then followed by either the value if it is an integer OR by the numerator of the entry, a forward slash and the denominator of the entry.
Sample Input
4 1 4 1 2 4 3 3 86 79 4 400 401
Sample Output
1 -1/30 2 1/3 3 -22388337 4 1/401
此题的递归方程已经给出:F(i,j ) = (i/j )*F(i-1, j-1),就是在处理成分数时比较麻烦,中间求公约数时出了点问题,一晚上无限纠结啊
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 100000
using namespace std;
__int64 f[405][405][2],MIN,sum1,sum2;//f[i][j][1]表示分子,f[i][j][2]表示分母
__int64 getsMIN(__int64 x,__int64 y)//求最大公约数用来约分,不然__int64会爆
{
x=(x<0)?-x:x;
y=(y<0)?-y:y;
__int64 t=x%y;
while(t!=0)
{
x=y;
y=t;
t=x%y;
}
return y;
}
int main()
{
f[0][1][1]=1;f[0][1][2]=1;
f[1][2][1]=1;f[1][2][2]=2;
f[1][1][1]=1;f[1][1][2]=2;
int i,j,P,T,m,k,ans;
for(i=2;i<=400;i++)
{
for(j=i+1,sum1=0,sum2=1;j>1;j--)
{
f[i][j][1] = i*f[i-1][j-1][1];
f[i][j][2] = j*f[i-1][j-1][2];
MIN=getsMIN(f[i][j][1],f[i][j][2]);
f[i][j][1] /= MIN;
f[i][j][2] /= MIN;//约分
// cout<<"f["<<i<<"]["<<j<<"][1]: f["<<i<<"]["<<j<<"][2]"<<endl;
// cout<<f[i][j][1]<<"/"<<f[i][j][2]<<endl;
if(sum2%f[i][j][2])
{
sum1 = sum1*f[i][j][2]+f[i][j][1]*sum2;
sum2 *= f[i][j][2];
}
else sum1 = sum1 + f[i][j][1]*(sum2/f[i][j][2]);
MIN=getsMIN(sum1,sum2);
sum1 /= MIN;
sum2 /= MIN;
}
f[i][1][2] = sum2;
f[i][1][1] = sum2-sum1;//最后一个只能用1减其余分数的和
MIN=getsMIN(f[i][1][1],f[i][1][2]);
// cout<<"MIN "<<MIN<<endl;
f[i][1][1] /= MIN;
f[i][1][2] /= MIN;
// cout<<"f["<<i<<"]["<<j<<"][1]: f["<<i<<"]["<<j<<"][2]"<<endl;
// cout<<f[i][1][1]<<"/"<<f[i][1][2]<<endl;
}
// cout<<f[3][1]<<endl;
scanf("%d",&P);
while(P--)
{
scanf("%d %d %d",&T,&m,&k);
if(!f[m][k][1])
printf("%d 0\n",T);
else
{
if(f[m][k][2]==1)
printf("%d %I64d\n",T,f[m][k][1]);
else printf("%d %I64d/%I64d\n",T,f[m][k][1],f[m][k][2]);
}
}
return 0;
}