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 ...
随机推荐
- PHP关联数组和哈希表(hash table) 未指定
PHP有数据的一个非常重要的一类,就是关联数组.又称为哈希表(hash table),是一种很好用的数据结构. 在程序中.我们可能会遇到须要消重的问题,举一个最简单的模型: 有一份username列表 ...
- C#多线程编程实例 螺纹与窗口交互
C#多线程编程实例 螺纹与窗口交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = new Thread ...
- 调整CentOS的文字登陆界面的分辨率
通过文字界面登陆到系统,切换到root权限. 用vi打开 /boot/grub/menu.lst 文件 ,因为menu.lst是grub.conf文件的快捷方式,终于打开的还是grub.conf文 ...
- Chapter 1 Securing Your Server and Network(6):为SQL Server访问配置防火墙
原文:Chapter 1 Securing Your Server and Network(6):为SQL Server访问配置防火墙 原文出处:http://blog.csdn.net/dba_hu ...
- redis基础的字符串类型
redis —— 第二篇 基础的字符串类型 我们都知道redis是采用C语言开发,那么在C语言中表示string都是采用char[]数组的,然后你可能会想,那还不简单,当我执行如下命令,肯定是直 接塞 ...
- POJ3581:Sequence(后缀数组)
Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An, you are to ...
- Codeforces 327B-Hungry Sequence(素数筛)
B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 分析Cocos2d-x横版ACT手游源 1、登录
我自己的游戏代码 因为 游戏源 盯着外面的 我们能够能够理解 /******************************************************************** ...
- APP漏洞导致移动支付隐患重重,未来之路怎样走?
没有一种支付是100%安全的,互联网及移动支付规模的增长,其交易的安全性须要银行.支付公司.App开发人员.用户等參与各方更加重视.当下手机支付似乎变成了一种时尚,用户们"刷手机" ...
- Eclipse正在使用Ant扑灭Android数据包错误的解决方案 – Perhaps JAVA_HOME does not point to the JDK
问题描写叙述: 在Eclipse中执行ant批量打包工具出错,日志信息例如以下: D:\Android\android-sdk-windows\tools\ant\build.xml:601: The ...