转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23100595

【原题】

D. Roman and Numbers
time limit per test

4 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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.

Input

The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).

Output

In a single line print a single integer — the number of numbers close to number n modulo m.

Sample test(s)
input
104 2

output
3

input
223 4

output
1

input
7067678 8

output
47

Note

In the first sample the required numbers are: 104, 140, 410.

In the second sample the required number is 232.

【大意】给你一个n,让你找有多少x满足:x mod m=0。其中x是n的一个排列。重复的只计算一种。

【分析】这真是一道状态压缩的好题目。什么是状态压缩呢?就是把一个状态串压成一个十进制数。在这题里,对于n中的每一位数,都能表示成0或1(取或是不取)。而且总的状态数只有2^len-1(len是n的位数),因此我们可以用DP推出每一种状态。f[i][j]表示在i这种状态(01串的十进制形式),余数为j时的方案数。显然,如果我想取当前的数第K个数——dight[k],f[i][j]就能推到f[i+2^(k-1)][(j*10+dight[k])%m]。当然,要注意这个位置是否已经用过,同时也要注意数字的判重情况。

【代码】

#include<cstdio>
#include<cstring>
using namespace std;
const int size=19;
bool flag[10];
long long dight[size],f[1<<size][101],i,j,p,m,k,n,t;
int main()
{scanf("%I64d%I64d",&p,&m);while (p){dight[++n]=p%10;p/=10;}for (i=1;i<=n/2;i++) t=dight[i],dight[i]=dight[n-i+1],dight[n-i+1]=t;f[0][0]=1;for (i=0;i<(1<<n)-1;i++)  //枚举每一种状态。而且可以证明,到某一种状态时,它一定在之前被推完了。for (j=0;j<m;j++){memset(flag,0,sizeof(flag));  //防止出现相同的情况。if (f[i][j]==0) continue;    //这种状态本身已经推不到。for (k=1;k<=n;k++)if (!flag[dight[k]])      //某一个数码没有出现过。{if (i&(1<<(k-1))) continue;  //这一位已经用过了if (i==0&&dight[k]==0) continue;  //前导0的问题flag[dight[k]]=true;f[i+(1<<(k-1))][(j*10+dight[k])%m]+=f[i][j];  //递推}}printf("%I64d",f[(1<<n)-1][0]);return 0;
}

codeforces #235 D. Roman and Numbers 题解相关推荐

  1. Codeforces791:Ordinary Numbers题解

    Codeforces Round#791(Div.3) B.Ordinary Numbers Ordinary Numbers题解 Codeforces Round#791(Div.3) B.Ordi ...

  2. Codeforces Round #686 (Div. 3) A-F题解

    Codeforces Round #686 (Div. 3) A-F题解 A. Special Permutation 题意 给定 nnn ,输出一个长度为 nnn 的全排列,每个位置 iii 上的数 ...

  3. Codeforces Round #693 (Div. 3)部分题解

    Codeforces Round #693 (Div. 3) 部分题解 D. Even-Odd Game 思路: 贪心:田忌赛马 (1)先将数组从大到小排序,取数时从大到小取,用一个ans变量记录取数 ...

  4. Codeforces Round #702 (Div. 3)A-G题解

    Codeforces Round #702 (Div. 3)A-G题解 比赛链接:https://codeforces.ml/contest/1490 这场F读错题意白给一发,G二分的if(dp[mi ...

  5. codeforces Round #645 (Div. 2)D题解

    Codeforces Round #645 (Div. 2)--D题解 作为一名菜鸡,理所当然得没有A出来,这道题数据放小就一水题了,可惜数据这块卡的死死的. 本题最重要的一点就是你要推出来一个结论: ...

  6. Codeforces 1092 D1 Great Vova Wall 题解

    Codeforces 1092 D1 Great Vova Wall 题解 题目 题意 代码 思路 总结 题目 原题链接 题意 一组数字,要求在经过水平和垂直增加高度后所有数值大小一致,不能阶跃式增加 ...

  7. Codeforces Round #670 (Div. 2)A-D题解

    Codeforces Round #670 (Div. 2)A-D题解 //写于rating值1987/2184 //补档 比赛链接:https://codeforces.ml/contest/140 ...

  8. Codeforces Round #674 (Div. 3)A-F题解

    Codeforces Round #674 (Div. 3)A-F题解 比赛链接:https://codeforces.com/contest/1426 A题 水题不写题解 #include<b ...

  9. codeforces 401D. Roman and Numbers 数位dp

    题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. 1 #include<bits/stdc+ ...

最新文章

  1. Hotel California
  2. python3 multiprocessing 多进程 列表类型 listproxy 清除内容
  3. HP ML110/120 G7配置阵列卡安装server 2003
  4. TDD--测试驱动开发的核心思想
  5. 谈Tensorflow的Batch Normalization
  6. 下面关于“栈”的描述,正确的是( )
  7. Counting Bits
  8. Data Lake Analytics: 以SQL方式查询Redis数据
  9. python多环境管理_怎样管理多个Python版本和虚拟环境
  10. sql查询初学者指南_面向初学者SQL Server查询执行计划–聚集索引运算符
  11. IDEA的Maxcomputer Studio开发
  12. ORACLE RAC 12C(12.1.0.2)数据库软件安装步骤
  13. java答题库_JAVA题库
  14. 计算机显示器刷新率怎么调,显示器刷新率怎么超频?电脑显示器提高屏幕刷新率超频教程...
  15. 有盘和无盘服务器,网吧系统的终极解决之道:无盘系统+有盘(转)
  16. 密码系列-Base32
  17. scrapy爬虫简单实例
  18. 分享两款雪花特效代码
  19. 硬核接亲!程序员被新娘要求现场写代码,结果万万没想到……
  20. knn matting matlab,一键抠图,毛发毕现:这个GitHub项目助你快速PS

热门文章

  1. AFL白皮书实现细节
  2. 离线安装ubuntu网卡驱动
  3. 2.3 Openwrt 模拟 spi 及其sx1301寄存器读写测试
  4. 2022-2028年中国视觉人工智能行业市场现状分析及发展前景展望报告
  5. WebGIS开发和Web开发的区别
  6. WinDebug的一些基本使用命令
  7. c++ memcpy函数的实现
  8. VSCODE集成golang 巨坑
  9. 如何提升自己电脑网速
  10. 【媒体应用:Android平台VLC媒体播放器探索之路系列】之一:VLC播放器开篇