题目就是指定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. .NET复习笔记

    .NET 基础知识点汇总 课前知识储备. 一.C#与.NET的区别? 1..NET/dotnet:一般指.Net Framework框架,一种平台,一种技术 2.C#(sharp):一种编程语言,可以 ...

  2. Python图片转换成矩阵,矩阵数据转换成图片

    # coding=gbk from PIL import Image import numpy as np # import scipy def loadImage(): # 读取图片 im = Im ...

  3. 转:UGUI与NGUI的区别与优缺点

    1. NGUI与UGUI的区别 1) uGUI的Canvas 有世界坐标和屏幕坐标   2) uGUI的Image可以使用material     3) UGUI通过Mask来裁剪,而NGUI通过Pa ...

  4. 开源入侵检测系统OSSEC搭建之三:Web界面安装

    注意:以下操作需在OSSEC服务端进行设置 一.下载analogi,存放于/var/www/html/下并赋予权限 [root@localhost ~]# wget https://github.co ...

  5. org.hibernate.PersistentObjectException: detached entity passed to persist异常

    再用jpa+spring+struts2开发的是时候遇到一个问题(采用了注解的方式,xml配置的道理是一样的),当我在注册用户的时候,注册第一个用户没有问题,但注册第二个用户开始就会抛出一个异常: j ...

  6. 10个最佳的PHP图像操作库

    Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...

  7. IDEA 创建Web项目并在Tomcat中部署运行

    IDEA 14.0.5 apache-tomcat-8.0.32 步骤:File->New Project,在Java列表中勾选Web Application(3.1),点击Next 建立web ...

  8. adb shell settings ....

    Android4.2的源码android-17\com\android\commands目录下较之前的版本多了一个settings命令,查看其中的SettingsCmd.java文件,末尾有命令的帮助 ...

  9. 查看Linux服务器网络状态

    ifconfig 用来显示所有网络接口的详细情况的,如:ip地址,子网掩码等. ethx是以太网网卡的名称. 配置文件在/etc/sysconfig/network-scripts/ifcfg-eth ...

  10. Android远程图片获取和本地缓存

    对于客户端——服务器端应用,从远程获取图片算是经常要用的一个功能,而图片资源往往会消耗比较大的流量,对 应用来说,如果处理不好这个问题,那会让用户很崩溃,不知不觉手机流量就用完了,等用户发现是你的应用 ...