hdu 4488 Faulhaber’s Triangle

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


《C++编程实例100篇》是一本深入实践、极具价值的编程教程,它针对C++编程语言提供了丰富的实例,旨在帮助读者更好地理解和掌握C++的各项特性与编程技巧。这本书的经典之处在于它将理论与实践相结合,通过100个精心设计的编程实例,覆盖了C++的各个核心领域,包括基础语法、面向对象编程、模板、异常处理、STL(标准模板库)等。 我们来探讨C++的基础语法。C++是C语言的增强版,它保留了C语言的高效性和灵活性,并引入了类、对象和继承等面向对象编程概念。基础语法包括变量声明、数据类型、运算符、控制结构(如if语句、for循环、while循环)、函数的定义和调用等。在实例中,你可能会遇到如何编写简单的程序,如计算两个数的和,或者实现一个简单的猜数字游戏。 C++的面向对象编程是其一大特色。通过类和对象,你可以构建复杂的软件系统。类是对象的蓝图,它定义了对象的属性和行为。实例化一个类,就是创建一个具体的对象。继承允许你创建新的类,这些类从现有的类派生,共享其属性和方法,同时可以添加新的功能。多态性是面向对象的另一个关键特性,它使得不同类型的对象可以对同一消息作出不同的响应。这些概念在实例中会以各种形式展现,例如设计一个图形界面的类层次,或实现一个简单的模拟游戏。 接下来是模板,C++的模板功能让代码更加通用,可以处理不同类型的数据。模板分为函数模板和类模板,前者可以创建泛型函数,后者可以创建泛型类。通过模板,你可以编写出高效且灵活的代码,比如实现一个通用的排序算法。 异常处理是C++中用于处理程序运行时错误的机制。当程序出现异常情况时,可以抛出一个异常,然后在适当的点捕获并处理这个异常。这使得代码能够优雅地处理错误,而不是让程序崩溃。实例中可能会有涉及文件操作或网络通信时可能出现的异常处理示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值