Faulhaber’s Triangle ( UVALIVE 6176)
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 ≤ m + 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
题意:
定义如下:
例如:
可以看成是 401 行401列的二维数组,每一位上都对应着一个分子和一个分母
除去第一行之外,有相应的公式来求(第一个图片)
对于第一行来说,其值等于 1 - ( 这一行值的总和)
例如第4行(从第0行开始)
F(4,1) = 1 - ( 1/3 + 1/2 + 1/5 ) = - 1/30
思路: 打表
注意~~~ 要用 long long (哇了)
从第二列开始用公式求,同时对分子和分母进行约分(找出最大公约数,然后化简),如果 F( i-1, j-1 )等于0的话,这个数的分子和分母也为 0
对于第一列:
从第二列之后开始对分母求最小公倍数(LCM),进行通分
第一个数字等于 (分子之和-LCM ) / LCM
需要注意两个情况:
①:如果分子之和-LCM ==0那么此数为0
②:同事需要进行约分操作
例如:
1/4 + 1/3 + 1/2 最小公倍数为 12
等于 3/12 + 4/12 + 6/12 = 13/12
那么第一个数字为 (12-13 ) / 12 = -1/12
输出的时候注意:
①:如果分子分母都为0,输出0
②:如果分母为1,只输出分子
③:如果分母为负,分子为正,取一下相反数在输出
例如上述样例中,-1 和 12 的最大公约数为 -1,约分之后分子为正,分母为负,所以特判一下
CODE:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
typedef long long LL;
using namespace std;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
const int MAX = 403;
// 注意要用 long long !!
struct NODE
{
LL a;
LL b;
}node[MAX][MAX];
int gcd(LL a,LL b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int F()
{
for(int i=2;i<=401;i++)
{
for(int j=2;j<=i+1;j++)
{
// 公式
LL x=i*node[i-1][j-1].a;
LL y=j*node[i-1][j-1].b;
if(x==0||y==0)
{
node[i][j].a=0;
node[i][j].b=0;
continue;
}
// 约分
int num=gcd(x,y);
node[i][j].a=x/num;
node[i][j].b=y/num;
}
// 求分母的最小公倍数
LL lcm=1;
for(int j=2;j<=i+1;j++){
if(node[i][j].a!=0&&node[i][j].b!=0)
lcm=node[i][j].b/gcd(node[i][j].b,lcm)*lcm;
}
// 求分子的和
LL ans=0;
for(int j=2;j<=i+1;j++)
if(node[i][j].a!=0&&node[i][j].b!=0){
ans+=node[i][j].a*(lcm/node[i][j].b);
}
LL sum = lcm-ans;
if(sum==0)
{
node[i][1].a=0;
node[i][1].b=0;
continue;
}
LL num=gcd(sum,lcm);
node[i][1].a=sum/num;
node[i][1].b=lcm/num;
}
}
int main()
{
int t,k,n,m;
node[0][1].a=1;
node[0][1].b=1;
node[1][1].a=1;
node[1][1].b=2;
node[1][2].a=1;
node[1][2].b=2;
F();
cin>>t;
while(t--)
{
cin>>k>>m>>n;
if(m==0){
cout<<k<<' '<<1<<endl;
continue;
}
if(node[m][n].a==node[m][n].b&&node[m][n].a==0){
cout<<k<<' '<<0<<endl;
continue;
}
if(node[m][n].b==1){
cout<<k<<' '<<node[m][n].a<<endl;
continue;
}
if(node[m][n].b<0&&node[m][n].a>0){
node[m][n].a=-node[m][n].a;
node[m][n].b=-node[m][n].b;
}
cout<<k<<' '<<node[m][n].a<<'/'<<node[m][n].b<<endl;
}
}