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. JVM学习(虚拟机栈、堆、方法区)自我看法

    堆(Heap): 此内存区域唯一目的就是存放对象实例,几乎所有的对象实例都在这里分配.这一点在java虚拟机规范中的描述是:所有的对象实例以及数组都要在堆上分配. 虚拟机栈(Stack): 虚拟机栈主 ...

  2. STL 补档

    STL 补档 1.vector 作用:它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据. vector在C++标准模板库中的部分内容,它是 ...

  3. Sqoop介绍、安装与操作

    搭建环境 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录下创建/app目录,用于存放 Hadoop等组件运行包.因为该目录用于安装hadoo ...

  4. JAVA中的内存们

    我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...

  5. LOVE POWER html与javaScript

    1.纯HTML与javaScript编写的表白动态图 1.对于世界而言,你是一个人:但是对于某个人,你是他的整个世界. 2.不要为那些不愿在你身上花费时间的人而浪费你的时间. 3.我爱你,不是因为你是 ...

  6. elasticsearch Discovery 发现模块学习

    发现模块和集群的形成 目标 发现节点 Master选举 组成集群,在Master信息发生变化时及时更新. 故障检测 细分为几个子模块 Discovery发现模块 Discover是在集群Master节 ...

  7. [VB.NET Tips]字符串连接

    在很多应用场景下我们都需要对字符串进行拼接操作. 在每一次连接字符串时,都要在堆上分配新的内存空间,每一个分配都有一定的消耗. 较长的字符串在堆中分配,对其进行连接操作需要花费很长的时间,先连接小的字 ...

  8. Falsk中的Request、Response

    Flask 中的Response 1.HTTPResponse('helloword') "helloword" from flask import Flask # 实例化Flas ...

  9. 离线环境下进行pip包安装

    内网服务器不能上网,但是需要在上面安装python-package 通过另外一台能上网的主机B 1. 下载需要离线安装的Packages 在B上执行如下命令: 安装单个Package $ pip in ...

  10. 更改hadoop集群yarn的webui中的开始时间和结束时间为本地时间

    yarn集群的webui地址为:http://rm:8088 执行任务后,任务的开始时间和结束时间都是utc时间,查看很不方便. 查找相关资料发现hadoop有补丁包,补丁地址:https://iss ...