【BZOJ】1072: [SCOI2007]排列perm(状压dp+特殊的技巧)
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
000 1
001 1
1234567890 1
123434 2
1234 7
12345 17
12345678 29
Sample Output
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+特殊的技巧)的更多相关文章
- BZOJ 1072 [SCOI2007]排列perm ——状压DP
[题目分析] 没什么好说的,水题. 代码比较丑,结果需要开long long 时间爆炸 [代码] #include <cstdio> #include <cstring> #i ...
- bzoj 1072: [SCOI2007]排列perm 状压dp
code: #include <bits/stdc++.h> #define N 1005 using namespace std; void setIO(string s) { stri ...
- BZOJ 1072: [SCOI2007]排列perm 状态压缩DP
1072: [SCOI2007]排列perm Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为 ...
- [BZOJ1072][SCOI2007]排列perm 状压dp
1072: [SCOI2007]排列perm Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2488 Solved: 1546[Submit][St ...
- B1072 [SCOI2007]排列perm 状压dp
很简单的状压dp,但是有一个事,就是...我数组开大了一点,然后每次memset就会T,然后开小就好了!!!震惊!以后小心点这个问题. 题干: Description 给一个数字串s和正整数d, 统计 ...
- 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】
题目链接:BZOJ 1072 这道题使用 C++ STL 的 next_permutation() 函数直接暴力就可以AC .(使用 Set 判断是否重复) 代码如下: #include <io ...
- BZOJ 1072 [SCOI2007]安排perm 如压力DP
意甲冠军:联系 方法:状压DP? 题解:这题事实上没啥好写的.不算非常难,推一推就能搞出来. 首先看到这个问题,对于被d整除这个条件,非常easy就想到是取余数为0,所以想到可能状态中刚開始含有取余数 ...
- 【以前的空间】bzoj 1072 [SCOI2007]排列perm
又颓废了一个下午,最近撸mc撸到丧失意识了,玩的有点恶心,于是找水题做,瞧不起颓废的自己啊. another水题. 这题题意很明显啦,就是找数字排列后组成的数去mod d=0后有多少种. 普通的搜索的 ...
- 暑假集训Day 4 P4163 [SCOI2007]排列 (状压dp)
状压dp (看到s的长度不超过10就很容易想到是状压dp了 但是这个题的状态转移方程比较特殊) 题目大意 给一个数字串 s 和正整数 d, 统计 s 有多少种不同的排列能被 d 整除(可以有前导 0) ...
随机推荐
- 算法笔记_041:寻找和为定值的多个数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 输入两个整数n和sum,要求从数列1,2,3,...,n中随意取出几个数,使得它们的和等于sum,请将其中所有可能的组合列出来. 2 解决方案 上述问题 ...
- 浅析CentOS和RedHat Linux的区别(转)
CentOS的简介 CentOS是Community ENTerprise Operating System的简称,我们有很多人叫它社区企业操作系统,不管你怎么叫它,它都是Linux操作系统的一个发行 ...
- Git高速入门——Git安装、创建版本号库以及经常使用命令
学习Git最全面的资料,在我看来是这本书--Pro Git,网上关于Git的教程有非常多,包含当中一些非常优秀的教程.这一系列的博客,主要是记录自己学习Git的经历.以及在这一过程中遇到的一些问题. ...
- Bootstrap 3 Menu Generator
Bootstrap 3 Menu Generator
- C#指南,重温基础,展望远方!(10)C#枚举
枚举类型是包含一组已命名常量的独特值类型. 需要定义包含一组离散值的类型时,可以定义枚举. 枚举使用一种整型值类型作为其基础存储, 并提供离散值的语义含义. 以下示例声明并使用名为“Color”的 e ...
- 关于分部视图(Partial View)
一.关于分部视图(Partial View) Partial View是可以应用在View中的,编写一次,在其他View中可以被反复使用.通常都是放在"Views——Shared" ...
- js 获取中文的拼音首字母
es6 + 模块化封装 "use strict"; module.exports = { //参数,中文字符串 //返回值:拼音首字母串数组 makePy (str) { if ( ...
- python 多线程 示例
import threading import Queue q = Queue.Queue() from test import * def worker1(x, y): #假设耗时 执行完毕 大于三 ...
- Atitit. 有限状态机 fsm 状态模式
Atitit. 有限状态机 fsm 状态模式 1. 有限状态机 1 2. "状态表"和"状态轮换表" 1 3. 有限状态机概念(状态(State)事件(Even ...
- iOS代码技巧之判断设备及状态
转自:http://my.oschina.net/joanfen/blog/149076 一.判断设备 01 //设备名称 02 return [UIDevice currentDevice].nam ...