题意:在杨辉三角中让你从最上面到 第 n 行,第 m 列所经过的元素之和最小,只能斜向下或者直向下走。

析:很容易知道,如果 m 在n的左半部分,那么就先从 (n, m)向左上,再直着向上,如果是在右半部分,那么就是先直着向上,再斜着左上。这样对应到,

左半部分:C(n, m) + C(n-1, m-1) + C(n-2, m-2) + ... + C(n-m, 0) + (n-m)

右半部分:C(n, m) + C(n-1, m) + C(n-2, m) + ... + C(m, m) + m

然后化简得到的答案就是C(n+1, m) + n - m,和C(n+1, m+1) + m。然后用Lucsa 定理就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <ctime>
#include <cstdlib>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL p; LL quick_pow(LL a, LL n){
LL ans = 1LL;
a %= p;
while(n){
if(n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
} LL C(LL n, LL m){
if(n < m) return 0;
LL a = 1LL, b = 1LL;
while(m){
a = a * n % p;
b = b * m % p;
--n; --m;
}
return a * quick_pow(b, p-2) % p;
} LL Lucas(LL n, LL m){
if(!m) return 1;
return C(n%p, m%p) * Lucas(n/p, m/p) % p;
} int main(){
int kase = 0;
LL m, n;
while(scanf("%I64d %I64d %I64d", &n, &m, &p) == 3){
printf("Case #%d: ", ++kase);
if(m * 2 < n) printf("%I64d\n", (Lucas(n+1, m) + n - m) % p);
else printf("%I64d\n", (Lucas(n+1, m+1) + m) % p);
}
}

HDU 3944 DP? (Lucas定理)的更多相关文章

  1. HDU 3944 DP? [Lucas定理 诡异的预处理]

    DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Subm ...

  2. 【bzoj2111】[ZJOI2010]Perm 排列计数 dp+Lucas定理

    题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Mogic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Mogic的,答案可能很 ...

  3. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  4. BZOJ 2111 [ZJOI2010]Perm 排列计数:Tree dp + Lucas定理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2111 题意: 给定n,p,问你有多少个1到n的排列P,对于任意整数i∈[2,n]满足P[i ...

  5. [CTSC2017][bzoj4903] 吉夫特 [状压dp+Lucas定理]

    题面 传送门 思路 一句话题意: 给出一个长度为 n 的序列,求所有长度大于等于2的子序列个数,满足:对于子序列中任意两个相邻的数 a和 b (b 在 a 前面),$C_a^b mod 2=1$,答案 ...

  6. bzoj 2111 [ZJOI2010]Perm 排列计数(DP+lucas定理)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2111 [题意] 给定n,问1..n的排列中有多少个可以构成小根堆. [思路] 设f[i ...

  7. hdu 3944 dp?

    DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Subm ...

  8. 【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理

    题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的 ...

  9. 【(好题)组合数+Lucas定理+公式递推(lowbit+滚动数组)+打表找规律】2017多校训练七 HDU 6129 Just do it

    http://acm.hdu.edu.cn/showproblem.php?pid=6129 [题意] 对于一个长度为n的序列a,我们可以计算b[i]=a1^a2^......^ai,这样得到序列b ...

随机推荐

  1. Codevs 2756 树上的路径

    2756 树上的路径  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description 给出一棵树,求出最小的k,使得,且在树中存在 ...

  2. Java实验--继承与多态

    ---恢复内容开始--- 题目如下: [实验任务一]:面积计算(设计型). 1. 实验要求: 实验报告中要求包括程序设计思想.程序流程图.源代码.运行结果截图.编译错误分析等内容. 2.实验内容: ( ...

  3. 【.Net Core 学习系列】-- EF Core实践(DB First)

    一.开发环境: VS2015, .Net Core 1.0.0-preview2-003156 二.准备数据: CREATE DATABASE [Blogging]; GO USE [Blogging ...

  4. 使用Spring定时任务并且通过AOP监控任务执行情况

    原文:http://www.open-open.com/code/view/1426250803279 本文讲的是通过Spring注解的方式实现任务调度.只要引入了spring-context包就能够 ...

  5. iOS用户体验之-modal上下文

    iOS用户体验之-modal上下文 何为模态视图,它的作用时聚焦当前.获得用户的注意,用户仅仅有完毕模态的任务才 退出模态视图.否则你将不能运行app的任务,比如,alert view,model v ...

  6. CMMI 2,3,4,5级涉及的过程域(PA)介绍

      CMMI中的PA即Process Area的缩写,中文称为过程域.简单的说就是做好一个事情需要的某一个方面,对于软件开发来说,就是做好软件开发需要的某一个方面. CMMI2.3级共有18个过程域( ...

  7. 【转载】How browsers work--Behind the scenes of modern web browsers (前端必读)

    浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工 作原理,我们将看到,从你在地址栏输入google.com到你看到google主页过程中都发生了什么. 将讨论的浏览器 今天,有五种主流浏览器- ...

  8. c++中拷贝构造函数,浅拷贝和深拷贝的区别

    在C++提供了一种特殊的构造函数,称为拷贝构造函数.拷贝构造函数具有一般构造函数的所有特性,其作用是使用一个已经存在的对象(由拷贝构造函数的参数指定的对象)去初始化一个新的同类对象,即完成本类对象的复 ...

  9. 【bzoj2152】【聪聪可可】【点分治】

    [问题描写叙述] 聪聪和可但是兄弟俩.他们俩常常为了一些琐事打起来,比如家中仅仅剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(但是他们家仅仅有一台电脑)--遇到这样的问题,普通情况下石头剪刀布就好 ...

  10. vsCode 常用快捷键(mac 版)

    光标多行显示: commond+Alt+topArrow/downArrow 查找:commond+F 查找并按顺序切换下一个:commond+G 跳转到某一行: ctrl+G 输入行号跳转 跳转到某 ...