钥匙计数之二 HDU - 1480

版权声明:本文为博主瞎写的,请随便转载 https://blog.csdn.net/sdut_jk17_zhangming/article/details/81812631

一把钥匙有N个槽,2<N<26槽深为1,2,3,4,5,6。每钥匙至少有3个不同的深度且相连的槽其深度之差不得为5。求这样的钥匙的总数。 

Input

本题无输入

Output

对2<N<26,输出满足要求的钥匙的总数。 

Sample Output

N=3: 104
N=4: 904
N=5: 5880
.
.
.
.
.
N=25: 8310566473196300280
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[30][10][1<<7];
int flag;
ll dfs(int pos,int prev,int diff)
{
    int i;
    bitset<7> bit = diff;
    if(pos==0)
    {
        if(bit.count()>=3) return 1;
        return 0;
    }
    if(dp[pos][prev][diff] != -1) return dp[pos][prev][diff];
    ll ans = 0;
    for(i=1;i<= 6;i++)
    {
        bitset<7> nxt = bit;
        if(abs(i-prev) == 5&&pos != flag) continue;
        nxt[i] = 1;
        ans += dfs(pos-1,i,nxt.to_ulong());
    }
    dp[pos][prev][diff] = ans;
    return ans;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int i;
    for(i=3;i<=25;i++)
    {
        flag = i;
        printf("N=%d: %lld\n",i,dfs(i,0,0));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdut_jk17_zhangming/article/details/81812631