Topcoder SRM 597 LittleElephantAndSubset
2017-01-23 21:12
417 查看
#include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #define mod 1000000007 #include <string.h> #define ll long long using namespace std; void Mod(int &a,int b){a+=b;if(a>=mod)a-=mod;} class LittleElephantAndSubset { public: int cnt[1025],dp[1025];//状压 /* 解题思路:先用dfs造出所有满足条件的数,考虑每个数字是否出现,记录每个状态所能形成的数的cnt值 再枚举子集 dp[i]=Σdp[j]*cnt[i-j] (j∈[0,i/2],且j为i的子集) 最终答案为Σdp[i] */ int n; void dfs(ll sum,int tot){ if(sum<=n)cnt[tot]++; else return; for(int i=0;i<10;i++)if(!(tot&(1<<i)))dfs(sum*10+i,tot|(1<<i)); } bool check(int a,int b){ for(int i=0;i<10;i++)if(!(a&(1<<i)))if(b&(1<<i))return false; return true; } int getNumber(int N) { n=N; memset(cnt,0,sizeof(cnt)); memset(dp,0,sizeof(dp)); dp[0]=1; for(int i=1;i<10;i++)dfs(i,(1<<i)); for(int i=0;i<1024;i++){ for(int j=0;j<i/2+1;j++){ if(check(i,j))Mod(dp[i],1LL*dp[j]*cnt[i-j]%mod); } } int ans=0; for(int i=1;i<1024;i++)Mod(ans,1LL*dp[i]); return ans; } };
相关文章推荐
- SRM 597 D2 L2:LittleElephantAndString
- topcoder SRM 592 DIV2 LittleElephantAndBooks
- topcoder SRM 592 DIV2 LittleElephantAndPermutationDiv2
- *[topcoder]LittleElephantAndString
- SRM 595 D2 L3:LittleElephantAndXor, dp
- *[topcoder]LittleElephantAndBalls
- 线段树 CodeForces 220B - Little Elephant and Array
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
- A. Little Elephant and Bits
- Codeforces Round #129 (Div. 2)A. Little Elephant and Rozdil
- 【CF】220B Little Elephant and Array
- codeforces 204A Little Elephant and Interval 美丽的区间转换
- AC日记——Little Elephant and Function codeforces 221a
- Little Elephant and Bits
- Codeforces Round #136 (Div. 2) Little Elephant and Array
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- AC日记——Little Elephant and Numbers codeforces 221b
- Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset
- bzoj4374 Little Elephant and Boxes【动态规划+折半搜索】