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. Winform将FastReport的report与PreviewControl建立绑定关系

    场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  2. Linux配置部署_新手向(四)——Redis安装与配置

    前言 配置完mysql之后,我们来紧接着安装redis,毕竟这些不用太多的思考,就是命令执行,配置文件,连接测试. 安装 首先,我们要看安装哪个版本,可以在Redis官网看看我们安装哪个版本. 在之前 ...

  3. 关于WebApi的跨域问题

    前端调用我后端接口时出现200,跨域问题 解决方案: 在webconfig中加入以下配置就OK了 <configuration> <system.webServer> < ...

  4. [CUDA] 00 - GPU Driver Installation & Concurrency Programming

    前言 对,这是一个高大上的技术,终于要做老崔当年做过的事情了,生活很传奇. 一.主流 GPU 编程接口 1. CUDA 是英伟达公司推出的,专门针对 N 卡进行 GPU 编程的接口.文档资料很齐全,几 ...

  5. java数据结构——数组(Array)

    数据结构+算法是我们学习道路上的重中之重,让我们一起进步,一起感受代码之美! /** * 让我们从最基本的数据结构——数组开始吧 * 增.删.改.查.插.显示 */ public class Seql ...

  6. 带UI的小初高数学学习软件

    结对编程项目总结   一.项目需求分析与功能总结 (1)用户注册功能 用户提供手机号码,点击注册将收到一个注册码,用户可使用该注册码完成注册. (2)设置密码功能 密码6-10位,必须含大小写字母和数 ...

  7. webdriver断言

    操作(action).辅助(accessors)和断言(assertion): 操作action: 模拟用户与 Web 应用程序的交互.一般用于操作应用程序的状态. 如点击链接,选择选项的方式进行工作 ...

  8. Hadoop源代码点滴-基础概念

    大数据特征:volume(数量).variety(多样性).velocity(产生的速度) 大数据特征:多.乱.快.杂 数据的来源:业务数据.日志.管理文档(OCR).互联网.物联网.外购

  9. mysql 排序规则

    一.对比 1.utf8_general_ci 不区分大小写,utf8_general_cs 区分大小写 2.utf8_bin: compare strings by the binary value ...

  10. gulp简单使用

    1.安装gulp,由于某些在下不能解决的原因,故使用gulp 3.9.1版本 安装命令: npm install gulp@3.9.1 注意不要直接使用 : npm install gulp 安装,直 ...