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

DP?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)
Total Submission(s): 2380    Accepted 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
 

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值