小W与网格
lucas定理 http://www.cnblogs.com/vongang/archive/2012/12/02/2798138.html
题目:http://hihocoder.com/contest/challenge13/problem/1
彬神的方法……
假设右上方向出格子 右上方向 宽 为 w ,高 为 h,
则从 上方的格子出去可以走 0 个 横向,1 个 横向 … 到 w 个 横向,
但是所走的 是 在 那个 位置 走 那几个 横向,是从 n 个 盒子 里 放 m 个 球的问题
同理 从右边的格子出去可以走 0 个高度,1个高度……到 h 个高度
其中 n ,m 为 1 要特殊处理
由于 四个方向计算了两次 ,所以要 - 4
贴代码……
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const double ESP = 10e-;
const int MOD = ;
typedef long long LL;
LL exp_mod(LL a, LL b, LL p) {
LL res = ;
while(b != ) {
if(b&) res = (res * a) % p;
a = (a*a) % p;
b >>= ;
}
return res;
} LL Comb(LL a, LL b, LL p) {
if(a < b) return ;
if(a == b) return ;
if(b > a - b) b = a - b; LL ans = , ca = , cb = ;
for(LL i = ; i < b; ++i) {
ca = (ca * (a - i))%p;
cb = (cb * (b - i))%p;
}
ans = (ca*exp_mod(cb, p - , p)) % p;
return ans;
} LL Lucas(int n, int m, int p) {
LL ans = ; while(n&&m&&ans) {
ans = (ans*Comb(n%p, m%p, p)) % p;
n /= p;
m /= p;
}
return ans;
}
int fun(int h,int w){
int ans = ;
for(int i = ;i <= h;i++){
ans += Lucas(w+i,i,MOD);
ans %= MOD;
}
for(int i = ;i < w;i++){
ans += Lucas(h+i,i,MOD);
ans %= MOD;
}
return ans;
}
int main(){
// freopen("input.txt","r",stdin);
int n,m,i,j;
while(~scanf("%d%d%d%d",&n,&m,&i,&j)){
if(n==){
printf("%d\n",m);
continue;
}else if(m==){
printf("%d\n",n);
continue;
}
int ans = fun(i-,j-);
ans += fun(n-i,j-);
ans %= MOD;
ans += fun(i-,m-j);
ans %= MOD;
ans += fun(n-i,m-j);
ans %= MOD;
ans = (ans-+MOD)%MOD;
printf("%d\n",ans);
}
return ;
}
小W与网格的更多相关文章
- XidianOJ 1076 小W喜欢的数字
题目描述 大家都知道,小W是一名大帅哥,当然比起Light还是有点儿差距的!帅气的小W认为0-9这些数字,只有1,3,5是完美的. 欲问小W为什么,小W总是说"帅哥,是不需要解释的" ...
- 武汉科技大学ACM :1008: 小t和小w
Problem Description 小t最近学了C语言,他想要在女朋友小w面前展示一下自己的能力,小w喜欢如样例所示的图形, 想让小t写一个程序来输出这样的图形,小t拿到后感觉有点困难,小t不想在 ...
- bzoj4665小w的喜糖 dp+容斥
4665: 小w的喜糖 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 120 Solved: 72[Submit][Status][Discuss] ...
- bzoj4665 小w的喜糖(dp+容斥)
4665: 小w的喜糖 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 222 Solved: 130[Submit][Status][Discuss ...
- bzoj2441【中山市选】小W的问题
题目描述 有一天,小W找了一个笛卡尔坐标系,并在上面选取了N个整点.他发现通过这些整点能够画出很多个“W”出来.具体来说,对于五个不同的点(x1, y1), (x2, y2), (x3, y3), ( ...
- 【BZOJ 4665】 4665: 小w的喜糖 (DP+容斥)
4665: 小w的喜糖 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 94 Solved: 53 Description 废话不多说,反正小w要发喜 ...
- 【BZOJ4665】小w的喜糖 容斥+组合数
[BZOJ4665]小w的喜糖 Description 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那 ...
- bzoj2441 [中山市选2011]小W的问题(debug中)
2441: [中山市选2011]小W的问题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 487 Solved: 186[Submit][Statu ...
- 小w的喜糖(candy)
小w的喜糖(candy) 题目描述 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那会有多少种方案使得每 ...
随机推荐
- sql server单表导入、导出
sql server单表导入.导出(通过CSV文件) 导出:直接打开查询分析器查询要导出表的信息(select * from 表),得到的结果全选,右键另存为 xxx.csv文件 (得到该表的所有 ...
- Qt 错误: 无法运行 release 下的可执行文件
学习Qt有一点时间了,但之前都是在debug版本下进行编译运行,偶然切换到release版本下,却出现了如下错误: 错误提示: This application failed to sta ...
- 如何正确地在手机上显示图片——QImage与QPixmap完全解析
引言 用Qt程序在手机上显示一幅图片对编程人员来说是再基础不过的一件事情了.那么先让大家看两段代码: //dangerous should not be used, cannot display ea ...
- android应用Dialog跳转到Activity
public void back(Context context){ intent = new Intent(); intent.setClass(context, IBookActivity.cla ...
- 基于visual Studio2013解决C语言竞赛题之0406数列求和
题目 解决代码及点评 这个题目,还是考察for循环的使用 以及数列规律,该数列的特点是第n个分子 = 第n-1个分子 + 第n-2个分子,分母也是此规律 而另外一个规律是第n个分子和第n- ...
- Block动画 和 Spring动画
Block动画: @interface BlockViewController () @property (weak, nonatomic) IBOutlet UIView *playView; @e ...
- Cocos2d-x 精灵碰撞检測(方法二)
将"Cocos2d-x 精灵碰撞检測(方法一)" update函数改动一下. 使用精灵boundingBox函数获取直接精灵边界框, 不用自己计算精灵矩形大小了,还比較精确,然后调 ...
- 一个IP每天只弹一次广告窗口
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- D - 二叉树遍历(推荐)
二叉树遍历问题 Description Tree Recovery Little Valentine liked playing with binary trees very much. Her ...
- Python-zip压缩-解压
#打包成zip文件 import zipfile f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) f.write('file_t ...