多米诺骨牌(domino) 高精度模板 Fib

多米诺骨牌(domino)

https://dsa.cs.tsinghua.edu.cn/oj/problem.shtml?id=186

问题描述

  小牛牛对多米诺骨牌有很大兴趣,然而她的骨牌比较特别,只有黑色和白色的两种。她觉得如果存在连续三个骨牌是同一种颜色,那么这个骨牌排列便是不美观的。现在她有n个骨牌要来排列,她想知道不美观的排列的个数。由于数字较大,数学不好的她不会统计,所以请你来帮忙。希望你帮她求出不美观的排列的个数。

输入数据

  只有一个正整数,即要排列的骨牌个数。

输出数据

  一个数,即不美观的排列个数。

样例输入

4

样例输出

6

样例解释

  有 6 种不美观的排列。

  黑黑黑黑,白白白白,黑黑黑白,白白白黑,黑白白白,白黑黑黑

数据范围

  20%的数据,n<=60;

  50%的数据,n<=6000;

  100%的数据,n<=10000。

  时间限制: 1 sec

  空间限制: 256 MB

提示

  动态规划、高精度加法。

注意:开头部分定义的 

#define MAXN 9999
#define MAXSIZE 500   //最大长度
#define DLEN 4

class BigNum
{
private:
    int a[1000];    //控制大数的位数
    int len;       //长度

}

对算法的正确性影响很大

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cmath>
#include<sstream>
using namespace std;

#define MAXN 9999
#define MAXSIZE 500   //最大长度
#define DLEN 4

class BigNum
{
private:
    int a[1000];    //控制大数的位数
    int len;       //长度
public:
    BigNum(){ len = 1;memset(a,0,sizeof(a)); }   //构造函数
    void XD();
    BigNum(const int);
    BigNum(const long long int);
    BigNum(const char*);
    BigNum(const string &);
    BigNum(const BigNum &);  //拷贝构造函数
    BigNum &operator = (const BigNum &);   //重载赋值运算符
    BigNum &operator = (const int &);
    BigNum &operator = (const long long int &);

    friend istream& operator >> (istream&,  BigNum&);   //重载输入运算符
    friend ostream& operator << (ostream&,  BigNum&);   //重载输出运算符

    template<typename T> BigNum operator << (const T &) const;
    template<typename T> BigNum operator >> (const T &) const;

    BigNum operator + (const BigNum &) const;   //重载加法运算符,大数加大数
    BigNum operator - (const BigNum &) const;   //重载减法运算符,大数减大数
    BigNum operator * (const BigNum &) const;   //重载乘法运算符,大数乘大数
    bool   operator > (const BigNum& b)const;   //重载大于
    bool   operator < (const BigNum& b) const;   //重载小于
    bool   operator == (const BigNum& b) const;  //重载等于符号
    template<typename T> BigNum operator / (const T  &) const;    //重载除法运算符,大数除整数
    template<typename T> BigNum operator ^ (const T  &) const;    //大数的n次方
    template<typename T> T    operator % (const T  &) const;    //大数对int取模

    template<typename T> BigNum operator + (const T& b) const {BigNum t = b; t = *this + t; return t;}
    template<typename T> BigNum operator - (const T& b) const {BigNum t = b; t = *this - t; return t;}
    template<typename T> BigNum operator * (const T& b) const {BigNum t = b; t = (*this) * t; return t;}
    template<typename T> bool   operator < (const T& b) const {BigNum t = b; return ((*this) < t);}
    template<typename T> bool   operator > (const T& b) const {BigNum t = b; return ((*this) > t);}
    template<typename T> bool   operator == (const T& b) const {BigNum t = b; return ((*this) == t);}

    bool   operator <= (const BigNum& b) const {return (*this) < b || (*this) == b;}
    bool   operator >= (const BigNum& b) const {return (*this) > b || (*this) == b;}
    bool   operator != (const BigNum& b) const {return !((*this) == b);}

    template<typename T> bool   operator >= (const T& b) const {BigNum t = b; return !((*this) < t);}
    template<typename T> bool   operator <= (const T& b) const {BigNum t = b; return !((*this) > t);}
    template<typename T> bool   operator != (const T& b) const {BigNum t = b; return !((*this) == t);}

