HDU 4488Faulhaber’s Triangle(模拟 题目有公式)

Faulhaber’s Triangle

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


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

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

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

Fo example:



The coefficients F(m,k) of these formulas form Faulhaber‘s Tr angle:
HDU 4488Faulhaber’s Triangle(模拟 题目有公式)_第1张图片

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
 

                  题目大意: 前面说那个多项式求和直接无视,看下面的递推公式,由左上角可以得到右下角,p(i,j)=p(i-1,j-1)*(i.j).然后每一行的和为一。

          解题思路:从最后面算到用乘法最前面,然后统计每一行的和,第一个数用1-即可。不过需要约公约数,然后符号也要弄到分子里面去,然后是分母为1直接输出分子,分子为0直接输出0.

          题目地址:Faulhaber’s Triangle

AC代码:
#include
#include
#include
#include
#include
using namespace std;

__int64 gcd(__int64 m,__int64 n)
{
     while(n)
     {
          __int64 t=m%n;m=n;n=t;
     }
     return m;
}

struct node
{
     __int64 a;   //分子
     __int64 b;   //分母
};
node p[405][405];

node chengfa(node r,int i,int j)  //乘法
{
     node p;
     __int64 x,y;
     x=r.a*i,y=r.b*j;
     __int64 tmp=gcd(x,y);
     x=x/tmp,y=y/tmp;
     if((x<0&&y<0)||(x>0&&y<0))
          x=-x,y=-y;
     p.a=x,p.b=y;
     return p;
}

node jiafa(node sum,node r)    //加法
{
     if(sum.a==0)
          return r;
       else if(r.a==0)
          return sum;
        else
        {
             __int64 temp=gcd(sum.b,r.b);
             __int64 y=sum.b/temp*r.b;
             __int64 x=sum.a*(y/sum.b)+r.a*(y/r.b);
             __int64 tmp=gcd(x,y);
             x=x/tmp,y=y/tmp;
             if((x<0&&y<0)||(x>0&&y<0))
                    x=-x,y=-y;
             sum.a=x,sum.b=y;
             return sum;
        }
}

int main()
{
     int i,j;
     p[0][1].a=1,p[0][1].b=1;
     node sum;
     for(i=1;i<=400;i++)
     {
          sum.a=0;
          for(j=i+1;j>1;j--)
          {
               if(p[i-1][j-1].a==0)
                    p[i][j].a=0;
                 else
                   p[i][j]=chengfa(p[i-1][j-1],i,j);
               sum=jiafa(sum,p[i][j]);
          }
          p[i][1].b=sum.b,p[i][1].a=sum.b-sum.a;
     }
     /*for(i=1;i<=20;i++)
     {
          for(j=1;j<=i+1;j++)
          {
               if(p[i][j].a==0)
                    cout<<"0 ";
                else
                    cout<


你可能感兴趣的:(模拟)