题目大意

Description

给定一个数 N(N<1018) , 求有多少个经过 N 重组的数是 M(M≤100) 的倍数.

注意: ①重组不能有前导零; ②重组的数相同, 则只能算一个数.

Input

第一行两个数 N , M .

Output

输出满足要求的数的个数.

Sample Input

223 4

Sample Output

1

题解

状压DP.

\(f[i][j]\)中, \(i\)是状态, 表示原数中哪些位已经被新数占用. 因此, 一个Naive的想法就是对于新数的每一位, 进行一次DP, 时间复杂度: \(2^{18} \times 18 \times 18\), 显然会TLE.

我们注意到, 每当我们进行一次转移, 也就是在新数中填入一位的时候, 状态\(i\)都只会变小, 因此我们从\(2^{18} - 1\)往下直接进行一次DP即可. 时间复杂度: \(2^{18} * 18\), 尚可接受.


#include <cstdio>
#include <cstring> const int LEN = 18, M = 100; int main()
{
#ifndef ONLINE_JUDGE
freopen("CF401D.in", "r", stdin);
#endif
static long long pw[LEN];
pw[0] = 1;
for(int i = 1; i < LEN; ++ i)
pw[i] = pw[i - 1] * 10;
long long n, m;
scanf("%lld%lld\n", &n, &m);
int len = 0;
long long tmp = n;
static int cnt[10];
for(; tmp; tmp /= 10, ++ len)
++ cnt[tmp % 10];
static long long fac[10];
for(int i = 0; i < 10; ++ i)
{
fac[i] = 1;
for(int j = 1; j <= cnt[i]; ++ j)
fac[i] *= j;
}
static long long f[1 << LEN][M];
memset(f, 0, sizeof(f));
f[(1 << len) - 1][0] = 1;
/*
for(int l = len - 1; ~ l; -- l)
for(long long i = 0; i < 1 << len; ++ i)
for(int j = 0; j < m; ++ j)
if(f[i][j])
{
for(int k = 0; k < len; ++ k)
{
if(n / pw[k] % 10 == 0 && l == len - 1)
continue;
if(i >> k & 1)
f[i ^ (1 << k)][(j + n / pw[k] % 10 * pw[l]) % m] += f[i][j]; }
f[i][j] = 0;
} */
for(int i = (1 << len) - 1; ~ i; -- i)
for(int j = 0; j < len; ++ j)
if(i >> j & 1 && (i ^ (1 << len) - 1 || n / pw[j] % 10 % 10))
for(int k = 0; k < m; ++ k)
f[i ^ (1 << j)][(k * 10 + n / pw[j] % 10) % m] += f[i][k];
long long ans = f[0][0];
for(int i = 0; i < 10; ++ i)
ans /= fac[i];
printf("%lld\n", ans);
}

Codeforces 401D Roman and Numbers的更多相关文章

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

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

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp

    题目链接: http://codeforces.com/problemset/problem/401/D D. Roman and Numbers time limit per test4 secon ...

  3. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  4. Codeforces Round #235 (Div. 2) D. Roman and Numbers(如压力dp)

    Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standard i ...

  5. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  6. 题解-Roman and Numbers

    题解-Roman and Numbers 前置知识: 数位 \(\texttt{dp}\) </> \(\color{#9933cc}{\texttt{Roman and Numbers} ...

  7. CF401D Roman and Numbers 状压DP

    CF401D 题意翻译 将n(n<=10^18)的各位数字重新排列(不允许有前导零) 求 可以构造几个mod m等于0的数字 题目描述 Roman is a young mathematicia ...

  8. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  9. CodeForces 682A Alyona and Numbers (水题)

    Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...

随机推荐

  1. leetcode-12-stack

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  2. HDU:4185-Oil Skimming

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...

  3. ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver

    The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. ...

  4. Windows中redis的下载及安装、设置

    本文是转载自:https://www.cnblogs.com/wxjnew/p/9160855.html 除了原文的东西还有自己遇到的一些问题,这里记录一下. 一.下载: 下载地址: https:// ...

  5. webservice soap wsdl简介

    先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含了运行环境,编程模型, 架构 ...

  6. IDEA-常用插件,使用FindBugs寻找bug,代码分析

    bug无处不在,但是我们总希望少一点bug. 最近发现了一款好用的寻找bug的插件,特此记下. 一.安装 路径:File-->Settings-->Plugins-->Browse ...

  7. html5/css3常考面试题

    一.HTML5 CSS3 CSS3有哪些新特性? 1. CSS3实现圆角(border-radius),阴影(box-shadow), 2. 对文字加特效(text-shadow.),线性渐变(gra ...

  8. Mime类型与文件后缀对照表及探测文件MIME的方法

    说明:刚刚写了一篇<IHttpHandler的妙用(2):防盗链!我的资源只有我的用户才能下载>的文章,网址:http://blog.csdn.net/zhoufoxcn/archive/ ...

  9. CI 分页类的使用

    分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...

  10. 添加字段的SQL语句的写法:

    alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参 --删除字段   -- alter table  [SolidDB].[dbo].tP ...