[HDU4927] Series 1 && 高精度+组合数

高精度模板 存档
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstring>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXL = 300;
const int L = 10000;
struct Hp {
	int Len, A[MAXL+10];
	Hp (int x = 0) {
		memset(A, 0, sizeof(A)); Len = 0;
		while(x) { A[Len++] = x % L; x /= L; }
	}
	Hp fix() {
		while( Len && !A[Len-1]) Len--;
		return *this;
	}
	Hp operator = (int x) {
		memset(A, 0, sizeof(A)); Len = 0;
		while(x) { A[Len++] = x % L; x /= L; }
		return *this;
	}
	int operator [] (const int &pos) const { return A[pos]; }
	int & operator [] (const int &pos) { return A[pos]; }
   
	Hp operator + (const Hp &b) const {
		Hp c;
		c.Len = max(Len, b.Len) + 1;
		int add = 0;
		for(int i = 0; i < c.Len; i++) {
			add += A[i] + b[i];
			c[i] = add % L;
			add /= L;
		}
		return c.fix();
	}
	Hp operator - (const Hp &b) const {
		Hp c;
		c.Len = max(Len, b.Len);
		int del = 0;
		for(int i = 0; i < c.Len; i++) {
			del += A[i] - b[i];
			c[i] = del;
			del = 0;
			if(c[i] < 0) {
				int tmp = (-c[i] - 1) / L + 1;
				c[i] += tmp * L;
				del -= tmp;
			}
		}
		return c.fix();
	}
	Hp operator * (const Hp &b) const {
		Hp c;
		c.Len = Len + b.Len;
		for(int i = 0; i < Len; i++) {
			int mul = 0;
			for(int j = 0; j <= b.Len; j++) {
				mul += A[i] * b[j] + c[i + j];
				c[i + j] = mul % L;
				mul /= L;
			}
		}
		return c.fix();
	}
	Hp operator / (const int &b) const {
		Hp c;
		c.Len = Len;
		int over = 0;
		for(int i = Len-1; i >= 0; i--) {
			over = over * L + A[i];
			c[i] = over / b;
			over %= b;
		}
		return c.fix();
	}
	Hp operator += (const Hp  &b) { *this = *this + b; return *this; }
	Hp operator -= (const Hp  &b) { *this = *this - b; return *this; }
	Hp operator *= (const Hp  &b) { *this = *this * b; return *this; }
	Hp operator /= (const int &b) { *this = *this / b; return *this; }
	
	bool operator <  (const Hp &b) const {
		if(Len != b.Len) return Len < b.Len;
		for(int i = Len-1; i >= 0; i--) 
			if(A[i] != b[i]) return A[i] < b[i];
		return false;
	}
	bool operator >  (const Hp &b) const { return b < *this; }
	bool operator <= (const Hp &b) const { return !(b < *this); }
	bool operator >= (const Hp &b) const { return !(*this < b); }
	bool operator != (const Hp &b) const { return b < *this || *this < b; }
	bool operator == (const Hp &b) const { return !(b < *this) && !(*this < b); }
	
};
int a[L+10];
void print(const Hp &A)
{
	PF("%d", A[A.Len-1]);
	for(int i = A.Len-2; i >= 0; i--) PF("%04d", A[i]);
	puts("");
}
int main()
{
	int T; SF("%d", &T); while(T--) {
		int n;
		Hp ans = 0, add = 0, del = 0, C = 1;
		SF("%d", &n);
		for(int i = n; i >= 1; i--) SF("%d", &a[i]);
		for(int i = 1; i <= n; i++) {
			if(i > 1) C = C * (n - i + 1) / (i - 1);
			if(i & 1) add += C * a[i];
			else del += C * a[i];
		}
		if(add < del) {
			putchar('-'); ans = del - add;
		}
		else ans = add - del;
		print(ans);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值