学堂 学堂 学堂公众号手机端

hdu3944 DP? (lucas定理+预处理)

lewis 1年前 (2024-04-27) 阅读数 15 #技术


DP?




Time Limit: 10000/3000 MS (Java/Others)Memory Limit: 128000/128000 K (Java/Others)


Total Submission(s): 2380Accepted Submission(s): 748




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


​​2011 Multi-University Training Contest 11 - Host by UESTC​​




Recommend


xubiao|We have carefully selected several similar problems for you: ​​3940​​​ ​​3941​​​ ​​3942​​​ ​​3943​​​ ​​3945​​



解析:假设k<=n/2,那么我们的最优路线就是从顶点一直往下走到某个位置,然后在一直斜45度走。


那么路线即为:


①从顶点一直往下走,直到(n-k-1,0)的位置,数字和为n-k


②从(n-k+1,0)开始斜45度走,数字和为:


C(n-k,0)+C(n-k,1)+C(n-k+1,2)+......+C(n,k)=C(n+1,k)


综上:ans=(C(n+1,k)+n-k)%p;


当k>n/2时,就要从右边走,由于对称性,直接把k=n-k即可。


注意:行、列的起始都是从 0 开始。



数据组数很多,这就要求我们要做预处理,虽然p<=10^4,但是 p 还是一个质数,而10^4以内的质数有且仅有1229个。所以我们可以直接将所有质数 p 作预处理,得到 x!%p 与 x!对p的逆(x<p)。



代码:


#include<cstdio>
#define maxn 10000
using namespace std;

bool flag[maxn];
int prime[1229+5];
int rev[1229+5][9973+3];
int fac[1229+5][9973+3];

int pow_mod(int x,int y,int p)
{
int ans=1;
while(y>0)
{
if(y&1)ans=ans*x%p;
x=x*x%p,y>>=1;
}
return ans;
}

int lucas(int n,int m,int j)
{
int a,b,ans=1;
while(n && m)
{
a=n%prime[j],b=m%prime[j];
if(a<b)return 0;
ans=ans*fac[j][a]%prime[j]*rev[j][b]%prime[j]*rev[j][a-b]%prime[j];
n/=prime[j],m/=prime[j];
}
return ans;
}

int main()
{
int i,j,k,x,y,p,n,ans,l,r;
for(i=2;i<=maxn;i++)
{
if(!flag[i])prime[++prime[0]]=i;
for(j=1;j<=prime[0] && i*prime[j]<maxn;j++)
{
flag[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}

for(i=1;i<=prime[0];i++)
{
fac[i][0]=fac[i][1]=1;
rev[i][0]=rev[i][1]=1;
for(j=2;j<prime[i];j++)
{
fac[i][j]=fac[i][j-1]*j%prime[i];
rev[i][j]=pow_mod(fac[i][j],prime[i]-2,prime[i]);
}
}

i=0;
while(scanf("%d%d%d",&n,&k,&p)!=EOF)
{
printf("Case #%d: ",++i);
if(k>n/2)k=n-k;
l=1,r=1229;
while(l<=r)
{
j=(l+r)>>1;
if(p<prime[j])r=j-1;
else l=j+1;
}
ans=lucas(n+1,k,l-1)+n-k;
printf("%d\n",ans%p);
}
return 0;
}



版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门