hdu4927 Series 1(大数加减乘除)

107 篇文章 0 订阅
37 篇文章 0 订阅

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 {A 1, A 2, . . . , A n}.

The zero-order series of A is A itself.

The first-order series of A is {B 1, B 2, . . . , B n-1},where B i = A i+1 - A i.

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 A 1, A 2, . . . , A n. (0<=A i<=10 5)
 

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

Sample Input
  
  
2 3 1 2 3 4 1 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 100000000000
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值