CF235D--状态压缩DP
来源:互联网 发布:php参考书籍 编辑:程序博客网 时间:2024/06/08 05:10
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
- it can be obtained by rearranging the digits of number n,
- it doesn't have any leading zeroes,
- the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).
In a single line print a single integer — the number of numbers close to number n modulo m.
104 2
3
223 4
1
7067678 8
47
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 18#define LL long long intLL dp[1<<maxn][102];LL digit[maxn];LL base[maxn];LL f[maxn];int main(){LL n,m;while(scanf("%I64d%I64d",&n,&m)==2){memset(base,0,sizeof(base));memset(dp,0,sizeof(dp));int num = 0;while(n){digit[num] = n%10;num++;base[n%10]++;n /= 10;}n = num;f[0] = 1;for(int i = 1;i < maxn;i++){f[i] = f[i-1] * i;}LL repeat = 1;for(int i = 0;i < 10;i++){repeat *= f[base[i]];}for(int i = 0;i < n;i++){if(digit[i]){dp[1<<i][digit[i]%m] = 1;}}for(int i = 1;i < (1<<n);i++){for(int j = 0;j < m;j++){if(dp[i][j] == 0)continue;for(int k = 0;k < n;k++){if((i & (1<<k)) == 0){dp[i | (1<<k)][(j*10+digit[k])%m] += dp[i][j];}}}}printf("%I64d\n",dp[(1<<n)-1][0]/repeat);}return 0;}
- CF235D--状态压缩DP
- 状态压缩dp
- pku1038状态压缩dp
- 状态压缩DP 入门
- HDU1074 状态压缩DP
- Poj3254 状态压缩DP
- 状态压缩DP入门
- 【状态压缩DP】互不侵犯
- 【状态压缩DP】电子竞技
- HDU1074 状态压缩DP
- 【状态压缩DP】互不侵犯
- 状态压缩 DP AHU420
- HDU2167 状态压缩DP
- SGU223 -状态压缩DP
- poj3254 状态压缩dp
- 状态压缩dp poj1185
- DP(2) 状态压缩
- poj2430 状态压缩DP
- 挨踢之路之学习流程篇(八)
- Geeks 面试题 : Optimal Binary Search Tree
- 【原理】HOG中的三线性插值
- Linux命令ln、cp、硬链接和软链接
- 【算法拾遗】阿里实习生电面题目:输出给定字符串的全部连续子串
- CF235D--状态压缩DP
- 阅读推荐
- const_cast一个空指针
- 【java语言】积累篇
- 周易正易 p399-499
- vi查找替换命令详解
- 2014-3-11 FPGA学习-数码管
- PAT Basic Level 1021. 个位数统计(15)
- 【十分钟系列课程】第二集~MySQL客户端安装及创建数据库