Punching Robot

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4928

Description

In this problem, you are given a grid map of N ×M (N rows and M

columns) where the rows are numbered 1. . . N from top to bottom,

and the columns are numbered 1. . . M from left to right. Your task

is to count in how many ways you can reach cell (N, M) from cell

(1, 1) given that you are only allowed to move right or downward

at any time, i.e. if your current location is at cell (r, c), then you

can only move to cell (r + 1, c) or (r, c + 1). However, we quickly

realized that this kind of problem could be too easy for you, thus,

not challenging. Therefore, we decided to put K punching robots

in the map. Each punching robot is able to punch any object which

lies in any of 3×3 cells centered at the robot (Figure 1). To simplify

the problem, you may assume that the punching areas of any robot

do not overlap.

Your (new) task is: count in how many ways you can reach cell (N, M) from cell (1, 1) without

being punched by any robot, given that you are only allowed to move right or downward at any time.

As the output can be very large, you need to modulo the output by 997. For example, consider the

following map of 4 x 10 with two punching robots at (3, 3) and (2, 8).

Figure 2.

In this example, there are 4 ways to reach (4, 10) from (1, 1) without being punched by any of the

robots. All those 4 paths only differ when they go from (1, 5) to (4, 6):

• . . . , (1, 5), (1, 6), (2, 6), (3, 6), (4, 6), . . .

• . . . , (1, 5), (2, 5), (2, 6), (3, 6), (4, 6), . . .

• . . . , (1, 5), (2, 5), (3, 5), (3, 6), (4, 6), . . .

• . . . , (1, 5), (2, 5), (3, 5), (4, 5), (4, 6),

Meanwhile, there is only one unique path from (1, 1) to (1, 5) and from (4, 6) to (4, 10).

Input

The first line of input contains an integer T (T ≤ 100) denoting the number of cases. Each case begins

with three integers: N, M, and K (2 ≤ N, M ≤ 1, 000, 000; 0 ≤ K ≤ 10) denoting the size of the map

and the number of punching robots respectively. The following K lines, each contains two integers: Ri

and Ci (1 < Ri < N; 1 < Ci < M) denoting the position of i-th robot (row and column respectively)

in the map. You are guaranteed that, for any two robots, the row difference or the column difference

will be at least 3, i.e. no two robots’ punching areas are overlapping. You are also guaranteed that cell

(1, 1) and cell (N, M) are not in punching areas of any robots.

Output

For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the answer

for that case modulo by 997.

Explanation for 2nd sample case:

The following figure represents the map for the 2nd sample

case.

