HDOJ 3944 DP ?, Hdoj3944dp

Source: Internet
Author: User

HDOJ 3944 DP ?, Hdoj3944dp

Try to walk the shortest distance along the edge. After reduction, C (n + 1, k) + n-k,

Pre-processing factorial, Lucas theorem combination number modulo


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.
InputInput 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.
OutputFor every test case, you shoshould 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 24 2 7
 
Sample Output
Case #1: 0Case #2: 5
 
Authorphyxnj @ UESTC
Source2011 Multi-University Training Contest 11-Host by UESTC

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





If there are both WA and TLE, which one does hdoj report first?

If an error occurs before TLE, WA is displayed.
If all the results provided before TLE are correct

Q: What is the meaning of HDOJ 1023? Why is it related to catlan numbers?

This is a problem with inbound and outbound stack.
After the calculation is summarized, the catlan function is obtained. It is very convenient to solve this problem.

Recursion: h (n) = h (0) * h (n-1) + h (1) * h (n-2) +... + h (n-1) h (0) (where n> = 2) Alternative recursive formula: h (n) = (4 * N-2)/(n + 1 )) * h (n-1); returns the recursive relationship h (n) = C (2n, n)/(n + 1) (n =, 3 ,...)

For each number, it must be pushed to and out of the stack once. We set the status of the incoming stack to '1' and the outbound stack to '0 '. All States of n numbers correspond to a 2n binary number consisting of n 1 and n 0. The number of operations waiting for the inbound stack is arranged in the order of 1? n. The number B of the inbound stack is greater than or equal to the number a (a ≤ B) of the outbound stack ), therefore, the total number of output sequences = the number of 2n binary numbers consisting of n 1 and n 0 from the left and right scans. The cumulative number of 1 is not less than 0. In the 2n-bit binary number, the number of n 1 values is c (2n, n), and the remaining n values of 1 are automatically set to 0. The number of solutions that subtract the number of non-conformities (from left to right scans, and the number of accumulative counts of 0 is greater than 1) is required. The number of non-conformities is characterized by left-to-right scans, in a certain odd digit 2 m + 1, the cumulative count of m + 1 0 and the cumulative count of m 1 will first appear, and the subsequent 2 (n-m) -1 bit has n-m 1 and n-m-1 0. If the next 2 (n-m)-1 on the 0 and 1 swap, making it n-m 0 and n-m-1 1, result 1 2n digits consisting of n + 1 0 and N-1 1 are obtained, that is, an undesired number corresponds to an arrangement consisting of n + 1 0 and N-1 1. In turn, any 2n binary number consisting of n + 1 0 and N-1 1, because the number of 0 is two more, 2n is an even number, therefore, the cumulative count of 0 must be greater than 1 on a certain odd digit. Similarly, 0 and 1 are swapped in the following parts to form a 2n digit consisting of n 0 and n 1, that is, the 2n digits of n + 1 0 and N-1 1 must correspond to an invalid number. Therefore, the noncompliant 2n digits correspond to n + 1 0 and N-1 1 in a one-to-one arrangement. Obviously, the number of non-conforming solutions is c (2n, n + 1 ). The total number of output sequences = c (2n, n)-c (2n, n + 1) = 1/(n + 1) * c (2n, n ).

There are 2n people in a row into the theater. Admission Fee: 5 yuan. Only n people have a 5-yuan bill, n others have only 10-yuan bills, and the theater has no other money. How many ways can one buy a ticket if 10 yuan is needed, is there a change of 5 yuan worth of money at the ticket office? (The arrival of 5 yuan is regarded as 5 yuan into the stack, and the arrival of 10 yuan is regarded as a 5 yuan out of the stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.