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.


题意:给出一个数字num和m。问通过又一次排列num中的各位数字中有多少个数(mod m)=0,直接枚举全排列肯定不行,能够用状压dp来搞..

dp[S][k]表示选了num中的S且(mod m)=k的方案种数,初始条件dp[0][0]=1,转移方为dp[i|1<<j[(10*k+num[j])%m]+=dp[i}[k];,注意到是多重排列。所以还须要除去反复的。

代码例如以下:

#include <iostream>
#include <cstring>
using namespace std; typedef long long ll; ll dp[1<<18][100],c[20];//dp[S][k]表示选了num中的S且(mod m)=k的方案种数 int main(int argc, char const *argv[])
{
char num[20];
int m;
while(cin>>num>>m) { memset(dp,0,sizeof dp);
memset(c,0,sizeof c);
dp[0][0]=1; ll div=1,sz=strlen(num),t=1<<sz;
for(int i=0;i<sz;i++) {
div*=(++c[num[i]-='0']);//可重排列最后要除的除数n1!*n2!*...nk!
} for(int i=0;i<t;i++) {
for(int j=0;j<sz;j++)if(!(i&1<<j)) {//集合S中不含j才转移
if(num[j]||i){//至少一个不为0保证无前导0
for(int k=0;k<m;k++) {
dp[i|1<<j][(10*k+num[j])%m]+=dp[i][k];
}
}
}
} cout<<dp[t-1][0]/div<<endl;
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

最新文章

  1. Verilog学习笔记简单功能实现(六)...............计数分频电路
  2. maven Connection refused: connect
  3. 让UIWebView弹出键盘上的按钮显示中文
  4. U3D 精灵的点击监听
  5. friend keyword 对于模板 并不只不过友元!!!
  6. win8 客户端源码
  7. 优化C/C++代码的小技巧(转)
  8. Spark1.3.0安装
  9. Java岗 面试考点精讲(基础篇01期)
  10. JVM——垃圾回收(GC)
  11. nginx根据url中的参数进行转发
  12. windows7 安装虚拟机,xsheel连接不上的问题,记录一下
  13. C++关于sort和priority_queue的运算符重载
  14. STL在数组算法的使用
  15. 一、怎样使用eclipse查看JDK源码
  16. Java多线程 -join用法
  17. SharePoint服务器端对象模型 完结
  18. JavaWeb JavaScript
  19. Anaconda( different versions) configuration in ubuntu 14
  20. _AppStart.cshtml 和 _PageStart.cshtml的妙用

热门文章

  1. 如何在 Swift 中优雅地处理 JSON
  2. proxool数据库连接池配置
  3. java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layou
  4. LVM 命令集总结(转)
  5. 思考的工作方式——计划经济or市场经济
  6. 【C++基金会 04】vector详细解释
  7. SpringAop进行日志管理。
  8. java处理Excel文件---excel文件的创建,删除,写入,读取
  9. 浅析Java中的final关键字(转)
  10. 《R实战》读书笔记三