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

首先来看一波水题(也就卡了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. mongodb完整安装

    在线下载安装依赖包 yum -y install gcc gcc-c++ yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison yu ...

  2. 记录一次jvm内存泄露的问题

    前些天,运维告诉我刚上线的java服务占用CPU过高. 以下是发现解决问题的具体流程. 1:通过#top命令查看,我的java服务确实把CPU几乎占满了,如图 可看到18400这个进程CPU占用达到了 ...

  3. java并发基础及原理

    java并发基础知识导图   一 java线程用法 1.1 线程使用方式 1.1.1 继承Thread类 继承Thread类的方式,无返回值,且由于java不支持多继承,继承Thread类后,无法再继 ...

  4. PHP current

    1.函数的作用:返回数组的当前元素 2.函数的参数: @params array &$array 3.例子: <?php $arr = [null,'PK',false]; : ; ec ...

  5. [HDU2294] Pendant - 矩阵加速递推

    Pendant Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. 算法---ALGO-3 Java K好数 蓝桥杯

    package Main; import java.io.InputStream; import java.util.Scanner; public class Main { public stati ...

  7. idea+springboot+mybatis逆向工程

    前提:使用idea开发,基于springboot.用到了mybatis的逆向工程 因为之前用eclipse开发ssm比较多,现在转idea 使用springboot 踩了一些坑,在这记录一下~ 注意事 ...

  8. python-json与字典的区别

    1.字典的类型是字典dict,是一种数据结构:json的类型是字符串str,json是一种格式: 接口测试是传参数payload时有时候是传的字符串,应该将payload的类型改为json  这点要注 ...

  9. 玩转ArduinoJson库 V5版本

    1.前言     一直以来,博主的事例代码中都一直使用到JSON数据格式.而很多初学者一直对JSON格式有很大疑惑,所以博主特意分出一篇博文来重点讲解Arduino平台下的JSON库--Arduino ...

  10. 【MongoDB详细使用教程】二、MongoDB基本操作

    目录 数据类型 数据库操作 集合操作 数据操作 增 查 改 修改整行 修改指定字段的值 删 数据类型 MongoDB常见类型 说明 Object ID 文档ID String 字符串,最常用,必须是有 ...