Tom and matrix

Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5226


Mean:

题意很简单,略。

analyse:

直接可以用Lucas定理+快速幂水过的,但是我却作死的用了另一种方法。

方法一:Lucas定理+快速幂水过

方法二:首先问题可以转化为求(0,0),(n,m)这个子矩阵的所有数之和。画个图容易得到一个做法,对于n<=m,答案就是2^0+2^1+...+2^m=2^(m+1)-1,对于n>m,答案由两部分构成,一部分是2^(m+1)-1,另一部分是sigma i:m+1->n f[i][m],f[i][m]表示第i行前m列的数之和,f数组存在如下关系,f[i][m]=f[i-1][m]*2-C[i-1][m],f[m][m]=2^m。还有另一种思路:第i列的所有数之和为C(i,i)+C(i+1,i)+...+C(n,i)=C(n+1,i+1),于是答案就是sigma i:0->min(n,m) C(n+1,i+1)。

Lucas定理:由于题目给定的模是可变的质数,且质数可能很小,那么就不能直接用阶乘和阶乘的逆相乘了,需要用到Lucas定理,公式:C(n,m)%P=C(n/P,m/P)*C(n%P,m%P),c(n,m)=0(n<m)。当然最终还是要预处理阶乘和阶乘的逆来得到答案。复杂度O(nlogP+nlogn)

Time complexity: O(n)

Source code: 

Lucas定理+快速幂

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-05-21-23.28
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std; const int maxn=;
struct cell
{
int x,y;
bool operator<(cell c) const
{
return x==c.x?(y<c.y):(x<c.x);
}
}p[];
LL mod;
LL Pow(LL a,LL b)
{
LL ret=;
a%=mod;
while(b)
{
if(b&) ret=ret*a%mod;
a=a*a%mod;
b>>=;
}
return ret%mod;
}
namespace lucas
{
LL A[maxn],inv[maxn];
void init()
{
A[]=,A[]=;
inv[]=;inv[]=;
for(int i=;i<maxn;i++)
{A[i]=A[i-]*(LL)i%mod;inv[i]=Pow(A[i],mod-);}
}
LL Lucas(LL a,LL b)
{ if(a<b) return ;
if(a<mod&&b<mod) return (A[a]*inv[b]%mod)*inv[a-b]%mod;
return Lucas(a/mod,b/mod)*Lucas(a%mod,b%mod)%mod;
}
}
using namespace lucas; int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
while(cin>>p[].x>>p[].y>>p[].x>>p[].y>>mod)
{
if(p[].y>p[].x&&p[].y>p[].x&&p[].y>p[].x) {printf("0\n");continue;}
init();
sort(p,p+);
if(!(p[].x<=p[].x && p[].y<=p[].y))
{
int x1=p[].x,y1=p[].y,x2=p[].x,y2=p[].y;
p[].x=x1,p[].y=y2,p[].x=x2,p[].y=y1;
}
LL sta=p[].x,en=p[].x,h=p[].y,ans=;
while(h<=p[].y && sta<=en )
{
if(sta<h) sta=h;
ans=(ans+Lucas(en+,h+)-Lucas(sta,h+)+mod)%mod;
h++;
}
printf("%lld\n",ans); }
return ;
}
/* */

