中石油训练赛 - Faulhaber’s Triangle(打表)
題目描述
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題目鏈接:點擊查看題目大意:給出公式:
初始化F(0,1)=1,求指定位置的值
題目分析:這個題目給了很多無用的信息,但讀懂需要打表后寫一個函數打完表然后直接查詢就行了,為了防止爆范圍,我特地用了long long分別儲存分子和分母,并寫了一個化簡函數,每次都除以兩個數的gcd來約分,并且將分母上的符號轉移到分子上方便輸出,直接上代碼吧,簡單打表:
#include<iostream> #include<string> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long LL;const int N=500;LL a[N][N],b[N][N];//分子,分母 void huajian(LL &a,LL &b) {LL gcd=__gcd(a,b);a/=gcd;b/=gcd;if(a<0&&b<0)//如果都為負數,全部轉正{a=-a;b=-b;}if(a>=0&&b<0)//如果符號在分母上,則轉移到分子上{a=-a;b=-b;} }void init() {a[0][1]=1;b[0][1]=1;for(int i=1;i<=400;i++){LL suma=0;//suma和sumb儲存第2~i+1列的值的總和LL sumb=1;//初始化為0/1(分數形式)for(int j=2;j<=i+1;j++){a[i][j]=i*a[i-1][j-1];b[i][j]=j*b[i-1][j-1];huajian(a[i][j],b[i][j]);LL tempa=suma*b[i][j]+sumb*a[i][j];//根據通分求和化簡的LL tempb=sumb*b[i][j];huajian(tempa,tempb);suma=tempa;sumb=tempb;}a[i][1]=sumb-suma;//最后給F(i,1)賦值b[i][1]=sumb;huajian(a[i][1],b[i][1]);} }int main() {init();int w;cin>>w;while(w--){int num,x,y;scanf("%d%d%d",&num,&x,&y);printf("%d %lld",num,a[x][y]);if(b[x][y]!=1)printf("/%lld",b[x][y]);printf("\n");}return 0; }?
總結
以上是生活随笔為你收集整理的中石油训练赛 - Faulhaber’s Triangle(打表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1036D V
- 下一篇: 中石油训练赛 - The King’s