DP? (hdu3944)
DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)
Total Submission(s): 2871 Accepted Submission(s): 894
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.
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.
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.
4 2 7
Case #2: 5
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 LL a[10005];
12 bool prime[10005];
13 int id[10005];
14 int biao[2000][10005];
15 LL quick(LL n,LL m,LL mod);
16 LL lucas(LL n,LL m,LL mod);
17 int main(void)
18 {
19 int i,j,k;
20 LL n,m,mod;
21 for(i=2; i<=1005; i++)
22 if(!prime[i])
23 for(j=i; (i*j)<=10005; j++)
24 prime[i*j]=true;
25 int tk=0;
26 for(i=2; i<=10005; i++)
27 if(!prime[i])
28 {
29 id[i]=tk;
30 biao[tk][0]=1;
31 biao[tk][1]=1;
32 for(j=2; j<i; j++)
33 {
34 biao[tk][j]=biao[tk][j-1]*j%i;
35 }
36 tk++;
37 }
38 int __ca=0;
39 while(scanf("%lld %lld %lld",&n,&m,&mod)!=EOF)
40 {
41 __ca++; LL cc=n-m;
42 n+=1;
43
44 m=min(m,cc);
45 LL ask=lucas(m,n,mod);
46 ask=ask+(n-m-1)%mod;
47 ask%=mod;
48 printf("Case #%d: ",__ca);
49 printf("%lld\n",ask);
50 }
51 return 0;
52 }
53 LL lucas(LL n,LL m,LL mod)
54 {
55 if(m==0)
56 {
57 return 1;
58 }
59 else
60 {
61 LL nn=n%mod;
62 LL mm=m%mod;
63 if(mm<nn)
64 return 0;
65 else
66 {
67 LL ni=biao[id[mod]][mm-nn]*biao[id[mod]][nn]%mod;
68 ni=biao[id[mod]][mm]*quick(ni,mod-2,mod)%mod;
69 return ni*lucas(n/mod,m/mod,mod);
70 }
71 }
72 }
73 LL quick(LL n,LL m,LL mod)
74 {
75 LL ans=1;
76 while(m)
77 {
78 if(m&1)
79 {
80 ans=ans*n%mod;
81 }
82 n=n*n%mod;
83 m/=2;
84 }
85 return ans;
86 }
DP? (hdu3944)的更多相关文章
- HDU-3944 DP?(组合数求模)
一.题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3944 二.题意 给一个巨大的杨辉三角,采用类似DP入门题“数字三角形”的方式求从顶点$(0, 0) ...
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- px、dp和sp,这些单位有什么区别?
DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...
随机推荐
- springcloud报Load balancer does not have available server for client: PROVIDER-SERVER
1.后台报错截图 这个的意思就是:负载均衡服务器中没有这个我自定义的PROVIDER-SERVER.开始我以为是Ribbon的原因,所以去折腾了一下,但是:最后不断往前推到之后发现本质是:在注册中心E ...
- Fllin(七)【Flink CDC实践】
目录 FlinkCDC 1.简介 2.依赖 3.flink stream api 4.flink sql 5.自定义反序列化器 6.打包测试 FlinkCDC 1.简介 CDC是Change Data ...
- android studio 使用 aidl(一)基础用法
最近公司需要开发一个项目用的到aidl,之前研究过eclipse版本的,但是好久了一直没用,现在需要捡起来,但是现在都用android studio了,所以查了下资料 都不是很全,我在这里总结一下,方 ...
- mysql_取分组后的前几行值
--方法一: select a.id,a.SName,a.ClsNo,a.Score from Table1 a left join Table1 b on a.ClsNo=b.ClsNo and a ...
- [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解
开始进入正题,我们都知道,Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃.相信大家在日常的工作当中都会经常遇到这个问题,解决的方案应该也是早已烂熟于心,即创建一 ...
- RestTemplate的exchange()方法,解决put和delete请求拿不到返回值的问题
嗷嗷待哺的controller(被调用provider的controller方法) //测试get少量参数 @RequestMapping(value = "detailsGetD" ...
- github单独下载某一个文件夹
可以借助svn工具进行下载,实现只下载repo下的指定文件夹内容 背景 需要下载这个文件夹下所有内容https://github.com/rabbitmq/rabbitmq-tutorials/tre ...
- 【Service】【Web】【Middleware】Tomcat
1. 概念 1.1. 官方网站:tomcat.apache.org 1.2. tomcat的组件 <Server> <Service> <Connector/> & ...
- java中子类继承父类什么?
1.继承public和protected修饰的属性和方法,不管子类和父类是否在同一个包: 2.继承默认权限修饰符修饰的属性和方法,前提是子类和父类在同一个包.
- 4、Linux下安装tomcat
Linux系统下安装tomcat 一.安装JDK 安装Tomcat之前需要安装JDk,安装JDk请参考:JDK安装. 二.Linux安装Tomcat 1.官网上下载Tomcat Apach ...