BZOJ--3209(数位DP)

2014-12-27 16:12:34

思路:基础数位DP,二进制拆分后,从高位往低位考虑,如果N的某位为0,那么只能放0,如果N的某位为1,那么可放0或1。若放1则继续考虑下一位,若放0则后面的1个数不受限制,用组合数考虑。

 1 #include <cstdio>
 2 typedef long long ll;
 3 const ll m = 10000007;
 4 ll N,ans,c[60][60],s[60];
 5 ll Pow(ll v,ll n){
 6     ll r = 1;
 7     while(n){
 8         if(n & 1) r = (r * v) % m;
 9         v = (v * v) % m;
10         n >>= 1;
11     }
12     return r;
13 }
14 void Dfs(int p,int n){
15     if(p == 0){
16         ans = (ans * (ll)n) % m;
17         return;
18     }
19     if(s[p] == 0)   Dfs(p - 1,n);
20     else{
21         Dfs(p - 1,n + 1);
22         for(int i = 0; i <= p - 1; ++i) if(n + i != 0)
23             ans = (ans * Pow(n + i,c[p - 1][i])) % m;
24     }
25 }
26 int main(){
27     for(int i = 0; i <= 50; ++i)
28         for(int j = 0; j <= i; ++j)
29             c[i][j] = (i == 0 || j == 0) ? 1 : c[i - 1][j] + c[i - 1][j - 1];
30     while(scanf("%lld",&N) != EOF){
31         int cur = 0;
32         while(N){
33             ++cur;
34             if(N & 1) s[cur] = 1; else s[cur] = 0;
35             N >>= 1;
36         }
37         ans = 1;
38         Dfs(cur,0);
39         printf("%lld\n",ans);
40     }
41     return 0;
42 }

 

posted @ 2014-12-27 16:16  Naturain  阅读(129)  评论(0编辑  收藏  举报