Faulhaber’s Triangle---Contest1539 - 2019年我能变强组队训练赛第十一场

http://icpc.upc.edu.cn/contest.php?cid=1539

题目描述

The sum of the m-th powers of the first n integers

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

For example:
S(n, 1) = (1 + . . . + n) = (1/2) ∗ n2+ (1/2) ∗ n
S(n, 2) = (1 + . . . + n2) = (1/3) ∗ n3+ (1/2) ∗ n2+ (1/6) ∗ n
S(n, 3) = (1 + . . . + n3) = (1/4) ∗ n4+ (1/2) ∗ n3) + (1/4) ∗ n2
S(n, 4) = (1 + . . . + n4) = (1/5) ∗ n5+ (1/2) ∗ n4) + (1/3) ∗ n3 − (1/30) ∗ n
The coefficients F(m, k) of these formulas form Faulhaber’s Triangle:
1
1/2 1/2
1/6 1/2 1/3
0 1/4 1/2 1/4
-1/30 0 1/3 1/2 1/5
0 -1/12 0 5/12 1/2 1/6
1/42 0 -1/6 0 1/2 1/2 1/7
where rows m start with 0 (at the top) and columns k go from 1 to m + 1
Each row of Faulhaber’s Triangle can be computed from the previous row by:
a) The element in row i and column j (j > 1) is (i/j) ∗ (theelementaboveleft); 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 Triangle as decimal fractions in lowest terms .

输入

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 ≤ m + 1).

输出

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.

样例输入

复制样例数据

4
1 4 1
2 4 3
3 86 79
4 400 401

样例输出

1 -1/30
2 1/3
3 -22388337
4 1/401
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

ll gcd(ll a,ll b){
	while(b){
		ll t=a%b;
		a=b;
		b=t;
	}
	return a;
}

ll sh[500][500],xi[500][500];
int main(){
	ll t;
	cin>>t;
	//将三角形存储 
	sh[0][1]=1;xi[0][1]=1;
	for(ll i=1;i<=401;i++){
		ll z=0,m=1;
		for(ll j=2;j<=i+1;j++){
			sh[i][j]=i*sh[i-1][j-1];//计算分子 
			xi[i][j]=j*xi[i-1][j-1];//计算分母 
			ll d=gcd(sh[i][j],xi[i][j]);
			if(d!=0){
				sh[i][j]/=d;//约分 
				xi[i][j]/=d;
			}
			//将该行出第一个分数外的其他所有分数进行同分相加的模拟 
			z=m*sh[i][j]+z*xi[i][j];//分子 
			m*=xi[i][j];//分母 
			d=gcd(z,m);
			if(d!=0){
				z/=d;//约分 
				m/=d;
			}
		}
		//计算该行的第一个分数
		sh[i][1]=m-z;//分子 
		xi[i][1]=m;//分母 
	}
	while(t--){
		ll x,l,r;
		cin>>x>>l>>r;
		if(sh[l][r]==0){//分子是0,分数就是0 
			cout<<x<<" "<<0<<endl;
		}
		else if(sh[l][r]%xi[l][r]==0){//分数是整数 
			cout<<x<<" "<<sh[l][r]/xi[l][r]<<endl;
		}
		else if(xi[l][r]<0){//分母化为正数 
			cout<<x<<" "<<-sh[l][r]<<"/"<<-xi[l][r]<<endl;
		}//一般情况 
		else cout<<x<<" "<<sh[l][r]<<"/"<<xi[l][r]<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值