HDU 4488 Faulhaber’s Triangle

来源:互联网 发布:淘宝刷销量被骗怎么办 编辑:程序博客网 时间:2024/06/02 11:16

Faulhaber’s Triangle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 396    Accepted Submission(s): 206


Problem Description
The sum of the mth powers of the first n integers
S(n,m) = SUM ( j= 1 to n)( jm)

Can be written as a polynomial of degree m+1 in n:

S(n,m) = SUM (k = 1 to m+1)(F(m,k) *nk)

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).
 

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
41 4 12 4 33 86 794 400 401
 

Sample Output
1 -1/302 1/33 -223883374 1/401
 
一个分数运算的模拟题:
输入m,k  输出F(m,k);
其中  F(i,j ) = (i/j )*F(i-1, j-1) 
并且每一行所有分数加起来等于1.
并且第i行的最后一个是1/(i+1);
根据以上规律就可以枚举所有F(i,j);
打个表,输出即可!

这个题其实不打表速度更快~(数据水嘛~)
然后就是得开long long 会爆int的!
在一点做这种题目 细心就行了!
这个题是比赛题目,但因为题目长没读~(长教训了!)

其中WA了一发,因为负号可能会出现在分母上,gcd时abs就解决了!
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib> using namespace std;typedef long long ll;const int maxn = 400 + 5;ll gcd(ll a,ll b) { return !b ? a : gcd(b,a%b); }struct Fr{ll fz,fm;Fr () : fz(1), fm(1) {}Fr (ll fz,ll fm): fz(fz), fm(fm){}Fr &operator += (const Fr & ff)  {ll g1 = gcd(abs(fm),abs(ff.fm));ll b1 = fm / g1 * ff.fm;fz = b1 / fm * fz + b1 / ff.fm * ff.fz;fm = b1;g1 = gcd(abs(fz),abs(fm));fm/= g1;fz/= g1;return *this;}Fr operator - (const Fr & ff)  {ll g1 = gcd(abs(fm),abs(ff.fm));ll b1 = fm / g1 * ff.fm;Fr t;t.fz = b1 / fm * fz - b1 / ff.fm * ff.fz;t.fm = b1;g1 = gcd(abs(fz),abs(fm));t.fm/=g1;t.fz/=g1;return t;}Fr operator * (const Fr & ff){Fr t(1,1);t.fz = fz * ff.fz;t.fm = fm * ff.fm;ll g = gcd(abs(t.fz),abs(t.fm));t.fz/=g;t.fm/=g;return t;}void show()const {if (fm == 1)printf("%I64d\n",fz);else printf("%I64d/%I64d\n",fz,fm);}}f[maxn][maxn];Fr unit(1,1);int T,cnt;int m,k;void init(){f[0][1].fz = 1;f[0][1].fm = 1;for (int i = 1; i < maxn; ++i){Fr tmp (1,i+1);f[i][i+1]=tmp;for (int j = i; j >= 1; --j){if (j > 1){Fr t (i,j);f[i][j] = t * f[i-1][j-1];tmp += f[i][j];}else f[i][j] = unit - tmp;}}}int main(){init();scanf("%d",&T);while(T--){scanf("%d%d%d",&cnt,&m,&k);printf("%d ",cnt);f[m][k].show();}return 0;}


0 0