option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" style="">题目链接:uva 1478 - Delta Wave

题目大意:对于每一个位置来说,能够向上,水平,向下。坐标不能位负。每次上下移动最多为1。 给定n问说有多少种不同的图。结果对10100取模。

解题思路:由于最后都要落回y=0的位置,所以上升的次数和下降的次数是同样的,而且上升下降的关系满足出栈入栈的关系。即卡特兰数。

所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和。

C(2∗in)∗f(i)=C(2∗i−2n)∗f(i−1)∗(n−2∗i+1)∗(n−2∗i+2)i∗(i+1)

注意取模后的前导0

#include <cstdio>
#include <cstring>
#include <iostream> using namespace std;
typedef long long type;
const int MAXN = 10005; struct bign {
int len, num[MAXN]; bign () {
len = 0;
memset(num, 0, sizeof(num));
}
bign (int number) {*this = number;}
bign (const char* number) {*this = number;} void DelZero ();
void Put (); void operator = (int number);
void operator = (char* number); bool operator < (const bign& b) const;
bool operator > (const bign& b) const { return b < *this; }
bool operator <= (const bign& b) const { return !(b < *this); }
bool operator >= (const bign& b) const { return !(*this < b); }
bool operator != (const bign& b) const { return b < *this || *this < b;}
bool operator == (const bign& b) const { return !(b != *this); } void operator ++ ();
void operator -- ();
bign operator + (const type& b);
bign operator + (const bign& b);
bign operator - (const type& b);
bign operator - (const bign& b);
bign operator * (const type& b);
bign operator * (const bign& b);
bign operator / (const type& b);
//bign operator / (const bign& b);
int operator % (const int& b);
}; /*Code*/
int main () {
int n;
while (scanf("%d", &n) == 1) {
bign ans = 0;
bign tmp = 1;
ans = ans + tmp;
for (int i = 1; i <= n/2; i++) {
tmp = tmp * 1LL * (n - 2 * i + 2) * (n - 2 * i + 1);
tmp = tmp / (1LL * i * (i + 1));;
ans = ans + tmp;
ans.len = min(ans.len, 100);
}
ans.Put();
printf("\n");
}
} void bign::DelZero () {
while (len && num[len-1] == 0)
len--; if (len == 0)
num[len++] = 0;
} void bign::Put () {
bool flag = false;
for (int i = len-1; i >= 0; i--) {
if (num[i] || flag) {
printf("%d", num[i]);
flag = true;
}
}
} void bign::operator = (char* number) {
len = strlen (number);
for (int i = 0; i < len; i++)
num[i] = number[len-i-1] - '0'; DelZero ();
} void bign::operator = (int number) { len = 0;
while (number) {
num[len++] = number%10;
number /= 10;
} DelZero ();
} bool bign::operator < (const bign& b) const {
if (len != b.len)
return len < b.len;
for (int i = len-1; i >= 0; i--)
if (num[i] != b.num[i])
return num[i] < b.num[i];
return false;
} void bign::operator ++ () {
int s = 1; for (int i = 0; i < len; i++) {
s = s + num[i];
num[i] = s % 10;
s /= 10;
if (!s) break;
} while (s) {
num[len++] = s%10;
s /= 10;
}
} void bign::operator -- () {
if (num[0] == 0 && len == 1) return; int s = -1;
for (int i = 0; i < len; i++) {
s = s + num[i];
num[i] = (s + 10) % 10;
if (s >= 0) break;
}
DelZero ();
} bign bign::operator + (const type& b) {
bign a = b;
return *this + a;
} bign bign::operator + (const bign& b) {
type bignSum = 0;
bign ans; for (int i = 0; i < len || i < b.len; i++) {
if (i < len) bignSum += num[i];
if (i < b.len) bignSum += b.num[i]; ans.num[ans.len++] = bignSum % 10;
bignSum /= 10;
} while (bignSum) {
ans.num[ans.len++] = bignSum % 10;
bignSum /= 10;
} return ans;
} bign bign::operator - (const type& b) {
bign a = b;
return *this - a;
} bign bign::operator - (const bign& b) {
type bignSub = 0;
bign ans;
for (int i = 0; i < len || i < b.len; i++) {
bignSub += num[i];
bignSub -= b.num[i];
ans.num[ans.len++] = (bignSub + 10) % 10;
if (bignSub < 0) bignSub = -1;
else bignSub = 0;
}
ans.DelZero ();
return ans;
} bign bign::operator * (const type& b) {
type bignSum = 0;
bign ans; ans.len = len;
for (int i = 0; i < len; i++) {
bignSum += num[i] * b;
ans.num[i] = bignSum % 10;
bignSum /= 10;
} while (bignSum) {
ans.num[ans.len++] = bignSum % 10;
bignSum /= 10;
}
return ans;
} bign bign::operator * (const bign& b) {
bign ans;
ans.len = 0; for (int i = 0; i < len; i++){
int bignSum = 0; for (int j = 0; j < b.len; j++){
bignSum += num[i] * b.num[j] + ans.num[i+j];
ans.num[i+j] = bignSum % 10;
bignSum /= 10;
}
ans.len = i + b.len; while (bignSum){
ans.num[ans.len++] = bignSum % 10;
bignSum /= 10;
}
}
return ans;
} bign bign::operator / (const type& b) { bign ans; type s = 0;
for (int i = len-1; i >= 0; i--) {
s = s * 10 + num[i];
ans.num[i] = s/b;
s %= b;
} ans.len = len;
ans.DelZero ();
return ans;
} int bign::operator % (const int& b) { bign ans; int s = 0;
for (int i = len-1; i >= 0; i--) {
s = s * 10 + num[i];
ans.num[i] = s/b;
s %= b;
} return s;
}

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)的更多相关文章

  1. HDU 1023 Train Problem II (大数卡特兰数)

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. Tiling(递推+大数)

    Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tili ...

  3. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  4. UVa 12034 - Race(递推 + 杨辉三角)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  6. UVA 10288 - Coupons(概率递推)

    UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...

  7. uva 11375 Matches (递推)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. UVa 10561 (SG函数 递推) Treblecross

    如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...

  9. UVA 557 - Burger(概率 递推)

     Burger  When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was ...