As you can see, there is no way you can reach (3, 5) from (1,

  1. without being punched by the robot.

Sample Input

4

4 10 2

3 3

2 8

3 5 1

2 3

5 5 0

10 9 3

9 3

6 8

3 4

Sample Output

Case #1: 4

Case #2: 0

Case #3: 70

Case #4: 648

Hint

题意

给你个(n,m)的方格,里面有一些坏的3*3位置,问你从(1,1)到(n,m)的方案数是多少

题解:

我们把3*3的拆成9个坏点,那么这道题就和CF的某道题一样了

http://www.cnblogs.com/qscqesze/p/4669136.html

但是这道题的模数是997,所以取逆元的时候可能有问题,你需要把997单独拿出来讨论一下就好了。

代码

#include<bits/stdc++.h>
using namespace std;
#define maxn 3000005
#define mod 997
typedef long long ll;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int cas=0;
struct Point
{
long long x,y;
}points[maxn];
bool cmp(Point a,Point b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
ll p=mod;
ll fac[maxn],num[maxn];
ll qpow(ll a,ll b)
{
ll ans=1;a%=mod;
for(ll i=b;i;i>>=1,a=a*a%mod)
if(i&1)ans=ans*a%mod;
return ans;
}
ll C(ll n,ll m)
{
if(m>n||m<0)return 0;
if(num[n]!=num[n-m]+num[m]) return 0;
ll s1=fac[n],s2=fac[n-m]*fac[m]%mod;
return s1*qpow(s2,mod-2)%mod;
}
ll f[maxn];
int main()
{
fac[0]=1;
for(int i=1;i<maxn;i++)
{
if(i%997!=0)
fac[i]=fac[i-1]*i%mod,num[i]=num[i-1];
else
{
num[i]=num[i-1];
int tmp=i;
while(tmp%997==0)
num[i]++,tmp/=997;
fac[i]=fac[i-1]*tmp;
}
}
int t;scanf("%d",&t);
while(t--){
int n=read(),m=read(),k=read();
for(int i=1;i<=k;i++)
{
points[i*9-8].x=read();
points[i*9-8].y=read();
points[i*9-8].x-=1;
points[i*9-8].y-=1; points[i*9-7].x=points[i*9-8].x-1;
points[i*9-7].y=points[i*9-8].y-1; points[i*9-6].x=points[i*9-8].x;
points[i*9-6].y=points[i*9-8].y-1; points[i*9-5].x=points[i*9-8].x+1;
points[i*9-5].y=points[i*9-8].y-1; points[i*9-4].x=points[i*9-8].x-1;
points[i*9-4].y=points[i*9-8].y; points[i*9-3].x=points[i*9-8].x+1;
points[i*9-3].y=points[i*9-8].y; points[i*9-2].x=points[i*9-8].x-1;
points[i*9-2].y=points[i*9-8].y+1; points[i*9-1].x=points[i*9-8].x;
points[i*9-1].y=points[i*9-8].y+1; points[i*9].x=points[i*9-8].x+1;
points[i*9].y=points[i*9-8].y+1;
}
k*=9;
points[++k].x=n-1;
points[k].y=m-1;
sort(points+1,points+k+1,cmp);
for(int i=1;i<=k;i++)
{
f[i]=C(points[i].x+points[i].y,points[i].x);
for(int j=1;j<i;j++)
{
if(points[j].y<=points[i].y)
{
f[i]+=(p-f[j]*C(points[i].x-points[j].x+points[i].y-points[j].y,points[i].x-points[j].x)%p);
f[i]%=p;
}
}
}
printf("Case #%d: %lld\n",++cas,f[k]%p);
}
}

UVALive 6916 Punching Robot dp的更多相关文章

  1. UVALive - 6916 Punching Robot Lucas+dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/96344 Punching Robot Time Limit: 1000MS64bit IO Format: ...

  2. 【BZOJ1408】[Noi2002]Robot DP+数学

    [BZOJ1408][Noi2002]Robot Description Input Output Sample Input 3 2 1 3 2 5 1 Sample Output 8 6 75 HI ...

  3. UVALive - 6952 Cent Savings dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...

  4. UVALive - 6529 找规律+dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...

  5. UVaLive 6801 Sequence (计数DP)

    题意:给定一个序列,有 n 个数,只有01,然后你进行k次操作,把所有的1变成0,求有多种方法. 析:DP是很明显的,dp[i][j] 表示进行第 i 次操作,剩下 j 个1,然后操作就两种,把1变成 ...

  6. UVaLive 6697 Homework Evaluation (DP)

    题意:给出一个长字符串,再给一个短字符串,进行匹配,如果第i个恰好匹配,则 +8,:如果不匹配,可以给长或短字符串添加-,先后匹配,这样-3, 连续的长字符串添加-,需要减去一个4:也可不给添加-,则 ...

  7. UVaLive 7374 Racing Gems (DP,LIS)

    题意:以辆赛车可以从x轴上任意点出发,他的水平速度允许他向每向上移动v个单位,就能向左或向右移动v/r个单位(也就是它的辐射范围是个等腰三角形) 现在赛车从x轴出发,问它在到达终点前能吃到的最多钻石. ...

  8. UVALive 6947 Improvements(DP+树状数组)

    [题目链接] https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sho ...

  9. UVaLive 3490 Generator (KMP + DP + Gauss)

    题意:随机字母组成一个串,有一个目标串,当这个由随机字母组成的串出现目标串就停止,求这个随机字母组成串的期望长度. 析:由于只要包含目标串就可以停止,所以可以先把这个串进行处理,也就是KMP,然后dp ...

随机推荐

  1. SQL语句(十二)分组查询

    (十二)分组查询 将数据表中的数据按某种条件分成组,按组显示统计信息 查询各班学生的最大年龄.最小年龄.平均年龄和人数 分组 SELECT <字段名表1> FROM <表名> ...

  2. 通达信zig函数的python实现

    通达信zig函数的python实现 代码 # coding: utf-8 """ Created on Sat Jan 05 18:53:39 2019 http://w ...

  3. Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

  4. 总有一些实用javascript的元素被人遗忘在角落-slice

    slice() 方法可从已有的数组中返回选定的元素. 好吧,我承认我竟然把它忘了! 这次我在回顾一下它 语法 arrayObject.slice(start,end) 数组.slice(起始,结束) ...

  5. PyTorch学习系列(九)——参数_初始化

    from:http://blog.csdn.net/VictoriaW/article/details/72872036 之前我学习了神经网络中权值初始化的方法 那么如何在pytorch里实现呢. P ...

  6. 关于阿里云和ucloud云服务器负载均衡器slb和ulb会话保持的配置

    在阿里云slb或者ucloud的ulb上对公司网站后台做了负载均衡以后,发现经常需要重新登录,单独访问没有这样的问题,问题就出在session的保持上,在云控制台中有配置会话的相关选项 阿里云的配置 ...

  7. 使用OpenSSL自建CA + Nginx配置HTTPS

    Ubuntu 16.04(ECS),OpenSSL 1.0.2g  1 Mar 2016,Nginx 1.10.3 (Ubuntu), 浏览器:Chrome 67,Firefox 61,Edge 40 ...

  8. MongoDB:数据导入CSV文件之错误记录

    测试主机1:Windows 10,MongoDB 3.6.3,WPS 10.1,Notepad++ 7.5.3, 测试主机2:Ubuntu 16.04,MongoDB 4, 今天测试了将数据从文件—— ...

  9. Kaggle案例分析3--Bag of Words Meets Bags of Popcorn

    项目描述:这是一个关于情感分析的教程.谷歌的Word2Vec(文本深度表示模型)是一个由深度学习驱动的方法, 旨在获取words内部的含义.Word2Vec试图理解单词之间的含义与语义关系.它类似于r ...

  10. MFC命名规范

    属性部分 全局变量:g_ 常量:c_ c++类成员变量:m_ 静态变量:s_ 类型部分 指针:p 函数:fn 无效:v 句柄:h 长整型:l 布尔:b 浮点型(有时也指文件):f 双字:dw 字符串: ...