hdu5698 百度之星2016round2b第3题
这题首先是找规律推公式,然后就是组合数学的知识了。
题目是问到第n行第m列的格式有几种方案,我们可以用手算的方法列出当n和m比较小时的所有答案
比如我列出以下8*8的矩阵
矩阵上的数表示从那个位置到最右下角一共有多少种方案。
求每个位置的值也简单,就是把它右下角的所有数加起来即可。
那么,把这个矩阵倒过来看,就是想要的结果矩阵了。
规律也很容易发现,首先,矩阵是对称的,所以我是只考虑m>=n的情况。
然后,可以发现每个位置的数就是一个组合数C(m + n - 4, n - 2)
最后就是求组合数取模了,C(m + n - 4, n - 2) %
然而,多年没做题的我,并不会组合数取模。找了以前的模板,是竹教主写的,看了好半天才明白,等我打完的时候,比赛刚结束。
比赛结束后交了一次,果然a了T_T
以下是代码
/*
* baidu/win.cpp
* Created on: 2016-5-22
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
LL Ext_gcd(LL a, LL b, LL &x, LL &y) {
if (b == ) {
x = , y = ;
return a;
}
LL ret = Ext_gcd(b, a % b, y, x);
y -= a / b * x;
return ret;
}
LL Inv(LL a, int m) { ///求除数a对m的逆元;
LL d, x, y, t = (LL) m;
d = Ext_gcd(a, t, x, y);
if (d == )
return (x % t + t) % t;
return -;
}
void work(int n, int m) {
int i;
const int mod = ;
LL sum = ;
for (i = n - m + ; i <= n; i++) {
sum *= (LL) i;
sum %= mod;
}
LL tmp = ;
for (i = ; i <= m; i++)
tmp *= i, tmp %= mod; sum *= Inv(tmp, mod);
sum %= mod;
printf("%I64d\n", sum);
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) == ) {
if (m < n) {
int tmp = m;
m = n;
n = tmp;
}
work(m + n - , n - );
}
return ;
}
hdu5698 百度之星2016round2b第3题的更多相关文章
- 2018"百度之星"程序设计大赛 - 资格赛 - 题集
1001 $ 1 \leq m \leq 10 $ 像是状压的复杂度. 于是我们(用二进制)枚举留下的问题集合 然后把这个集合和问卷们的答案集合 $ & $ 一下 就可以只留下被选中的问题的答 ...
- 2014年百度之星资格赛第三题Xor Sum
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheus 将向 Zeu ...
- 2014年百度之星资格赛第四题Labyrinth
Problem Description 度度熊是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫.该迷宫仅仅能从矩阵左上角第一个方格開始走.仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能 ...
- 2018 百度之星 初赛 第六题 HDU6349
三原色图 Accepts: 281 Submissions: 1261 Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 262144/ ...
- 2019 年百度之星—初赛一 B题 Game
题目链接 题意:最开始可以选择任意位置,在一个坐标轴上,依次走到一个区间里面,可以选择走一步两步,求最小步数. 思路:贪心,刚开始合并区间,确定初始位置以及方向.往右走肯定到左端点,往左走先到右端点, ...
- 百度之星B题(组合数)
Problem B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 2016百度之星 初赛2A ABEF
只做了1001 1002 1005 1006.剩下2题可能以后补? http://acm.hdu.edu.cn/search.php?field=problem&key=2016%22%B0% ...
- 2016百度之星 资格赛ABCDE
看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...
- HDU 5688:2016"百度之星" - 资格赛 Problem D
原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- 【CF56E】Domino Principle(线性扫描,伪DP)
每块多米诺骨牌所在的位置设为x,每块多米诺骨牌高度为h.如果将x位置上的多米诺骨牌向右翻到,它就可以影响[x+1, x+h-1]范围内的所有多米诺骨牌,让他们也翻到,同时这些被翻到的多米诺骨牌还能影响 ...
- scroll与按钮的位置
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 转载:linux编程,命令行参数输入getopt
下面资料来自百度百科: getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const ...
- react状态
组件的生命周期分成三个状态: Mounting:已插入真实 DOM Updating:正在被重新渲染 Unmounting:已移出真实 DOM React 为每个状态都提供了两种处理函数,will 函 ...
- css查缺补漏1
css可以写在哪里 1.和要装饰的标签写在一起 2.内部样式表(内嵌式)是写在head头部标签中,并且用style标签定义 3.外部样式表(外链式) <head><link rel= ...
- Redis - 事务操作与详解
https://blog.csdn.net/J080624/article/details/81669560 写的比较清楚的一个帖子
- SpringMVC整合MongoDB
首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...
- python 常用库, 随时添加中
python只有你想不到的库,只要你能想到,肯定有对应的库 前面*的为标准库,**的是pip里面的,***则是要手动安装的1)python运行时服务* copy: copy模块提供了对复合(compo ...
- 移除array中重复的item
//move the repeated item NSInteger index = [orignalArray count] - 1; for (id o ...