栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3472 Holey Square Tiling

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3472 Holey Square Tiling

#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(){        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;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379700.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号