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

最新文章

  1. keras 入门之 regression
  2. 回文串---Palindrome
  3. swift init继承问题
  4. more命令
  5. Eclipse开发JavaWeb程序报Server Tomcat v7.0 at localhost was unable to start
  6. 解决main No MyBatis mapper was found in 的警告
  7. java 运行项目不放到tomcat下的webapps文件夹下放到自己建的文件夹中的处理办法
  8. 关于jQuery的cookies插件2.2.0版设置过期时间的说明
  9. perl post发送json数据
  10. NFS工作原理
  11. [LeetCode] My Calendar I 我的日历之一
  12. 查询优化--小表驱动大表(In,Exists区别)
  13. csrf_token 跨站请求设置
  14. CentOS 7 install slurm cluster
  15. c# 委托初用法
  16. C#程序分析
  17. 【CF718E】Matvey&#39;s Birthday BFS+动态规划
  18. handsontable-developer guide-cell function
  19. {Emgu}{C#}保存图片、视频等
  20. 【刷题】洛谷 P3804 【模板】后缀自动机

热门文章

  1. IOS8 Playground介绍
  2. Android应用更新自动检测下载
  3. webpack 通用模块(每个页面都用到的js)编译
  4. python 的集合 set()操作
  5. windows下LIB和DLL的区别与使用
  6. struts2 转发、重定向概述
  7. 什么是Coded UI
  8. MySQL主从不一致的几种故障总结分析、解决和预防
  9. WebSocket的几个模块(node.js)(未完)
  10. Redis(十四):主从复制