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. Ztree结合jbox实现弹窗树结构

    点击添加分类,弹出事项选择框为jbox <a href="#" id="down{{row.id}}" style="display:none& ...

  2. cinder的组件

    跟nova相似,cinder也有很多组件,每个组件负责各自的业务,然后共同协作完成volume的管理.组件之间的通信方式与nova个组件之间的通信方式相同,都是通过消息队列进行通信. cinder-a ...

  3. ffmpe安装

    原文:https://www.jianshu.com/p/905df3d9e753 下载安装 下载最新源码包并解压 $ wget http://ffmpeg.org/releases/ffmpeg-3 ...

  4. IC设计前后端流程与EDA工具

    IC前端设计(逻辑设计)和后端设计(物理设计)的区分: 以设计是否与工艺有关来区分二者:从设计程度上来讲,前端设计的结果就是得到了芯片的门级网表电路. 前端设计的流程及使用的EDA工具 1.架构的设计 ...

  5. 《我是IT小小鸟》读后感

    <我是IT小小鸟>读后感 说实话,我根本不喜欢看这本书,要不是因为老师要求我也不会去看的,其实当老师提起这本书的时候我还是有点兴趣,去看的,可是看了很多后,觉得这根本不适合我,里面说的都是 ...

  6. 201621123037 《Java程序设计》第11周学习总结

    作业11-多线程 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程 ...

  7. 【数据库】同一字段根据不同条件更新的sql语句的写法

    语法: update test set 字段1=case when 条件1 then 值1 when 条件2 then 值2 end 示例: update PMS_ProjectInfo set Pr ...

  8. java 集合 父类的使用子类的方法时候 底层自动转型为子类的数据类型

    跟继承多态不一样 继承多态需要显示转型

  9. hbase快速入门

    hbase 是什么? Apache HBase is an open-source, distributed, versioned, non-relational database modeled a ...

  10. bzoj2429- 聪明的猴子

    题意其实就是说有很多个点,求一组边把它们都连接起来,并且最大的那条边最小.很明显这就是一个最小生成树,是一颗保证最长边最短的树. 代码 刚刚学了个Borůvka算法,于是写了两个. Borůvka # ...