    BigNum& operator += (const BigNum& b) {*this = *this + b; return *this;}
    BigNum& operator -= (const BigNum& b) {*this = *this - b; return *this;}
    BigNum& operator *= (const BigNum& b) {*this = *this * b; return *this;}
    template<typename T> BigNum& operator /= (const T& b) {*this = *this/b; return *this;}
    template<typename T> BigNum& operator %= (const T& b) {*this = *this%b; return *this;}
    template<typename T> BigNum& operator += (const T& b) {*this = *this+b; return *this;}
    template<typename T> BigNum& operator -= (const T& b) {*this = *this-b; return *this;}
    template<typename T> BigNum& operator *= (const T& b) {*this = *this*b; return *this;}
    template<typename T> BigNum& operator ^= (const T& b) {*this = *this^b; return *this;}

    BigNum operator ++ (int) {BigNum t = *this; *this += 1; return t;}
    BigNum operator -- (int) {BigNum t = *this; *this -= 1; return t;}
    BigNum& operator -- () {*this -= 1; return *this;}
    BigNum& operator ++ () {*this += 1; return *this;}

    template<typename T> BigNum& operator <<= (const T& b) {*this = *this << b; return *this;}
    template<typename T> BigNum& operator >>= (const T& b) {*this = *this >> b; return *this;}

    template<typename T> BigNum friend operator + (const T& a, const BigNum& b) {BigNum t = a; t = t + a; return t;}
    template<typename T> BigNum friend operator - (const T& a, const BigNum& b) {BigNum t = a; t = t - b; return t;}
    template<typename T> BigNum friend operator * (const T& a, const BigNum& b) {BigNum t = a; t = t * b; return t;}
    template<typename T> friend bool operator < (const T& a, const BigNum& b) {return b > a;}
    template<typename T> friend bool operator > (const T& a, const BigNum& b) {return b < a;}
    template<typename T> friend bool operator <= (const T& a, const BigNum& b) {return b >= a;}
    template<typename T> friend bool operator >= (const T& a, const BigNum& b) {return b <= a;}
    template<typename T> friend bool operator == (const T& a, const BigNum& b) {return b == a;}
    template<typename T> friend bool operator != (const T& a, const BigNum& b) {return b != a;}

    void print();       //输出大数
    int Size();            //返回大数长度
    int the_first();    //返回第一个数字
    int the_last();        //返回最后一位数字
    int to_int();       //转化为整数
    long long int to_long();
    string to_String();        //转化为string类型
    //char* to_char();
};

BigNum::BigNum(const int b)     //将一个int类型的变量转化为大数
{
    int c,d = b;
    len = 0;
    memset(a,0,sizeof(a));
    while(d > MAXN){
        c = d - (d / (MAXN+1)) * (MAXN+1);
        d = d / (MAXN+1);
        a[len++] = c;
    }
    a[len++] = d;
}
BigNum::BigNum(const long long int b)
{
    long long int c,d = b;
    len = 0;
    memset(a,0,sizeof(a));
    while(d > MAXN){
        c = d - (d / (MAXN+1)) * (MAXN+1);
        d = d / (MAXN+1);
        a[len++] = c;
    }
    a[len++] = d;
}
BigNum::BigNum(const string& s)
{
    int t,k,index,l,i;
    memset(a,0,sizeof(a));
    l = s.size();
    len = l/DLEN;
    if(l%DLEN)
        len++;
    index = 0;
    for(i = l-1; i >=0 ;i -= DLEN){
        t = 0;
        k = i-DLEN+1;
        if(k < 0) k = 0;
        for(int j = k; j <= i; j++)
            t = t*10 + s[j]-'0';
        a[index++] = t;
    }
}
BigNum::BigNum(const char* s)     //将一个字符串类型的变量转化为大数
{
    int t,k,index,l,i;
    memset(a,0,sizeof(a));
    l = strlen(s);
    len = l/DLEN;
    if(l%DLEN)
        len++;
    index = 0;
    for(i = l-1; i >= 0; i -= DLEN){
        t = 0;
        k = i - DLEN + 1;
        if(k < 0) k = 0;
        for(int j = k; j <= i; j++)
            t = t*10 + s[j] - '0';
        a[index++] = t;
    }
}
BigNum::BigNum(const BigNum & b) : len(b.len)  //拷贝构造函数
{
    memset(a,0,sizeof(a));
    for(int i = 0 ; i < len ; i++)
        a[i] = b.a[i];
}
BigNum & BigNum::operator = (const BigNum& n)   //重载赋值运算符,大数之间进行赋值运算
{
    len = n.len;
    memset(a,0,sizeof(a));
    for(int i = 0 ; i < len ; i++)
        a[i] = n.a[i];
    return *this;
}
BigNum & BigNum::operator = (const int& num)
{
    BigNum t(num);
    *this = t;
    return *this;
}
BigNum & BigNum::operator = (const long long int& num)
{
    BigNum t(num);
    *this = t;
    return *this;
}
void XD()
{
    cout << "A hidden egg! Good luck for u!" << endl;
}
istream& operator >> (istream & in, BigNum & b)   //重载输入运算符
{
    char ch[MAXSIZE*4];
    int i = -1;
    in>>ch;
    int l = strlen(ch);
    int cnt = 0, sum = 0;
    for(i = l-1; i >= 0; ){
        sum = 0;
        int t = 1;
        for(int j = 0; j < 4 && i >= 0; j++,i--,t *= 10)
            sum += (ch[i]-'0')*t;
        b.a[cnt] = sum;
        cnt++;
    }
    b.len = cnt++;
    return in;

}
ostream& operator << (ostream& out, BigNum& b)   //重载输出运算符
{
    int i;
    cout << b.a[b.len - 1];
    for(i = b.len - 2 ; i >= 0 ; i--){
        cout.width(DLEN);
        cout.fill('0');
        cout << b.a[i];
    }
    return out;
}