方法二:

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-05-21-02.58
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
struct cell
{
int x,y;
bool operator<(cell c) const
{
return x==c.x?(y<c.y):(x<c.x);
}
}p[];
LL mod;
LL inv[],A[];
inline LL Pow(LL a,LL b)
{
LL ret=;
a%=mod;
while(b)
{
if(b&) ret=ret*a%mod;
a=a*a%mod;
b>>=;
}
return (ret-)%mod;
} void init()
{
A[]=,A[]=;
inv[]=;inv[]=;
for(int i=;i<;i++)
{A[i]=A[i-]*(LL)i%mod;inv[i]=Pow(A[i],mod-);}
}
LL Lucas(LL a,LL b)
{
if(a<b) return ;
if(a<mod&&b<mod) return (A[a]*inv[b]%mod)*inv[a-b]%mod;
return Lucas(a/mod,b/mod)*Lucas(a%mod,b%mod)%mod;
} inline LL Pow(LL b)
{
b=b+;
if(b<) return ;
LL a=;
LL ret=;
a%=mod;
while(b)
{
if(b&) ret=ret*a%mod;
a=a*a%mod;
b>>=;
}
return (ret-)%mod;
} inline int calc_Matrix(int x,int y)
{
if(x<||y<) return ;
if(x<=y)
return Pow(x);
else
{
LL sum1=Pow(y);
LL tmp=Pow(y)-Pow(y-);
LL sum2=;
for(int i=y+;i<=x;++i)
{
tmp=tmp*-(int)Lucas((LL)i-,(LL)y);
tmp%=mod;
sum2+=tmp;
sum2%=mod;
}
return (sum1+sum2)%mod;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
while(cin>>p[].x>>p[].y>>p[].x>>p[].y>>mod)
{
if(p[].y>p[].x&&p[].y>p[].x&&p[].y>p[].x) {printf("0\n");continue;}
init();
sort(p,p+);
if(!(p[].x<=p[].x && p[].y<=p[].y))
{
int x1=p[].x,y1=p[].y,x2=p[].x,y2=p[].y;
p[].x=x1,p[].y=y2,p[].x=x2,p[].y=y1;
}
cout<<(calc_Matrix(p[].x,p[].y)-calc_Matrix(p[].x-,p[].y)-calc_Matrix(p[].x,p[].y-)+calc_Matrix(p[].x-,p[].y-))%mod<<endl;
}
return ;
}
/* */

组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix的更多相关文章

  1. HDU 5226 Tom and matrix(组合数学+Lucas定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5226 题意:给一个矩阵a,a[i][j] = C(i,j)(i>=j) or 0(i < ...

  2. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

  3. 【组合数+Lucas定理模板】HDU 3037 Saving

    acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...

  4. uoj86 mx的组合数 (lucas定理+数位dp+原根与指标+NTT)

    uoj86 mx的组合数 (lucas定理+数位dp+原根与指标+NTT) uoj 题目描述自己看去吧( 题解时间 首先看到 $ p $ 这么小还是质数,第一时间想到 $ lucas $ 定理. 注意 ...

  5. HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description   Sample Input 2 Sample Outp ...

  6. 2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)

    题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少 ...

  7. 【(好题)组合数+Lucas定理+公式递推(lowbit+滚动数组)+打表找规律】2017多校训练七 HDU 6129 Just do it

    http://acm.hdu.edu.cn/showproblem.php?pid=6129 [题意] 对于一个长度为n的序列a,我们可以计算b[i]=a1^a2^......^ai,这样得到序列b ...

  8. UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)

    题目:传送门. 题意:t组数据,每组给定n,m,k.有n个格子,m种颜色,要求把每个格子涂上颜色且正好适用k种颜色且相邻的格子颜色不同,求一共有多少种方案,结果对1e9+7取余. 题解: 首先可以将m ...

  9. hdu 4704 sum(费马小定理+快速幂)

    题意: 这题意看了很久.. s(k)表示的是把n分成k个正整数的和,有多少种分法. 例如: n=4时, s(1)=1     4 s(2)=3     1,3      3,1       2,2 s ...

随机推荐

  1. Maven Learning - Direct Dependencies & Transitive Dependencies

    Dependencies declared in your project's pom.xml file often have their own dependencies. The main dep ...

  2. 更改ubuntu mysql data目录位置

    很多时候,mysql的数据会非常大,数据默认放在/var/lib/mysql,由于/var所划分的空间不够大,所以我们需要将mysql数据存放路径修改一下,放到大分区里面,以便可以应付mysql数据增 ...

  3. 3D立体显示大屏幕拼接视频墙系统解决方案【转】

    http://shop.souvr.com/thread-123416-1-1.html 随着3D立体视像.全息影像等技术不断取得突破性进展,国内外越来越多的公司投身3D显示领域,产品层出不穷.3D技 ...

  4. C# WinForm 技巧十: 开发工具

    一.摘要   为了开发效率就应该为这个框架开发一个配套工具.来生成固定格式的代码.工具界面如下:   二.数据库整理篇   添加表主键 修改表说明 修改表字段说明 生成数据库文档 导出数据库里相同的字 ...

  5. CSS选择器实现搜索功能 驱动过滤搜索技术

    一.CSS选择器可以用来实现搜索功能 CSS选择器可以用来实现搜索功能. 作者以前提过CSS3的选择器结合表单元素可以用来控制元素的显隐,这里,类似的,还是CSS3的选择器,用来过滤和搜索页面元素. ...

  6. OpenResty(nginx)操作mysql的初步应用

    OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项.   OpenResty ...

  7. 移植UE4的模型操作到Unity中

    最近在Unity上要写一个东东,功能差不多就是在Unity编辑器上的旋转,移动这些,在手机上也能比较容易操作最好,原来用Axiom3D写过一个类似的,有许多位置并不好用,刚好在研究UE4的源码,在模型 ...

  8. PowerShell读取Windows产品密钥

    之前大多数人可能用过VBS读取Windows产品密钥的VBS脚本,VBS脚本通常都比较隐晦.难懂,今天忙里偷闲,随手写了一个用于读取Windows产品密钥的PowerShell脚本. 代码如下: == ...

  9. Promise 使用心得

        this.testPromise=function(){         return new Promise(function(resolve,reject){             co ...

  10. c++ 访问者模式(visitor pattern)

    概述: 我们去银行柜台办业务,一般情况下会开几个个人业务柜台的,你去其中任何一个柜台办理都是可以的.我们的访问者模式可以很好付诸在这个场景中:对于银行柜 台来说,他们是不用变化的,就是说今天和明天提供 ...