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.

状态DP  dp[S | (1 << j) ][(k * 10 + a[j])] += dp[S][k];  ( s 里不包括 第j 个元素)

dp[S][k] 代表 取s 代表 所取的元素集合,k代表对m的取模,则这个数组代表在这一状态的数目

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <bitset> using namespace std; typedef long long ll; #define maxn (1 << 18) ll dp[maxn][],fac[];
int s[],num[];
int m,len = ;
ll n; void init() {
ll t = n;
while(t) {
s[len++] = t % ;
num[t % ]++;
t /= ;
} fac[] = ;
for(int i = ; i <= ; i++) {
fac[i] = fac[i - ] * i;
} for(int S = ; S < ( << len); S++) {
for(int j = ; j < m; j++) {
dp[S][j] = ;
}
}
} void solve() {
init(); dp[][] = ; for(int S = ; S < ( << len); ++S) {
for(int j = ; j < len; ++j) {
if(!(S & ( << j))) {
for(int k = ; k < m; ++k) {
if(S || s[j])
dp[S | ( << j)][(k * + s[j]) % m]
+= dp[S][k]; }
} } } for(int i = ; i < ; i++) {
if(num[i] > ) {
dp[( << len) - ][] /= fac[ num[i] ];
} }
printf("%I64d\n",dp[( << len) - ][]); } int main () { //freopen("sw.in","r",stdin); scanf("%I64d%d",&n,&m); solve(); return ; }

cf div2 235 D的更多相关文章

  1. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. 离线dfs CF div2 707 D

    http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...

  3. cf div2 239 D

    D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. cf div2 236 D

    D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  6. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. cf div2 238 c

    C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. cf div2 234 E

    E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...

  9. CF div2 D BFS

    http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...

随机推荐

  1. 链接与ELF文件格式的复习

    在这里复习一下链接的知识: 什么是链接(linking):把源代码形成的模块独立编译后组装成一个整体的的过程叫做链接. 链接主要过程包括:地址和空间分配(address and storage all ...

  2. 标签控制器,UITabBarController

    注意: 1.tabbar高度不可设置,可通过_tabbar.tabbar.frame设置tabbar的位置 2.tabbar不同页面添加同一个视图后其那面添加的不起作用,只有最后一个才具有所添加的仕途 ...

  3. request.getSession();为什么不用response儿用request!

    首先回答为什么分别是response和request这两个内置对象.你得先明白你通过获取对象是做什么用的,是往哪用的.第一个PrintWriter out=response.getWriter()是想 ...

  4. c++11:copy_n

    copy_n: Copies exactly count values from the range beginning at first to the range beginning at resu ...

  5. ADO.NET笔记——使用Command执行增删改操作,通过判断ExecuteNonQuery()返回值检查是否操作成功

    相关知识: ExecuteNonQuery()方法:执行CommandText属性所制定的操作,返回受影响的记录条数.该方法一般用来执行SQL中的UPDATE.INSERT和DELETE等操作 对于U ...

  6. PHP 图片文件上传代码

    通过 PHP,可以把文件上传到服务器.里面加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的 ...

  7. Linux之mount命令详解

    linux下挂载(mount)光盘映像文档.移动硬盘.U盘.Windows和NFS网络共享linux是个优秀的开放源码的操作系统,能够运行在大到巨型小到掌上型各类电脑系统上,随着linux系统的日渐成 ...

  8. gcc编译出现的问题

    /usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error 解决办法:g++ -std=c++11

  9. Apache网站根目录

    Apache环境配置好了,把项目放到网站根目录下的htdocs文件夹下,运行网站的时候不需要加上该文件夹的名称,Apache直接在上面找对应的项目

  10. matlab实现贝塞尔曲线绘图pdf查看

    贝塞尔曲线绘图方法: %Program 3.7 Freehand Draw Program Using Bezier Splines %Click in Matlab figure window to ...