http://www.lydsy.com/JudgeOnline/problem.php?id=1072

首先无限膜拜题解orz表示只会暴力orz

数据那么小我竟然想不到状压!

orz

这种题可以取模设状态orz

f[i,j]表示状态为i,mod d为j的方案

则答案为f[all, 0]

转移就太简单了orz

f[i|1<<k, (j*10+c[k])%d]+=f[i, j]

然后按照多重集合的做法最后答案/(xi!)

然后我有一sb错一直没找到,wa了n发。。。

QAQ

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=12;
int f[3005][1005], d, n, c[N], x[N], p[N];
int main() {
int cs=getint();
char s[N];
p[0]=1;
for1(i, 1, 11) p[i]=p[i-1]*i;
while(cs--) {
scanf("%s", s);
n=strlen(s); read(d);
CC(x, 0);
rep(i, n) c[i]=s[i]-'0', x[c[i]]++;
int all=(1<<n)-1;
for1(i, 0, all) rep(j, d) f[i][j]=0;
f[0][0]=1;
rep(i, all) rep(j, d) if(f[i][j])
rep(k, n) if(!(i&(1<<k))) f[i|(1<<k)][(j*10+c[k])%d]+=f[i][j];
int ans=f[all][0];
rep(i, 10) ans/=p[x[i]];
printf("%d\n", ans);
}
return 0;
}

  


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

【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】

    题目链接:BZOJ 1072 这道题使用 C++ STL 的 next_permutation() 函数直接暴力就可以AC .(使用 Set 判断是否重复) 代码如下: #include <io ...

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

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

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

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

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

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

随机推荐

  1. sae python中Mysql中文乱码的解决

    一開始我用的是: db=MySQLdb.connect(db=sae.const.MYSQL_DB,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_P ...

  2. WPF显示经常使用的几个显示文字控件TextBox, TextBlock, Lable

    TextBox, TextBlock. Lable 当中TextBox 和Lable均继承了Control类 能够对其进行模板编辑. 而TextBlock没有继承Control所以不能对其进行模板编辑 ...

  3. Linux内核开发之异步通知与异步I/O(一)

    “小王,听说过锦上添花吧..”我拍拍下王的头说. “还锦上添花你,为你上次提的几个东东,我是头上长包..”小王气愤地瞪着我. “啊,为啥这样呢,本来还特意拒绝了MM的月份,抽出时间打算给你说点高级的东 ...

  4. 【Linux】centos和ubuntu下php5安装redis2.24扩展

    1.服务器先安装redis-server,这是毋庸置疑的!!! 2.服务器开启redis-server,配置相关参数 3.配置好redis服务器后,再安装php的redis扩展phpredis. 一. ...

  5. mosquitto ---mosquitto-auth-plug

    https://github.com/jpmens/mosquitto-auth-plug This is a plugin to authenticate and authorize Mosquit ...

  6. VS2008 格式化时候乱码 或者 为全为0

    前几天一直发现写入文件的数据全是0 ,找了非常长时间发现问题在以下的地方: P:a是一个float数,如31.000000,然后运行以下的格式化语句时,结果str的值全是0. (我知道讲float格式 ...

  7. Ubuntu安装deb软件包错误(依赖关系问题)解决

    执行命令 sudo dpkg -i XXX.deb 返回依赖关系错误提示 执行 sudo apt-get -f install 这条命令将自动安装需要的依赖包. 再次执行命令 sudo dpkg -i ...

  8. JEECG 多数据源设计

    转至元数据起始   为什么要有多数据源? 通常一个系统只需要连接一个数据库就可以了,Jeecg数据源是配置在spring-mvc-hibernate.xml文件中,这种数据源我们叫做主数据源.但是在企 ...

  9. 366. Fibonacci【Naive】

    Find the Nth number in Fibonacci sequence. A Fibonacci sequence is defined as follow: The first two ...

  10. iOS应用代码段瘦身办法

    iOS应用代码段瘦身办法 大型app应对苹果官方代码段大小限制的小伎俩… 背景 苹果官方文档 对二进制 __TEXT 段大小有限制: 代码实在瘦不下去怎么办? 解决方案 利用 rename_secti ...