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 ...
随机推荐
- 用\r做出进度条
在做ftp作业的时候,需要做一个上传和下载的进度条,做的时候发现用\r很容易就能做出来 def show_progress(self, has, total): rate = float(has) / ...
- 简单了解hash
hash,译为散列或哈希.就是把任意长度的输入(可变类型除外)经过hash算法,输出成固定长度的输出,该输出就是hash值.哈希值比原有的输出占用空间要小,但是不同的输出可能会hash出一样的值,所以 ...
- AJAX小练习
/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- UVA1589——xiangqi
开始碰到这个题时觉得太麻烦了直接跳过没做,现在放假了再次看这个题发现没有想象中那么麻烦,主要是题目理解要透彻,基本思路就是用结构体数组存下红方棋子,让黑将军每次移动一下,看移动后是否有一个红方棋子可以 ...
- JS实现——用3L和5L量出4L的水
把以下代码保存成donglanguage.html文件,使用Google或360浏览器打开 <!DOCTYPE html> <html> <head> <me ...
- Java-一个数组中的元素复制到另一个数组
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length).其中五个参数分别表示为: ...
- 2章 perl标量变量
标量变量 单单存储一个值得变量 ,单个标量值 $name 为变量 区分大小写 $barney=$barney*2 第一次 取值 等号右边 :第二次 赋值 等号左边 双目操作符 ...
- 使用 Rails Webpacker 安裝 Foundation 6
動機 由於 foundation-rails 6.4.1 版本有個 Issue 目前還沒合併.加上 Rails 已經支援了 webpack 2.x.這篇文章純粹紀錄另外一種做法. 準備 開始使用之前需 ...
- django model的设计
1 设计blog的显示界面: 在blog/admin.py修改 from django.contrib import admin from blog.models import BlogPost # ...
- C#拆箱和装箱成本
从原理上可以看出,装箱时,生成的是全新的引用对象,这会有时间损耗,也就是造成效率降低. 文章:.Net常见面试题整理(2)——装箱和拆箱 装箱,产生新的引用对象,并且赋值,然后引用. 拆箱,往往跟随着 ...