尽量沿着边走距离最短。化减后 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

Problem Description



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.
 
Input
Input to the problem will consists of series of up to 100000 data sets. For each data there is a line contains three integers n, k(0<=k<=n<10^9) p(p<10^4 and p is a prime) . Input is terminated by end-of-file.
 
Output
For every test case, you should output "Case #C: " first, where C indicates the case number and starts at 1.Then output the minimum sum mod p.
 
Sample Input
1 1 2
4 2 7
 
Sample Output
Case #1: 0
Case #2: 5
 
Author
phyxnj@UESTC
 
Source
 

#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?的更多相关文章

  1. hdu 3944 dp?

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

  2. HDOJ 1069 DP

    开启DP之路 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 描述一下: 就是给定N(N<=20)个方体,让你放置,求放置的最高高度,限制条件 ...

  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. hdoj 1257 DP||贪心

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. HDOJ 1260 DP

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

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

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

  7. HDU 3944 DP? (Lucas定理)

    题意:在杨辉三角中让你从最上面到 第 n 行,第 m 列所经过的元素之和最小,只能斜向下或者直向下走. 析:很容易知道,如果 m 在n的左半部分,那么就先从 (n, m)向左,再直着向上,如果是在右半 ...

  8. [SinGuLaRiTy] 组合数学题目复习

    [SinGuLaRiTy] Copyright (c) SinGuLaRiTy 2017.  All Rights Reserved. [CQBZOJ 2011] 计算系数 题目描述 给定一个多项式( ...

  9. 找规律/数位DP HDOJ 4722 Good Numbers

    题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...

随机推荐

  1. 【转载】万能adapter

    adapter总是自己写,其实使用现成的框架会节省不少代码 原文地址:https://github.com/hongyangAndroid/baseAdapter base-adapter Andro ...

  2. PAT L3-011. 直捣黄龙

    最短路. 先求出一个只包含最短路的$DAG$,然后在$DAG$上$dp$一下就可以了.英文变数字还有$map$转一下. #include<map> #include<set> ...

  3. Hibernate 与Spring整合出现 hibernate.HibernateException: createCriteria is not valid without active transaction

    当 Hibernate 和 Spring 整合时,在 Spring 中指定的 Hibernate.cfg.xml 文件内容中要注释掉以下内容: <!-- Enable Hibernate's a ...

  4. Poj2482 Stars in Your Window(扫描线)

    题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...

  5. Loj#6432「PKUSC2018」真实排名(二分查找+组合数)

    题面 Loj 题解 普通的暴力是直接枚举改或者不改,最后在判断最后对哪些点有贡献. 而这种方法是很难优化的.所以考虑在排序之后线性处理.首先先假设没有重复的元素 struct Node { int p ...

  6. Java 对象池实现

    http://blog.csdn.net/bryantd/article/details/1100019 http://www.cnblogs.com/devinzhang/archive/2012/ ...

  7. CodeForces - 1000D Yet Another Problem On a Subsequence

    题面在这里! 好智障的一个dp啊,一段开头的数字相当于下面要跟多少个数,直接滚动数组dp就行了... #include<bits/stdc++.h> #define ll long lon ...

  8. 十一. 图形、图像与多媒体4.Graphics类的绘图方法

    Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力.本节讲解Graphics类,下节讲解Graphics2D. Graphics类提供基本的几何图形绘制方法,主要有:画线段 ...

  9. Java高级架构师(一)第34节:Nginx的Http模块部分的指令

    默认长链接的数目在100个 默认长链接的超时时间,一般在75S.

  10. PHP数组和数据结构(下)未完。。。。

    1.数组的遍历 (1)each(): 接受一个数组作为参数,返回数组中当前元素的键/值对,并向后移动数组指针到下一个元素的位置 键/值对被返回为带有四个元素的关联和索引混合的数组,键名分别为0,1,k ...