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 ...
随机推荐
- SpringBoot 2.0 + Nacos + Sentinel 流控规则集中存储
前言 Sentinel 原生版本的规则管理通过API 将规则推送至客户端并直接更新到内存中,并不能直接用于生产环境.不过官方也提供了一种 Push模式,扩展读数据源ReadableDataSource ...
- http响应
1.http响应 2.响应行常见状态码 200 :请求成功. 302 :请求重定向. 当访问网址A时,由于网址A服务器端的拦截器或者其他后端代码处理的原因,会被重定向到网址B. 304 :请求资源没有 ...
- .netCore部署在IIS上遇到的问题(500.19,500.21错误)
1.确保IIS功能都安装上了. 2.确保.netcore 的最新sdk已安装. 3.应用程序池改成无托管代码 4.500.19错误 错误原因,没有安装 DotNetCore.2.0.5-Windows ...
- JAVA自学笔记 - 从零开始
文中记录的内容都是博主从自己的学习笔记中总结的. 如果遇到问题,或者有不一样的看法,欢迎提出! 1安装JDK 从Oracle官网下载JDK,我使用的版本是1.7.0.80. 操作系统是win7 64位 ...
- tomcat启动出现乱码
打开tomcat目录下apache-tomcat-9.0.26\conf\logging.properties 替换全文 encoding = UTF-8 为 encoding = GBK: rest ...
- 多事之秋-最近在阿里云上遇到的问题:负载均衡失灵、服务器 CPU 100%、被 DDoS 攻击
昨天 22:00~22:30 左右与 23:30~00:30 左右,有1台服役多年的阿里云负载均衡突然失灵,造成通过这台负载均衡访问博客站点的用户遭遇 502, 503, 504 ,由此给您带来麻烦, ...
- Python学习-列表深浅拷贝
一.先看一个简单的赋值语句 lst1 = ['France', 'Belgium', 'England'] lst2 = lst1 # lst1.append('Uruguay') lst2.appe ...
- 记一次java-selenium自动抢红包最简单案例1
案例网址:http://xinyue.qq.com/act/pc/xyjf/a20170907envelopes/index.htm?ADTAG=AD_gw.home.pt.2_dyghb.20170 ...
- 讨论c/c++计算小数的精度问题
求出所有100以下整数与一位小数相乘等于相加的浮点数这个有Bug浮点数计算时精度会出现误差 除非使用非常精确的类型或限制浮点的位数 比如 #include <iostream> int m ...
- Spring Environment的加载
这节介绍environment,默认环境变量的加载以及初始化. 之前在介绍spring启动过程讲到,第一步进行环境准备时就会初始化一个StandardEnvironment.下图为Environm ...