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. Xcode 打包 ipa 包

    原地址:http://zengwu3915.blog.163.com/blog/static/27834897201362831449893/ 上次介绍了木有证书的打包ipa.如果安装证书后,我们还有 ...

  2. 清除tomcat的缓存

    删除tomcat目录下的work目录中的Catalina目录就好了!

  3. model中设置默认值时 ,使用 lambda 与否的差别以及datetime的默认值方法

    'date': '2013-01-01'  #固定值 'date': time.strftime('%Y-%m-%d')  #启动时候的值 'date': lambda *a: time.strfti ...

  4. 〖Android〗查找Android中的/system/lib中增加的lib文件是否在apk文件中

    #!/bin/bash - #=============================================================================== # # F ...

  5. HDU 5336 XYZ and Drops

    Problem Description XYZ is playing an interesting game called "drops". It is played on a r ...

  6. 15-spring学习-集合表达式

    spring认为,数组和List集合是等价的. 要想操作list集合,利用“{内容,内容...}”的形式就可以完成了(类似数组). package com.Spring.ELDemo; import ...

  7. 快速掌握Gif动态图实现代码

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前言:Gif一种动态图片,网上有很多制作这个的工具,包括PS都有,但作为一名程序员,我觉得如果自己通过编写代码把它实现,不但是对代码的掌握与复习, ...

  8. [原]SQL相关路径查询脚本

    --1.查询机器名 SELECT @@servername AS 机器名称 --查询已安装的SQL实例名 SELECT * FROM Sys.Servers --2.查询SQL安装路径 DECLARE ...

  9. php检测iis环境是否支持htaccess

    php检测iis环境是否支持htaccess的方法. modrewrite.php <?php echo "mod_rewrite works"; ?> open_me ...

  10. atitit.设计文档---操作日志的实现

    atitit.设计文档---操作日志的实现 日志查询 1 ----mybatis  配置... 1 添加日志 1 日志查询 <a class="l-link" href=&q ...