Codeforces 401D Roman and Numbers
题目大意
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的更多相关文章
- codeforces 401D. Roman and Numbers 数位dp
题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. #include<bits/stdc++. ...
- 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 ...
- 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 ...
- 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 ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- 题解-Roman and Numbers
题解-Roman and Numbers 前置知识: 数位 \(\texttt{dp}\) </> \(\color{#9933cc}{\texttt{Roman and Numbers} ...
- CF401D Roman and Numbers 状压DP
CF401D 题意翻译 将n(n<=10^18)的各位数字重新排列(不允许有前导零) 求 可以构造几个mod m等于0的数字 题目描述 Roman is a young mathematicia ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
- CodeForces 682A Alyona and Numbers (水题)
Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...
随机推荐
- logging记录了其他操作的问题
做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...
- 蓝牙nrf52832的架构和开发(转载)
相比TI的CC254X.DIALOG的DA1458X,nordic推出的nrf51822和nrf52832在架构和开发商都有自己独特的地方.这几颗产品都是蓝牙低功耗芯片.DA1458X使用OTP硬件架 ...
- HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树
题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...
- luogu1939 【模板】矩阵加速(数列)
upd:现在推荐使用一个长度为 \(n\) 的一维向量.若状态矩阵 \(F\) 对下一时间的状态矩阵 \(F'\) 有影响,则 \(F'=FA\) 中的 转移矩阵 \(A\) 的赋值方法是: 若状态矩 ...
- apache 虚拟主机配置(根据不同的域名映射到不同网站)
最近弄了台香港服务器做测试,Web服务器软件用的是Apache2.2,机器只有一台,ip只有一个,但是想测试几个站点,于是尝试了下Apache的虚拟主机配置.之前已经写过一篇博文了——<Apac ...
- 《Effective Java》笔记 :(一)创建和销毁对象
一 .考虑用静态工厂方法代替构造器 1. 静态工厂方法与设计模式中的工厂方法模式不同,注意不要混淆 例子: public static Boolean valueOf(boolean b){ retu ...
- Leetcode 423.从英文中重建数字
从英文中重建数字 给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 &quo ...
- [python subprocess学习篇] 调用系统命令
http://www.jb51.net/article/57208.htm 3).Popen.communicate(input=None):与子进程进行交互.向stdin发送数据,或从stdout和 ...
- request.getHeader("referer")
在开发web程序的时候,有时我们需要得到用户是从什么页面连过来的,这就用到了referer. 它是http协议,所以任何能开发web程序的语言都可以实现,比如jsp中是: request.getHea ...
- layui.code代码装饰器
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...