POJ 3472 Holey Square Tiling (高精度)

题意:下面这个图形,(n+1) * (n+1) 的格子,里面是(n-1) * (n-1)的洞,用1*2的瓷砖去铺,问有多少种方法。

给个论文,里面有公式http://www.math.unam.mx/EMIS/journals/JIS/VOL7/Tauraso/tauraso3.pdf



#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
using namespace std;
const int D = 10000;

class HugeInt
{
    public:
    int len;
    int a[2500];
    HugeInt();
    HugeInt operator+(const HugeInt&);
    HugeInt operator+(int);
    HugeInt operator-(const HugeInt&);
    HugeInt operator-(int);
    HugeInt operator*(const HugeInt&);
    HugeInt operator*(int);
    void output();
};

HugeInt::HugeInt()
{
    len = 1;
    memset(a,0,sizeof(a));
}

HugeInt HugeInt::operator+(int x)
{
    HugeInt ret = *this;
    ret.a[0] += x;
    for(int i = 0; i < ret.len - 1 && ret.a[i] >= D; i++)
    {
        ret.a[i+1] += ret.a[i] / D;
        ret.a[i] %= D;
    }

    while(ret.a[ret.len-1] >= D)
    {
        ret.a[ret.len] += ret.a[ret.len-1] / D;
        ret.a[ret.len-1] %= D;
        ret.len++;
    }
    return ret;
}


HugeInt HugeInt::operator+(const HugeInt &x)
{
    HugeInt ret = *this;
    ret.len = max(ret.len, x.len);
    for(int i = 0; i < ret.len ; i++)
    {
        ret.a[i] += x.a[i];
        if(ret.a[i] >= D) { ret.a[i+1]++; ret.a[i] -= D; }
    }
    if(ret.a[ret.len] > 0) ret.len++;
    return ret;
}

HugeInt HugeInt::operator-(int x)
{
    HugeInt ret = *this;
    ret.a[0] -= x;
    for(int i = 0; i < ret.len && ret.a[i] < 0; i++)
    {
        ret.a[i+1] += (ret.a[i] - D + 1) / D; //+1是为了处理-10000等特殊情况
        ret.a[i] -= (ret.a[i] - D + 1) / D * D;
    }
    while(ret.a[ret.len-1] == 0 && ret.len > 1) ret.len--;
    return ret;
}


HugeInt HugeInt::operator-(const HugeInt &x) //保证被减数大于减数
{
    int i;
    HugeInt ret = *this;
    for(i = 0; i < x.len; i++)
    {
        ret.a[i] = ret.a[i] - x.a[i];
        if(ret.a[i] < 0) { ret.a[i+1]--; ret.a[i] += D; }
    }
    while(ret.a[i] < 0) { ret.a[i] += D; ret.a[++i]--; }
    while(ret.a[ret.len-1] == 0 && ret.len > 1) ret.len--;
    return ret;
}

HugeInt HugeInt::operator*(int x)
{
    HugeInt ret = *this;
    ret.a[0] *= x;
    for(int i = 1; i < ret.len; i++)
    {
        ret.a[i] *= x;
        if(ret.a[i-1] >= D)
        {
            ret.a[i] += ret.a[i-1] / D;
            ret.a[i-1] %= D;
        }
    }
    while(ret.a[ret.len-1] >= D)
    {
        ret.a[ret.len] += ret.a[ret.len-1] / D;
        ret.a[ret.len-1] %= D;
        ret.len++;
    }
    while(ret.a[ret.len-1] == 0 && ret.len > 1) ret.len--;
    return ret;
}

HugeInt HugeInt::operator*(const HugeInt &x)
{
    HugeInt ret;
    ret.len = len + x.len - 1;
    for(int i = 0; i < len; i++)
    {
        for(int j = 0; j < x.len; j++)
        {
            ret.a[i+j] += a[i] * x.a[j];
            if(ret.a[i+j] >= D)
            {
                ret.a[i+j+1] += ret.a[i+j] / D;
                ret.a[i+j] %= D;
            }
        }
    }
    if(ret.a[ret.len] > 0) ret.len++;
    while(ret.a[ret.len-1] == 0 && ret.len > 1) ret.len--;
    return ret;
}


void HugeInt::output()
{
    /*cout << a[len-1];
	for(int i = len - 2; i >= 0; i--)
        for(int j = 1000; j > 0; j /= 10)
            cout << a[i] / j % 10;
    cout << endl; */

    int i, j, k = 0;
    char str[10000];
	for(i = len - 1; i >= 0; i--)
        for(j = 1000; j > 0; j /= 10)
            str[k++] = a[i] / j % 10 + '0';

    for(i = 0; str[i] == '0'; i++);
    if(i == k) cout << "0" << endl;
    else if(k - i <= 3)
    {
        while(i < k) cout << str[i++];
        cout << endl;
    }
    else
    {
        int cnt = (k - i) % 3;
        if(cnt == 0) cnt = 3;
        for(j = i; j < i + cnt; j++) cout << str[j];
        for( ; j < k; j += 3) cout << ',' << str[j] << str[j+1] << str[j+2];
        cout << endl;
    }
}

HugeInt fib[10002];

int main()
{
    fib[1] = fib[1] + 1;
    for(int i = 2; i <= 10000; i++)
        fib[i] = fib[i-1] + fib[i-2];
    int n;
    while(cin >> n)
    {
        HugeInt ret;
        if(n % 2) ret = fib[n] * fib[n] * 2 - 1;
        else ret = fib[n] * fib[n] * 2 + 1;
        ret = ret * ret * 4;
        ret.output();
    }
    return 0;
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值