UVa11472 - Beautiful Numbers

Problem B
Beautiful Numbers
Time Limit : 3 seconds
 

 

An N-based number is beautiful if all of the digits from 0 to N-1 are used in that number and the difference between any two adjacent digits is exactly 1 (one). For example, 9876543210 is a 10-based beautiful number. You have to calculate the number beautiful numbers that has got atmost M digits..

Note: No leading zero is allowed in a beautiful number.

 

 
 Input  
 The first line of input is an integer T (T<100) that indicates the number of test cases. Each case starts with a line containing two integers N and M ( 2≤N≤10 & 0≤M≤100 ). 
   
 Output 
 For each case, output the number of beautiful N-based numbers, which are using less than or equal to M digits in a single line. You have to give your output modulo 1000000007. 
   
 Sample InputSample Output  
  3
2 4
3 7
10 10
3
31
1
题目很短,题意也很好理解

思路 dp[lastnum][dep(数字个数)][各位数使用情况(bitmask)]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
ll dp[11][110][1100];
int n,m;
ll dfs(int last,int num,int used){
    if(dp[last][num][used] != -1) return dp[last][num][used];
    if(num==0) return used == (1<<n)-1;
    if(last == n){
        ll ans = 0;
        for(int i = 1; i <= n-1; i++) ans = (ans + dfs(i,num-1,1<<i))%MOD;
        return dp[last][num][used] = ans;
    }
    ll ans = 0;
    int tmp;
    if(last>0){
        tmp = 1<<(last-1);
        ans = (ans + dfs(last-1,num-1,used|tmp))%MOD;
    }
    if(last<n-1){
        tmp = 1<<(last+1);
        ans = (ans + dfs(last+1,num-1,used|tmp))%MOD;
    }
    return dp[last][num][used] = ans;// 1010 10 101
}
int main(){
    int ncase;
    cin >> ncase;
    while(ncase--){
        scanf("%d%d",&n,&m);
        memset(dp,-1,sizeof dp);
        ll ans = 0;
        for(int i = 0; i <= m; i++) ans = (dfs(n,i,0)+ans)%MOD;
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值