题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1157&pid=2

Description

985走入了一个n * n的方格地图,他已经知道其中有一个格子是坏的。现在他要从(1, 1)走到(n, n),每次只可以向下或者向右走一步,问他能否到达(n,n)。若不能到达输出-1,反之输出到达(n,n)的方案数。
 

Input

第一行输入一个整数t,代表有t组测试数据。
每组数据第一行输入三个整数n,x,y,分别代表方格地图的大小以及坏掉格子的位置。
注:1 <= t <= 20,1 <= n <= 30,1 <= x,y <= n。

Output

若可以到达(n,n)则输出方案数对1e9 + 7取余的结果,反之输出-1。
 
Sample Input

Sample Output

-

分析:组合数/dp

dp:

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define N 100
using namespace std; int main()
{
int T,i,n,j,dp[N][N],x,y; scanf("%d", &T); while(T--)
{
scanf("%d %d %d", &n,&x,&y);
memset(dp,,sizeof(dp)); for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==x&&j==y)///
dp[i][j]=;
else if(i==&&j==)///
dp[i][j]=;///1和2顺序反了,wa~wa~wa~
else
dp[i][j]=dp[i-][j]+dp[i][j-]; dp[i][j]%=;
} if(dp[n][n]==)
printf("-1\n");
else
printf("%d\n", dp[n][n]);
}
return ;
}

组合数:

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define N 10005
using namespace std; long long c(long long n,long long m)
{
if(m > n/)
m = n-m;
long long a =, b =;
for(int i =; i <= m; i++)
{
a *= n-i+;
b *= i;
if(a % b ==)
{
a /= b;
b =;
}
}
return a/b;
}
int main()
{
int t, n, x, y;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &n, &x, &y);
if((x == && y == )||(x == n && y == n))
{
printf("-1\n");
continue;
}
else
{
long long a = c(n*-, n-);
long long b = c(n*-x-y, n-x) * c( x+y--, x-);
int ans = (a-b)%;
printf("%d\n", ans);
}
}
return ;
}

Contest - 多校训练(985专场) Problem C: 985的方格难题的更多相关文章

  1. 算法训练 A+B Problem

     算法训练 A+B Problem   时间限制:1.0s   内存限制:512.0MB      问题描述 输入A,B. 输出A+B. 输入格式 输入包含两个整数A,B,用一个空格分隔. 输出格式 ...

  2. 2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记

    2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记 题意 多测.每次给你一个数\(n\),你可以同时问无数 ...

  3. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  4. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  5. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

    HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...

  6. Problem H: 小火山的围棋梦想 多校训练2(小火山专场)

    题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...

  7. Problem A: 小火山的跳子游戏 多校训练2(小火山专场)(周期)

    题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1158&pid=0 zzuli 1905  题意:如果k=1的话是1,2,3,4. ...

  8. 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...

  9. 【2016多校训练4】Multi-University Training Contest 4

    1001  Another Meaning 题意:字符串A中包含的字符串B可以翻译或不翻译,总共有多少方案. 题解:动规,dp[i]表示A的第i位为止有多少方案. 转移方程: dp[i]=dp[i-1 ...

随机推荐

  1. chapter 13_1 算术类的元方法

    假设用table来表示集合,用function去计算集合的交集.并集. 为了保持名称空间的整齐,将这些函数存入一个名为Set的table中. 现在,假设用“+”来计算两个集合的并集,那么就要让所有用于 ...

  2. java实现UDP聊天---转载

    import java.io.*; import java.net.*; class Send implements Runnable { private DatagramSocket ds; pub ...

  3. 洛谷-烤鸡-BOSS战-入门综合练习1

    题目背景 Background 猪猪hanke得到了一只鸡  题目描述 Description 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10 ...

  4. Nginx正向代理让无法直接上网的机器通过代理上网

    Nginx正向代理让无法直接上网的机器通过代理上网 在阿里云平台买了几台ECS.但是只要其中一台开通了公网.由于要初始化系统环境,需要网络安装相关依赖. Nginx正向代理配置: 一.Nginx 正向 ...

  5. jQuery,使用on代替delegate,live 写法区别

    早期对页面上后期加载的动态元素,赋事件或值的时候,是使用live的.  由于效率比较低(其实数据不多也感觉不出来),后面使用delegate委托来代替了,再后面,1.7以后使用on 来代替delega ...

  6. 使用计算监控(Using computed observables)

    计算监控(Computed Observables) 如果有两个监控属性firstName, lastName,此时我们要显示full name,我们要怎么办呢? 这时,可以创建一个computed ...

  7. 10- python 网络爬虫分析

    Python 网络爬虫简单分析 import urllib2 response = urllib2.urlopen("http://www.baidu.com") print re ...

  8. sublime3安装

    1.下载安装程序 http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%20Build%203059%20Setup.exe 2.下载注册机 http:/ ...

  9. hdu 1212 Big Number(大数取模)

    Problem Description As we know, Big Number is always troublesome. But it's really important in our A ...

  10. model中字段格式验证

    注释部分在前端不显示 /// <summary> /// 链接地址 /// </summary> [Display(Name = "链接地址")] //[D ...