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 ...
随机推荐
- 不安装oracle客户端用sqlplus连接数据库
在不安装oracle客户端情况下用sqlplus连接数据库: 1.去官网下载 http://www.oracle.com/technetwork/topics/winx64soft-089540.ht ...
- (43)zabbix报警媒介介绍
zabbix触发器到了要发送通知的情况下,需要一个中间介质来接收并传递它的消息给运维们,以往用nagios,通常用脚本发送邮件或者发送飞信来达到报警.这个脚本实际上就是一个媒介了. zabbix有如下 ...
- python数据类型、字符编码、文件处理-练习
练习-字符串 # 写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分) name = " aleX" # ) 移除 name 变量对应的值两边的空格,并输出处理 ...
- JS原生增删,判断class是否存在方法
function hasClass(obj, cls) { if (obj.className) { return obj.className.match(new RegExp('(\\s|^)' + ...
- 【xdebug】 windows xdebug 配置
[xdebug] zend_extension = C:\phpStudy\php53n\ext\php_xdebug-2.6.1-7.0-vc14-nts-x86_64.dllxdebug.idek ...
- LeetCode(112) Path Sum
题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Java中File类的使用介绍
1.创建File对象的几种方式 import java.io.File; public class MyFile { public static void main(String[] args) { ...
- Repo command reference
Repo command reference In this document init sync upload diff download forall prune start status Rep ...
- ThreadLocalClient小应用
今天遇到hibernate.find就会进行权限配置,如果某处想要跳过权限配置,则可以如下 ThreadLocalClient.get().envParamMap.put(DataAuthority. ...
- PYDay4-基本数据类型、字符串、元组、列表、字典
1.关于编码: utf-8 与gbk都是对Unicode 编码的简化,utf-8是针对所有语言的精简,gbk是针对中文的精简 py3默认字符集为UTF-8,取消了Unicode字符集,如后面的编程过程 ...