首页 > ACM题库 > HDU-杭电 > hdu 4488-faulhaber’s triangle[解题报告]hoj
2015
07-17

hdu 4488-faulhaber’s triangle[解题报告]hoj

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

题目大意:就是让你求第m行第k个数,如果是整数就输出整数,否则就以分数的形式输出。

解题思路:比赛的时候我想如果一个一个的求肯定会超时,所以不敢下手,下来问别人,他们说的也是一个个求不会超时,我就自己写,结果老是re,原因就是除了0,哎郁闷啊,搞了一上午,式子别人都写了,还写不出来,汗颜!!

AC代码:

#include<stdio.h>
#include<math.h>
struct node
{
	long long x,y;
}a[402][402];
long long getmax(long long x,long long y)
{
	long long t;
	if(x<0)  x=-x;
	if(y<0) y=-y;
	t=x%y;
	while(t!=0)
	{
		x=y;
		y=t;
		t=x%y;
	}
	return y;
}
void init()
{
	int i,j;
	long long s1,s2,m;
	a[0][1].x=1;
	a[0][1].y=1;
	for(i=1;i<=400;i++)
	{
		s1=1;//s1表示分母的积,s2分子的和
		s2=0;
		for(j=2;j<=i+1;j++)
		{
			a[i][j].x=a[i-1][j-1].x*i;
			a[i][j].y=a[i-1][j-1].y*j;
			if(a[i][j].y!=0)//如果为0,不用化简也不用求和
			{
				s2=a[i][j].x*s1+s2*a[i][j].y;
				s1=a[i][j].y*s1;
				m=getmax(a[i][j].x,a[i][j].y);
				a[i][j].x/=m;
				a[i][j].y/=m;
				m=getmax(s2,s1);
				s1/=m;
				s2/=m;
			}
		}
		if(s1-s2==0)
		{
			a[i][1].x=0;
			a[i][1].y=0;//此处我把结果为0的,分子分母全设为0;
		}
		else
		{
			m=getmax(s1-s2,s1);
			a[i][1].x=(s1-s2)/m;
			a[i][1].y=s1/m;
		}
	}
}
int main()
{
	int p,m,k,q;
	scanf("%d",&p);
	init();
	while(p--)
	{
		scanf("%d%d%d",&q,&m,&k);
		if(a[m][k].y==0)  printf("%d %d\n",q,0);
		else if(a[m][k].x%a[m][k].y==0)  printf("%d %I64d\n",q,a[m][k].x/a[m][k].y);
		else printf("%d %I64d/%I64d\n",q,a[m][k].x,a[m][k].y);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。