Tom and matrix

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 867    Accepted Submission(s): 284

Problem Description
Tom was on the way home from school. He saw a matrix in the sky. He found that if we numbered rows and columns of the matrix from 0, then,
ai,j=Cji

if i < j, ai,j=0

Tom suddenly had an idea. He wanted to know the sum of the numbers in some rectangles. Tom needed to go home quickly, so he wouldn't solve this problem by himself. Now he wants you to help him.
Because the number may be very large, output the answer to the problem modulo a prime p.

 
Input
Multi test cases(about 8). Each case occupies only one line, contains five integers, x1、y1、x2、y2、p.x1≤x2≤105,y1≤y2≤105,2≤p≤109.

You should calculate ∑x2i=x1∑y2j=y1ai,j mod p

 
Output
For each case, print one line, the answer to the problem modulo p.
 
Sample Input
0 0 1 1 7
1 1 2 2 13
1 0 2 1 2
 
Sample Output
3
4
1
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  6242 6241 6240 6239 6238 
题目大意:若i ≥ j,那么a[i][j] = C(i,j),否则a[i][j] = 0,给一个子矩阵(x1,y1,x2,y2),问矩阵和.
分析:ans = sum(x2,y2) - sum(x1-1,y2) - sum(x2,y1-1) + sum(x1-1,y1-1). sum(x,y)表示(0,0,x,y)矩阵的和.
          怎么计算sum呢?画一个图可以发现对答案有贡献的区域是一个三角形,非常像是杨辉三角,结合Hdu3037的方法,可以把每一列的答案变成1个组合数.接下来就是组合数的计算问题了.可以预处理出阶乘和逆元的阶乘,直接取模运算.但是p是会变的,如果p特别小的话,答案就会出现0,事实上并不是0,因为n!,m!,(n-m)!都有p这个因子,但是p是可以被约分掉的,直接用逆元乘的话是保留了这个p的,所以会WA.
          当p比较小的时候,划定一个界限:C(n,m) % p,p ≤ n,如果用lucas定理就能解决这一问题.当p比较大的时候,直接算就可以了.
坑点:下标是从0开始的.
经验教训:当模数p小于n/m,且p为质数时,用lucas定理就能有效避免包含p这个因子而出现的问题.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; ll x3, y3, x4, y4, p, ans;
ll sum[], ni[], nijie[]; ll qpow(ll a, ll b)
{
ll res = ;
while (b)
{
if (b & )
res = (res * a) % p;
a = (a * a) % p;
b >>= ;
}
return res;
} ll solve2(ll a, ll b)
{
ll temp1 = sum[a];
ll temp2 = nijie[b] * nijie[a - b] % p;
return temp1 * temp2 % p;
} ll solve(ll a, ll b)
{
if (b > a)
return ;
return qpow(sum[b], p - ) * qpow(sum[a - b], p - ) % p * sum[a] % p;
} ll C(ll a, ll b)
{
if (a < b)
return ;
if (a >= p)
return solve(a % p, b % p) * C(a / p, b / p) % p;
else
return solve2(a, b);
} int main()
{
while (cin >> x3 >> y3 >> x4 >> y4 >> p)
{
sum[] = ;
ni[] = ;
sum[] = ;
nijie[] = ;
nijie[] = ;
for (ll i = ; i <= min(x4 + , p); i++)
{
sum[i] = (sum[i - ] * i) % p;
ni[i] = (p - p / i) * ni[p % i] % p;
nijie[i] = (nijie[i - ] * ni[i]) % p;
}
ans = ;
for (ll i = y3; i <= y4; i++)
{
ans += C(x4 + , i + );
ans %= p;
}
for (ll i = y3; i <= y4; i++)
{
ans = (ans - C(x3, i + ) + p) % p;
ans %= p;
}
printf("%lld\n", (ans + p) % p);
} return ;
}

Hdu5226 Tom and matrix的更多相关文章

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

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

  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. Bestcoder Tom and matrix

    问题描述 Tom放学回家的路上,看到天空中出现一个矩阵.Tom发现,如果矩阵的行.列从0开始标号,第i行第j列的数记为ai,j,那么ai,j=Cji 如果i < j,那么ai,j=0 Tom突发 ...

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

  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. acdeream Matrix Multiplication

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

  8. HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  9. What is an eigenvector of a covariance matrix?

    What is an eigenvector of a covariance matrix? One of the most intuitive explanations of eigenvector ...

随机推荐

  1. [network]交换机中用户权限

    LEVEL 0(访问级):可以执行用于网络诊断等功能的命令.包括ping.tracert.telnet等命令,执行该级别命令的结果不能被保存到配置文件中. LEVEL 1(监控级):可以执行用于系统维 ...

  2. 在Windows2008下添加iscsi存储出现磁盘Offine(The disk is offine because of policy set by an adminstrator)的解决方法

    打开CMD命令行输入如下命令: DISKPART.EXE DISKPART> san SAN Policy : Offline Shared DISKPART> san policy=On ...

  3. Ubuntu18.04重装指南

    Guide google chrome sougou 谷歌服务助手\(\rightarrow\)谷歌访问助手(谷歌应用商店)登录谷歌账号(cnyalitea@gmail.com)然后同步. \(\te ...

  4. [python]序列的重复操作符

    当你需要需要一个序列的多份拷贝时,重复操作符非常有用,它的语法如下: sequence * copies_int In [1]: a = [1,2,3,4] In [2]: a * 5 Out[2]: ...

  5. [转]oracle数据库定时任务dbms_job的用法详解

    这篇文章给大家详细介绍了dbms_job的用法,用于安排和管理作业队列,通过使用作业,可以使ORACLE数据库定期执行特定的任务.有需要的朋友们可以参考借鉴.   一.dbms_job涉及到的知识点 ...

  6. python中取整的几种方法

    #encoding:utf-8import math #向上取整print "math.ceil---"print "math.ceil(2.3) => " ...

  7. 第9次Scrum会议(10/21)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/21 17:20~17:45,总计25min.地点:东北师范 ...

  8. 团队Alpha冲刺(六)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  9. hdu 5524

    由于是完全二叉树,所以我们可以预先知道整棵树的形状,因此可以判断根节点的两个子节点哪个是满二叉树,哪个不是满二叉树(必然是一边满,一边不满),对于满的子节点,我们可以直接求出它的不同子树的个数,也就是 ...

  10. 《我是一只it小小鸟》观后感

    在这个学期开始的时候我们的老师推荐给我们这本书.在很多的网站上只要一提到IT,总会有人推荐这本书,我在读这本书之前看了很多关于它的书评,其中有一位网友的一句话让我对它产生了很大的兴趣:“印象最深的是书 ...