Just sum it [uvalive 5063] DP + combination number

Source: Internet
Author: User

It's time for TLE to die. It's just over n optimizations, and it's full of tears.

Question:

A_1, a2... A9, AI indicates that there is an ai I. Ask how many different numbers are composed of these numbers and what is it after mod 1000000007.

Question:

First, the method is clear:

Number of BITs, which indicates the position of a certain number. If there is an L-bit, the number I is on the J-bit, then the contribution of I on this bit is I * 10 ^ (J-1) * (the remaining number is arranged into the different numbers of L-1 bits ).

Under all different digits, the sum of the values contributed by all numbers at different positions is the result.

Then the difficulty is how to find out: the remaining numbers are arranged into different numbers of L-1 bits.

Brute force: several times are used to enumerate each number. For example, if sigma (PI) = L-1, the result is (L-1 )! /(P1 !) * (P2 !) * .. (Pi !)), The worst 10 ^ 10 times, must be TLE

DP:

DP [I] [J] indicates the number of J bits formed by 1 .. I-1.

State equation:

DP [I + 1] [J + k] + = DP [I] [J] * C (J + k, k) (0 = <k <= ai)

After being changed to DP, I thought it was a, but it was still TLE.

Optimization 1:

After careful observation, we can find that, when the number of digits is determined, no matter which bit the number is, (the remaining number is arranged as the number of different numbers in the column 1-1) remains unchanged, for example, 6xx, x6x, xx6, the number of XX arrays is determined, so you can directly 666 * (the number of XX arrays)

Optimization 2:

After optimization 1 is added, it is still T. It is found that the above 666 can be optimized. 666 = 6*111. You can pre-process 1 with different lengths first .. the value of 1.

Optimization 3:

Still T. Change the enumeration order. enumerate the numbers first and then the enumerated length. The result is equivalent, however, you can use the change number once and then calculate the Dp value of the remaining number, instead of calculating it once at each time, the final complexity is O (9*9*9)

It is estimated that the number of data groups for this question is large, and the time card is too tight.

Code:

# Include <cstdlib>

# Include <stdio. h>

# Include <iostream>

# Include <memory. h>

# Include <stdio. h>

Using namespace STD;

# Define mod 1000000007.

Long long extended_gcd (long a, long B, long & K, long & T)

{

If (B = 0)

{

K = 1;

T = 0;

Return;

 

}

Else

{

Long long tp_gcd;

Tp_gcd = extended_gcd (B, A % B, K, t );

Long long temp;

Temp = K;

K = T;

T = temp-(A/B) * t;

Return tp_gcd;

}

}

Long long in [1000];

Long long inv (long X)

{

Long long K, T;

If (in [x]! =-1) Return in [x];

Extended_gcd (x, Mod, K, t );

While (k <0) K + = MOD;

While (k> = mod) K-= MOD;

In [x] = K;

Return K;

}

Int A [20];

Long long DP [20] [100];

Long long CV [2, 200] [2, 200];

Long long P11 [200];

Long long C (int n, int X)

{

If (CV [N] [x]! =-1) {return CV [N] [X];}

Long long ans = 1;

Int I, J;

For (I = 1; I <= N; I ++)

Ans = ans * I % MOD;

For (I = 1; I <= x; I ++)

Ans = ans * inv (I) % MOD;

For (I = 1; I <= N-X; I ++)

Ans = ans * inv (I) % MOD;

CV [N] [x] = ans;

Return ans;

}

Long long cal_dp ()

{

Memset (DP, 0, sizeof (DP ));

Int I, J;

DP [1] [0] = 1;

For (I = 1; I <= 9; I ++)

For (j = 0; j <= 90; j ++)

{

Long long TMP = DP [I] [J];

If (TMP = 0) continue;

For (int K = 0; k <= A [I]; k ++)

DP [I + 1] [J + k] = (DP [I + 1] [J + k] + CV [J + k] [J] * TMP % mod) % MOD;

}

}

Int main (INT argc, char ** argv)

{

Long long tcase, I, j, sum, ans;

Memset (in,-1, sizeof (in ));

Scanf ("% LLD", & tcase );

For (I = 0; I <= 100; I ++)

For (j = 0; j <= 100; j ++)

CV [I] [J] =-1;

For (I = 0; I <= 100; I ++)

For (j = 0; j <= 100; j ++)

C (I, j );

P11 [1] = 1;

For (I = 2; I <= 100; I ++)

P11 [I] = (P11 [I-1] * 10 + 1) % MOD;

While (tcase --)

{

Sum = 0;

Memset (A, 0, sizeof ());

For (I = 1; I <= 9; I ++)

{

Scanf ("% d", & A [I]);

Sum + = A [I];

}

Ans = 0;

For (I = 1; I <= 9; I ++) // The first few

If (A [I]> 0)

{

A [I] --;

Cal_dp ();

For (int l = 1; L <= sum; l ++) // number of digits

{

Long long T = 0;

T = I * P11 [l];

Long long DDD = DP [10] L-1];

Ans = (ANS + T * DDD % mod) % MOD;

// If (ANS <0) ans + = MOD;

}

A [I] ++;

}

// Cout <ans <Endl;

Printf ("% LLD/N", ANS );

}

Return 0;

}

 

 

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.