您的位置:首页 > 其它

hdu1483 钥匙计数之一(数位dp)

2016-07-18 13:54 246 查看
钥匙计数之一

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1616 Accepted Submission(s): 694

Problem Description

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

Input

本题无输入

Output

对N>=2且N<=31,输出满足要求的锁匙的总数。

Sample Output

N=2: 0

N=3: 8

N=4: 64

N=5: 360

..

..

..

..

..

..

..

N=31: …

直接数位dp,看了网上的数学找规律,真心觉得叼,然而我太菜,不会玄学推公式,老老实实用数位dp,还有用状压dp写的,膜。

#include<bits/stdc++.h>
using namespace std;

#define read(T) int T;\
scanf("%d",&(T));\
while(T--)
#define si(x) scanf("%d",&(x))
#define sii(x,y) scanf("%d%d",&(x),&(y))
#define cls(x,y) memset((x),(y),sizeof((x)));
#define sl(x) scanf("%lld",&(x))
typedef long long LL;
typedef unsigned long long ULL;
const int maxint=((~((unsigned)(0)))>>1);
const LL maxll=((~((unsigned long long)(0)))>>1);

const int maxn=105;
const int maxm=105;
const int maxk=105;

LL dp[31][35][5][2];

LL dfs(int pos,int num,int last,int f) {
if(pos<0) {
if((((num>>2)&1)+((num>>3)&1))>=1&&f==1)return 1LL;
return 0LL;
}
if(~dp[pos][num][last][f])return dp[pos][num][last][f];
LL ans=0;
for(int i=1; i<=4; ++i) {
ans+=dfs(pos-1,num|(1<<i),i,f||(last&&(abs(last-i)>=3)));
}
return dp[pos][num][last][f]=ans;
}

int main() {
//    freopen("Myans.txt","w",stdout);
cls(dp,-1)
for(int n=2; n<=31; ++n) {
cout<<"N="<< n << ": " << dfs(n-1,0,0,0) << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: