codevs 1515 跳 贪心+lucas
一个人初始在(0, 0), 想到(n, m)去, 没到一个格子, 花费的值为C(n, m), 求最小值。
C(n, m)的定义为, 如果n==0||m==0, 则为1, 否则C(n, m) = C(n-1, m)+C(n, m-1)。
很容易看出来贪心的策略, 先横着或竖着走max(m, n)个格子,代价为max(m, n)+1, 然后在竖着或横着走,代价是一个组合数, C(n+m+1, min(n, m) )-1。 组合数用lucas算就好。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
ll pow(ll a, ll b) {
ll ret = ;
while(b) {
if(b&1LL) {
ret = ret*a%mod;
}
a = a*a%mod;
b >>= 1LL;
}
return ret;
}
ll C(ll n, ll k) {
if(n<k)
return ;
ll s1 = , s2 = ;
for(int i = ; i<k; i++) {
s1 = s1*(n-i)%mod;
s2 = s2*(i+)%mod;
}
return s1*pow(s2, mod-)%mod;
}
ll lucas(ll a, ll b) {
if(b == )
return ;
return C(a%mod, b%mod)*lucas(a/mod, b/mod);
}
int main()
{
ll n, m;
cin>>n>>m;
ll ans = max(m, n);
ans %= mod;
ans += lucas(m+n+, min(m, n));
ans %= mod;
cout<<ans<<endl;
return ;
}
codevs 1515 跳 贪心+lucas的更多相关文章
- codevs 1515 跳
/* 画矩阵找规律发现是杨辉三角 Cg (i,j)= C (i+j,i); 贪心走的话 沿着0行(列)一直走然后拐直角 拐弯后每个格子都累加 Cg (n,0) + Cg (n,1) + Cg (n,2 ...
- 1515 跳 - Wikioi
题目描述 Description邪教喜欢在各种各样空间内跳.现在,邪教来到了一个二维平面.在这个平面内,如果邪教当前跳到了(x,y),那么他下一步可以选择跳到以下4个点:(x-1,y), (x+1,y ...
- NOIP 2015复赛提高组Day2 T1==Codevs 4768 跳石头
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中 ...
- Codevs 4768 跳石头 NOIP2015 DAY2 T1
4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 一年一度的"跳石头"比赛又要开始了! ...
- [CodeVs1515]跳(lucas定理+费马小定理)
嘿嘿嘿好久没写数学题了,偶尔看到一道写一写... 题目大意:一个(n+1)*(m+1)[0<=n, m<=10^12,n*m<=10^12]的矩阵,C(0,0)=1,C(x,y)=C ...
- codevs 4768跳石头
传送门 4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在 ...
- codevs 4768 跳石头
传送门 表示去年不会,二分是啥都不知道,一脸懵逼. 今年再做,虽然知道二分是啥了,但依旧不会,蒙蔽了好几天,最后还是看了题解. #include<cstdio> #define M 510 ...
- Codevs 均分纸牌(贪心)
题目描述 Description 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...
- Codevs No.2144 砝码称重2
2016-05-31 22:01:16 题目链接: 砝码称重2 (Codevs No.2144) 题目大意: 给定N个砝码,求称出M的重量所需砝码最小个数 解法: 贪心 使砝码数量最小,当然是每个砝码 ...
随机推荐
- Redis的持久化选项
Redis提供了两种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里面.另一种方法叫只追加文件(append-only ...
- xcode UILabel创建和隐藏
// 创建label UILabel *label = [[UILabel alloc] init]; // 设置显示的文字 label.text = @"Hello world!Hello ...
- printf("%d, %d\n", i++, ++i)的输出结果是确定的吗???
1. 问题描述 以下代码的输出结果是什么? 题目1: ; printf("%d, %d\n", i++, ++i); 题目2: ; printf("%d, %d, %d, ...
- 学习ExtjsFor.NET(第二个案例-Array的Every方法)
Ext.Array.every(Array array,Function fn,Object scope)是一个遍历的方法. array是数组,fn是方法,scope是作用域.every返回true和 ...
- iis 配置php
1.CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一种语 ...
- iOS iOS9下修改回HTTP模式进行网络请求
升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错 The resource could not be loaded because the App Transport Sec ...
- U盘安装CentOS6.x报错:Missing ISO 9660 Image
以前都是DVD安装CentOS,这次因为装固态硬盘,然后把光驱给卸载了.所以就尝试用U盘引导安装CentOS,结果安装时竟然出现了Missing ISO 9660 Image的错误. 解决方案: 将C ...
- BZOJ 1812: [Ioi2005]riv( 树形dp )
树背包, 左儿子右兄弟来表示树, dp(x, y, z)表示结点x, x的子树及x的部分兄弟共建y个伐木场, 离x最近的伐木场是z时的最小代价. 时间复杂度O(N^2*K^2) ----------- ...
- Exception和RuntimeException
public class RuntimeExceptionDemo01 { public static void main(String[] args) { String string=&qu ...
- 男装电子零售商East Dane即将面世_衣装_YOKA时尚网
男装电子零售商East Dane即将面世_衣装_YOKA时尚网 男装电子零售商East Dane即将面世