FJ省队集训DAY4 T3
2016-07-12 09:10
197 查看
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> typedef long long ll; ll read(){ ll t=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();} while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} return t*f; } int n,K,lim,ans=0; int a[20],tot=0,cnt[10],C[15][15]; ll pw_[30],*pw; void dp(int id,bool f,bool f2) { if (id==0) { ++ans; return; } if (f2) { for (int i=1;i<=(f?9:a[id]);i++) if (cnt[i]>0) { --cnt[i]; dp(id-1,f|(i<a[id]),0); ++cnt[i]; } return; } if (f) { int tmp=1; for (int j=0;j<=9;j++) tmp*=C[id][cnt[j]], id-=cnt[j]; ans+=tmp; return; } for (int i=0;i<=a[id];i++) if (cnt[i]>0) { --cnt[i]; dp(id-1,f|(i<a[id]),0); ++cnt[i]; } } void dfs(int num,int tot2,int used){ if (num==9){ cnt[9]=tot2-used; int p=-1,g=0; for (int i=0;i<=9&&p==-1;g+=cnt[i++]){ if (g<(tot2+1)/2&&g+cnt[i]>=(tot2+1)/2) p=i; } ll val=0; for (int i=0;i<=9;i++) val+=cnt[i]*pw[i-p]; if (val<=lim) dp(tot2,tot2<tot,1); return; } for (int i=0;i<=tot2-used;i++){ cnt[num]=i; dfs(num+1,tot2,used+i); } } void init(){ pw=pw_+12; for (int i=-9;i<=9;i++){ pw[i]=1; for (int j=1;j<=K;j++) pw[i]*=i; } C[0][0]=1; for (int i=1;i<=12;i++){ C[i][0]=1; for (int j=1;j<=i;j++) C[i][j]=C[i-1][j]+C[i-1][j-1]; } } int main(){ n=read();K=read();lim=read();init(); while (n){ a[++tot]=n%10; n/=10; } for (int i=1;i<=tot;i++) dfs(0,i,0); printf("%d\n",ans); }
相关文章推荐
- JS导出PDF插件的方法(支持中文、图片使用路径)
- Unity3d UGUI 滚动视图ScrollView的使用教程之简易双肩包
- 自定义错误信息
- R语言分析南京房价
- android之merge布局
- Codeforces 204A Little Elephant and Interval
- 把你的旧笔记本变成 Chromebook
- 常用正则表达式 总结
- CSS深入理解
- MapReduce: 一种简化的大规模集群数据处理法
- mysql 复合索引详解
- Maven类包冲突终极解决小技若干
- 自我介绍
- HDU2091(基础题)
- 基础的名词概念
- shell脚本生成指定字符的随机串
- 日期比较
- leetcode-35. Search Insert Position
- OC利用正则表达式获取网络资源(网络爬虫)
- 关系型数据库三范式