CF401D Roman and Numbers 状压DP
CF401D
题意翻译
将n(n<=10^18)的各位数字重新排列(不允许有前导零) 求 可以构造几个mod m等于0的数字
题目描述
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 n n , modulo m m m .
Number x x x is considered close to number n n n modulo m m m , if:
- it can be obtained by rearranging the digits of number n n n ,
- it doesn't have any leading zeroes,
- the remainder after dividing number x x x by m m 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.
输入输出格式
输入格式:
The first line contains two integers: n n n $ (1<=n<10^{18}) $ and m m m (1<=m<=100) (1<=m<=100) (1<=m<=100) .
输出格式:
In a single line print a single integer — the number of numbers close to number n n n modulo m m m .
输入输出样例
104 2
3
223 4
1
7067678 8
47
说明
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
分析:
题目描述确实比较吓人, n位数字重新排列最多可以创造出多少个%m == 0 的数;
转移方程 : f[i|(1 << k)][(j * 10 + x) % m] += f[i][j];
意义:对于第k位数x, 都可以由不选他转移到选他, 就是 i -> i |(1 << k);
然后第二维就由 j -> (j *10 + x) % m (显然);
注意 : 因为状态压缩是暴力的把每一位数当成与前边的数都不一样, 比如 11 ,应该算一次, 但是我们却算了两次;
方案 : 1. 最后除以cnt!(cnt为一个数出现了多少次)。
2. 直接去重。
代码奉上:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn (1 << 18) + 5
#define int long long int n, m; int s[21]; int f[maxn][105]; char ch[20]; bool vis[20]; signed main()
{
scanf("%s%lld", &ch, &m); int n = strlen(ch); f[0][0] = 1; int e = (1 << n);
for(register int i = 0 ; i < e ; i ++)
{
for(register int j = 0 ; j < m ; j ++)
{
memset(vis, 0, sizeof vis);
for(register int k = 0 ; k < n ; k ++)
{
int x = ch[k] - '0';
if(i & (1 << k)) continue;
if(i == 0 && x == 0) continue;
if(vis[x]) continue;
vis[x] = 1;
f[i|(1<<k)][(j*10+x)%m] += f[i][j];
}
}
} cout << f[e-1][0];
return 0; }
CF401D Roman and Numbers 状压DP的更多相关文章
- 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 ...
- CF449D Jzzhu and Numbers (状压DP+容斥)
题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...
- 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 ...
- [poj2411] Mondriaan's Dream (状压DP)
状压DP Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One nigh ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- TZOJ 2289 Help Bob(状压DP)
描述 Bob loves Pizza but is always out of money. One day he reads in the newspapers that his favorite ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- Codeforces Beta Round #8 C. Looking for Order 状压dp
题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...
随机推荐
- hive 元数据解析
在使用Hive进行开发时,我们往往需要获得一个已存在hive表的建表语句(DDL),然而hive本身并没有提供这样一个工具. 要想还原建表DDL就必须从元数据入手,我们知道,hive的元数据并不存放在 ...
- 基于python-django框架的支付宝支付案例
目录 @ 一. 开发前的准备 1. 必须了解的知识 SDK:软件开发工具包,可以为开发者提供快速开发的工具 沙箱环境:也就是测试环境 支付宝支付金额的精度:小数点后两位(面试) 支付宝用的什么加密方式 ...
- 前台提交数据到node服务器(post方式)
post方式同样有两种办法,一种是表单提交,一种是ajax提交. 在此之前需要安装一个中间件:body-parser,安装好后在app.js头部引入: bodyParser = require('bo ...
- Python2与Python3的map()
1. map()函数 Python2中,map(func, seq1[,seq2[...[,seqn)将func作用于seq*的每个序列的索引相同的元素,并最终生成一个[func(seq1[0], s ...
- 使用springboot最新版本mysql-Connector连接数据库时报错解决
在连接数据库时,使用了最新版本的mysql-Connector,即6.0以上版本 1.报错如下: Loading class `com.mysql.jdbc.Driver'. This is depr ...
- python3连接MySQL实现增删改查
PyMySQL 安装 在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装. PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL. 如果还未安 ...
- php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期
<?php //本周一 echo date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); // ...
- Android自动跳过app开屏广告
跳过开屏广告,体验流畅人生 开屏广告 是应用启动时显示的广告,一般右下角(或右下角)有倒计时跳过,不主动点击就会 等待3到5秒 后再进入App 自动跳过 是跳过应用的开屏广告的App 一图胜千文,来我 ...
- SpringBoot自定义异常,优雅解决业务逻辑中的错误
概要 你是不是在为业务逻辑中出现的异常弄的焦头烂额,常常在后台报错,前端却无法提示错误内容,导致用户体验极差?比如下单失败,前端只能提示下单失败,但是却不知道为什么失败,是库存不足,还是余额不足,亦或 ...
- 【Elasticsearch 搜索之路】(一)什么是 Elasticsearch?
本篇文章对 Elasticsearch 做了基本介绍,在后续将通过专栏的方式持续更新,本系列以 Elasticsearch7 作为主要的讲解版本,欢迎各位大佬指正,共同学习进步! 一般涉及大型数据库的 ...