题目链接:BZOJ 1072

这道题使用 C++ STL 的 next_permutation() 函数直接暴力就可以AC 。(使用 Set 判断是否重复)

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set> using namespace std; int T, d, l, Ans;
int A[15]; char Str[15]; typedef long long LL; LL Num; set<LL> S; int main()
{
scanf("%d", &T);
for (int Case = 1; Case <= T; Case++) {
scanf("%s%d", Str, &d);
Ans = 0;
S.clear();
l = strlen(Str);
for (int i = 0; i < l; i++) A[i] = Str[i] - '0';
sort(A, A + l);
while (true) {
Num = 0;
for (int j = 0; j < l; j++) Num = Num * 10 + A[j];
if (S.count(Num) == 0 && Num % d == 0) {
++Ans;
S.insert(Num);
}
if (!next_permutation(A, A + l)) break;
}
printf("%d\n", Ans);
}
return 0;
}

  

但是比较慢,正常一点的解法应该是使用状压 DP 。

设定一个状态 f[i][j] ,其中 i 的二进制表示当前已经使用了原数串中的某些位 (在 i 中为 1 的位) ,j 表示当前的数字 mod d 的值。f[i][j] 表示达到这个状态的方案数。

那么状态转移就是 : f[i | (1<<k)][(j * 10 + A[k]) % d] += f[i][j]  ((i & (1<<k)) == 0)

由于原排列中的数字可能有重复的,所以我们计算了很多重复的方案数。

如果某个数字 i 在排列中出现了 Cnt[i] 次,那么最后的答案 Ans 应该 Ans /= (Cnt[i])! (排列数)。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; int T, d, l, Ans;
int A[15], Cnt[15], f[1024 + 5][1000 + 5]; char Str[15]; int main()
{
scanf("%d", &T);
for (int Case = 1; Case <= T; Case++) {
scanf("%s%d", Str, &d);
l = strlen(Str);
memset(Cnt, 0, sizeof(Cnt));
for (int i = 0; i < l; i++) {
A[i] = Str[i] - '0';
++Cnt[A[i]];
}
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 0; i < (1 << l); i++) {
for (int j = 0; j < d; j++) {
for (int k = 0; k < l; k++) {
if ((i & (1 << k)) == 0)
f[i | (1 << k)][(j * 10 + A[k]) % d] += f[i][j];
}
}
}
Ans = f[(1 << l) - 1][0];
for (int i = 0; i <= 9; i++) {
for (int j = 1; j <= Cnt[i]; j++) {
Ans /= j;
}
}
printf("%d\n", Ans);
}
return 0;
}

  

[BZOJ 1072] [SCOI2007] 排列perm 【状压DP】的更多相关文章

  1. BZOJ 1072 [SCOI2007]排列perm ——状压DP

    [题目分析] 没什么好说的,水题. 代码比较丑,结果需要开long long 时间爆炸 [代码] #include <cstdio> #include <cstring> #i ...

  2. bzoj 1072: [SCOI2007]排列perm 状压dp

    code: #include <bits/stdc++.h> #define N 1005 using namespace std; void setIO(string s) { stri ...

  3. BZOJ 1072: [SCOI2007]排列perm 状态压缩DP

    1072: [SCOI2007]排列perm Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为 ...

  4. [BZOJ1072][SCOI2007]排列perm 状压dp

    1072: [SCOI2007]排列perm Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2488  Solved: 1546[Submit][St ...

  5. B1072 [SCOI2007]排列perm 状压dp

    很简单的状压dp,但是有一个事,就是...我数组开大了一点,然后每次memset就会T,然后开小就好了!!!震惊!以后小心点这个问题. 题干: Description 给一个数字串s和正整数d, 统计 ...

  6. BZOJ 1072 [SCOI2007]排列perm

    1072: [SCOI2007]排列perm Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1268  Solved: 782[Submit][Sta ...

  7. BZOJ 1072 [SCOI2007]安排perm 如压力DP

    意甲冠军:联系 方法:状压DP? 题解:这题事实上没啥好写的.不算非常难,推一推就能搞出来. 首先看到这个问题,对于被d整除这个条件,非常easy就想到是取余数为0,所以想到可能状态中刚開始含有取余数 ...

  8. 【以前的空间】bzoj 1072 [SCOI2007]排列perm

    又颓废了一个下午,最近撸mc撸到丧失意识了,玩的有点恶心,于是找水题做,瞧不起颓废的自己啊. another水题. 这题题意很明显啦,就是找数字排列后组成的数去mod d=0后有多少种. 普通的搜索的 ...

  9. 暑假集训Day 4 P4163 [SCOI2007]排列 (状压dp)

    状压dp (看到s的长度不超过10就很容易想到是状压dp了 但是这个题的状态转移方程比较特殊) 题目大意 给一个数字串 s 和正整数 d, 统计 s 有多少种不同的排列能被 d 整除(可以有前导 0) ...

随机推荐

  1. [置顶] Array ArrayList LinkList的区别剖析

    这是一个面试中我们经常被问到的问题 Array.ArrayList.LinkList之间的区别:Array.ArrayList.LinkList均属于泛型的范畴,都用来存放元素,主要区别是Array是 ...

  2. [io PWA] Great libraries and tools for great Progressive Web Apps

    sw-toolbox: Github It provides a cononical implementation of all the runtime caching strategies that ...

  3. phpnow安装教程

    点评:搭建 PHP 其实不很难,只是有点繁琐.要是自己搭建一次 PHP + MySQL 环境很是费时.更糟的是,很多新手在配置 PHP 时常常出现这样那样的问题.诸如 mysql 扩展.zend 安装 ...

  4. Mac OS命令行运行Sublime Text

    Opening Sublime Text on command line as subl on Mac OS? Mac OS subl http://www.phodal.com/blog/mac-o ...

  5. HTML5移动开发中的input输入框类型

    HTML5规范引入了许多新的input输入框类型 在HTML5移动开发中,通过这些新的输入框类型来显示定制后的键盘布局,用户体验更好,更容易填写各种表单 本文中,实测手机为肾4S与米4 数字类型num ...

  6. 各种语言HMAC SHA256实现

    语言包含: Javascript ,PHP,Java,Groovy,C#,Objective C,Go,Ruby,Python,Perl,Dart,Swift,Rust,Powershell. Jav ...

  7. 谁是谁的first-child

    看过CSS伪类选择器之后,心想也就如此嘛,:first-child选择元素的第一个子元素,有什么难的,可一到实践中,还是到处碰壁啊. <body> <ul class="f ...

  8. 判断浏览器是否支持FileReader

    1.js代码: //判断浏览器是否支持FileReader if (typeof FileReader == "undefined") { document.write(" ...

  9. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  10. 【POJ3468】【树状数组区间修改】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...