HDU 3304 calculates the number of integers divisible by prime p in row N of the Yang Hui triangle.

Source: Internet
Author: User

Equivalent to C (n, m) % P = 0. Evaluate the number of M.

N! /(M! * (N-m )!) % P = 0

N! The number of P factors equals to M! And (n-m )! Factors and

That is, F (n) = f (m) + f (n-m) // F (n) indicates n! Number of factors contained

F (n) = N/P + N/(P ^ 2) + ....

That is:

N/P + N/P ^ 2 +... = m/P + M/P ^ 2 + .. + (n-M)/P ^ 2 +... (1)

Because N/P ^ I> = m/P ^ I + (n-M)/P ^ I,

Therefore, it is equivalent to N/P ^ I = m/P ^ I + (n-M)/P ^ I (2) for any I)

N = N/P ^ I * P ^ I + N % P ^ I (note that all division here is Division)

(2) Both sides are multiplied by P ^ I

N-N % P ^ I = m-m % P ^ I + N-M + (n-m) % P ^ I

N % P ^ I-m % P ^ I = (n-m) % P ^ I> = 0

It is equivalent to M % (P ^ I) <= n % (P ^ I) (3) for any positive integer I. (// This step from 2 to 3 is very important and is a key point of this question)

Write N and M as numbers N0, N1, N2, N3, N4,... M0, M1, M2, M3, M4 ,....

 

According to (3) it can be obtained by mathematical induction. For any bit, Ni is greater than or equal to Mi.

 

When I = 0; apparently true

Assume that I = K is true. When I = k + 1

Because M % (P ^ K) <= n % (P ^ K)

N (k + 1) nk .. n1 = N (k + 1) (NK % (P ^ K) (indicating placement by P in hexadecimal format)

Therefore, ensure that N % (P ^ (k + 1)> = m % (P ^ (k + 1 ))

The maximum value must be n (k + 1)> = m (k + 1)

 

That is, (3) the necessary and sufficient conditions are M0 <= N0, M1 <= N1 ....

 

So the value of M is (N0 + 1) * (N1 + 1) * (n2 + 1 )......

 

•:

// HDU 3304

# include
using namespace STD;
int main ()
{< br> int N, P, ANS, Cs;
cs = 0;
while (CIN> P> N)
{< br> CS ++;
If (n = 0) & (P = 0) break;
ans = 1;
while (n! = 0)
{< br> ans = ans * (N % P + 1) % 10000;
N = N/P;
}< br> cout <"case" If (ANS <1000) cout <'0 ';
If (ANS <100) cout <'0';
If (ANS <10) cout <'0 ';
the cout }< BR >}

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.