zoukankan      html  css  js  c++  java
  • bzoj1072【SCOI2007】排列perm

    1072: [SCOI2007]排列perm

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 1479  Solved: 928
    [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

    Source




    状压DP题目

    f[i][j]表示状态为i,余数为j的方案数。




    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #define F(i,j,n) for(int i=j;i<=n;i++)
    #define D(i,j,n) for(int i=j;i>=n;i--)
    #define ll long long
    using namespace std;
    int t,d,n,ans;
    int f[1050][1005],cnt[20],fac[20],g[1050],p[20];
    char s[20];
    inline int read()
    {
    	int x=0,f=1;char ch=getchar();
    	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    inline int calc(int x)
    {
    	int ret=0;
    	while (x){ret+=x&1;x>>=1;}
    	return ret;
    }
    inline void dp(int x)
    {
    	F(i,0,n-1) if ((1<<i)&x)
    	{
    		int tmp=p[g[x]-1]%d*(s[i]-'0')%d;
    		F(j,0,d-1) f[x][(j+tmp)%d]+=f[x^(1<<i)][j];
    	}
    }
    int main()
    {
    	fac[0]=1;
    	F(i,1,10) fac[i]=fac[i-1]*i;
    	p[0]=1;
    	F(i,1,10) p[i]=p[i-1]*10;
    	F(i,0,1023) g[i]=calc(i);
    	t=read();
    	while (t--)
    	{
    		memset(f,0,sizeof(f));
    		scanf("%s%d",s,&d);
    		n=strlen(s);
    		f[0][0]=1;
    		F(i,1,(1<<n)-1) dp(i);
    		ans=f[(1<<n)-1][0];
    		memset(cnt,0,sizeof(cnt));
    		F(i,0,n-1) cnt[s[i]-'0']++;
    		F(i,0,9) ans/=fac[cnt[i]];
    		printf("%d
    ",ans);
    	}
    }
    


  • 相关阅读:
    linux 运维
    mariadb replication
    phpmyadmin
    Objective-C设计模式——单例Singleton(对象创建)
    收藏iOS学习资料
    axios拦截器
    vue单页面优化
    html设置http缓存代码
    js数组去重,排序的几种方法
    前端移动端问题
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7142753.html
Copyright © 2011-2022 走看看