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&lt;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 .

输入输出样例

输入样例#1:

104 2
输出样例#1:

3
输入样例#2:

223 4
输出样例#2:

1
输入样例#3:

7067678 8
输出样例#3:

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] [j] , i表示一个二进制数, 1代表选这个数, j代表由n个数中选出x个组成的数%m==j;
 

转移方程 : 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的更多相关文章

  1. 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 ...

  2. CF449D Jzzhu and Numbers (状压DP+容斥)

    题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...

  3. 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 ...

  4. [poj2411] Mondriaan's Dream (状压DP)

    状压DP Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One nigh ...

  5. 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 ...

  6. 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 ...

  7. TZOJ 2289 Help Bob(状压DP)

    描述 Bob loves Pizza but is always out of money. One day he reads in the newspapers that his favorite ...

  8. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  9. 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 ...

随机推荐

  1. Nginx的架构及工作流程

    NGINX是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器.NGINX以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而闻名,也是为解决C10K问题 ...

  2. Fragment的创建与通信

    由于这里涉及到接口回调的问题,所以先来看一看什么是接口回调: 这就好比老板和员工的微妙关系,老板需要员工去工作,员工挣钱了以后还要告诉老板自己挣了多少钱,然后由老板来处理这些钱. 首先创建一个接口: ...

  3. CabloyJS带你轻松走进NodeJS全栈开发-免费课程 作者亲授

    课程说明 B站直播 为回馈新老同学对开源框架CabloyJS的支持与厚爱,快速而轻松的开启NodeJS全栈开发之旅.2019年9月5日至9月11日在B站开启了一波免费直播培训课程 课程信息,请点击链接 ...

  4. '\b'退格符号笔记

    今天在给小孩儿讲for循环输出最后一个输出项没有空格的情况 借助标记,选择在第二个至最后一个的输出项前添加空格 ; ; i < n; i++) { ) cout << a[i]; e ...

  5. 搭建第一个node服务器

    1.在项目文件夹根目录创建app.js: (1)先引入模块 const http = require('http');//http是安装好node就有的一个模块,是用来创建http服务器的 (2)创建 ...

  6. Eclipse通过SVN导入项目遇到的问题记录

    问题一.把子项目导入为project 原因:一个大的文件夹,里面有各个小项目,需要把自己添加需求的醒目导入为Maven Project 1.右键选 Import as project 2.右键 -&g ...

  7. 第六届蓝桥杯java b组第十题

    10.压缩变换(程序设计) 小明最近在研究压缩算法. 他知道,压缩的时候如果能够使得数值很小,就能通过熵编码得到较高的压缩比. 然而,要使数值很小是一个挑战. 最近,小明需要压缩一些正整数的序列,这些 ...

  8. SpringBootSecurity学习(05)网页版登录内存中配置默认用户

    默认用户 前面的例子中我们使用的都是配置文件中配置好的默认用户: 除了可以配置账号密码,还可以在配置文件中配置角色: 这个角色是后面实现权限过滤的重要内容,后面会重点讨论. 在内存中配置默认用户 这样 ...

  9. 打印方案之web打印

    前言: 前一段时间在工作中,遇到需要通过打印实现对报表或者工作流清单等事情时,都需要运用到打印功能,那么 ,这个时候你会怎么处理? 在这里,我们可以通过最简单的方式实现web打印功能,简单易懂,方便快 ...

  10. 1.python环境配置 - python基础入门

    工欲善其事必先利其器,python学习首先要做得就是配置python环境.配置环境只需要下载Pycharm 和 Anaconda两个安装包即可,请跟上我得步伐,一步一步操作. 重要的事情说三遍: 先安 ...