hdu4488 Faulhaber’s Triangle(模拟)

来源:互联网 发布:算法基础 打开算法之门 编辑:程序博客网 时间:2024/06/09 17:34

Faulhaber’s Triangle

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


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
 

Source
Greater New York 2012

题目大意:给出自然数幂和的系数矩阵公式,要求输出某个系数。

题目分析:公式给了,直接模拟输出即可。因为是分数表示的,所以用一个二维结构体存储每个数即可。

稍微细心点即可。

详情请见代码:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<cctype>#include<map>#include<set>#include<queue>#include<vector>using namespace std;const int N = 405;const int INF = 0x3f3f3f3f;const double eps = 1e-5;const double PI = acos(-1.0);typedef __int64 ll;struct node{    bool sig;    ll fz,fm;}lcm[N][N];ll gcd(ll m,ll n){    while(n)    {        ll tmp = m % n;        m = n;        n = tmp;    }    return m;}int main(){    int t,i,j,cas;    int m,k;    lcm[0][1].sig  = true;    lcm[0][1].fm = lcm[0][1].fz = 1;    for(i = 1;i <= 400;i ++)    {        for(j = 2;j <= i + 1;j ++)        {            lcm[i][j].sig = lcm[i - 1][j - 1].sig;            lcm[i][j].fz = i * lcm[i - 1][j - 1].fz;            lcm[i][j].fm = j * lcm[i - 1][j - 1].fm;            ll tmp = gcd(lcm[i][j].fm,lcm[i][j].fz);            lcm[i][j].fm /= tmp;            lcm[i][j].fz /= tmp;        }        ll tfz = 1;        ll tfm = 1;        bool sig = true;        for(j = 2;j <= i + 1;j ++)        {            if(sig)            {                if(lcm[i][j].sig)                    tfz = tfz * lcm[i][j].fm - tfm * lcm[i][j].fz;                else                    tfz = tfz * lcm[i][j].fm + tfm * lcm[i][j].fz;            }            else            {                if(lcm[i][j].sig)                    tfz = -tfz * lcm[i][j].fm - tfm * lcm[i][j].fz;                else                    tfz = -tfz * lcm[i][j].fm + tfm * lcm[i][j].fz;            }            if(tfz > 0)                sig = true;            else            {                tfz = -tfz;                sig = false;            }            tfm = tfm * lcm[i][j].fm;            ll tmp = gcd(tfz,tfm);            tfz /= tmp;            tfm /= tmp;        }        lcm[i][1].sig = sig;        lcm[i][1].fm = tfm;        lcm[i][1].fz = tfz;    }    scanf("%d",&t);    while(t --)    {        scanf("%d%d%d",&cas,&m,&k);        printf("%d ",cas);        if(lcm[m][k].fz == 0)            printf("0\n");        else        {            if(lcm[m][k].sig == false)                putchar('-');            if(lcm[m][k].fm == 1)                printf("%I64d\n",lcm[m][k].fz);            else                printf("%I64d/%I64d\n",lcm[m][k].fz,lcm[m][k].fm);        }    }    return 0;}//140MS3492K

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米note2开关键不灵敏怎么办 大学生论文检测系统忘记密码怎么办 快手实验室没有k歌怎么办 皮肤一到冬天就会起皮脱落怎么办 嘴上的皮撕破了怎么办 身体皮肤干燥起皮怎么办 把公司数据库弄宕机了怎么办 地铁上临时变站怎么办 大学实践课挂了怎么办 军训的人长得矮怎么办 军训脸被晒黑了怎么办 晚上吃了油炸的怎么办 楼梯又陡又窄怎么办 小孩不认真做作业怎么办 水厂出水水质不达标怎么办 野猪铁夹子夹到怎么办 燃气灶开孔尺寸大了怎么办 苹果电脑视频显示缺少插件怎么办 安装拼多多显示校验失败怎么办 中央庭黑盒被抢怎么办 lab值a值偏高怎么办 橱房管道堵死了怎么办 微信被别的手机同步登录怎么办 计算机机房空调坏了怎么办 被蝎子蛰了怎么办怎么处理? 月嫂走了孩子闹怎么办 转业军人自建房有困难怎么办 转业军人没配偶想异地安置怎么办 转业军人档案和身份证不符怎么办 转业军人个人资被单位遗失怎么办 空军飞行学员停飞后怎么办 2020年不脱贫的农户怎么办 马自达8停产后配件怎么办 钢铁雄心3没将领怎么办 江苏低于二本线怎么办 电工证快过期了怎么办 天正建筑打不开高版本的图纸怎么办 打开天正只显示cad怎么办 天正软件显示过期了怎么办 四岁宝宝经常吐怎么办 穿越火线登陆不上去怎么办