template<typename T> BigNum BigNum::operator << (const T& b) const
{
    T temp = 1;
    for(int i = 0; i < b; i++)
        temp *= 2;
    BigNum t = (*this) * temp;
    return t;
}
template<typename T> BigNum BigNum::operator >> (const T& b) const
{
    T temp = 1;
    for(int i = 0; i < b; i++)
        temp *= 2;
    BigNum t = (*this) / temp;
    return t;
}

BigNum BigNum::operator + (const BigNum& b) const   //两个大数之间的相加运算
{
    BigNum t(*this);
    int i,big;
    big = b.len > len ? b.len : len;
    for(i = 0 ; i < big ; i++){
        t.a[i] += b.a[i];
        if(t.a[i] > MAXN){
            t.a[i + 1]++;
            t.a[i] -=MAXN+1;
        }
    }
    if(t.a[big] != 0)
        t.len = big + 1;
    else
        t.len = big;
    return t;
}
BigNum BigNum::operator - (const BigNum& b) const   //两个大数之间的相减运算
{
    int i,j,big;
    bool flag;
    BigNum t1,t2;
    if(*this>b){
        t1 = *this;
        t2 = b;
        flag = 0;
    }
    else{
        t1 = b;
        t2 = *this;
        flag = 1;
    }
    big = t1.len;
    for(i = 0 ; i < big ; i++){
        if(t1.a[i] < t2.a[i]){
            j = i + 1;
            while(t1.a[j] == 0)
                j++;
            t1.a[j--]--;
            while(j > i)
                t1.a[j--] += MAXN;
            t1.a[i] += MAXN + 1 - t2.a[i];
        }
        else
            t1.a[i] -= t2.a[i];
    }
    t1.len = big;
    while(t1.a[t1.len - 1] == 0 && t1.len > 1){
        t1.len--;
        big--;
    }
    if(flag)
        t1.a[big-1] = 0-t1.a[big-1];
    return t1;
}

BigNum BigNum::operator * (const BigNum& b) const   //两个大数之间的相乘运算
{
    BigNum ret;
    int i,j,up;
    int temp,temp1;
    for(i = 0 ; i < len ; i++){
        up = 0;
        for(j = 0 ; j < b.len ; j++){
            temp = a[i] * b.a[j] + ret.a[i + j] + up;
            if(temp > MAXN){
                temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
                up = temp / (MAXN + 1);
                ret.a[i + j] = temp1;
            }
            else{
                up = 0;
                ret.a[i + j] = temp;
            }
        }
        if(up != 0) ret.a[i + j] = up;
    }
    ret.len = i + j;
    while(ret.a[ret.len - 1] == 0 && ret.len > 1)
        ret.len--;
    return ret;
}
template<typename T> BigNum BigNum::operator / (const T& b) const
{
    BigNum ret;
    T i,down = 0;
    for(i = len - 1 ; i >= 0 ; i--){
        ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
        down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
    }
    ret.len = len;
    while(ret.a[ret.len - 1] == 0 && ret.len > 1)
        ret.len--;
    return ret;
}
template<typename T> T BigNum::operator % (const T& b) const
{
    T i,d=0;
    for (i = len-1; i>=0; i--){
        d = ((d * (MAXN+1))% b + a[i])% b;
    }
    return d;
}


