HDOJ 3944 DP?
尽量沿着边走距离最短。化减后 C(n+1,k)+ n - k,
预处理阶乘,Lucas定理组合数取模
DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)
Total Submission(s): 1899 Accepted Submission(s): 633

Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,…and the column from left to right 0,1,2,….If using C(n,k) represents the number of row n, column k. The Yang Hui Triangle has a regular pattern as follows.
C(n,0)=C(n,n)=1 (n ≥ 0)
C(n,k)=C(n-1,k-1)+C(n-1,k) (0<k<n)
Write a program that calculates the minimum sum of numbers passed on a route that starts at the top and ends at row n, column k. Each step can go either straight down or diagonally down to the right like figure 2.
As the answer may be very large, you only need to output the answer mod p which is a prime.
1 1 2
4 2 7
Case #1: 0
Case #2: 5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; LL n,k,p; LL fact[1300][11000]; LL QuickPow(LL x,LL t,LL m)
{
if(t==0) return 1LL;
LL e=x,ret=1LL;
while(t)
{
if(t&1LL) ret=(ret*e)%m;
e=(e*e)%m;
t>>=1LL;
}
return ret%m;
} int prime[2000],pr;
bool vis[10100]; void get_prime()
{
for(int i=2;i<10100;i++)
{
if(vis[i]==false)
prime[pr++]=i;
for(int j=2*i;j<10100;j+=i)
vis[j]=true;
}
} void get_fact()
{
for(int i=0;i<1240;i++)
{
fact[i][0]=1LL;
for(int j=1;j<=prime[i]+10;j++)
{
fact[i][j]=(fact[i][j-1]*j)%prime[i];
}
}
} LL Lucas(LL n,LL m,LL p)
{
LL ret=1LL;
int id=lower_bound(prime,prime+pr,p)-prime;
while(n&&m)
{
LL a=n%p,b=m%p;
if(a<b) return 0;
ret=(ret*fact[id][a]*QuickPow((fact[id][b]*fact[id][a-b])%p,p-2,p)%p)%p;
n/=p; m/=p;
}
return ret%p;
} int main()
{
get_prime();
get_fact();
int cas=1;
while(scanf("%I64d%I64d%I64d",&n,&k,&p)!=EOF)
{
if(k>n/2) k=n-k;
LL ans=(Lucas(n+1,k,p)+n-k)%p;
printf("Case #%d: %I64d\n",cas++,ans);
}
return 0;
}
HDOJ 3944 DP?的更多相关文章
- hdu 3944 dp?
DP? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Subm ...
- HDOJ 1069 DP
开启DP之路 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 描述一下: 就是给定N(N<=20)个方体,让你放置,求放置的最高高度,限制条件 ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- hdoj 1257 DP||贪心
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDOJ 1260 DP
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 3944 DP? [Lucas定理 诡异的预处理]
DP? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Subm ...
- HDU 3944 DP? (Lucas定理)
题意:在杨辉三角中让你从最上面到 第 n 行,第 m 列所经过的元素之和最小,只能斜向下或者直向下走. 析:很容易知道,如果 m 在n的左半部分,那么就先从 (n, m)向左,再直着向上,如果是在右半 ...
- [SinGuLaRiTy] 组合数学题目复习
[SinGuLaRiTy] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 2011] 计算系数 题目描述 给定一个多项式( ...
- 找规律/数位DP HDOJ 4722 Good Numbers
题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...
随机推荐
- 用javascript写一个显示时间差 几分钟前 几小时前 几天前 几周前 大于一个月显示日期
window.onload = function(){ var show_times = $(".times span"); for(var i=0;i<show_times ...
- Poj3580 Super Memo(FHQ-Treap)
题面 题解 对于操作$1$,我们可以对于每个节点打一个$add$标记,下放就行了 对于操作2,可以参考这篇题解的上一篇,不赘述 对于操作4,可以将区间裂成两部分,然后再插入合并 对于操作5,可以将区间 ...
- linux shell date 用当天时间做备份文件名
#!/bin/bash #date 显示时间,我们可以用时间的不同做为备份文件的名字,这样以前的备份就不会被覆盖 datename=$(date +%Y%m%d-%H%M%S) ...
- Python开发基础-Day8-装饰器扩展和迭代器
wraps模块 让原函数保留原来的说明信息 import time import random from functools import wraps def auth(func): '''auth ...
- BestCoder Round #65 (ZYB's Premutation)
ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- JQuery总结+实例
JQuery是什么? Jquery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari ...
- bzoj 3790: 神奇项链
3790: 神奇项链 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H 购买了 ...
- JSON语法及JSON定义规范化
版权声明:本文为博主原创文章,未经博主允许不得转载. https://www.cnblogs.com/zhuhui-site/p/10090541.html 一.前言 JSON(JavaScrip ...
- while an existing transition or presentation is occurring; the navigation stack will not be updated
使用UIAlertController提示信息,在之后使用navigation进行逻辑跳转时,出现popToViewController:transition: called on <UINav ...
- WebAPI 操作返回
定义了一个返回枚举: public enum ResultExceptionEnum { 积分不足 = , 支付失败 = , 用户不存在 = , 验证码发送失败 = , 验证码不正确 = , 账号已存 ...