zoukankan      html  css  js  c++  java
  • SCU 2009(数位dp)

    传送门:Zeros and Ones

    题意:求总数位为n包含0和1个数相同且整除k的二进制数的个数。

    分析:设dp[pos][num][md]表示还有pos位已包含num个1且模k余md的符合条件的二进制数的个数,裸数位dp题。

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #define LL long long
    #define N 3000010
    using namespace std;
    int n,k;
    LL dp[70][70][105];
    LL dfs(int pos,int num,int md,int fzore)
    {
        if(!pos)
        {
            return num==n/2&&!md;
        }
        if(~dp[pos][num][md])return dp[pos][num][md];
        LL ans=0;
        for(int i=0;i<=1;i++)
        {
            if(fzore)
            {
                if(i==1)ans+=dfs(pos-1,num+1,(md*2+i)%k,0);
            }
            else
            {
                if(i==1)ans+=dfs(pos-1,num+1,(md*2+i)%k,0);
                else ans+=dfs(pos-1,num,(md*2+i)%k,0);
            }
        }
        dp[pos][num][md]=ans;
        return ans;
    }
    int main()
    {
        int T,cas=1;
    
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&k);
            printf("Case %d: ",cas++);
            memset(dp,-1,sizeof(dp));
            if(k==0||n%2==1)
            {
                puts("0");continue;
            }
            printf("%lld
    ",dfs(n,0,0,1));
        }
    }
    View Code
  • 相关阅读:
    topcoder srm 445 div1
    topcoder srm 440 div1
    topcoder srm 435 div1
    topcoder srm 430 div1
    topcoder srm 400 div1
    topcoder srm 380 div1
    topcoder srm 370 div1
    topcoder srm 425 div1
    WKWebView强大的新特性
    Runtime那些事
  • 原文地址:https://www.cnblogs.com/lienus/p/4304298.html
Copyright © 2011-2022 走看看