BZOJ 1072 排列
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 \le d \le 1000, 1 \le T \le 15$
\(f[i][j]\)表示状态为\(i\)(用二进制表示,若某位为\(1\),则该位在排列中)余数为\(j\)的方案数。于是,我们可以枚举排列长度,利用已知排列进行转移(应该很好YY,脑补一下)。代码应该好理解。
#include<vector>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define maxn (12)
#define maxm (1010)
char s[maxn]; int n,m,f[1<<maxn][maxm],mi[maxn],jie[maxn]; vector <int> vec[maxn];
inline void dfs(int now,int bin,int have)
{
if (now > n) { vec[have].push_back(bin); return; }
dfs(now+1,bin<<1|1,have+1); dfs(now+1,bin<<1,have);
}
inline void dp()
{
for (int i = 0;i <= n;++i) vec[i].clear();
dfs(1,0,0);
mi[0] = 1;
for (int i = 1;i <= n;++i) mi[i] = 10*mi[i-1]%m;
memset(f,0,sizeof(f));
for (int i = 1;i <= n;++i) f[1<<(i-1)][(s[i]-'0')%m] = 1;
for (int i = 2;i <= n;++i)
{
int ni = vec[i].size();
for (int ii = 0;ii < ni;++ii)
{
int p = vec[i][ii];
for (int j = 1;j <= n;++j)
if (p & (1<<(j-1)))
{
int q = p^(1<<(j-1));
for (int k = 0;k < m;++k)
f[p][(k+mi[i-1]*(s[j]-'0')%m)%m] += f[q][k];
}
}
}
for (int i = '0';i <= '9';++i)
{
int cnt = 0;
for (int j = 1;j <= n;++j) cnt += s[j] == i;
f[(1<<n)-1][0] /= jie[cnt];
}
}
int main()
{
freopen("1072.in","r",stdin);
freopen("1072.out","w",stdout);
jie[0] = 1;
for (int i = 1;i <= 10;++i) jie[i] = i*jie[i-1];
int T; scanf("%d\n",&T);
while (T--)
{
scanf("%s %d\n",s+1,&m); n = strlen(s+1);
dp(); printf("%d\n",f[(1<<n)-1][0]);
}
fclose(stdin); fclose(stdout);
return 0;
}
BZOJ 1072 排列的更多相关文章
- [BZOJ 1072] 排列perm
Link: BZOJ 1072 传送门 Solution: 一道直接next_permutation纯暴力就能过的题? 难道2007年时大家都不知道next_permutation这个函数吗 还是用复 ...
- [BZOJ 1072] [SCOI2007] 排列perm 【状压DP】
题目链接:BZOJ 1072 这道题使用 C++ STL 的 next_permutation() 函数直接暴力就可以AC .(使用 Set 判断是否重复) 代码如下: #include <io ...
- BZOJ 1072: [SCOI2007]排列perm 状态压缩DP
1072: [SCOI2007]排列perm Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为 ...
- BZOJ 1072 [SCOI2007]排列perm
1072: [SCOI2007]排列perm Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1268 Solved: 782[Submit][Sta ...
- BZOJ 1072: [SCOI2007]排列perm [DP 状压 排列组合]
题意:给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0) 100%的数据满足:s的长度不超过10, 1<=d<=1000, 1<=T<=15 看到整 ...
- 【以前的空间】bzoj 1072 [SCOI2007]排列perm
又颓废了一个下午,最近撸mc撸到丧失意识了,玩的有点恶心,于是找水题做,瞧不起颓废的自己啊. another水题. 这题题意很明显啦,就是找数字排列后组成的数去mod d=0后有多少种. 普通的搜索的 ...
- 【62.89%】【BZOJ 1072】[SCOI2007]排列perm
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1862 Solved: 1171 [Submit][Status][Discuss] Descri ...
- BZOJ 1072 [SCOI2007]排列perm ——状压DP
[题目分析] 没什么好说的,水题. 代码比较丑,结果需要开long long 时间爆炸 [代码] #include <cstdio> #include <cstring> #i ...
- bzoj 1072: [SCOI2007]排列perm【状压dp】
先写了个next_permutation结果T了,于是开始写状压 设f[s][i]为选取状态为s,选的数模d为i的方案数,去重的话直接除以每个数字的出现次数的阶乘即可 #include<iost ...
随机推荐
- LINQ多条件OR模糊查询
本文章转载:http://www.cnblogs.com/guyun/archive/2012/10/18/2729888.html 需求是这样的,有一张表tbl(Key[int],Value[str ...
- 用Excel完成专业化数据统计、分析工作
使用Excel可以完成很多专业软件才能完成的数据统计.分析工作,比如:直方图.相关系数.协方差.各种概率分布.抽样与动态模拟.总体均值判断,均值推断.线性.非线性回归.多元回归分析.时间序列等.本专题 ...
- Teamcity+SVN+VisualStudio在持续集成简明教程
Teamcity+SVN+VisualStudio持续集成 简明教程 一.写在最前: 1. 各组件版本号例如以下: Teamcity(简称tc)版本号:8.1.4 SVN版本号:Tortoi ...
- 高仿优酷Android客户端图片左右滑动(自动切换)
本例是用ViewPager去做的实现,支持自动滑动和手动滑动,不仅优酷网,实际上有很多商城和门户网站都有类似的实现: 具体思路: 1. 工程中需要添加android-support-v4.jar,才能 ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- java获取当前方法
1.获取当前方法堆栈,我们一般用 StackTraceElement[] stes = Thread.currentThread().getStackTrace(); 想要获取当前方法,切记不够灵活, ...
- 了解Unicode编码
制定Unicode编码标准的组织有两个,一个是国际标准化组织ISO,一个是多语言软件制造商组成的统一码联盟. 通用字符集UCS(Universal Character Set)是由ISO制定的编码方案 ...
- 安装Visual Studio 2013 中文社区版
Visual Studio 2013 免费了,我收到邮件后,立即从邮件的下载连接安装了 Visual Studio Community 2013 with Update 4 . 安装后几天没打开,今天 ...
- PetaPoco 存储过程
1 执行不带参数的存储过程 public List<dynamic> ceshiProc() { string sql = @"EXEC [dbo].[p_ceshi1]&quo ...
- Adb connect监听指定的主机和端口/Adb监听Visual Studio Emulator for Android模拟器
语法: adb connect <host>[:<port>] 使用实例: adb connect //如果连接成功则返回 connected to 说明 在使用Visual ...