UVA 12672 Eleven (DP)

Source: Internet
Author: User

12672-eleven

Time limit:5.000 seconds

In this problem, we refer to the digits of a positive integer as the sequence of digits required to write
It in base ten without leading zeros. For instance, the digits of N = 2090 is of course 2, 0, 9, and 0.
Let N is a positive integer. We call a positive integer M an eleven-multiple-anagram of N if and
only if (1) the digits of M was a permutation of the digits of N, and (2) M is a multiple of 11. You are
Required to write a program this given N, calculates the number of its eleven-multiple-anagrams.
As an example, consider again N = 2090. The values that meet the first condition above is 2009,
2090, 2900, 9002, 9020 and 9200. Among those, only 2090 and 9020 satisfy the second condition, so
The answer for N = 2090 is 2.


Input
The input file contains several test cases, each of the them as described below.
A single line, contains an integer N (1≤n≤10^100).


Output
For each test case, output a line with an integer representing the number of Eleven-multiple-anagrams
of N. Because This number can is very large, you is required to output the remainder of dividing it
by 109 + 7.


Sample Input
2090
16510
201400000000000000000000000000


Sample Output
2
12
0

A good DP question.

Ask for a number after a row (excluding the preamble 0), how many numbers can be divisible by 11.

For multiples of 11, you can find a rule that:

(sum of odd digit numbers-even numbers sum)% 11 = = 0 Number can be divisible by 11

Because as a multiple of 11, it all conforms to:

77000 85481 can be divisible by 11.

7700 because each of its neighboring bits has an equal property.

770 for odd digits to carry, it is equivalent to 10 less (i.e.-10), while the even number is more than 1 (that is, 1), or is divisible by 11

The 11 even numbers are the same, the even number is carry, which is equivalent to minus 10, or +10, while the odd digits are 1 (that is, +1).

------------

85481

Then, set a dp[i][j][k] to indicate the use of the I-bit (0~9) number, the odd digit has the number of J, the remainder is the combination of K number of how many.

#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intMoD = 1e9+7;Const intN =205 ;Const intM = One ; LL Cnt[m], dp[m][n][m], c[n][n];strings;voidInit () {c[0][0] =1 ;  for(inti =1; i < N; ++i) {         for(intj =0; J <= I; ++j) {C[i][j]= (j==0)?1:(c[i-1][j]+c[i-1][j-1])%MoD; }    }}voidRun () {memset (CNT,0,sizeofCNT); Memset (DP,0,sizeofDP); intn = s.size (), N2 = N/2, N1 = n-N2;  for(inti =0; I < n; ++i) cnt[9-(s[i]-'0') ]++ ; dp[0][0][0] =1; LL sum =0 ;  for(inti =0; I <Ten; ++i) {//Digit         for(intj =0; J <= N1; ++J) {//Odd used             for(intK =0; K < One; ++K) {//remainder                if(!dp[i][j][k] | | J > SUM)Continue ; intJ1 = j, J2 = Sum-J;  for(intz =0; Z <= cnt[i]; ++z) {                    intZ1 = Z, z2 = cnt[i]-Z; if(J1 + z1 > N1 | | j2 + z2 > N2)Continue ; LL tmp=Dp[i][j][k]; if((n&1) && i==9) TMP = TMP * c[j1+z1-1][Z1]%MoD; ElseTMP = tmp * C[J1+Z1][Z1]%MoD; if(! (n&1) && i==9) TMP = TMP * c[j2+z2-1][Z2]%MoD; ElseTMP = tmp * C[J2+Z2][Z2]%MoD; int_i = i +1, _j = J1 + z1, _k = (k + z1* (9-I.)-z2* (9-I.) + One*10000)% One; Dp[_i][_j][_k]= (Dp[_i][_j][_k] + tmp)%MoD; } }} sum+=Cnt[i]; } cout<< dp[Ten][n1][0] <<Endl;}intMain () {Init (); while(Cin >>s) Run ();}
View Code

UVA 12672 Eleven (DP)

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.