编程介的小学生 2017-11-18 02:59 采纳率: 20.5%
浏览 647
已结题

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

  • 写回答

1条回答 默认 最新

  • HHi3s 2018-08-03 21:49
    关注
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    
    #define LL long long
    #define Maxn 405
    LL f[Maxn][Maxn],g[Maxn][Maxn];
    LL d;
    
    LL gcd(LL a,LL b)
    {
        if(b == 0) return a;
        return gcd(b,a%b);
    }
    void init()
    {
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        f[0][1] = g[0][1] = 1;
        LL fz,fm;
        for(int i=1;i<=400;i++)
        {
            fz = 0,fm = 1;
            for(int j=2;j<=i+1;j++)
            {
                f[i][j] = i * f[i-1][j-1];
                g[i][j] = j * g[i-1][j-1];
                d = gcd(f[i][j],g[i][j]);
                if(d!=0)
                {
                    f[i][j] /= d;
                    g[i][j] /= d;
                }
                fz = fz * g[i][j] + fm * f[i][j];
                fm *= g[i][j];
                d = gcd(fz,fm);
                if(d!=0)
                {
                    fz /= d;
                    fm /= d;
                }
            }
            f[i][1] = fm - fz;
            g[i][1] = fm;
        }
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    #endif
        init();
        int p;
        int m,r,c;
        scanf(" %d",&p);
        while(p--)
        {
            scanf(" %d %d %d",&m,&r,&c);
            printf("%d ",m);
            if(f[r][c] == 0) printf("0\n");
            else if(f[r][c] % g[r][c] == 0) printf("%lld\n",f[r][c]/g[r][c]);
            else if(g[r][c] < 0) printf("%lld/%lld\n",-f[r][c],-g[r][c]);
            else printf("%lld/%lld\n",f[r][c],g[r][c]);
        }
        return 0;
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献