今天学长讲的卡特兰数真的是卡的一批,整个全是高精的题,这时我就使用重载运算符,然后一下午就过去了

首先来看一波水题(也就卡了2小时)

.

A. 网格

内存限制:512 MiB 时间限制:1000 ms 标准输入输出
 
 

题目描述

原题来自:BZOJ 3907

某城市的街道呈网格状,左下角坐标为 A(0,0)A(0, 0)A(0,0),右上角坐标为 B(n,m)B(n, m)B(n,m),其中 n≥mn \ge mn≥m。现在从 A(0,0)A(0, 0)A(0,0) 点出发,只能沿着街道向正右方或者正上方行走,且不能经过图示中直线左上方的点,即任何途径的点 (x,y)(x, y)(x,y) 都要满足 x≥yx \ge yx≥y,请问在这些前提下,到达 B(n,m)B(n, m)B(n,m) 有多少种走法。

这道题是把对角线上方的对角线翻折,然后,自己一看就是C(n+m,n)-C(n+m,n-1),然后就是高精部分,我就不说了;

先看一下我一开始打的代码:

前方高能!

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define maxn 10000
#define base 10000
//可能重载运算符码量稍大qwq,而且巨难调试
struct Bigint
{
int c[maxn],len,sign;
Bigint(){memset(c,,sizeof(c)),len=,sign=;}
void Zero()
{
while(len>&&c[len]==)len--;
if(len==&&c[len]==)sign=;
}
void Write(char *s)
{
int k=,l=strlen(s);
for(int i=l-;i>=;i--)
{
c[len]+=(s[i]-'')*k;
k*=;
if(k==base)
k=,len++;
}
}
void Read()
{
char s[maxn]={};
scanf("%s",s);
Write(s);
}
void Print()
{
if(sign)printf("-");
printf("%d",c[len]);
for(int i=len-;i>=;i--)printf("%d",c[i]);
printf("\n");
}
bool operator < (const Bigint &a)const
{
if(len!=a.len)return len<a.len;
for(int i=len;i>=;i--)
if(c[i]!=a.c[i])return c[i]<a.c[i];
return ;
}
bool operator > (const Bigint &a)const
{
return a<*this;
}
Bigint operator = (int a)
{
char s[];
sprintf(s,"%d",a); //int ->string
Write(s);
return *this;
}
Bigint operator + (const Bigint &a)
{
Bigint r;
r.len=max(len,a.len)+;
for(int i=;i<=r.len;i++)
{
r.c[i]+=c[i]+a.c[i];
r.c[i+]+=r.c[i]/base;
r.c[i]%=base;
}
r.Zero();
return r;
}
Bigint operator + (const int &a)
{
Bigint b;b=a;
return *this+b;
}
Bigint operator - (const Bigint &a)
{
Bigint b,c;
b=*this;
c=a;
if(c>b)
{
swap(b,c);
b.sign=;
}
for(int i=;i<=b.len;i++)
{
b.c[i]=b.c[i]-c.c[i];
if(b.c[i]<)
{
b.c[i]+=base;
b.c[i+]--;
}
}
b.Zero();
return b;
}
Bigint operator - (const int &a)
{
Bigint b;b=a;return *this-b;
}
Bigint operator * (const Bigint &a)
{
Bigint r;
r.len=len+a.len+;
for(int i=;i<=len;i++)
{
for(int j=;j<=a.len;j++)
r.c[i+j-]+=c[i]*a.c[j];
}
for(int i=;i<=r.len;i++)
{
r.c[i+]+=r.c[i]/base;
r.c[i]%=base;
}
r.Zero();
return r;
}
Bigint operator * (const int &a)
{
Bigint b;b=a;
return *this*b;
}
Bigint operator / (const Bigint &b)
{
Bigint r,t,a;
a=b;
r.len=len;
for(int i=len;i>=;i--)
{
t=t*base+c[i];
int div,ll=,rr=base;
while(ll<=rr)
{
int mid=(ll+rr)/;
Bigint k=a*mid;
if(k>t)rr=mid-;
else
{
ll=mid+;
div=mid;
}
}
r.c[i]=div;
t=t-a*div;
}
r.Zero();
return r;
}
Bigint operator / (const int &a)
{
Bigint b;b=a;
return *this/b;
}
};
int main()
{
//freopen("cd.txt","r",stdin);
Bigint a,b,c;
int n,m;
scanf("%d%d",&n,&m);
a=,b=;
for(int i=m+;i<=n+m;i++)
a=a*i;
if(m+-n>)
a=a*(m+-n);
for(int i=;i<=n;i++)
b=b*i;
c=a/b;
c.Print();
//cout<<clock()<<endl;
return ;
}

高能代码

