CF401D(Roman and Numbers)状压DP
来源:互联网 发布:印象笔记mac画图 编辑:程序博客网 时间:2024/05/16 02:24
题意:给一个数n和m(n (1 ≤ n < 1018) m (1 ≤ m ≤ 100).)问n的各位数字重新组合(非0开头)有多少种不同的数能被m整除。当时想到了正解,但是没仔细算long long ans[1<<18][100]以为会超限,当时问适牛跟着用了一个map来映射状态,结果比赛时样例过了,最后81组数据测试时第80组TLE。其实现在想想也许搞个hash就不会TLE了。 long long ans[1<<18][100]大概只有200M,题目给了400M,想想如果N给10^20就不行了,应该是故意卡的,还有时间复杂度也是刚刚卡过,以后每看题一定要先严格计算好时间和空间,不能乱YY。
还是回忆下正解吧:
ans[j][k]的j转化为二进制表示n的每个位置是否被用到,k是指模m等于k的数量。转移也是暴力转的,最坏复杂度是:(1<<18)*18*m=470M,题目给了4s,是可以过的。给的n 18位和m<=100貌似都是为了让过这个复杂度。
代码:
/***************************************************** author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>using namespace std;#define eps 1e-8char s[25];int m;long long ans[1<<18][105];long long fac(int n){ long long out=1; for(int i=1;i<=n;i++) out*=i; return out;}int tool[10];int main(){ scanf("%s%d",s,&m); int len=strlen(s); sort(s,s+len); for(int i=0;i<len;i++) { if(s[i]-'0') ans[1<<i][(s[i]-'0')%m]=1; tool[s[i]-'0']++; } for(int i=1;i<(1<<len)-1;i++) { for(int k=0;k<m;k++) { for(int j=0;j<len;j++) { if(i&(1<<j))continue; ans[i+(1<<j)][(k*10+(s[j]-'0'))%m]+=ans[i][k]; } } } long long out=ans[(1<<len)-1][0]; for(int i=0;i<=9;i++) out/=fac(tool[i]); cout<<out<<'\n'; return 0;}
0 0
- CF401D(Roman and Numbers)状压DP
- cf 401D. Roman and Numbers 数位dp,状压
- Codeforces 401D Roman and Numbers【状压dp】
- Codeforces 401D Roman and Numbers 状压DP
- codeforces 401D Roman and Numbers (数位dp)
- codeforces#235_div2_D Roman and Numbers ,dp
- Codeforces Round #235 (Div. 2) D. Roman and Numbers(状压dp)
- Codeforces Round #235 (Div. 2) D Roman and Numbers(状态DP)
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
- Codeforces Round #235 (Div. 2) D. Roman and Numbers(状态压缩DP)
- [数位dp] Codeforces 401D Roman and Numbers
- [Codefoces 401D]Roman and Numbers 数位dp
- Codeforce 401D Roman and Numbers[数位DP+状态压缩]
- CodeForces 401D Roman and Numbers【数位dp+状态压缩】
- cf235,D Roman and Numbers(状态压缩dp)引发对dp中几种状态类型的思考
- CF401D
- codeforces 401D Roman and Numbers(状态压缩)
- Codeforces 401D Roman and Numbers(记忆化搜索)
- 手机sim卡被格式化了数据怎么找回来
- html的table的建立及一些相关的应用
- Visual C# 2005如何连接至Access数据库
- how to install ia32-libs for Ubuntu13.10
- 32.64
- CF401D(Roman and Numbers)状压DP
- HBase简介(很好的梳理资料)
- apache自带的ab压力测试工具用法详解
- eclipse导入工程中文乱码问题
- 【c++系列】函数模板
- fread和fwrite
- sql server 格式化字符串加前置0或前导0
- Asp.Net Set Http Timeout
- 绿色版Mysql的安装配置