问题描述
Tom放学回家的路上,看到天空中出现一个矩阵。Tom发现,如果矩阵的行、列从0开始标号,第i行第j列的数记为ai,j,那么ai,j=Cji
如果i < j,那么ai,j=0
Tom突发奇想,想求一个矩形范围内所有数的和。Tom急着回家,当然不会自己算,所以就把任务交给你了。
因为数可能很大,答案对一个质数p取模。
输入描述
输入包含多组数据(大约8组)。每组数据只有一行五个非负整数,x1、y1、x2、y2、p,你要求的是∑x2i=x1∑y2j=y1ai,j模p后的值。
x1≤x2≤105,y1≤y2≤105,2≤p≤109
输出描述
对于每组数据输出一行,答案模p。
输入样例
0 0 1 1 7
1 1 2 2 13
1 0 2 1 2
输出样例
3
4
1 对于一个 矩阵的排列组合,C(X1,Y1) +C(X1,Y1+1)+....+(C(X1,Y2)=C(X1+1,Y2)-C(X1,Y1+1);
每一列都可以这样化,
所以后面就是:C(X,Y)%P的问题,这里证明LUCAS定律
C(X,Y)=【C(X/P,Y/P)+(X%P,Y%P)】%P;
来自百度百科:

复习lucas
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define LL long long
using namespace std;
const int N = ;
const int mod = ;
LL f[N],num[N];
LL p;
void init()
{
f[]=;
for(int i=;i<=N-;i++)
{
int tmp=i;
num[i]=num[i-];
while(tmp%p==)
{
num[i]++;
tmp/=p;
}
f[i]=f[i-]*tmp%p;
}
} LL inv(LL a,LL n)
{
LL res=;
a%=p;
while(n)
{
if(n&)res=res*a%p;
a=a*a%p;
n>>=;
}
return res;
} LL calc(int a,int b)
{
if(a<b)return ;
if(num[a]-num[b]-num[a-b])return ;
else return f[a]*inv(f[b],p-)%p*inv(f[a-b],p-)%p;
}
//先提取出阶乘里面的P 后面才能求逆元
int main()
{
int x1,y1,x2,y2; while(scanf("%d%d%d%d%I64d",&x1,&y1,&x2,&y2,&p)>)
{
init();
LL ans=;
for(int i=y1;i<=y2;i++)
{
ans=((ans+calc(x2+,i+)-calc(x1,i+))%p+p)%p;
}
printf("%I64d\n",ans);
}
}

因为p是素数,所以a!=0 关于p的逆为 a^-1=a^(p-2)%p;小费马定理

‘因为p 很小 所以要先预处理 阶乘可不可过能能mod p==0;


												

Bestcoder Tom and matrix的更多相关文章

  1. Hdu5226 Tom and matrix

    Tom and matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix

    Tom and matrix Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...

  3. 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 < ...

  4. HDU-5226 Tom and matrix(组合数求模)

    一.题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5226 二.题意 给一个大矩阵,其中,$a[i][j] = C_i^j$.输入5个参数,$x_1, ...

  5. BestCoder Round #40

    T1:Tom and pape (hdu 5224) 题目大意: 给出一个矩形面积N,求周长的最小值.(长&&宽&&面积都是正整数) N<=109 题解: 没啥好 ...

  6. WGCNA构建基因共表达网络详细教程

    这篇文章更多的是对于混乱的中文资源的梳理,并补充了一些没有提到的重要参数,希望大家不会踩坑. 1. 简介 1.1 背景 WGCNA(weighted gene co-expression networ ...

  7. HDU5569/BestCoder Round #63 (div.2) C.matrix DP

    matrix Problem Description Given a matrix with n rows and m columns ( n+m is an odd number ), at fir ...

  8. BestCoder Round #81 (div.2) B Matrix

    B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  9. acdeream Matrix Multiplication

    D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...

随机推荐

  1. tomcat常用的优化和配置

    Tomcat 5常用优化和配置 1.JDK内存优化: Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.bat,Unix下,在文 ...

  2. ViewController的lifecycle和autolayout

  3. IE(IE6/IE7/IE8)支持HTML5标签--20150216

    让IE(ie6/ie7/ie8)支持HTML5元素,我们需要在HTML头部添加以下JavaScript,这是一个简单的document.createElement声明,利用条件注释针对IE来调用这个j ...

  4. [POJ] 3362 Telephone Lines

    Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7978 Accepted: 2885 Descr ...

  5. 【http】http协议的队首阻塞

    1 队首阻塞 就是需要排队,队首的事情没有处理完的时候,后面的人都要等着. 2 http1.0的队首阻塞 对于同一个tcp连接,所有的http1.0请求放入队列中,只有前一个请求的响应收到了,然后才能 ...

  6. LeetCode(111) Minimum Depth of Binary Tree

    题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  7. mysql复制延迟排查

    今天收到报警,提示从库延时,首先当然是上去查看情况,首先查看机器负载,如下: 可以看到使用cpu已经100%,io没有等待.那么查看mysql是什么情况,执行show processlist没有发现任 ...

  8. B树总结

    B树是一种平衡的多路查找树,一棵m阶B树或为空树,或满足下列特性: 1.  每个节点之多有m棵子树 2.  若根节点不是叶子节点,则至少有两颗子树 3.  除根之外所有非终端节点至少有[m/2]可子树 ...

  9. python pdb模块

    参考文件http://pythonconquerstheuniverse.wordpress.com/category/Python-debugger/ 翻译不是一一对应 Debug功能对于devel ...

  10. 树状数组 Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

    C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...