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 secondsmemory 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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- 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 ...
- 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 ...
- Codeforces Round #235 (Div. 2)
A. Vanya and Cards time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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 ...
- 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 ...
- Codeforces Round #235 (Div. 2)C、Team
#include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; cin & ...
随机推荐
- 【js】走近小程序(2) 常见问题总结
一.API请求? 二.基础库兼容? 三.不同页面之间的传值 一.API请求? wx.request({ url: 'test.php', // 仅为示例,并非真实的接口地址 data: { x: ...
- 为什么企业依赖于 NoSQL
如果你关注大数据科技动向,你对 NoSQL 一定不陌生,NoSQL 是一个分布式数据库.在过去时间,数据存储一直关系型数据库天下,有着良好的控制并发操作.事务功能.虽然RDBMS很优秀,但是随着时间的 ...
- iOS 开发之UIStackView的应用
————————————————UIStackView的应用———————————————— 一:先讲下优势: 对于排布列表式控件的布局需求,用UIStackView控件,开发中为我们省去了繁琐的代码 ...
- Spring第四天——SSH整合
(从整合开始,使用回归使用eclipse) 一.三大框架版本: struts2 hibernate5 spring4 二.SSH三大框架回顾: Hibernate: ORM思想 核心配置文件: 单独 ...
- 【转】numpy教程
[转载说明] 本来没有必要转载的,只是网上的版本排版不是太好,看的不舒服.所以转过来,重新排版,便于自己查看. 基础篇 NumPy的主要对象是同种元素的多维数组. 这是一个所有的元素都是一种类型.通过 ...
- 【转载】ATL问题集
原文:http://blog.csdn.net/fengrx/article/details/4171629 这些问题是以前在csdn当版主是一些朋友整理的,今天找到了,贴到这里来! #1 如何使用控 ...
- 前端- css - 总结
1.css层叠样式表 1.什么是CSS? CSS是指层叠样式表(Cascading Style Sheets),样式定义如何显示HTML元素,样式通常又会存在于样式表中. 也就是说把HTML元素的样式 ...
- JAVAWEB 遍历mysql结果集时 字段为0、false、null的问题
foreach遍历查询mysql中的tinyint字段时一直查都是各种0,false,null 发现原来是实体类中的变量名和mysql中的列名不一样出的bug 所以说列名和实体类中的相关变量名是要保持 ...
- 单元测试时 出现找不到类或者 NoClassDefFoundError 的问题
这种情况下,启动或重启下服务器即可
- Nginx应用场景
1. Nginx应用场景 1)http服务器.Nginx可以独立的提供http服务,可以做网页静态服务器(也就是将静态文件放到nginx目录下,通过nginx来访问就ok) 2)虚拟主机,可以在一 ...