随机推荐

  1. LaTeX 图片插入

    一般格式: \begin{figure} \centering \includegraphics[scale=0.2]{Figure211.png} \caption{In the case of a ...

  2. 资源管理器也玩多标签:QT TabBar v1.5.0.0a3

    http://zmingcx.com/explorer-also-play-more-tags-qt-tabbar-v1-5-0-0a3.html浏览器中的标签浏览功能非常受欢迎,安装QT TabBa ...

  3. Chrome应用技巧之颜色拾取

    之前在Chrome应用店找了个插件实现拾色功能.并且很不理想.不知道是不是曾经Chrome自带的开发工具没提供到拾色功能还是我没发现.今天无意中发现Chomer自带的开发工具可拾色,请看以下的GIF动 ...

  4. html5-canvas绘图操作方法

    <script>function draw(){    var c=document.getElementById("mycanvas");    c.width=50 ...

  5. 点滴积累【JS】---JQuery实现条形统计图,适用于选择题等统计

    效果: 思路:前台JS实现动态数据效果,后台可以拼接字符串或者用JSON加载数据 代码: <%@ Page Language="C#" AutoEventWireup=&qu ...

  6. 未设置BufferSize导致FTP下载速度过慢的问题

    開始下载前设置BufferSize就可以解决: ftpClient.setBufferSize(1024*1024); 查看commons-net的源代码.能够发现假设未设置该參数.将会一个字节一个字 ...

  7. MongoDB 将Json数据直接写入MongoDB的方法

    Json转Bson MongoDB中是以Bson数据格式进行存储的,Json字符串没有办法直接写入MongoDB 可以将Json字符串转换成DBObject或者Document,然后写入MongoDB ...

  8. zookeeper伪分布式安装

    本文介绍zookeeper伪分布式安装. 所谓 “伪分布式集群” 就是在1台PC中启动多个zookeeper的实例.“完全分布式集群” 是每1台PC启动1个ZooKeeper实例. 由于我的测试环境P ...

  9. vs2008、vs2012添加公共头文件

    当我们使用第三方库(opencv.boost)的时候,往往需要把它们的头文件和库文件添加到工程中去,然而如果每次新建工程都添加,那就太笨了,下面介绍方法可以让每个新建的工程都默认添加设置好的头文件和库 ...

  10. The Definitive Guide To Django 2 学习笔记(三) URLconfs 和松耦合

    前面的例子体现了一个设计模式中的重要思想,松耦合. 不论我们是将/time/改成/current_time/,还是新建一个/another-time-page/同样指向views.py中的 curre ...