bzoj 1072状压DP

1072: [SCOI2007]排列perm

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2293  Solved: 1448
[Submit][Status][Discuss]

Description

  给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0)。例如123434有90种排列能
被2整除,其中末位为2的有30种,末位为4的有60种。

Input

  输入第一行是一个整数T,表示测试数据的个数,以下每行一组s和d,中间用空格隔开。s保证只包含数字0, 1
, 2, 3, 4, 5, 6, 7, 8, 9.

Output

  每个数据仅一行,表示能被d整除的排列的个数。

Sample Input

7
000 1
001 1
1234567890 1
123434 2
1234 7
12345 17
12345678 29

Sample Output

1
3
3628800
90
3
6
1398

HINT

 

在前三个例子中,排列分别有1, 3, 3628800种,它们都是1的倍数。

【限制】

100%的数据满足:s的长度不超过10, 1<=d<=1000, 1<=T<=15

 
数据范围 小  3
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[1029][1028],d;
int num[12],len,pt[18];
int tot[12],v[12];
char str[18];
void dp()
{
    for(int i=0; i<=pt[len]; ++i)
        for(int j=0; j<=d; ++j)
            f[i][j]=0;
    f[0][0]=1;
    for(int i=0; i<pt[len]-1; ++i)
        for(int j=0; j<d; ++j)
            if(f[i][j])
            {
                for(int k=0; k<len; ++k)
                    if(((pt[k])&i)==0)
                    {
                        f[i|pt[k]][(j*10+num[k])%d]+=f[i][j];
                    }
            }
}
int main()
{
    int T;
    pt[0]=1;
    for(int i=1; i<=10; ++i) pt[i]=pt[i-1]<<1;
    for(scanf("%d",&T); T--;)
    {
        scanf("%s%d",str,&d);
        len=strlen(str);
        for(int i=0; i<=9; ++i) tot[i]=0,v[i]=1;
        for(int i=0; i<len; ++i)
        {
            num[i]=str[i]-'0';
            ++tot[num[i]];
            v[num[i]]*=tot[num[i]];
        }
        dp();
        for(int i=0; i<=9; ++i) f[pt[len]-1][0]/=v[i];
        printf("%d\n",f[pt[len]-1][0]);
    }
}

 

 
 
posted @ 2017-07-13 19:29  Billyshuai  阅读(164)  评论(0编辑  收藏  举报