Bestcoder Tom and matrix
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的更多相关文章
- Hdu5226 Tom and matrix
		
Tom and matrix Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
 - 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix
		
Tom and matrix Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...
 - 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 < ...
 - HDU-5226 Tom and matrix(组合数求模)
		
一.题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5226 二.题意 给一个大矩阵,其中,$a[i][j] = C_i^j$.输入5个参数,$x_1, ...
 - BestCoder Round #40
		
T1:Tom and pape (hdu 5224) 题目大意: 给出一个矩形面积N,求周长的最小值.(长&&宽&&面积都是正整数) N<=109 题解: 没啥好 ...
 - WGCNA构建基因共表达网络详细教程
		
这篇文章更多的是对于混乱的中文资源的梳理,并补充了一些没有提到的重要参数,希望大家不会踩坑. 1. 简介 1.1 背景 WGCNA(weighted gene co-expression networ ...
 - 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 ...
 - BestCoder Round #81 (div.2) B Matrix
		
B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
 - acdeream  Matrix Multiplication
		
D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...
 
随机推荐
- CS193p Lecture 5 - View Controller Lifecycle
			
1. UITextView @property(nonatomic,readonly,retain) NSTextStorage *textStorage 是 NSMutableAttributedS ...
 - HTML 显示和隐藏浏览器滚动条
			
滚动条和overflow有关 显示: overflow-x:auto; overflow-y:auto; overflow-x:scroll; overflow-y:scroll; 隐藏: overf ...
 - IntelliJ IDEA 中自定义模板代码的缩写
			
方法一:新建 Live Template step1. 点击 File – Setting step2.选择 Live Template,点击右侧的+号,选择 Template Group step3 ...
 - Python9-迭代器-生成器-day13
			
迭代器# print('__iter__' in dir(int))# print('__iter__' in dir(list))# print('__iter__' in dir(dict))# ...
 - shell 几中专用修饰符 :- :+ := ${variable:offset:length}
			
1.${variable:-word} ${variable:-word} 如果variable已经被设置了,且不为空,则代入它的值,否则代入word; $ fruit=peach $ echo ${ ...
 - joyoi2020/lfyzoj114 Rainbow 的信号
			
位与位间互不影响.一位一位计算. 长度为 \(1\) 的区间,选出概率为 \(1/n^2\).其余区间,选出概率为 \(2/n^2\).(这里的区间 \(l \leq r\)) 枚举右端点.记 \(l ...
 - Fatal error: Call to a member function rowCount() on a non-object in /opt/lampp/htdocs/xampp/assets/update.php on line 6
			
$sql = "SELECT * from idea ORDER BY datetime DESC LIMIT 50;"; $result = $pdo->query($sq ...
 - centos 7 smplayer vlc播放器
			
centos7安装多媒体播放器SMPlayer 2017-03-13 21:37:14 分类: LINUX 转自:https://wiki.centos.org/TipsAndTricks/Multi ...
 - 牛腩新闻发布系统(五):VS网站发布及常见问题
			
导读:在千万个回眸中,终于看见了牛腩的归途.好吧,牛腩该整合的都整合完毕了,到了发布的时候了.这时候,不得不再次感慨那句不知道感慨了多少次的感慨:为什么,我要遭遇这么多的坎坷?下面,结合自己的情况,说 ...
 - 九度oj 题目1137:浮点数加法
			
题目描述: 求2个浮点数相加的和 题目中输入输出中出现浮点数都有如下的形式:P1P2...Pi.Q1Q2...Qj对于整数部分,P1P2...Pi是一个非负整数 对于小数部分,Qj不等于0 输入: 对 ...