[数位dp] Codeforces 401D Roman and Numbers
来源:互联网 发布:虚拟币交易软件 编辑:程序博客网 时间:2024/05/01 18:54
题目链接:
http://codeforces.com/problemset/problem/401/D
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.
题目意思:
给一个n(n<=10^18),m(m<=100),求把n中各位数字重排后组成的数(不能又前置零)能被m整除的个数。
解题思路:
数位dp
dp[s][m]表示当剩下的数组成的状态为s,已用的数字组成的余数为m时,能组成满足要求的种数。
最每一个剩下 的数枚举,去掉即可。注意每一位可能有相同的数,只需要一个。
代码:
//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;ll dp[1<<19][101]; //dp[s][m]表示还剩下状态为s的数,前面的余数为m时,满足要求的种数//这样可以避免前面余数为m的情况的重复计算ll n,m;int cnt,va[20];void init(ll cur){ cnt=0; while(cur) { va[cnt++]=cur%10; cur/=10; }}ll dfs(ll cur,ll fi){ if(!cur) //每一位已全部搞定 { if(!fi) return 1; return 0; } if(dp[cur][fi]!=-1) //之前已经求,现在不需要求了 return dp[cur][fi]; int num[10]; memset(num,0,sizeof(num)); ll res=0; for(int i=0;i<cnt;i++) { if(cur&(1<<i)) //第i个数还在里面 { if(num[va[i]]) continue; num[va[i]]=1; res+=dfs(cur^(1<<i),(fi*10+va[i])%m); //把第i个数放进去 } } return dp[cur][fi]=res; //保存}int main(){ //printf("%d\n",1<<18); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%I64d%I64d",&n,&m)) { init(n); memset(dp,-1,sizeof(dp)); int num[10]; memset(num,0,sizeof(num)); int lim=(1<<cnt)-1; ll ans=0; for(int i=0;i<cnt;i++) { if(num[va[i]]||!va[i]) //注意首位不为零 continue; num[va[i]]=1; ans+=dfs(lim^(1<<i),va[i]%m); } printf("%I64d\n",ans); } return 0;}
- [数位dp] Codeforces 401D Roman and Numbers
- CodeForces 401D Roman and Numbers【数位dp+状态压缩】
- codeforces 401D Roman and Numbers (数位dp)
- [Codefoces 401D]Roman and Numbers 数位dp
- Codeforce 401D Roman and Numbers[数位DP+状态压缩]
- cf 401D. Roman and Numbers 数位dp,状压
- Codeforces 401D Roman and Numbers【状压dp】
- Codeforces 401D Roman and Numbers 状压DP
- Codeforces Round #235 (Div. 2) / 410D Roman and Numbers (带有整除性质的数位DP)
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
- CodeForces 401D Roman and Numbers
- codeforces 401D Roman and Numbers
- codeforces 401D D. Roman and Numbers(状态压缩dp+数论)
- codeforces 401D Roman and Numbers(状态压缩)
- Codeforces 401D Roman and Numbers(记忆化搜索)
- codeforces #235 D. Roman and Numbers 题解
- codeforces#235_div2_D Roman and Numbers ,dp
- Codeforces 55D Beautiful numbers 数位dp
- 使用 python 开发 Web Service
- android暂停或停止其他音乐播放器的播放
- Android4.3 Bluetooth 抓取hci log
- Mysql数据库查询按时间递减显示
- douban读书的API
- [数位dp] Codeforces 401D Roman and Numbers
- hadoop 出错:org.apache.hadoop.security.AccessControlException: Permission denied:
- 2014年4月操作系统份额:Windows XP仍然在26%以上,Linux微涨
- cf 427D Match & Catch 后缀数组
- wifi共享妙招大比拼
- iOS7—Day by day—Day0:UIKit Dynamics
- 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute标记。
- 尽快了广告法斯蒂芬焊接钢管
- Dom4j读写文件时的编码问题