ACM Learning process-hdu5407 CRB and candies (number theory)

Source: Internet
Author: User

Problem Description

CRB has N different candies. He is going to eat K candies.
He wonders how many combinations he can select.
Can you answer he question for all K (0≤k≤n)?
CRB is too hungry to check all the your answers one by one, so he only asks least common multiple (LCM) of all answers.

Input

There is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For the test case there is one line containing a single integer N.
1≤t≤300
1≤n≤106

Output

For each test case, output a single INTEGER–LCM modulo 1000000007 (109+7).

Sample Input

5

1

2

3

4

5

Sample Output

1

2

3

12

10

The title requires the least common multiple of all C (n, i) (0 <= i <= N) .

If you go directly to the LCM and ask for T -Off,

Even if all n! offline the inverse element complexity is O (n)

Then for loop C (n, i) is O (n)

Then use the LCM to find least common multiple is O (log (a)), the worst case approach O (log (10^9+7)) ~

So the complexity of the worst is o (30n), the group of data, the final need O (10^10) around.

The one who does not take the words O (3*10^8) around. Card A constant number of orders of magnitude.

A conclusion obtained by the Kummer theorem is used in the puzzle.

Vp ([C (n, 0), C (n, 1),..... C (n, n)]) = VP ([1, 2, ...., n+1])-VP (N+1)

where Vp (k) indicates the number of prime p in K

That is, Max (VP (C (n, i)) = Max (VP (i+1))-VP (N+1) (0 <= i <= N)

First Kummer theorem:i <= n is a positive integer, assuming that in p -ary ,(n-i) + i took place  T >= 0 rounding, then p^t | C (n, i).

I found a proof-of-process online.

And then that's the proof :

When n = SUM (ci*p^i), (0 <= i <= maxn) ,

Max (VP (c (n, i)) = VP (c (n, p^maxn-1))

Case 1:

if n = p^ (maxn+1)-1

Vp (C (n, p^maxn-1)) = 0

Case 2:

Else

Vp (C (n, p^maxn-1)) = Maxn-min (i | ci! = p-1)

The first type: for n = p^ (maxn+1)-1 ,

Obviously N-i + i is impossible to carry, no matter how much, because a certain person (p-1) + (p-1) can not get p-1 in the case of rounding , Since the low is not carry, the high-level is impossible to get p-1.

So Vp (C (n, p^maxn-1)) = 0

The second type: for n! = p^ (maxn+1)-1 cases,

Any Ci must be less than or equal to p-1,

Naturally for the lowest bit Ci = p-1 case, it is definitely not the result of the rounding

If the lowest bit does not carry, the sub-low Ci = p-1 Situation, is certainly not a carry get.

So it is impossible for low-level p-1 to carry , until the low Ci! = p-1,

You can start rounding at this point,

To make it 100% , naturally take p-1 as one of the Addend, with another addend carry to get Ci.

And since this starts every carry,

If you encounter Ci = p-1, then because the previous is carry, that is, the result is The result of a+b+1, here if a take P-1, so even if carry, can also get p-1, so, for any Ci <= p-1, can be obtained by carry.

n = (CMAXN) (CmaxN-1) ... (C2) (C1) (C0)

P^maxn-1 = (p-1) .... (p-1) (p-1) (p-1)

So Vp (C (n, p^maxn-1)) = Maxn-min (i | ci! = p-1)

This equation was proved by just proving the conclusion of the equation left Max (VP (C (n, i)) = Max (VP (i+1))-VP (N+1) (0 <= i <= N) and then proving the same conclusion on the right side of the equation.

First for Max (Vp (i+1)),

Assuming K is the maximum value of T, then the first K is the minimum value of p^t,

Since p^t <= n+1,

If the length of the n+1 in the P -system is greater than n , then it must be n = p^ (maxn+1)-1, n+1 = p^ (maxn+1) .

Then the right side equals (maxn+1)-(maxn+1) = 0.

The rest of the situation so T is naturally maxn(maxn is the p - system under n+1 the maximum length)

The next step is to prove Vp (n+1) = min (i | ci! = p-1) .

First for n in p p-1 Span style= "font-family: the song Body;" > The case, then natural n+1 lowest bit impossible carry, Not possible for 0 , Nature VP (n+1)  = 0 0

For the case where the lowest bit is p-1 , then the n+1 minimum bit is 0, at least p can be divisible n+ 1 ,

Then if the back is still p-1 so still carry 0, then know that is not the p-1 of the nature does not carry.

So Vp (n+1) equals n Low, the first one not equal to p-1 , naturally equals to the right of the equation.

And so the evidence was completed.

Max (VP (C (n, i)) = Max (VP (i+1))-VP (N+1) (0 <= i <= N)

With this formula, it proves [C (n, 0), C (n, 1),..... C (n, n)] = [1, 2, ....., n+1]/(n+1)

The mass factor exponent is equal on both sides of the equation, and the natural equation is equal.

Then the end result is the product of all P^maxn/p^k (where maxN is the highest number of p in n+1 , K is p can divide n+1 maximum number of times).

That is, the product of all P^maxn divided by the product of p^K, the numerator equals [1, 2, 3,.... n+1], and the denominator equals n+1.

The result is consistent with the conclusion of the puzzle.

Prove the process a little rubbing ....

If K and maxn are found in order, the complexity is O (Num*log (p)), where num is the number of primes ,P is the prime number.

If two points are found, O (Num*log (LOGP))

Code: O (Num*log (p))

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<Set>#include<map>#include<queue>#include<string>#include<algorithm>#defineLL Long Longusing namespacestd;ConstLL MOD = 1e9+7;Const intMAXN = 1e6+5;BOOLISPRIM[MAXN];intN, Prim[maxn], top;//wagered sieve method for calculating primesvoidIsprim () {memset (Isprim,true,sizeof(Isprim)); isprim[0] = isprim[1] =false;//Initialize     for(LL i =2; i < MAXN; ++i)//Sieve Method    {        if(Isprim[i]) { for(LL j = i*i; J < maxn; J + = i)//The upper bound is too large to explode int{Isprim[j]=false; }        }    }}voidinit () {Isprim (); Top=0;  for(inti =2; i < MAXN; ++i)if(Isprim[i]) prim[top++] =i;}voidWork () {LL ans=1;  for(inti =0; I < top && Prim[i] <= n+1; ++i) { for(LL v = prim[i]; v <= n+1; V *=Prim[i]) {            if((n+1)%v) Ans= (Ans*prim[i])%MOD; }} printf ("%i64d\n", ans);}intMain () {//freopen ("test.in", "R", stdin);init (); intT; scanf ("%d", &T);  for(intTimes =0; Times < T; ++Times ) {scanf ("%d", &N);    Work (); }    return 0;}

ACM Learning process-hdu5407 CRB and candies (number theory)

Related Article

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.