hdu4927 Series 1(大数加减乘除)

来源:互联网 发布:sql identity属性 编辑:程序博客网 时间:2024/06/13 22:55

Series 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 691    Accepted Submission(s): 256


Problem Description
Let A be an integral series {A1, A2, . . . , An}.

The zero-order series of A is A itself.

The first-order series of A is {B1, B2, . . . , Bn-1},where Bi = Ai+1 - Ai.

The ith-order series of A is the first-order series of its (i - 1)th-order series (2<=i<=n - 1).

Obviously, the (n - 1)th-order series of A is a single integer. Given A, figure out that integer.
 

Input
The input consists of several test cases. The first line of input gives the number of test cases T (T<=10).

For each test case:
The first line contains a single integer n(1<=n<=3000), which denotes the length of series A.
The second line consists of n integers, describing A1, A2, . . . , An. (0<=Ai<=105)
 

Output
For each test case, output the required integer in a line.
 

Sample Input
231 2 341 5 7 2
 

Sample Output
0-5
 

Author
BUPT
 


java:

import java.util.Scanner;import java.math.BigInteger;public class Main {    public static void main(String[] args) {         Scanner cin =  new Scanner(System.in);         BigInteger[] a;        a = new BigInteger[3005];        int cas, n;        cas    = cin.nextInt();;        for (int k = 0; k < cas; k++) {            n = cin.nextInt();            for (int i = 0; i < n; i++)                a[i] = cin.nextBigInteger();            BigInteger ans = BigInteger.valueOf(0);            BigInteger c = BigInteger.valueOf(1);;            for (int i = 0; i < n; i++) {                BigInteger tmp = c.multiply(a[n-i-1]);                if (i%2 == 0)                    ans = ans.add(tmp);                else                    ans = ans.subtract(tmp);                tmp = c.multiply(BigInteger.valueOf(n-i-1));                c = tmp.divide(BigInteger.valueOf(i+1));            }            System.out.println(ans);        }    } }

大数加减乘除:

#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <queue>#include <map>#include <stack>#include <list>#include <vector>using namespace std;#define LL __int64#define M 100000000000int a[3010];LL c[300],ans[300],cc[300];void MUL(int x){for (int i=1;i<=c[0];i++)c[i]=c[i]*x;for (int i=1;i<=c[0];i++){c[i+1]+=c[i]/M;c[i]=c[i]%M;}while (c[c[0]+1]){c[0]++;c[c[0]+1]=c[c[0]]/M;c[c[0]]%=M;}}void DIV(int y){for (int i=c[0];i>1;i--){c[i-1]+=(c[i]%y)*M;c[i]/=y;}c[1]/=y;while (c[c[0]]==0) c[0]--;}void ADD(){int f=-1,i;if (ans[0]<0){if (-ans[0]>c[0]) f=1;else if (-ans[0]<c[0]) f=0;else{for (i=c[0];i>0;i--)if (ans[i]>c[i]){f=1;break;}else if (ans[i]<c[i]){f=0;break;}if (i==0){memset(ans,0,sizeof(ans));ans[0]=1;return;}}}else{int l=max(ans[0],c[0]);for (i=1;i<=l;i++){ans[i+1]+=(ans[i]+c[i])/M;ans[i]=(ans[i]+c[i])%M;}while (ans[ans[0]+1]){ans[0]++;ans[ans[0]+1]+=ans[ans[0]]/M;ans[ans[0]]%=M;}return;}if (f!=-1){if (f){for (i=1;i<=-ans[0];i++){ans[i]-=c[i];if (ans[i]<0){ans[i]+=M;ans[i+1]-=1;}}while (ans[-ans[0]]==0) ans[0]++;}else{for (i=1;i<=c[0];i++){ans[i]=c[i]-ans[i];if (ans[i]<0){ans[i]+=M;c[i+1]--;}}ans[0]=c[0];while (ans[ans[0]]==0) ans[0]--;}}}void SUB(){int i;if (ans[0]<0){int l=max(-ans[0],c[0]);for (i=1;i<=l;i++){ans[i+1]+=(ans[i]+c[i])/M;ans[i]=(ans[i]+c[i])%M;}while (ans[-ans[0]+1]) {ans[0]--;ans[-ans[0]+1]+=ans[-ans[0]]/M;ans[-ans[0]]%=M;}}else {int f=0;if (ans[0]>c[0]) f=1;else if (ans[0]<c[0]) f=0;else{for (i=ans[0];i>0;i--)if (ans[i]>c[i]){f=1;break;}else if (ans[i]<c[i]){f=0;break;}if (i==0){memset(ans,0,sizeof(ans));ans[0]=1;return;}}if (f){for (i=1;i<=ans[0];i++){ans[i]-=c[i];if (ans[i]<0){ans[i+1]--;ans[i]+=M;}}while (ans[ans[0]]==0) ans[0]--;}else{for (i=1;i<=c[0];i++){ans[i]=c[i]-ans[i];if (ans[i]<0){ans[i]+=M;c[i+1]--;}}ans[0]=-c[0];while (ans[-ans[0]]==0) ans[0]++;}}}int main(){int T,n,i,x,y;scanf("%d",&T);while (T--){scanf("%d",&n);for (i=1;i<=n;i++)scanf("%d",&a[i]);memset(c,0,sizeof(c));memset(ans,0,sizeof(ans));int j=0;   //0- 1+ c[0]=1;c[1]=1;ans[0]=-1;ans[1]=a[1];if (n&1) {ans[0]=1;j=1;}x=n-1,y=1;for (i=2;i<=n;i++){j=(j+1)%2;MUL(x);x--;DIV(y);y++;memcpy(cc,c,sizeof(c));MUL(a[i]);if (j) ADD();else SUB();memcpy(c,cc,sizeof(cc));}if (ans[0]<0){printf("-");ans[0]=-ans[0];}printf("%I64d",ans[ans[0]]);for (i=ans[0]-1;i>0;i--)printf("%011I64d",ans[i]);printf("\n");}return 0;}


0 0
原创粉丝点击