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 ...
随机推荐
- Ural 1309 Dispute (递归)
意甲冠军: 给你一个数列: f(0) = 0 f(n) = g(n,f(n-1)) g(x,y) = ((y-1)*x^5+x^3-xy+3x+7y)%9973 让你求f(n) n <= 1e ...
- c# winfrom DataGridView使行高不可改变,使列头高度不可改变,
// 禁止用户改变DataGridView1的所有列的列宽 //DataGridView1.AllowUserToResizeColumns = false; //禁止用户改变DataGridView ...
- Linux开机自启动
永久打开或则关闭 chkconfig iptables on chkconfig iptables off 即时生效,重启后还原 service iptables start service ipta ...
- SimpleDateFormat使用特定的解释
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的详细类. 它同意格式化 ...
- mac平台adb、tcpdump捕手android移动网络数据包
在移动电话的发展app当我们希望自己的下才能看到app网络发出请求,这个时候我们需要tcpdump工具包捕获.实现tcpdump空灵,以下步骤需要: 在这里,在android 华为手机 P6对于样本 ...
- shell脚本—根据文件个数定时备份
最近在了解Jenkins时,出现这样一个问题:linux ext3系统目录下的子目录个数不能超过31998个(参考:http://www.xshell.net/linux/1267.html),但je ...
- JAVA学习笔记 -- 包资源文件jar包裹
初学者GUI,使用Eclipse出口jar包裹,不能显示最小化的图标集(hello.png根文件下的项目文件夹文件).码如下面: import javax.swing.JFrame; import j ...
- Swift继承
//声明一个基类 class vehicle { var maxPassenger : Int = 0 var manufacturer : String! func description() -& ...
- BZOJ 1096 ZJOI2007 仓库建设 边坡优化
标题效果:特定n植物,其中一些建筑仓库,有一点使,假设没有仓库仓库向右仓库.最低消费要求 非常easy边坡优化--在此之前刷坡优化的情况下,即使这道题怎么错过 订购f[i]作为i点建设化妆i花费所有安 ...
- xaml的margin和css的margin对比
css margin xaml margin 例子 1 css margin:10px 5px 15px 20px;上右下左 上外边距是 10px 右外边距是 5px 下外边距是 15px 左外边距是 ...