Codeforces Round #235 (Div. 2) D. Roman and Numbers(状压dp)
2014-12-19 01:36
671 查看
Roman and Numbers
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
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.
Input
The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the number of numbers close to number n modulo m.
Sample test(s)
input
output
input
output
input
output
Note
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;
}
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
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.
Input
The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the number of numbers close to number n modulo m.
Sample test(s)
input
104 2
output
3
input
223 4
output
1
input
7067678 8
output
47
Note
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) / 410D Roman and Numbers (带有整除性质的数位DP)
- Codeforces Round #235 (Div. 2) D Roman and Numbers(状态DP)
- Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(素数筛法)
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- Codeforces Round #267 (Div. 2) C. George and Job DP
- Codeforces Round #313 (Div. 1) C - Gerald and Giant Chess dp
- Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
- DP Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(dp)
- 【Codeforces Round 331 (Div 2)D】【DP 记忆化搜索 期望DP区间性质 好题】Wilbur and Trees 砍树随机从右从左概率左倒右倒的期望底面覆盖长度
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
- Codeforces Round #235 (Div. 2) A. Vanya and Cards
- Codeforces Round #302 (Div. 2) E. Remembering Strings(状压dp)
- Codeforces Round #189 (Div. 1) Kalila and Dimna in the Logging Industry 斜率DP
- Codeforces Round #160 (Div. 2) A. Roma and Lucky Numbers
- Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- Codeforces Round #232 (Div. 2) B.On Corruption and Numbers(整数拆分)
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心