Considering the length of S is particularly small, only 10, you can consider the pressure DP.
Set F[s][d] means that all the positions in the set S (denoted by a binary press) are selected, and the result of D modulo is the total number of schemes of D; It is not difficult to think of transfer and initialization.
Initialization: F[0][0]=1 0 represents an empty collection here
Transfer: f[s][(d * + s[i]-' 0 ')% d]=sum{f[s0][d]} S0 is a subset of s and just one element less than s
Note that the number of repetitions is calculated more than once. There are examples. So the final answer is divided by the factorial of the number of repetitions.
Look at the code and you know it.
(Add a topic to be used in the form of pressure-pressing techniques, especially in the case of the pressure DP topic: When S ' is a true subset of S, there must be S ' <s. This allows you to determine the order of the enumerations at DP time. )
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Constsize_t Max_len ( -);Constsize_t Max_d (1050);Constsize_t Max_bit ( the); unsignedintt;unsignedintd;unsignedintLen;Charstr[max_len];unsignedintf[max_bit][max_d];unsignedintcnt[Ten];intMain () {CIN>>T; while(t--) {memset (F,0,sizeof(F)); memset (Cnt,0,sizeof(Cnt)); CIN>> Str >>D; Len=strlen (STR); for(unsignedinti =0; I! = len;++i)++cnt[str[i]-'0']; f[0][0] =1; for(unsignedintS =1; S < (1<< Len); + +S) for(unsignedinti =0; I < len;++i)if(S & (1<<i)) for(unsignedintD =0;d < d;++d) f[s][(d*Ten+ Str[i]-'0')% D] + = f[s ^ (1<<i)] [d]; unsignedint&ans = f[(1<< Len)-1][0]; for(unsignedinti =0; I! =Ten;++i) for(unsignedintg =2; G <= cnt[i];++g) Ans/=G; cout<< Ans <<Endl; } return 0;}
Bzoj 1072
Bzoj 1072 [SCOI2007] Arrange Perm