template<typename T> BigNum BigNum::operator^(const T& n) const    //大数的n次方运算
{
    BigNum t,ret(1);
    int i;
    if(n < 0) return 0;
    if(n == 0)
        return 1;
    if(n == 1)
        return *this;
    int m = n;
    while(m > 1){
        t =* this;
        for(i = 1; (i<<1) <= m;i <<= 1)
            t = t*t;
        m-=i;
        ret=ret*t;
        if(m == 1) ret = ret * (*this);
    }
    return ret;
}

bool BigNum::operator > (const BigNum& b) const   //大数和另一个大数的大小比较
{
    int tot;
    if(len > b.len)
        return true;
    else if(len == b.len){
        tot = len - 1;
        while(a[tot] == b.a[tot] && tot >= 0)
            tot--;
        if(tot >= 0 && a[tot] > b.a[tot])
            return true;
        else
            return false;
    }
    else
        return false;
}

bool BigNum::operator < (const BigNum& b) const
{
    int tot;
    if(len > b.len)
        return false;
    else if(len == b.len){
        tot = len - 1;
        while(a[tot] == b.a[tot] && tot >= 0)
            tot--;
        if(tot >= 0 && a[tot] > b.a[tot])
            return false;
        else
            return false;
    }
    else
        return true;
}

bool BigNum::operator == (const BigNum& b) const
{
    int tot = len-1;
    if(len != b.len)
        return false;
    while(a[tot] == b.a[tot] && tot >= 0)
        tot--;
    if(tot < 0)
        return true;
    return false;
}

void BigNum::print()    //输出大数
{
    int i;
    cout << a[len - 1];
    for(i = len-2; i >= 0; i--){
        cout.width(DLEN);
        cout.fill('0');
        cout << a[i];
    }
    cout << endl;
}
int BigNum::Size()
{
    int t = a[len-1],cnt = 0;
    while(t){ t /= 10; cnt++; }
    cnt += (len-1)*4;
    return cnt;
}
int BigNum::the_first()
{
    int t = a[len-1];
    while(t > 10){ t /= 10;}
    return t;
}
int BigNum::the_last()
{
    int t = a[0];
    return t%10;
}
int BigNum::to_int()
{
    int i,num;
    num = a[len-1];
    for(i = len-2; i >= 0; i--)
        num = num*(MAXN+1) + a[i];
    return num;
}
long long int BigNum::to_long()
{
    int i;
    long long int num;
    num = a[len-1];
    for(i = len-2; i >= 0; i--)
        num = num*(MAXN+1) + a[i];
    return num;
}

string to_string2(int n)
{
ostringstream stream;
stream<<n; //n为int类型
return stream.str();
}
string BigNum::to_String()
{
    int i;
    string s = "",tp = "";
    s += to_string2(a[len-1]);
    for(i = len-2; i >= 0; i--){
        tp = to_string2(a[i]);
        int tot = tp.size();
        tp.insert(tp.begin(),4-tot,'0');
        s = s + tp;
    }
    return s;
}

BigNum Fibonacci(int n)
{
    BigNum arr[2] = {0,1};
    if(n <= 1)
        return arr[n];
    BigNum num; //用来保存计算结果
    int i;
    for(i = 2; i <= n; i++)
    {
        num = arr[1] + arr[0];
        arr[0] = arr[1];
        arr[1] = num;
    }
    return num;
}


int main()
{
	BigNum a=1,b,c;
	int n;
	cin>>n;
	for(int i = 1; i <= n; i++)
	a=a*2;
	//a.print();
	
	c=Fibonacci(n+1);
    //c.print();
    b=a-c-c;
	b.print();
}

猜你喜欢

转载自blog.csdn.net/qiang_____0712/article/details/87869172