uva 10213 How Many Pieces of Land 分割圆 高精度

Problem:
一个椭圆上有N个点,把他们两两连接,问最多能划分成多少个部分。
solution:
有n个点,将一个圆分割成N个部分共需要:
r_G=\frac{1}{24}n(n^3-6n^2+23n-18)+1
详细推导过程:https://en.wikipedia.org/wiki/Dividing_a_circle_into_areas
http://www.arbelos.co.uk/Papers/Chords-regions.pdf

而题目中的数据量又要求我们必须要用高精度运算。
高精度运算模板:http://blog.csdn.net/cfarmerreally/article/details/52123946

note:
要多做题,掌握了大量的题型后再回头看就容易多了。

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;

#define ms(s) memset(s,0,sizeof(s))
typedef unsigned long long ULL;
typedef long long LL;

const int INF = 0x3fffffff;

const int maxLen = 1005;
struct BigInt{//not support negative
    long long num[maxLen];
    int digit = 0;

    BigInt() = default;
    BigInt(char a[]){
        int la = strlen(a);
        int i = la-8, j = 0;

        for(; i >= 0; i -= 8)
            sscanf(a+i,"%8d",&num[j++]);
        int t;  num[j] = 0;
        if(i+8 > 0){
            for(int k = 0; i+8 > 0; ++k){
                sscanf(a+k,"%1d",&t);
                num[j] = num[j]*10 + t;
                --i;
            }
            ++j;
        }
        num[j] = -1;  digit = j;
    }
    BigInt(int a){
        int j = 0;
        num[j++] = a%100000000;
        if(a >= 100000000)
            num[j++] = a/100000000;
        num[j] = -1;  digit = j;
    }
    BigInt(long long a){
        int j = 0;
        num[j++] = a%100000000;
        a /= 100000000;
        if(a > 0){
            num[j++] = a%100000000;
            a /= 100000000;
            if(a > 0){
                num[j++] = a%100000000;
            }
        }
        num[j] = -1;  digit = j;
    }

    int bigCmp(const BigInt rhs){
        if(digit > rhs.digit)
            return 1;
        else if(digit < rhs.digit)
            return -1;
        else{
            for(int i = digit-1; i >= 0; --i){
                if(num[i] > rhs.num[i])
                    return 1;
                else if(num [i] < rhs.num[i])
                    return -1;
            }
            return 0;
        }
    }

    BigInt operator + (const BigInt rhs){
        BigInt ans;  int idx = 0, carry = 0;

        while(num[idx]!=-1 && rhs.num[idx]!=-1){
            ans.num[idx] = num[idx]+rhs.num[idx]+carry;
            carry = ans.num[idx] / 100000000;
            ans.num[idx++] %= 100000000;
        }
        if(num[idx] != -1){
            while(num[idx] != -1){
                ans.num[idx] = num[idx] + carry;
                carry = ans.num[idx] / 100000000;
                ans.num[idx++] %= 100000000;
            }
        }
        else{
            while(rhs.num[idx] != -1){
                ans.num[idx] = rhs.num[idx] + carry;
                carry = ans.num[idx] / 100000000;
                ans.num[idx++] %= 100000000;
            }
        }
        if(carry > 0)
            ans.num[idx++] = carry;
        ans.num[idx] = -1;
        ans.digit = idx;

        return ans;
    }

    BigInt operator * (const int rhs){
        BigInt ans;  int carry = 0,idx = 0;

        if(rhs != 0){
            for(idx = 0; num[idx] != -1; ++idx){
                ans.num[idx] = num[idx]*rhs + carry;
                carry = ans.num[idx] / 100000000;
                ans.num[idx] %= 100000000;
            }
            if(carry > 0)
                ans.num[idx++] = carry;
        }
        else{
            ans.num[idx++] = 0;
        }
        ans.num[idx] = -1;
        ans.digit = idx;
        return ans;
    }

    BigInt operator - (const BigInt rhs){//l >= r
        BigInt ans;
        int idx = 0, carry = 0;
        while(rhs.num[idx] != -1){
            ans.num[idx] = num[idx] - rhs.num[idx] - carry;
            carry = 0;
            if(ans.num[idx] < 0){
                ans.num[idx] += 100000000;
                carry = 1;
            }
            ++idx;
        }
        while(num[idx] != -1){
            ans.num[idx] = num[idx] - carry;
            carry = 0;
            if(ans.num[idx] < 0){
                ans.num[idx] += 100000000;
                carry = 1;
            }
            ++idx;
        }
        ans.num[idx] = -1;
        ans.digit = idx;
        return ans;
    }

    void frac(int n){
        num[0] = 1;  num[1] = -1;  digit = 1;
        int carry, idx;
        for(int i = 2; i <= n; ++i){
            carry = 0;
            for(idx = 0; num[idx] != -1; ++idx){
                num[idx] = num[idx]*i + carry;
                carry = num[idx] / 100000000;
                num[idx] %= 100000000;
            }
            if(carry > 0)
                num[idx++] = carry;
            num[idx] = -1;
            digit = idx;
        }
    }

    long long operator % (const int rhs){
        int idx = digit-1;
        long long r = num[idx--]%rhs;
        while(idx >= 0){
            r = (r*100000000LL+num[idx])%rhs;
            --idx;
        }
        return r;
    }

    BigInt operator / (const int rhs){
        BigInt ans;
        int i = digit-1, r = 0;
        while(i >= 0){
            ans.num[i] = (num[i]+r*100000000LL)/(long long)rhs;
            r = (num[i]+r*100000000LL)%(long long)rhs;
            --i;
        }
        ans.digit = digit;
        while(ans.digit > 1 && ans.num[ans.digit-1] == 0)
            ans.digit--;
        ans.num[ans.digit] = -1;
        return ans;
    }

    void Print(){
        if(digit > 0){
            int i = digit-1;
            printf("%lld",num[i]);
            while(i--)
                printf("%08lld",num[i]);
            printf("\n");
        }
    }
};

int main(){
//    freopen("F:\\input.txt","r",stdin);
//    freopen("F:\\output.txt","w",stdout);
//    ios::sync_with_stdio(false);

    int s;
    LL n;
    scanf("%d",&s);
    BigInt ans;
    while(s--){
        scanf("%lld",&n);
        ans = (((BigInt(n)*n)*n)*n+(BigInt(n)*n)*23+BigInt(24)-((BigInt(n)*n)*n)*6-BigInt(n)*18)/24;
        ans.Print();
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值