codeforces 258B Little Elephant and Elections 数位DP+排列组合
2014-08-08 23:01
232 查看
题意:大概是从1~n中取7个数,左边6个数中的4,或7个数之和小于右边1个数中的4,7个数之和;
先用hash[0~9]存储下前n个数中,4,7个数之和的数字有多少 ,接下来就是组合知识了,这个真心不会
写了好久没写出来,只好百度了QAQ,还是太弱了
先用hash[0~9]存储下前n个数中,4,7个数之和的数字有多少 ,接下来就是组合知识了,这个真心不会
写了好久没写出来,只好百度了QAQ,还是太弱了
#include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #define maxn 30 #define mod 1000000007 using namespace std; #define ll long long ll limit[40],dp[40][400],k; ll hash[10],sb; void fun(ll pos,ll x,ll s)//dfs求出所有组合的种类 { if(pos>5) { sb=(sb+s)%mod; return ; } for(ll i=0;i<x;i++) { if(hash[i]) { hash[i]--; fun(pos+1,x-i,(hash[i]+1)*s%mod); hash[i]++; } } } ll dfs(ll now,ll sum,bool flag) { if(now<=0) return sum==k; if(!flag&&dp[now][sum]!=-1) return dp[now][sum]; ll bound=flag?limit[now]:9,ret=0; for(ll i=0;i<=bound;i++) { ret+=dfs(now-1,sum+(i==4)+(i==7),flag&&(i==bound)); } if(!flag) dp[now][sum]=ret; return ret; } ll count(ll x) { ll len=0; while(x) { limit[++len]=x%10; x/=10; } return dfs(len,0,true); } int main() { ll n; while(cin>>n) { for(k=0;k<=9;k++)//预处理 { memset(dp,-1,sizeof(dp)); hash[k]=count(n)-count(0); } sb=0; ll i; for(i=0;i<=9;i++) { if(hash[i]) { hash[i]--; fun(0,i,hash[i]+1); hash[i]++; } } cout<<sb<<endl; } return 0; }
相关文章推荐
- 【codeforces】55D. Beautiful numbers 数位DP
- CodeForces - 55D 区间内数字能被每个数位上非0数字整除的个数
- codeforces 55D Beautiful numbers Codeforces Beta Round #51(数位dp)
- Codeforces 204A Little Elephant and Interval(数位统计)
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
- 【动态规划】【数位DP】[Codeforces 55 D]Beautiful numbers
- codeforces 55D. Beautiful numbers(数位dp)
- CodeForces - 55D Beautiful numbers 数位dp+离散化
- Codeforces 55D Beautiful numbers 数位DP
- Codeforces 55D - Beautiful numbers(数位dp)好
- codeforces 55D D. Beautiful numbers(数位dp+数论)
- CodeForces - 55D 数位dp
- Codeforces 55D Beautiful Number (数位dp)
- Codeforces 55D Beautiful numbers 数位DP
- Codeforces 55D Beautiful Number (数位统计)
- Codeforces 55D Beautiful numbers【数位dp】
- CodeForces 55D Beautiful numbers (数位DP+状态简化,5级)
- CodeForces 54C-First Digit Law(数位,概率dp)
- [数位DP] Codeforces 809C. Find a car
- CodeForces 55D Beautiful numbers (数位DP)