码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 12063(dp记忆化)

时间:2015-05-07 06:30:27      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:dp   动态规划   记忆化搜索   

UVA - 12063

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

技术分享

Binary numbers and their pattern of bits are always very interesting to computer programmers. In this problem you need to count the number of positive binary numbers that have the following properties:

  • The numbers are exactly N bits wide and they have no leading zeros.
  • The frequency of zeros and ones are equal.
  • The numbers are multiples of K.

Input 

The input file contains several test cases. The first line of the input gives you the number of test cases, T ( 1技术分享T技术分享100). Then T test cases will follow, each in one line. The input for each test case consists of two integers, N ( 1技术分享N技术分享64) and K ( 0技术分享K技术分享100).

Output 

For each set of input print the test case number first. Then print the number of binary numbers that have the property that we mentioned.

Sample Input 

5
6 3
6 4
6 2
26 3
64 2

Sample Output 

Case 1: 1
Case 2: 3
Case 3: 6
Case 4: 1662453
Case 5: 465428353255261088


Illustration: Here‘s a table showing the possible numbers for some of the sample test cases:

6 3 6 4 6 2
101010 111000 111000
  110100 110100
  101100 101100
    110010
    101010
    100110

Source

Root :: Prominent Problemsetters :: Monirul Hasan
Root :: ACM-ICPC Dhaka Site Regional Contests :: 2004 - Dhaka
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Exercises
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: More Advanced Topics :: More Advanced Dynamic Programming :: DP + bitmask

 Status


求长度为n的串(不含前导0),0的个数和1的个数相等且是k的倍数的个数。




#include<bits/stdc++.h>
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
using namespace std;
typedef long long ll;
ll dp[40][40][100];
int n,k,tot;
ll d(int ones,int zeros,int mod)
{
    ll & res = dp[ones][zeros][mod];
    if(res!=-1)return res;
    if(ones==tot&&zeros==tot) return res = (mod==0);
    if(ones==tot)return res = d(ones,zeros+1,(mod<<1)%k);
    if(zeros==tot)return res = d(ones+1,zeros,(mod<<1|1)%k);
    return res = d(ones+1,zeros,(mod<<1|1)%k) + d(ones,zeros+1,(mod<<1)%k);
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas = 1; cas <= T; ++cas) {
        memset(dp,-1,sizeof dp);
        scanf("%d%d",&n,&k);
        ll res = 0;
        tot = n>>1;
        if((n&1)==0&&k)res = d(1,0,1);
        printf("Case %d: %lld\n", cas,res);
    }
    return 0;
}


UVA 12063(dp记忆化)

标签:dp   动态规划   记忆化搜索   

原文地址:http://blog.csdn.net/acvcla/article/details/45546913

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!