Codeforces Round #235 (Div. 2) D. Roman and Numbers(如压力dp)
4 seconds
512 megabytes
standard input
standard output
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 < 1018) and m (1 ≤ m ≤ 100).
In a single line print a single integer — the number of numbers close to number n modulo m.
104 2
3
223 4
1
7067678 8
47
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
题意:给出一个数字num和m。问通过又一次排列num中的各位数字中有多少个数(mod m)=0,直接枚举全排列肯定不行,能够用状压dp来搞..
dp[S][k]表示选了num中的S且(mod m)=k的方案种数,初始条件dp[0][0]=1,转移方为dp[i|1<<j[(10*k+num[j])%m]+=dp[i}[k];,注意到是多重排列。所以还须要除去反复的。
代码例如以下:
#include <iostream>
#include <cstring>
using namespace std; typedef long long ll; ll dp[1<<18][100],c[20];//dp[S][k]表示选了num中的S且(mod m)=k的方案种数 int main(int argc, char const *argv[])
{
char num[20];
int m;
while(cin>>num>>m) { memset(dp,0,sizeof dp);
memset(c,0,sizeof c);
dp[0][0]=1; ll div=1,sz=strlen(num),t=1<<sz;
for(int i=0;i<sz;i++) {
div*=(++c[num[i]-='0']);//可重排列最后要除的除数n1!*n2!*...nk!
} for(int i=0;i<t;i++) {
for(int j=0;j<sz;j++)if(!(i&1<<j)) {//集合S中不含j才转移
if(num[j]||i){//至少一个不为0保证无前导0
for(int k=0;k<m;k++) {
dp[i|1<<j][(10*k+num[j])%m]+=dp[i][k];
}
}
}
} cout<<dp[t-1][0]/div<<endl;
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
Codeforces Round #235 (Div. 2) D. Roman and Numbers(如压力dp)的更多相关文章
- 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 ...
- 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 #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...
- 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 #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- 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 & ...
- Codeforces Round #235 (Div. 2) B. Sereja and Contests
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
随机推荐
- Mongodb安装和配置
Mongodb之安装配置 安装 Mongodb的下载地址为Mongodb官网.下载时.你能够选择是安装包或者是压缩包. 下载完毕后.双击安装包并安装. 安装完毕后.你能够在安装文件夹看到下图中所见的文 ...
- TextView于getCompoundDrawables()使用演示样本的方法
MainActivity例如下列: package cc.testcompounddrawables; import android.app.Activity; import android.grap ...
- 2014 I/O返回:Google连接一切
文/蒋涛 6月.WWDC 2014与Google I/O (大部分演讲视频都公开.Youtube须要FQ.很值得一看)相继召开. 今年是我第三年參加Google I/O大会.三年间,Google积累 ...
- 第三篇——第二部分——第二文 计划搭建SQL Server镜像
原文:第三篇--第二部分--第二文 计划搭建SQL Server镜像 本文紧跟上一章:SQL Server镜像简介 本文出处:http://blog.csdn.net/dba_huangzj/arti ...
- 第四章——SQLServer2008-2012资源及性能监控(3)
原文:第四章--SQLServer2008-2012资源及性能监控(3) 本文为本系列最后一章,监控内存使用.监控服务器的内存是非常重要的事情,有很多情况会引起内存消耗.所以要经常性地做检查. 本文将 ...
- ubuntu-14.04 系统安装mysql-5.6.21
1.安装mysql前准备工作 (1).从官网下载mysql-5.6.21.tar.gz (2).tar -zxvf mysql-5.6.21-tar.gz 会生成mysql-5.6.21的目录 ...
- nodejs http静态服务器
使用nodejs写的很简单的静态服务器,没有做cluster处理,没有做缓存处理,不支持访问文件夹,功能只有一个,就是获取到文件后再返回文件内容. var fs = require('fs'); va ...
- Repository、IUnitOfWork
Repository.IUnitOfWork 在领域层和应用服务层之间的代码分布与实现 本来早就准备总结一下关于Repository.IUnitOfWork之间的联系以及在各层中的分布,直到看到田园里 ...
- Java中的逆变与协变(转)
看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); ...
- vs2010模板修改
使用visual studio 2010好久了,也遇到了不少问题,下面跟大家分享一些. 模板修改 说明: 主要工具: 以visual studio 2010作为例子,具体目录可能会根据不同的安装目录 ...