题目链接:

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. window.open 防止浏览器拦截

    https://blog.csdn.net/sinat_37255207/article/details/89374416 网上试了很多方法 最终只有一种可以 var newWin = window. ...

  2. 各国货币json文件

    [ {"countryname":"","name":"请选择","currency":" ...

  3. MongoDB Python官方驱动 PyMongo 的简单封装

    最近,需要使用 Python 对 MongodB 做一些简单的操作,不想使用各种繁重的框架.出于可重用性的考虑,想对 MongoDB Python 官方驱动 PyMongo 做下简单封装,百度一如既往 ...

  4. Redis全方位讲解--主从复制

    前言 前面介绍了redis持久化和容灾备份,这篇会介绍redis主从复制和redis持久化在主从复制中的一些应用.因为本人没有那么多服务器或机器,所以这里主要介绍下如何在docker容器中搭建主从复制 ...

  5. CQRS简单入门(Golang)

    一.简单入门之入门 CQRS/ES和领域驱动设计更搭,故整体分层沿用经典的DDD四层.其实要实现的功能概要很简单,如下图. 基础框架选择了https://github.com/looplab/even ...

  6. 20145234黄斐《Java程序设计》MyDC

    http://git.oschina.net/jiataiji/java

  7. P3674 小清新人渣的本愿

    P3674 小清新人渣的本愿 一道妙不可言的题啊,,, 一看就知道是个莫队 考虑求答案 1号操作就是个大bitset,动态维护当前的bitset \(S\),把能取哪些值都搞出来,只要\(S\ and ...

  8. jmeter no-gui模式动态传递场景参数

    jmeter进行性能压测时,有时候需要在linux上no-gui模式下运行,为了在no-gui模式下更方便的设置脚本的运行的场景, 将脚本的线程数,运行时间设置为动态参数,可以在脚本运行时动态设置“线 ...

  9. frp+TeamViewer 完美解决TeamViewer5分钟商业提醒

    必要条件:必须有一个公网服务器 frp是一个开源的端口转发工具,中文使用说明及下载地址在这里  https://github.com/fatedier/frp/blob/master/README_z ...

  10. 腾讯x5webview集成实战

    应用中许多网页由于优化的不够理想,出现加载慢,加载时间长等,而且因为碎片化导致兼容性问题,有一些网页有视频内容,产品还提出各种小窗需求,搞得心力憔悴.找到公开的有crosswalk和x5webview ...