题目链接:

http://codeforces.com/problemset/problem/401/D

D. Roman and Numbers

time limit per test4 seconds
memory limit per test512 megabytes
#### 问题描述
> 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, modulo m.
>
> Number x is considered close to number n modulo m, if:
>
> it can be obtained by rearranging the digits of number n,
> it doesn't have any leading zeroes,
> the remainder after dividing number x by 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 (1 ≤ n  In a single line print a single integer — the number of numbers close to number n modulo m.
####样例输入
> 104 2

样例输出

3

题意

给你n和m,你可以对n的数位进行重排得到新的数,统计所有得到的数中能被m整除的有多少个。

题解

重排所有情况为len!个,len<=18我们可以考虑状压来做,然后处理下%m的余数就可以了。

dp[i][j]表示状态为i,%m==j的所有情况。

最后注意下前导零和去重就可以了。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- LL dp[1<<19][101]; int main() {
LL x; int m;
scf("%I64d%d",&x,&m); int arr[22],n=0;
while(x){ arr[n++]=x%10; x/=10; } clr(dp,0);
dp[0][0]=1;
bool vis[11];
rep(i,1,(1<<n)){
clr(vis,0);
rep(j,0,n) if(i&(1<<j)){
///去除前导零
if(arr[j]==0&&((i^(1<<j))==0)) continue;
///vis用来去重的,相同的数字谁排最后都一样,算一次就够了。
if(vis[arr[j]]) continue;
vis[arr[j]]=1;
rep(k,0,m){
dp[i][(k*10+arr[j])%m]+=dp[i^(1<<j)][k];
}
}
} prf("%I64d\n",dp[(1<<n)-1][0]); return 0;
} //end-----------------------------------------------------------------------

Notes

codeforces的空间限制是512MB!!!,int能开到10^8,long long 能开到5e7。

Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp的更多相关文章

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

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  3. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  4. Codeforces Round #384 (Div. 2) E. Vladik and cards 状压dp

    E. Vladik and cards 题目链接 http://codeforces.com/contest/743/problem/E 题面 Vladik was bored on his way ...

  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 Round #235 (Div. 2)

    A. Vanya and Cards time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Codeforces Round #493 (Div. 2)D. Roman Digits 第一道打表找规律题目

    D. Roman Digits time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  8. Codeforces Round #532(Div. 2) A.Roman and Browser

    链接:https://codeforces.com/contest/1100/problem/A 题意: 给定n,k. 给定一串由正负1组成的数. 任选b,c = b + i*k(i为任意整数).将c ...

  9. Codeforces Round #235 (Div. 2)C、Team

    #include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; cin & ...

随机推荐

  1. Mybatis联合查询记录,左连接参数操作

    公司业务需求要做个列表的排序 而实际排序的字段不再本库中,需要跨库去拿到字段,因为是微服务体系架构,不可能Left join跨库的表,所以决定调用一次跨服务的API拿到排序相关的对象,里面包含需要排序 ...

  2. 筑基期—C语言

    1.1 环境: 在ANSIC的任何一种是实现中,存在两种不同的环境.第一种是翻译环境,第二种是执行环境.标准明确说明这两种环境不必在同一台机器上,交叉编译器就是在一台机器上运行,但它所产生的可执行代码 ...

  3. Java基础——类加载机制

    什么叫类加载 JVM把 .class 字节码文件加载到内存,并进行相关的校验.解析.初始化,最终转换为虚拟机可用的JAVA类型的过程,称为JVM类加载机制. (当然,JVM并不关心class文件的来源 ...

  4. WPF 主题

    原文:WPF 主题 WPF 切换主题 string packUri = String.Format(@"/WpfControlLibrary1;component/Dictionary1.x ...

  5. c++ 标准流文件

    一.标准流stdin,stdout,stderr   标准输入流stdin: 是程序可以读取其输入的位置.缺省情况下,进程从键盘读取 stdin . fscanf(stdin,"%d%d%f ...

  6. python基础学习1-描述符

    #!/usr/bin/env python # -*- coding:utf-8 -*- #描述符就是将某种特殊类型的类的实例指派给另一个类的属性 #特殊类型指 实现了 # __get__(self, ...

  7. 【HNOI2013】游走

    题面 题解 图上的期望大部分是\(dp\),无向图的期望大部分是高斯消元 设\(f[i]\)表示走到点\(i\)的期望,\(d[i]\)表示\(i\)的度,\(to(i)\)表示\(i\)能到达的点集 ...

  8. springboot之assembly的文件配置

    一.在使用springboot框架的时候,存在一个问题.就是我们配置yaml文件,需要单独提出来做参数修改.当然这个是可以通过spring.profiles.active的方式来配置dev,prod等 ...

  9. git删除所有提交历史记录

    这种方式是最快最有效的 进项目根目录启动git bash,然后执行这些即可 最后的 git push -f origin master 会失败,直接在idea里push就能成功了 .Checkout ...

  10. 约束4:唯一约束,Check约束和null

    大家知道,关系型数据库的逻辑运算的结果是三值型的,TRUE,FALSE和UNKNOWN,特别是,NULL值和任何值都不相等,任何值和NULL的比较,返回的逻辑结果都是unknown.而NULL值在唯一 ...