zoukankan      html  css  js  c++  java
  • [BZOJ1072][SCOI2007]排列perm

    1072: [SCOI2007]排列perm

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2534  Solved: 1580 [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

    其实暴力next_permutation可以过

    #pragma GCC optimize ("O2") 
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    char buf[10000000], *ptr = buf - 1;
    inline int readint(){
        int n = 0;
        while(*++ptr < '0' || *ptr > '9');
        while(*ptr >= '0' && *ptr <= '9') n = (n << 1) + (n << 3) + (*ptr++ & 15);
        return n; 
    }
    int s[15];
    int main(){
        buf[fread(buf, sizeof(char), sizeof(buf), stdin)] = 0;
        int T = readint();
        while(T--){
            int tot = 0, ans = 0, d;
            while(*++ptr < '0' || *ptr > '9');
            while(*ptr <= '9' && *ptr >= '0') s[++tot] = *ptr++ & 15;
            sort(s + 1, s + tot + 1);
            d = readint();
            long long sum;
            do{
                sum = 0;
                for(int i = 1; i <= tot; i++)
                    sum = (sum << 1) + (sum << 3) + s[i];
                if(sum % d == 0) ans++;
            }while(next_permutation(s + 1, s + tot + 1));
            printf("%d
    ", ans); 
        }
        return 0;
    }

    正解是状压

    设$f[S][j]$表示选择了的数字状态为$S$,且组成的数字模$d$的余数为$j$的方案数

    转移显然。。

    但是有重复元素,除以一下每个数字的全排列即可

    #pragma GCC optimize ("O2") 
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    char buf[10000000], *ptr = buf - 1;
    inline int readint(){
        int n = 0;
        while(*++ptr < '0' || *ptr > '9');
        while(*ptr >= '0' && *ptr <= '9') n = (n << 1) + (n << 3) + (*ptr++ & 15);
        return n; 
    }
    int s[15], cnt[15], val[15];
    int f[1024][1000];
    int main(){
        buf[fread(buf, sizeof(char), sizeof(buf), stdin)] = 0;
        int T = readint();
        while(T--){
            int tot = 0, Max, d;
            while(*++ptr < '0' || *ptr > '9');
            while(*ptr >= '0' && *ptr <= '9')
                s[++tot] = *ptr++ & 15;
            Max = (1 << tot) - 1;
            for(int i = 0; i <= 9; i++){
                cnt[i] = 0;
                val[i] = 1;
            }
            for(int i = 1; i <= tot; i++){
                cnt[s[i]]++;
                val[s[i]] *= cnt[s[i]];
            }
            d = readint();
            for(int i = 0; i <= Max; i++)
                for(int j = 0; j < d; j++)
                    f[i][j] = 0;
            f[0][0] = 1;
            for(int i = 0; i < Max; i++)
                for(int j = 0; j < d; j++)
                    if(f[i][j]){
                        for(int k = 1; k <= tot; k++)
                            if((i & 1 << k - 1) == 0) f[i | 1 << k - 1][(j * 10 + s[k]) % d] += f[i][j]; 
                    }
            int ans = f[Max][0];
            for(int i = 0; i <= 9; i++) ans /= val[i];
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    jeecms 强大的采集功能优化 转载 https://blog.csdn.net/jeff06143132/article/details/7099003
    JEECMS自定义标签开发步骤
    jeecms之全文检索
    jeecms怎么修改后台访问路径?
    jeecms 基本架构研究
    Java JNI初探
    《高效程序员的45个习惯》
    Java并发编程:Thread类的使用
    Thread详解
    JAVA多线程实现的四种方式
  • 原文地址:https://www.cnblogs.com/ruoruoruo/p/7636645.html
Copyright © 2011-2022 走看看