• 大数板子


    #include<bits/stdc++.h>  
    using namespace std;  
    struct BigInteger  
    {  
        static const int BASE=10000;  
        static const int WIDTH=4;  
        vector<long long>s;  
          
        BigInteger(long long num=0) { *this=num; }
        BigInteger operator = (long long num) 
        {  
            s.clear();  
            do  
            {  
                s.push_back(num%BASE);  
                num/=BASE;  
            }while(num>0);  
            return *this;  
        }  
        BigInteger operator = (const string& str)
        {  
            s.clear();  
            int x,len=(str.length()-1)/WIDTH+1;  
            for(int i=0;i<len;i++)  
            {  
                int end=str.length()-i*WIDTH;  
                int start=max(0,end-WIDTH);  
                sscanf(str.substr(start,end-start).c_str(),"%d",&x);  
                s.push_back(x);  
            }  
            return *this;  
        }  
              
        BigInteger operator + (const BigInteger& b) const
        {  
            BigInteger c;  
            c.s.clear();  
            for(int i=0,g=0;;i++)  
            {  
                if(g==0&&i>=s.size()&&i>=b.s.size()) break;  
                int x=g;  
                if(i<s.size()) x+=s[i];  
                if(i<b.s.size()) x+=b.s[i];  
                c.s.push_back(x%BASE);  
                g=x/BASE;  
            }  
            return c;  
        }  
          
        BigInteger operator - (const BigInteger& b) const
        {  
            BigInteger c;  
            c.s.clear();  
            for(int i=0,g=0;;i++)  
            {  
                if(g==0&&i>=s.size()&&i>=b.s.size()) break;  
                int x=g;g=0;  
                if(i<s.size()) x+=s[i];  
                if(i<b.s.size()) x-=b.s[i];  
                if(x<0)    
                {  
                    x+=BASE;g=-1;  
                }  
                if(x!=0) c.s.push_back(x);  
            }  
            return c;  
        }  
          
        BigInteger operator * (const BigInteger& b) const
        {  
            BigInteger c;  
            c.s.clear();  
            int lena=s.size(),lenb=b.s.size(),lenc=lena+lenb;  
            for(int i=0;i<lenc;i++)  
            {  
                c.s.push_back(0);  
            }  
            for(int i=0;i<lena;i++)  
            {  
                for(int j=0;j<lenb;j++)  
                {  
                    c.s[i+j]+=s[i]*b.s[j];  
                }  
            }  
            for(int i=0;i<lenc-1;i++)    
            {    
                c.s[i+1]+=c.s[i]/BASE;  
                c.s[i]%=BASE;  
            }  
              
            for(int i=lenc-1;c.s[i]==0;i--)  
            {  
                c.s.pop_back();  
            }  
            return c;  
        }  
          
        BigInteger operator / (const long long& b) const
        {  
            BigInteger c;  
            c.s.clear();  
            for(int i=0;i<s.size();i++)  
            {  
                c.s.push_back(0);  
            }  
            long long g=0;  
            for(int i=s.size()-1;i>=0;i--)    
            {     
                c.s[i]=(s[i]+g*BASE)/b;     
                g=s[i]+g*BASE-c.s[i]*b;     
            }  
            for(int i=s.size()-1;c.s[i]==0;i--)  
            {  
                c.s.pop_back();  
            }  
            return c;  
        }  
          
        BigInteger operator % (const long long& b) const
        {  
            long long ans=0,lena=s.size();  
            for(int i=lena-1;i>=0;i--)  
            {  
                ans=(ans*BASE+s[i])%b;  
            }  
            return ans;  
        }  
        /*BigInteger operator / (const BigInteger& b)   const// /运算符 (大数除大数) 
        { 
             
        } 
        BigInteger operator % (const BigInteger& b) const// %运算符 (大数取模大数) 
        { 
             
        }*/  
          
      
        BigInteger operator += (const BigInteger& b)
        {  
            *this=*this+b;return *this;  
        }  
        BigInteger operator -= (const BigInteger& b)
        {  
            *this=*this-b;return *this;  
        }  
        BigInteger operator *= (const BigInteger& b) 
        {  
            *this=*this*b;return *this;  
        }  
        BigInteger operator /= (const long long& b)
        {  
            *this=*this/b;return *this;  
        }  
        BigInteger operator %= (const long long& b) 
        {  
            *this=*this%b;return *this;  
        }  
        /*BigInteger operator /= (const BigInteger& b)
        { 
            *this=*this/b;return *this; 
        } 
        BigInteger operator %= (const BigInteger& b)
        { 
            *this=*this%b;return *this; 
        }*/  
          
        bool operator < (const BigInteger& b) const
        {  
            if(s.size()!=b.s.size()) return s.size()<b.s.size();  
            for(int i=s.size()-1;i>=0;i--)  
                if(s[i]!=b.s[i]) return s[i]<b.s[i];  
            return false;  
        }  
        bool operator > (const BigInteger& b) const { return b<*this; } 
        bool operator <= (const BigInteger& b) const { return !(b<*this); }
        bool operator >= (const BigInteger& b) const { return !(*this<b); }  
        bool operator != (const BigInteger& b) const { return b<*this||*this<b; } 
        bool operator == (const BigInteger& b) const { return !(b<*this)&&!(*this<b); }
    };  
    int tp;  
    ostream& operator << (ostream &out,const BigInteger &x)
    {  
        out<<x.s.back();  
        for(int i= x.s.size()-2;i>=0;i--)  
        {  
            char buf[20];  
            sprintf(buf,"%04d",x.s[i]);  
            for(int j=0;j<strlen(buf);j++) out<<buf[j];  
        }  
        return out;  
    }  
    istream& operator >> (istream &in, BigInteger &x)
    {  
        string s;  
        if(!(in >> s)) return in;  
        x=s;  
        return in;  
    }  
    int main()  
    {
        int t;
        cin>>t;
        while(t--)
        {
            BigInteger a;
            cin>>a;
            if(a==1)
                cout<<"0"<<endl;
            else
                cout<<a*(a-1)/2<<endl;
        }
        return 0;  
    }
    View Code
  • 相关阅读:
    LeetCode 1122. Relative Sort Array (数组的相对排序)
    LeetCode 46. Permutations (全排列)
    LeetCode 47. Permutations II (全排列 II)
    LeetCode 77. Combinations (组合)
    LeetCode 1005. Maximize Sum Of Array After K Negations (K 次取反后最大化的数组和)
    LeetCode 922. Sort Array By Parity II (按奇偶排序数组 II)
    LeetCode 1219. Path with Maximum Gold (黄金矿工)
    LeetCode 1029. Two City Scheduling (两地调度)
    LeetCode 392. Is Subsequence (判断子序列)
    写程序判断系统是大端序还是小端序
  • 原文地址:https://www.cnblogs.com/lipu123/p/13018918.html
Copyright © 2020-2023  润新知