然后,我就直接站一下,我今天一下午顺便集齐的BigInt操作:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 2000
#define base 10000 struct Bign
{
int c[maxn],len,sign;
//初始化
Bign(){memset(c,,sizeof(c)),len = ,sign = ;}
//高位清零
void Zero()
{
while(len > && c[len] == )len--;
if(len == && c[len] == )sign = ;
}
//压位读入
void Write(char *s)
{
int k = ,l = strlen(s);
for(int i = l - ;i >= ;i--)
{
c[len] += (s[i] - '') * k;
k *= ;
if(k == base)
{
k = ;
len++;
}
}
}
void Read()
{
char s[maxn] = {};
scanf("%s",s);
Write(s);
}
//输出
void Print()
{
if(sign)printf("-");
printf("%d",c[len]);
for(int i = len - ;i >= ;i--)printf("%04d",c[i]);
printf("\n");
}
//重载 = 运算符,将低精赋值给高精
Bign operator = (int a)
{
char s[];
sprintf(s,"%d",a);
Write(s);
return *this;//this只能用于成员函数,表示当前对象的地址
}
//重载 < 运算符
bool operator < (const Bign &a)const
{
if(len != a.len)return len < a.len;
for(int i = len;i >= ;i--)
{
if(c[i] != a.c[i])return c[i] < a.c[i];
}
return ;
}
bool operator > (const Bign &a)const
{
return a < *this;
}
bool operator <= (const Bign &a)const
{
return !(a < *this);
}
bool operator >= (const Bign &a)const
{
return !(*this < a);
}
bool operator != (const Bign &a)const
{
return a < *this || *this < a;
}
bool operator == (const Bign &a)const
{
return !(a < *this) && !(*this < a);
}
bool operator == (const int &a)const
{
Bign b;b = a;
return *this == b;
} //重载 + 运算符
Bign operator + (const Bign &a)
{
Bign r;
r.len = max(len,a.len) + ;
for(int i = ;i <= r.len;i++)
{
r.c[i] += c[i] + a.c[i];
r.c[i + ] += r.c[i] / base;
r.c[i] %= base;
}
r.Zero();
return r;
}
Bign operator + (const int &a)
{
Bign b;b = a;
return *this + b;
}
//重载 - 运算符
Bign operator - (const Bign &a)
{
Bign b,c;// b - c
b = *this;
c = a;
if(c > b)
{
swap(b,c);
b.sign = ;
}
for(int i = ;i <= b.len;i++)
{
b.c[i] -= c.c[i];
if(b.c[i] < )
{
b.c[i] += base;
b.c[i + ]--;
}
}
b.Zero();
return b;
}
Bign operator - (const int &a)
{
Bign b;b = a;
return *this - b;
}
//重载 * 运算符
Bign operator * (const Bign &a)
{
Bign r;
r.len = len + a.len + ;
for(int i = ;i <= len;i++)
{
for(int j = ;j <= a.len;j++)
{
r.c[i + j - ] += c[i] * a.c[j];
}
}
for(int i = ;i <= r.len;i++)
{
r.c[i + ] += r.c[i] / base;
r.c[i] %= base;
}
r.Zero();
return r;
}
Bign operator * (const int &a)
{
Bign b;b = a;
return *this * b;
}
//重载 / 运算符
Bign operator / (const Bign &b)
{
Bign r,t,a;
a = b;
//if(a == 0)return r;
r.len = len;
for(int i = len;i >= ;i--)
{
t = t * base + c[i];
int div,ll = ,rr = base;
while(ll <= rr)
{
int mid = (ll + rr) / ;
Bign k = a * mid;
if(k > t)rr = mid - ;
else
{
ll = mid + ;
div = mid;
}
}
r.c[i] = div;
t = t - a * div;
}
r.Zero();
return r;
}
Bign operator / (const int &a)
{
Bign b;b = a;
return *this / b;
}
//重载 % 运算符
Bign operator % (const Bign &a)
{
return *this - *this / a * a;
}
Bign operator % (const int &a)
{
Bign b;b = a;
return *this % b;
}
}; int main()
{
freopen("Bign.in","r",stdin);
freopen("Bign.out","w",stdout);
Bign a,b,c,d,e,f,g;
a.Read();
b.Read();
c = a + b;
c.Print();
d = a - b;
d.Print();
e = a * b;
e.Print();
f = a / b;
f.Print();
g = a % b;
g.Print();
return ;
}

BigInt

然后第二题也是卡特兰数的水题,但是.............我又卡了2个小时

重载运算符,确实很好用,但是不能拿重载之前的低精的复杂度来考虑,稍有不慎,重载运算符的复杂度就能高的惊人!

