How Many Pieces of Land ? UVA - 1021 大整数类型

问题

分析

这道题就是道数学题,使用平面图中的欧拉公式,V-E+F=2
然后枚举一个点上的连出的所有的对角边,,对角边一边是i个点,另一边是n-2-i个点,这条边上有 { i ∗ ( n − 2 − i ) } \{i*(n-2-i)\} {i(n2i)}个交点,被分为了${i*(n-2-i)+1}段,一共有n个点,得到了公式
V = n + n / 4 ∑ i = 1 n − 3 { i ∗ ( n − 2 − i ) } V=n+n/4\sum_{i=1}^{n-3}\{i*(n-2-i)\} V=n+n/4i=1n3{i(n2i)}
E = n + n / 2 ∑ i = 1 n − 3 { i ∗ ( n − 2 − i ) + 1 } E=n+n/2\sum_{i=1}^{n-3}\{i*(n-2-i)+1\} E=n+n/2i=1n3{i(n2i)+1}
然后利用平方和公式 ∑ i = 1 n i 2 = n ( n + 1 ) ( 2 n + 1 ) 6 \sum_{i=1}^ni^2=\frac{n(n+1)(2n+1)}{6} i=1ni2=6n(n+1)(2n+1)简化计算
学到了一个:使用python自带高精度,用python3写:https://www.cnblogs.com/forth/p/9724109.html
Java大整数类型:https://www.2cto.com/kf/201502/374756.html
C++ 高精度(完全版): https://blog.csdn.net/zypang1/article/details/77651449

//下次还是用python写吧,下面就是调用函数了
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
class BigInteger{
    vector<int> s;
    static const int Base=10000;  //这里一定要加static,不然会报错
    static const int Width=4;
    BigInteger& operator = (long long num) {
        s.clear();
        do{
            s.push_back(num%Base);
            num/=Base;
        }while(num);
        return *this;
    }
    void clearZeros(){
        for(int i=s.size()-1;i>=0;--i){
            if(s[i]==0)
                s.pop_back();
        }
        if(s.empty()) s.push_back(0);
    }
    BigInteger& operator = (const string &num){
        s.clear();
        int len=(num.size()-1)/Width+1;
        int x=0;
        int end=num.size(),start=(len-1)*Width,l=end-start;
        for(int i=0;i<len;++i){
            s.push_back(stoi(num.substr(start,l)));
            start=start-Width;
            l=Width;
        }
        clearZeros(); //消除多余的0;
        return *this;
    }
    BigInteger operator + (const BigInteger &rhs) const {
        int len=max(s.size(),rhs.s.size()),carry=0; //进位
        BigInteger ans;
        for(int i=0;i<len;++i){
            int t=carry;
            if(i<s.size()) t+=s[i];
            if(i<rhs.s.size()) t+=rhs.s[i];
            carry=t/Base;
            ans.s.push_back(t%Base);
        }
        if(carry>0) ans.s.push_back(carry);
        return ans;
    }
    
    BigInteger operator * (const BigInteger &rhs) const {
        BigInteger ans;
        for(int i=0;i<rhs.s.size();++i){
            BigInteger temp;
            for(int k=0;k<i;++k){
                temp.s.push_back(0);  //结尾的0,这里的一位相当于10000
            }
            int carry=0;
            for(int j=0;j<s.size();++j){
                int result=s[j]*rhs.s[i]+carry;
                carry=result/Base;
                temp.s.push_back(result%Base);
            }
            while(carry>0){
                temp.s.push_back(carry%Base);
                carry/=Base;
            }
            ans=ans+temp;
        }
        return ans;
    }
    BigInteger operator - (const BigInteger&rhs) const {
        BigInteger ans;
        int carry=0;
        for(int i=0;i<s.size();++i){
            int diff=s[i]-carry;
            if(i<rhs.s.size()) diff-=rhs.s[i];
            carry=0;
            while(diff<0){
                ++carry;
                diff+=Base;
            }
            ans.s.push_back(diff);
        }
        ans.clearZeros();
        return ans;
    }

    BigInteger operator / (int rhs) const {
        BigInteger ans;
        vector<int> t;
        long long rmder=0;  //余数
        for(int i=s.size()-1;i>=0;--i){
            long long temp=rmder*Base+s[i];
            long long div=temp/rhs;
            rmder=temp%rhs;
            t.push_back(div);
        }
        for(int i=t.size()-1;i>=0;--i)  //倒换顺序
            ans.s.push_back(t[i]);
        ans.clearZeros();
        return ans;
    }

    friend ostream& operator << (ostream& out,const BigInteger&rhs){
        out<<rhs.s.back();
        for(int i=rhs.s.size()-2;i>=0;--i){
            char buf[5];
            sprintf(buf,"%04d",rhs.s[i]);  //填充0
            cout<<string(buf);
        }
        return out;
    }

    bool operator < (const BigInteger& rhs) const
    {
        if(s.size()!=rhs.s.size()) return s.size()<rhs.s.size();
        for(int i=s.size()-1;i>=0;--i)
        {
            if(s[i]!=rhs.s[i])
                return s[i]<rhs.s[i];
        }
        return false;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值