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. hibernate 一对多映射关系

    1.   单项多对一映射       custom(顾客)与order(订单) :一个顾客可以有多个订单,一个订单只能有一个顾客       配置方法:在多的一端配置<many -to one& ...

  2. VKP5 Price Calculation – List Variant & KZPBL (Delete site level)

    List Variant: Configuration in Logistic General –> Retail Pricing –> Sales Price Calculation – ...

  3. u-boot ctr0.S详解 包含_main函数

    /** ****************************************************************************** * @author    Maox ...

  4. WP开发笔记——控件倾斜效果

    创建一个基本的 Windows Phone 应用程序并添加 TiltEffect 类文件. 添加要倾斜的控件的分类. 全局应用 IsTiltEnabled 依赖项属性,以便为所有的指定控件提供倾斜功能 ...

  5. Android 技术用于汇总

    id 名词 含义 详细  1 Android CTS     CTS 全称 Compatibility Test Suite 兼容性测试工具 当产品开发出来以后,并定制了自己的 Android 系统后 ...

  6. 转: js操作cookie

    cookie的几个概念 http://dearhappyfish.blog.163.com/blog/static/1901094152012422114753777/ js操作cookie 转:ht ...

  7. WindowsMediaPlayer控件批量添加文件至播放列表

    思路: 1.读取批定路径的目录文件. 2.用List存放. 3.循环List列表添加到播放列表. public void VidieoPlay() { //WindowsMediaPlayer1.ui ...

  8. tomcat6.0 数据库连接池配置问题

    tomcat6.0 数据库连接池配置问题: 连接池配好后,启动tomat后,输入项目系统的登录名和密码,报 Cannot create JDBC driver of class '' for conn ...

  9. Winform上传下载文件代码

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  10. EditorWindow 和MenuItem

    using UnityEngine; using System.Collections; using UnityEditor; public class ClipEventEditor : Edito ...