BigInt 的使用!的更多相关文章

  1. Navicat软件中mysql中int、bigint、smallint和tinyint的区别、布尔类型存储以及乱码问题的解决

    很长时间不写博客了,最近一直在忙这学校的比赛都忘记更新博客了.新的任务又要开始了,我们要准备<2017年中国大学生计算机设计大赛软件服务外包竞赛>.这次不能再想像之前那样有PC端的功能作为 ...

  2. bigint数据类型

    尽管int依然是SQL Server 2000中最主要的整数数据类型,但是SQL Server 2000还是新增加了整数数据类型bigint,它应用于整数超过int数据范围的场合. int数据类型所表 ...

  3. 数据类型int、bigint、smallint 和 tinyint范围

      bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. int ...

  4. TINYINT,SMALLINT,MEDIUMINT,INT,INTEGER,BIGINT;text,longtext,mediumtext,ENUM,SET等字段类型区别

    http://www.path8.net/tn/archives/951 MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型.本节首先给出可用类型的一个概述, ...

  5. mysql整数字段 int bigint smallint tinyint

    mysql中int.bigint.smallint和tinyint的区别与长度 未完待续...

  6. DATETIME类型和BIGINT 类型互相转换

    项目中使用BIGINT来存放时间,以下代码用来转换时间类型和BIGINT类型 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========= ...

  7. mysql中int、bigint、smallint 和 tinyint的区别

    使用整数数据的精确数字数据类型. bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储 ...

  8. MySql中的tinying,smallint,int,bigint的类型介绍——转载

    tinyint 从 0 到 255 的整型数据.存储大小为 1 字节. smallint 从 -2^15 (-32,768) 到 2^15 – 1 (32,767) 的整型数据.存储大小为 2 个字节 ...

  9. 微软BI 之SSIS 系列 - 2008 版本中变量 Int64 无法保存 bigint 类型数据的BUG

    开篇介绍 这是今天在帮别人看一个 2008R2 版本的项目时发现的一个 Bug,这个 Bug 在 SQL SERVER 2012 有的版本中可能已经解决,但在论坛上看到有的仍然存在. 在 SQL SE ...

  10. mysql中int、bigint、smallint 和 tinyint的区别详细介绍

    1 bytes = 8 bit ,一个字节最多可以代表的数据长度是2的8次方 11111111 在计算机中也就是 -128到127 1.BIT[M] 位字段类型,M表示每个值的位数,范围从1到64,如 ...

随机推荐

  1. 关于5G目前形式的浅谈

    目前市面上已经有十几款5G手机已经上市了,但是到2019年底的覆盖率还是非常的低,所以很多换机的用户可以再等一等,2020应该是出来的都是5G手机,也都是支持双模的NA/NSA的两种组网模式,因为工信 ...

  2. 局部敏感哈希LSH(Locality-Sensitive Hashing)——海量数据相似性查找技术

    一. 前言     最近在工作中需要对海量数据进行相似性查找,即对微博全量用户进行关注相似度计算,计算得到每个用户关注相似度最高的TOP-N个用户,首先想到的是利用简单的协同过滤,先定义相似性度量(c ...

  3. 全球首个开放应用模型 OAM 开源 | 云原生生态周报 Vol. 23

    作者 | 临石.元毅.冬岛.衷源.天元 业界要闻 全球首个开放应用模型 OAM 开源 2019 年 10 月 17 日,阿里巴巴合伙人.阿里云智能基础产品事业部总经理蒋江伟(花名:小邪)在 Qcon ...

  4. Windows系统调用中的系统服务表描述符

     Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html Windows系统调用中的系统服务表描述符 在前面,我们将解过 ...

  5. 《深入理解Java虚拟机》-----第9章 类加载及执行子系统的案例与实战

    概述 在Class文件格式与执行引擎这部分中,用户的程序能直接影响的内容并不太多, Class文件以何种格式存储,类型何时加载.如何连接,以及虚拟机如何执行字节码指令等都是由虚拟机直接控制的行为,用户 ...

  6. firefox 实用插件推荐和使用

    1.firefox安装插件 2.firebug 3.Cookie editor 4.Tamper data 5.user agent switcher 6.hackbar 7.httpfox抓包工具 ...

  7. Cobalt Strike之CHM、LNK、HTA钓鱼

    CHM钓鱼 CHM介绍 CHM(Compiled Help Manual)即“已编译的帮助文件”.它是微软新一代的帮助文件格式,利用HTML作源文,把帮助内容以类似数据库的形式编译储存.利用CHM钓鱼 ...

  8. PHP array_unique

    1.函数的作用:移除数组中重复的值 2.函数的参数: @params array $array @params int $sort_flag SORT_REGULAR : 通常方法比较(不改变类型) ...

  9. table表格中文字超出显示省略号

    第一步: table {table-layout:fixed:}列宽由表格宽度和列宽度设定,不随文字多少变化 第二步: td { white-space:nowrap;/*文本不会换行,文本会在在同一 ...

  10. ubuntu16.04 gcc升级到7.3

    下载gcc- wget https://mirrors.ustc.edu.cn/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz 运行 download_prerequisites ...