题目就是指定n,求卡特兰数Ca(n)%m。求卡特兰数有递推公式、通项公式和近似公式三种,因为要取余,所以近似公式直接无法使用,递推公式我简单试了一下,TLE。所以只能从通项公式入手。

Ca(n) = (2*n)! / n! / (n+1)!

思想就是把Ca(n)质因数分解,然后用快速幂取余算最后的答案。不过,算n!时如果从1到n依次质因数分解,肯定是要超时的,好在阶乘取余有规律,不断除素因子即可。

最后还是擦边过,可能筛法写得一般吧,也算是题目要求太柯刻。

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
#ifdef ON_LOCAL_DEBUG
#else
#endif typedef long long LL;
const int MAXN = ;
const int N = ;
bool isPrime[N + ];//多用两个元素以免判断边界
int pn, pt[MAXN], num[MAXN]; void init_prime_table() {
memset(isPrime, true, sizeof(isPrime));
int p = , q, del;
double temp;
while (p <= N) {
while (!isPrime[p]) { p++; }
if (p > N) {//已经结束
break; }
temp = (double) p;
temp *= p;
if (temp > N)
break;
while (temp <= N) {
del = (int) temp; isPrime[del] = false;
temp *= p; }
q = p + ;
while (q < N) {
while (!isPrime[q]) { q++; }
if (q >= N) { break;}
temp = (double) p;
temp *= q;
if (temp > N) break;
while (temp <= N) {
del = (int) temp;
isPrime[del] = false;
temp *= p;
}
q++;
}
p++;
}
pn = ;
for (int i = ; i <= N; i++) {
if (isPrime[i]) {
pt[pn++] = i;
}
}
} int modular_exp(int a, int b, int c) {
LL res, temp;
res = % c, temp = a % c;
while (b) {
if (b & ) {
res = res * temp % c;
}
temp = temp * temp % c;
b >>= ;
}
return (int) res;
} void getNums(int n) {
int x = * n;
int len = pn;
for (int i = ; i < len && pt[i] <= x; i++) {
int r = , a = x;
while (a) {
r += a / pt[i];
a /= pt[i];
}
num[i] = r;
}
x = n;
for (int i = ; i < len && pt[i] <= x; i++) {
int r = , a = x;
while (a) {
r += a / pt[i];
a /= pt[i];
}
num[i] -= r;
}
x = n + ;
for (int i = ; i < len && pt[i] <= x; i++) {
int r = , a = x;
while (a) {
r += a / pt[i];
a /= pt[i];
}
num[i] -= r;
}
} int main() {
#ifdef ON_LOCAL_DEBUG
freopen("data.in", "r", stdin);
// freopen("test.in", "r", stdin);
// freopen("data.out", "w", stdout);
#endif
int n, m;
init_prime_table();
while (scanf("%d%d", &n, &m) == ) {
getNums(n);
LL ans = ;
int n2 = * n;
for (int i = ; ans && (i < pn) && pt[i] <= n2; i++) {
if (num[i] > ) {
ans = ans * (LL) modular_exp(pt[i], num[i], m);
ans %= m;
}
}
printf("%d\n", (int)ans);
}
return ;
}

bjfu1238 卡特兰数取余的更多相关文章

  1. POJ 3070 + 51Nod 1242 大斐波那契数取余

    POJ 3070 #include "iostream" #include "cstdio" using namespace std; class matrix ...

  2. hdoj 4828 卡特兰数取模

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  3. LightOJ-1214-Large Division-大数取余

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  4. 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

    题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...

  5. HDU-4828 卡特兰数+带模除法

    题意:给定2行n列的长方形,然后把1—2*n的数字填进方格内,保证每一行,每一列都是递增序列,求有几种放置方法,对1000000007取余: 思路:本来想用组合数找规律,但是找不出来,搜题解是卡特兰数 ...

  6. HDU 4828 Grids(卡特兰数+乘法逆元)

    首先我按着我的理解说一下它为什么是卡特兰数,首先卡特兰数有一个很典型的应用就是求1~N个自然数出栈情况的种类数.而这里正好就对应了这种情况.我们要满足题目中给的条件,数字应该是从小到大放置的,1肯定在 ...

  7. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  8. FZU 2098 刻苦的小芳(卡特兰数,动态规划)

    Problem 2098 刻苦的小芳 Accept: 42 Submit: 70 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Descr ...

  9. HDU 6084 寻找母串(卡特兰数)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6084 [题目大意] 对于一个串S,当它同时满足如下条件时,它就是一个01偏串: 1.只由0和1两种 ...

随机推荐

  1. 使用wget和ftp共享文件

    一.需求 有一个机器A,上面那有很多文件.现在新买一个机器B,不想用U盘复制,就想把A弄成个服务器,然后B登录到A,想要什么文件就下载什么文件. 二.Win7实现 A是Win7和Ubuntu双系统,首 ...

  2. lintcode:四个数之和

    题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...

  3. lintcode :nth to Last Node In List 链表倒数第n个节点

    题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...

  4. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  5. 使用PowerDesigner进行数据库建模入门

    阅读目录 两种重要模型 创建表和主外键 创建视图和存储过程 生成数据库 PowerDesigner(简称PD)是一种强大的数据库建模工具,使用PD可以创建业务模型,UML类图等,当然最主要的功能是数据 ...

  6. 在浏览器控制台输出内容 console.log(string);

    在浏览器控制台中写如数据 1添加    <script type="text/javascript">djConfig = { isDebug: true };< ...

  7. C++:常类型Const

    常类型:使用类型修饰符const说明的类型,常类型的变量或对象成员的值在程序运行期间是不可改变的. 3.10.1 常引用 如果在说明引用时用const修饰,则被说明的引用为常引用.如果用常引用做形参, ...

  8. 基于Struts2框架实现登录案例 之 程序国际化

    国际化牵涉的知识非常多,这里只能简单的介绍,程序国际化的一般做法是:在jsp页面时, 不是直接输出信息,而是输出一个key值,该key值在不同语言环境下找到对应资源文件下的 对应信息,因此首先要创建满 ...

  9. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  10. Linux使用者管理(1)---用户账号

    linux很重要的应用就是作为服务器的操作系统.服务器的作用是给多用户提供各种“服务”(可能是读服务器上的文件,或者是利用服务器进行数值计算)那么如果多用户共同拥有一台服务器,就需要对服务器上的用户进 ...