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

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

.

内存限制: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 mnm。现在从 A(0,0)A(0, 0)A(0,0) 点出发,只能沿着街道向正右方或者正上方行走,且不能经过图示中直线左上方的点,即任何途径的点 (x,y)(x, y)(x,y) 都要满足 x≥yx \ge yxy,请问在这些前提下,到达 B(n,m)B(n, m)B(n,m) 有多少种走法。

BigInt 的使用!

 

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

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

前方高能!

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<cstdlib>
  6 #include<ctime>
  7 using namespace std;
  8 #define maxn 10000
  9 #define base 10000
 10 //可能重载运算符码量稍大qwq,而且巨难调试
 11 struct Bigint
 12 {
 13     int c[maxn],len,sign;
 14     Bigint(){memset(c,0,sizeof(c)),len=1,sign=0;}
 15     void Zero()
 16     {
 17         while(len>1&&c[len]==0)len--;
 18         if(len==1&&c[len]==0)sign=0;        
 19     } 
 20     void Write(char *s)
 21     {
 22         int k=1,l=strlen(s);
 23         for(int i=l-1;i>=0;i--)
 24         {
 25             c[len]+=(s[i]-'0')*k;
 26             k*=10;
 27             if(k==base)
 28                 k=1,len++;
 29         }
 30     }
 31     void Read()
 32     {
 33         char s[maxn]={0};
 34         scanf("%s",s);
 35         Write(s);
 36     }
 37     void Print()
 38     {
 39         if(sign)printf("-");
 40         printf("%d",c[len]);
 41         for(int i=len-1;i>=1;i--)printf("%d",c[i]);
 42         printf("\n");
 43     } 
 44     bool operator < (const Bigint &a)const
 45     {
 46         if(len!=a.len)return len<a.len;
 47         for(int i=len;i>=1;i--)
 48             if(c[i]!=a.c[i])return c[i]<a.c[i];
 49         return 0;
 50     }
 51     bool operator > (const Bigint &a)const
 52     {
 53         return a<*this;
 54     }
 55     Bigint operator = (int a)
 56     {
 57         char s[100];
 58         sprintf(s,"%d",a);      //int ->string
 59         Write(s);
 60         return *this;
 61     }
 62     Bigint operator + (const Bigint &a)
 63     {
 64         Bigint r;
 65         r.len=max(len,a.len)+1;
 66         for(int i=1;i<=r.len;i++)
 67         {
 68             r.c[i]+=c[i]+a.c[i];
 69             r.c[i+1]+=r.c[i]/base;
 70             r.c[i]%=base; 
 71         }
 72         r.Zero();
 73         return r;
 74     } 
 75     Bigint operator + (const int &a)
 76     {
 77         Bigint b;b=a;
 78         return *this+b;
 79     }
 80     Bigint operator - (const Bigint &a)
 81     {
 82         Bigint b,c;
 83         b=*this;
 84         c=a;
 85         if(c>b)
 86         {
 87             swap(b,c);
 88             b.sign=1;
 89         }
 90         for(int i=1;i<=b.len;i++)
 91         {
 92             b.c[i]=b.c[i]-c.c[i];
 93             if(b.c[i]<0)
 94             {
 95                 b.c[i]+=base;
 96                 b.c[i+1]--;
 97             }
 98         }
 99         b.Zero();
100         return b;
101     } 
102     Bigint operator - (const int &a)
103     {
104         Bigint b;b=a;return *this-b;
105     }
106     Bigint operator * (const Bigint &a)
107     {
108         Bigint r;
109         r.len=len+a.len+2;
110         for(int i=1;i<=len;i++)
111         {
112             for(int j=1;j<=a.len;j++)
113                 r.c[i+j-1]+=c[i]*a.c[j];
114         } 
115         for(int i=1;i<=r.len;i++)
116         {
117             r.c[i+1]+=r.c[i]/base;
118             r.c[i]%=base;
119         }
120         r.Zero();
121         return r;
122     } 
123     Bigint operator * (const int &a)
124     {
125         Bigint b;b=a;
126         return *this*b;
127     }
128     Bigint operator / (const Bigint &b)
129     {
130         Bigint r,t,a;
131         a=b;
132         r.len=len;
133         for(int i=len;i>=1;i--)
134         {
135             t=t*base+c[i];
136             int div,ll=0,rr=base;
137             while(ll<=rr)
138             {
139                 int mid=(ll+rr)/2;
140                 Bigint k=a*mid;
141                 if(k>t)rr=mid-1;
142                 else
143                 {
144                     ll=mid+1;    
145                     div=mid; 
146                 } 
147             }
148             r.c[i]=div;
149             t=t-a*div;
150         }
151         r.Zero();
152         return r; 
153     }
154     Bigint operator / (const int &a)
155     {
156         Bigint b;b=a;
157         return *this/b;
158     }
159 };
160 int main()
161 {
162     //freopen("cd.txt","r",stdin);
163     Bigint a,b,c;
164     int n,m;
165     scanf("%d%d",&n,&m);
166     a=1,b=1;
167     for(int i=m+2;i<=n+m;i++)
168         a=a*i;
169     if(m+1-n>0)
170     a=a*(m+1-n);
171     for(int i=1;i<=n;i++)
172         b=b*i;
173     c=a/b;
174     c.Print();
175     //cout<<clock()<<endl;
176     return 0;
177 } 
高能代码

相关文章:

  • 2022-12-23
  • 2021-07-13
  • 2021-12-13
  • 2021-11-01
  • 2022-12-23
  • 2021-08-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
相关资源
相似解决方案